Welcome to the "trac"-ing site of soap4r!
[soap4r] [httpclient] [openpgp4u] [pkcs1] [logger] [csv] [vtr]

root/trunk/lib/soap/rpc/soaplet.rb

Revision 2000, 4.6 kB (checked in by nahi, 1 year ago)
  • removed backward compatibility definitions for soap4r-1.4.X. closes #445.
    • removed SOAPlet#{app_scope_router,add_servant} -> use methods in HTTPServer instead.
    • removed SOAP::WSDLDriver#generateEncodeType -> use SOAP::WSDLDriver#generate_explicit_type
    • removed SOAP::SOAPGenerator -> use SOAP::Generator instead.
    • removed compatibility method definitions for ruby-1.6.X.
  • rpc/encoded service + detail element without xsi:type attribute caused NameError? since 1.5.6. (#435)
  • extract attr_proxy definition. added soap/attrproxy.rb and wsdl/xmlSchema/ref.rb.
  • Property svn:eol-style set to native
  • Property svn:keywords set to author date id revision
Line 
1 # SOAP4R - SOAP handler servlet for WEBrick
2 # Copyright (C) 2000-2007  NAKAMURA, Hiroshi <nahi@ruby-lang.org>.
3
4 # This program is copyrighted free software by NAKAMURA, Hiroshi.  You can
5 # redistribute it and/or modify it under the same terms of Ruby's license;
6 # either the dual license version in 2003, or any later version.
7
8
9 require 'webrick/httpservlet/abstract'
10 require 'webrick/httpstatus'
11 require 'soap/rpc/router'
12 require 'soap/streamHandler'
13 begin
14   require 'stringio'
15   require 'zlib'
16 rescue LoadError
17   warn("Loading stringio or zlib failed.  No gzipped response supported.") if $DEBUG
18 end
19
20
21 warn("Overriding WEBrick::Log#debug") if $DEBUG
22 require 'webrick/log'
23 module WEBrick
24   class Log < BasicLog
25     alias __debug debug
26     def debug(msg = nil)
27       if block_given? and msg.nil?
28         __debug(yield)
29       else
30         __debug(msg)
31       end
32     end
33   end
34 end
35
36
37 module SOAP
38 module RPC
39
40
41 class SOAPlet < WEBrick::HTTPServlet::AbstractServlet
42 public
43   attr_reader :options
44   attr_accessor :authenticator
45
46   def initialize(router = nil)
47     @router = router || ::SOAP::RPC::Router.new(self.class.name)
48     @options = {}
49     @authenticator = nil
50     @config = {}
51   end
52
53   def allow_content_encoding_gzip=(allow)
54     @options[:allow_content_encoding_gzip] = allow
55   end
56
57   ###
58   ## Servlet interfaces for WEBrick.
59   #
60   def get_instance(config, *options)
61     @config = config
62     self
63   end
64
65   def require_path_info?
66     false
67   end
68
69   def do_GET(req, res)
70     res.header['Allow'] = 'POST'
71     raise WEBrick::HTTPStatus::MethodNotAllowed, "GET request not allowed"
72   end
73
74   def do_POST(req, res)
75     logger.debug { "SOAP request: " + req.body } if logger
76     if @authenticator
77       @authenticator.authenticate(req, res)
78       # you can check authenticated user with SOAP::RPC::SOAPlet.user
79     end
80     begin
81       conn_data = ::SOAP::StreamHandler::ConnectionData.new
82       setup_req(conn_data, req)
83       @router.external_ces = @options[:external_ces]
84       Mapping.protect_threadvars(:SOAPlet) do
85         SOAPlet.user = req.user
86         SOAPlet.cookies = req.cookies
87         conn_data = @router.route(conn_data)
88         setup_res(conn_data, req, res)
89       end
90     rescue Exception => e
91       conn_data = @router.create_fault_response(e)
92       res.status = WEBrick::HTTPStatus::RC_INTERNAL_SERVER_ERROR
93       res.body = conn_data.send_string
94       res['content-type'] = conn_data.send_contenttype || "text/xml"
95     end
96     if res.body.is_a?(IO)
97       res.chunked = true
98       logger.debug { "SOAP response: (chunked response not logged)" } if logger
99     else
100       logger.debug { "SOAP response: " + res.body } if logger
101     end
102   end
103
104   def self.cookies
105     get_variable(:Cookies)
106   end
107
108   def self.cookies=(cookies)
109     set_variable(:Cookies, cookies)
110   end
111
112   def self.user
113     get_variable(:User)
114   end
115
116   def self.user=(user)
117     set_variable(:User, user)
118   end
119
120 private
121
122   def self.get_variable(name)
123     if var = Thread.current[:SOAPlet]
124       var[name]
125     else
126       nil
127     end
128   end
129
130   def self.set_variable(name, value)
131     var = Thread.current[:SOAPlet] ||= {}
132     var[name] = value
133   end
134
135   def logger
136     @config[:Logger]
137   end
138
139   def setup_req(conn_data, req)
140     conn_data.receive_string = req.body
141     conn_data.receive_contenttype = req['content-type']
142     conn_data.soapaction = parse_soapaction(req.meta_vars['HTTP_SOAPACTION'])
143   end
144
145   def setup_res(conn_data, req, res)
146     res['content-type'] = conn_data.send_contenttype
147     cookies = SOAPlet.cookies
148     unless cookies.empty?
149       res['set-cookie'] = cookies.collect { |cookie| cookie.to_s }
150     end
151     if conn_data.is_nocontent
152       res.status = WEBrick::HTTPStatus::RC_ACCEPTED
153       res.body = ''
154       return
155     end
156     if conn_data.is_fault
157       res.status = WEBrick::HTTPStatus::RC_INTERNAL_SERVER_ERROR
158     end
159     if outstring = encode_gzip(req, conn_data.send_string)
160       res['content-encoding'] = 'gzip'
161       res['content-length'] = outstring.size
162       res.body = outstring
163     else
164       res.body = conn_data.send_string
165     end
166   end
167
168   def parse_soapaction(soapaction)
169     if !soapaction.nil? and !soapaction.empty?
170       if /^"(.+)"$/ =~ soapaction
171         return $1
172       end
173     end
174     nil
175   end
176
177   def encode_gzip(req, outstring)
178     unless encode_gzip?(req)
179       return nil
180     end
181     begin
182       ostream = StringIO.new
183       gz = Zlib::GzipWriter.new(ostream)
184       gz.write(outstring)
185       ostream.string
186     ensure
187       gz.close
188     end
189   end
190
191   def encode_gzip?(req)
192     @options[:allow_content_encoding_gzip] and defined?(::Zlib) and
193       req['accept-encoding'] and
194       req['accept-encoding'].split(/,\s*/).include?('gzip')
195   end
196 end
197
198
199 end
200 end
Note: See TracBrowser for help on using the browser.