Parent

Unicorn::CGIWrapper

The beginning of a complete wrapper around Unicorn’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 us if you see special warnings. This work is still very alpha so we need testers to help work out the various corner cases.

Constants

NPH

these are stripped out of any keys passed to CGIWrapper.header function

CONNECTION
(Not documented)
CHARSET
(Not documented)
COOKIE
(Not documented)
STATUS
(Not documented)
Status
(Not documented)
SET_COOKIE

some of these are common strings, but this is the only module using them and the reason they’re not in Unicorn::Const

CONTENT_TYPE
(Not documented)
CONTENT_LENGTH
(Not documented)
RACK_INPUT
(Not documented)
RACK_ERRORS
(Not documented)
HEADER_MAP

this maps CGI header names to HTTP header names

Attributes

env_table[R]

(Not documented)

body[R]

(Not documented)

Public Class Methods

new(rack_env, *args) click to toggle source

Takes an a Rackable environment, plus any additional CGI.new arguments These are used internally to create a wrapper around the real CGI while maintaining Rack/Unicorn’s view of the world. This this will NOT deal well with large responses that take up a lot of memory, but neither does the CGI nor the original CGIWrapper from Mongrel…

    # File lib/unicorn/cgi_wrapper.rb, line 57
57:   def initialize(rack_env, *args)
58:     @env_table = rack_env
59:     @status = nil
60:     @head = {}
61:     @headv = Hash.new { |hash,key| hash[key] = [] }
62:     @body = StringIO.new("")
63:     super(*args)
64:   end

Public Instance Methods

header(options = "text/html") click to toggle source

The header is typically called to send back the header. In our case we collect it into a hash for later usage. This can be called multiple times to set different cookies.

     # File lib/unicorn/cgi_wrapper.rb, line 83
 83:   def header(options = "text/html")
 84:     # if they pass in a string then just write the Content-Type
 85:     if String === options
 86:       @head[CONTENT_TYPE] ||= options
 87:     else
 88:       HEADER_MAP.each_pair do |from, to|
 89:         from = options.delete(from) or next
 90:         @head[to] = from.to_s
 91:       end
 92: 
 93:       @head[CONTENT_TYPE] ||= "text/html"
 94:       if charset = options.delete(CHARSET)
 95:         @head[CONTENT_TYPE] << "; charset=#{charset}"
 96:       end
 97: 
 98:       # lots of ways to set cookies
 99:       if cookie = options.delete(COOKIE)
100:         set_cookies = @headv[SET_COOKIE]
101:         case cookie
102:         when Array
103:           cookie.each { |c| set_cookies << c.to_s }
104:         when Hash
105:           cookie.each_value { |c| set_cookies << c.to_s }
106:         else
107:           set_cookies << cookie.to_s
108:         end
109:       end
110:       @status ||= options.delete(STATUS) # all lower-case
111: 
112:       # drop the keys we don't want anymore
113:       options.delete(NPH)
114:       options.delete(CONNECTION)
115: 
116:       # finally, set the rest of the headers as-is, allowing duplicates
117:       options.each_pair { |k,v| @headv[k] << v }
118:     end
119: 
120:     # doing this fakes out the cgi library to think the headers are empty
121:     # we then do the real headers in the out function call later
122:     ""
123:   end
out(options = "text/html") click to toggle source

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. This will have no effect if called the second time if the first “outputted” anything.

     # File lib/unicorn/cgi_wrapper.rb, line 129
129:   def out(options = "text/html")
130:     header(options)
131:     @body.size == 0 or return
132:     @body << yield if block_given?
133:   end
rack_response() click to toggle source

finalizes the response in a way Rack applications would expect

    # File lib/unicorn/cgi_wrapper.rb, line 67
67:   def rack_response
68:     # @head[CONTENT_LENGTH] ||= @body.size
69:     @headv[SET_COOKIE].concat(@output_cookies) if @output_cookies
70:     @headv.each_pair do |key,value|
71:       @head[key] ||= value.join("\n") unless value.empty?
72:     end
73: 
74:     # Capitalized "Status:", with human-readable status code (e.g. "200 OK")
75:     @status ||= @head.delete(Status)
76: 
77:     [ @status || 500, @head, [ @body.string ] ]
78:   end
stdinput() click to toggle source

Used to wrap the normal stdinput variable used inside CGI.

     # File lib/unicorn/cgi_wrapper.rb, line 136
136:   def stdinput
137:     @env_table[RACK_INPUT]
138:   end
stdoutput() click to toggle source

return a pointer to the StringIO body since it’s STDOUT-like

     # File lib/unicorn/cgi_wrapper.rb, line 141
141:   def stdoutput
142:     @body
143:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.