lib/soap/rpc/soaplet.rb in soap4r-1.5.5.20061022 vs lib/soap/rpc/soaplet.rb in soap4r-1.5.6
- old
+ new
@@ -1,7 +1,7 @@
# SOAP4R - SOAP handler servlet for WEBrick
-# Copyright (C) 2001-2005 NAKAMURA, Hiroshi <nahi@ruby-lang.org>.
+# Copyright (C) 2000-2007 NAKAMURA, Hiroshi <nahi@ruby-lang.org>.
# This program is copyrighted free software by NAKAMURA, Hiroshi. You can
# redistribute it and/or modify it under the same terms of Ruby's license;
# either the dual license version in 2003, or any later version.
@@ -39,14 +39,16 @@
class SOAPlet < WEBrick::HTTPServlet::AbstractServlet
public
attr_reader :options
+ attr_accessor :authenticator
def initialize(router = nil)
@router = router || ::SOAP::RPC::Router.new(self.class.name)
@options = {}
+ @authenticator = nil
@config = {}
end
# for backward compatibility
def app_scope_router
@@ -79,16 +81,24 @@
raise WEBrick::HTTPStatus::MethodNotAllowed, "GET request not allowed"
end
def do_POST(req, res)
logger.debug { "SOAP request: " + req.body } if logger
+ if @authenticator
+ @authenticator.authenticate(req, res)
+ # you can check authenticated user with SOAP::RPC::SOAPlet.user
+ end
begin
conn_data = ::SOAP::StreamHandler::ConnectionData.new
setup_req(conn_data, req)
@router.external_ces = @options[:external_ces]
- conn_data = @router.route(conn_data)
- setup_res(conn_data, req, res)
+ Mapping.protect_threadvars(:SOAPlet) do
+ SOAPlet.user = req.user
+ SOAPlet.cookies = req.cookies
+ conn_data = @router.route(conn_data)
+ setup_res(conn_data, req, res)
+ end
rescue Exception => e
conn_data = @router.create_fault_response(e)
res.status = WEBrick::HTTPStatus::RC_INTERNAL_SERVER_ERROR
res.body = conn_data.send_string
res['content-type'] = conn_data.send_contenttype || "text/xml"
@@ -99,12 +109,41 @@
else
logger.debug { "SOAP response: " + res.body } if logger
end
end
+ def self.cookies
+ get_variable(:Cookies)
+ end
+
+ def self.cookies=(cookies)
+ set_variable(:Cookies, cookies)
+ end
+
+ def self.user
+ get_variable(:User)
+ end
+
+ def self.user=(user)
+ set_variable(:User, user)
+ end
+
private
+ def self.get_variable(name)
+ if var = Thread.current[:SOAPlet]
+ var[name]
+ else
+ nil
+ end
+ end
+
+ def self.set_variable(name, value)
+ var = Thread.current[:SOAPlet] ||= {}
+ var[name] = value
+ end
+
def logger
@config[:Logger]
end
def setup_req(conn_data, req)
@@ -113,9 +152,18 @@
conn_data.soapaction = parse_soapaction(req.meta_vars['HTTP_SOAPACTION'])
end
def setup_res(conn_data, req, res)
res['content-type'] = conn_data.send_contenttype
+ cookies = SOAPlet.cookies
+ unless cookies.empty?
+ res['set-cookie'] = cookies.collect { |cookie| cookie.to_s }
+ end
+ if conn_data.is_nocontent
+ res.status = WEBrick::HTTPStatus::RC_ACCEPTED
+ res.body = ''
+ return
+ end
if conn_data.is_fault
res.status = WEBrick::HTTPStatus::RC_INTERNAL_SERVER_ERROR
end
if outstring = encode_gzip(req, conn_data.send_string)
res['content-encoding'] = 'gzip'