Class: Unicorn::CGIWrapper
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
| Class Chain: 
          self,
          CGI
         | |
| Instance Chain: 
          self,
          CGI
         | |
| Inherits: | CGI 
 | 
| Defined in: | lib/unicorn/cgi_wrapper.rb | 
Overview
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.
Constant Summary
- 
    CHARSET =
    # File 'lib/unicorn/cgi_wrapper.rb', line 31Completely ignored. Why is CGI doing this? 'charset'.freeze 
- 
    CONNECTION =
    # File 'lib/unicorn/cgi_wrapper.rb', line 30Completely ignored, ::Unicornoutputs the date regardless'connection'.freeze 
- 
    CONTENT_LENGTH =
    # File 'lib/unicorn/cgi_wrapper.rb', line 40this is NOT Const::CONTENT_LENGTH'Content-Length'.freeze 
- 
    CONTENT_TYPE =
    
 # File 'lib/unicorn/cgi_wrapper.rb', line 39'Content-Type'.freeze 
- 
    COOKIE =
    # File 'lib/unicorn/cgi_wrapper.rb', line 32this gets appended to Content-Type 'cookie'.freeze 
- 
    HEADER_MAP =
    # File 'lib/unicorn/cgi_wrapper.rb', line 45this maps CGI header names to HTTP header names { 'status' => Status, 'type' => CONTENT_TYPE, 'server' => 'Server'.freeze, 'language' => 'Content-Language'.freeze, 'expires' => 'Expires'.freeze, 'length' => CONTENT_LENGTH, }
- 
    NPH =
    # File 'lib/unicorn/cgi_wrapper.rb', line 29these are stripped out of any keys passed to #header function 'nph'.freeze 
- 
    RACK_ERRORS =
    
 # File 'lib/unicorn/cgi_wrapper.rb', line 42'rack.errors'.freeze 
- 
    RACK_INPUT =
    
 # File 'lib/unicorn/cgi_wrapper.rb', line 41'rack.input'.freeze 
- 
    SET_COOKIE =
    # File 'lib/unicorn/cgi_wrapper.rb', line 38some of these are common strings, but this is the only module using them and the reason they’re not in Const'Set-Cookie'.freeze 
- 
    STATUS =
    # File 'lib/unicorn/cgi_wrapper.rb', line 33maps (Hash,Array,String) to “Set-Cookie” headers 'status'.freeze 
- 
    Status =
    # File 'lib/unicorn/cgi_wrapper.rb', line 34stored as @status 'Status'.freeze 
Class Method Summary
- 
    
      .new(rack_env, *args)  ⇒ CGIWrapper 
    
    constructor
    Takes an a Rackable environment, plus any additional CGI.newarguments These are used internally to create a wrapper around the real CGI while maintaining Rack/Unicorn’s view of the world.
Instance Attribute Summary
- #body readonly
- #env_table readonly
Instance Method Summary
- 
    
      #header(options = "text/html")  
    
    The header is typically called to send back the header. 
- 
    
      #out(options = "text/html")  
    
    The dumb thing is people can call header or this or both and in any order. 
- 
    
      #rack_response  
    
    finalizes the response in a way Rack applications would expect. 
- 
    
      #stdinput  
    
    Used to wrap the normal stdinput variable used inside CGI. 
- 
    
      #stdoutput  
    
    return a pointer to the StringIO body since it’s STDOUT-like. 
Constructor Details
    .new(rack_env, *args)  ⇒ CGIWrapper 
  
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 60
def initialize(rack_env, *args) @env_table = rack_env @status = nil @head = {} @headv = Hash.new { |hash,key| hash[key] = [] } @body = StringIO.new("") super(*args) end
Instance Attribute Details
#body (readonly)
[ GitHub ]# File 'lib/unicorn/cgi_wrapper.rb', line 26
attr_reader :body
#env_table (readonly)
[ GitHub ]# File 'lib/unicorn/cgi_wrapper.rb', line 25
attr_reader :env_table
Instance Method Details
#header(options = "text/html")
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 86
def header( = "text/html") # if they pass in a string then just write the Content-Type if String === @head[CONTENT_TYPE] ||= else HEADER_MAP.each_pair do |from, to| from = .delete(from) or next @head[to] = from.to_s end @head[CONTENT_TYPE] ||= "text/html" if charset = .delete(CHARSET) @head[CONTENT_TYPE] << "; charset=#{charset}" end # lots of ways to set cookies if = .delete(COOKIE) = @headv[SET_COOKIE] case when Array .each { |c| << c.to_s } when Hash .each_value { |c| << c.to_s } else << .to_s end end @status ||= .delete(STATUS) # all lower-case # drop the keys we don't want anymore .delete(NPH) .delete(CONNECTION) # finally, set the rest of the headers as-is, allowing duplicates .each_pair { |k,v| @headv[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")
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 132
def out( = "text/html") header() @body.size == 0 or return @body << yield if block_given? end
#rack_response
finalizes the response in a way Rack applications would expect
# File 'lib/unicorn/cgi_wrapper.rb', line 70
def rack_response # @head[CONTENT_LENGTH] ||= @body.size @headv[SET_COOKIE].concat(@output_cookies) if @output_cookies @headv.each_pair do |key,value| @head[key] ||= value.join("\n") unless value.empty? end # Capitalized "Status:", with human-readable status code (e.g. "200 OK") @status ||= @head.delete(Status) [ @status || 500, @head, [ @body.string ] ] end
#stdinput
Used to wrap the normal stdinput variable used inside CGI.
# File 'lib/unicorn/cgi_wrapper.rb', line 139
def stdinput @env_table[RACK_INPUT] end
#stdoutput
return a pointer to the StringIO body since it’s STDOUT-like
# File 'lib/unicorn/cgi_wrapper.rb', line 144
def stdoutput @body end