Class: Mongrel::CGIWrapper
- Inherits:
-
CGI
- Object
- CGI
- Mongrel::CGIWrapper
- Defined in:
- lib/mongrel/cgi.rb
Overview
The beginning of a complete wrapper around Mongrel’s internal HTTP processing system but maintaining the original Ruby CGI module. Use this only as a crutch to get existing CGI based systems working. It should handle everything, but please notify me if you see special warnings. This work is still very alpha so I need testers to help work out the various corner cases.
The CGIWrapper.handler attribute is normally not set and is available for frameworks that need to get back to the handler. Rails uses this to give people access to the RailsHandler#files (DirHandler really) so they can look-up paths and do other things with the files managed there.
In Rails you can get the real file for a request with:
path = @request.cgi.handler.files.can_serve(@request['PATH_INFO'])
Which is ugly but does the job. Feel free to write a Rails helper for that. Refer to DirHandler#can_serve for more information on this.
Constant Summary collapse
- REMOVED_KEYS =
these are stripped out of any keys passed to CGIWrapper.header function
[ "nph","status","server","connection","type", "charset","length","language","expires"]
Instance Attribute Summary collapse
-
#default_really_final ⇒ Object
Set this to false if you want calls to CGIWrapper.out to not actually send the response until you force it.
-
#handler ⇒ Object
Returns the value of attribute handler.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#args ⇒ Object
Used to wrap the normal args variable used inside CGI.
-
#env_table ⇒ Object
Used to wrap the normal env_table variable used inside CGI.
-
#header(options = "text/html") ⇒ Object
The header is typically called to send back the header.
-
#initialize(request, response, *args) ⇒ CGIWrapper
constructor
Takes an HttpRequest and HttpResponse object, plus any additional arguments normally passed to CGI.
-
#out(options = "text/html", really_final = @default_really_final) ⇒ Object
The dumb thing is people can call header or this or both and in any order.
-
#send_cookies(to) ⇒ Object
Takes any ‘cookie’ setting and sends it over the Mongrel header, then removes the setting from the options.
-
#status ⇒ Object
Computes the status once, but lazily so that people who call header twice don’t get penalized.
-
#stdinput ⇒ Object
Used to wrap the normal stdinput variable used inside CGI.
-
#stdoutput ⇒ Object
The stdoutput should be completely bypassed but we’ll drop a warning just in case.
Constructor Details
#initialize(request, response, *args) ⇒ CGIWrapper
Takes an HttpRequest and HttpResponse object, plus any additional arguments normally passed to CGI. These are used internally to create a wrapper around the real CGI while maintaining Mongrel’s view of the world.
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mongrel/cgi.rb', line 42 def initialize(request, response, *args) @request = request @response = response @args = *args @input = request.body @head = {} @out_called = false @default_really_final=true super(*args) end |
Instance Attribute Details
#default_really_final ⇒ Object
Set this to false if you want calls to CGIWrapper.out to not actually send the response until you force it.
33 34 35 |
# File 'lib/mongrel/cgi.rb', line 33 def default_really_final @default_really_final end |
#handler ⇒ Object
Returns the value of attribute handler.
30 31 32 |
# File 'lib/mongrel/cgi.rb', line 30 def handler @handler end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
29 30 31 |
# File 'lib/mongrel/cgi.rb', line 29 def @options end |
Instance Method Details
#args ⇒ Object
Used to wrap the normal args variable used inside CGI.
160 161 162 |
# File 'lib/mongrel/cgi.rb', line 160 def args @args end |
#env_table ⇒ Object
Used to wrap the normal env_table variable used inside CGI.
165 166 167 |
# File 'lib/mongrel/cgi.rb', line 165 def env_table @request.params end |
#header(options = "text/html") ⇒ Object
The header is typically called to send back the header. In our case we collect it into a hash for later usage.
nph – Mostly ignored. It’ll output the date. connection – Completely ignored. Why is CGI doing this? length – Ignored since Mongrel figures this out from what you write to output.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/mongrel/cgi.rb', line 60 def header( = "text/html") # if they pass in a string then just write the Content-Type if .class == String @head['Content-Type'] = unless @head['Content-Type'] else # convert the given options into what Mongrel wants @head['Content-Type'] = ['type'] || "text/html" @head['Content-Type'] += "; charset=" + ['charset'] if .has_key? "charset" if ['charset'] # setup date only if they use nph @head['Date'] = CGI::rfc1123_date(Time.now) if ['nph'] # setup the server to use the default or what they set @head['Server'] = ['server'] || env_table['SERVER_SOFTWARE'] # remaining possible options they can give @head['Status'] = ['status'] if ['status'] @head['Content-Language'] = ['language'] if ['language'] @head['Expires'] = ['expires'] if ['expires'] # drop the keys we don't want anymore REMOVED_KEYS.each {|k| .delete(k) } # finally just convert the rest raw (which puts 'cookie' directly) # 'cookie' is translated later as we write the header out .each{|k,v| @head[k] = v} end # doing this fakes out the cgi library to think the headers are empty # we then do the real headers in the out function call later "" end |
#out(options = "text/html", really_final = @default_really_final) ⇒ Object
The dumb thing is people can call header or this or both and in any order. So, we just reuse header and then finalize the HttpResponse the right way. Status is taken from the various options and converted to what Mongrel needs via the CGIWrapper.status function.
We also prevent Rails from actually doing the final send by adding a second parameter “really_final”. Only Mongrel calls this after Rails is done. Since this will break other frameworks, it defaults to a different setting for rails (false) and (true) for others.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/mongrel/cgi.rb', line 127 def out( = "text/html", really_final=@default_really_final) if @out_called || !really_final # don't do it more than once or if it's not the really final call return end header() @response.start status do |head, body| (head) @head.each {|k,v| head[k] = v} body.write(yield || "") end @out_called = true end |
#send_cookies(to) ⇒ Object
Takes any ‘cookie’ setting and sends it over the Mongrel header, then removes the setting from the options. If cookie is an Array or Hash then it sends those on with .to_s, otherwise it just calls .to_s on it and hopefully your “cookie” can write itself correctly.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/mongrel/cgi.rb', line 98 def (to) # convert the cookies based on the myriad of possible ways to set a cookie if @head['cookie'] = @head['cookie'] case when Array .each {|c| to['Set-Cookie'] = c.to_s } when Hash .each_value {|c| to['Set-Cookie'] = c.to_s} else to['Set-Cookie'] = ['cookie'].to_s end @head.delete('cookie') end # @output_cookies seems to never be used, but we'll process it just in case @output_cookies.each {|c| to['Set-Cookie'] = c.to_s } if @output_cookies end |
#status ⇒ Object
Computes the status once, but lazily so that people who call header twice don’t get penalized. Because CGI insists on including the options status message in the status we have to do a bit of parsing.
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/mongrel/cgi.rb', line 148 def status if not @status stat = @head["Status"] stat = stat.split(' ')[0] if stat @status = stat || "200" end @status end |
#stdinput ⇒ Object
Used to wrap the normal stdinput variable used inside CGI.
170 171 172 |
# File 'lib/mongrel/cgi.rb', line 170 def stdinput @input end |
#stdoutput ⇒ Object
The stdoutput should be completely bypassed but we’ll drop a warning just in case
175 176 177 178 |
# File 'lib/mongrel/cgi.rb', line 175 def stdoutput STDERR.puts "WARNING: Your program is doing something not expected. Please tell Zed that stdoutput was used and what software you are running. Thanks." @response.body end |