Class: Bijou::MiniCGI

Inherits:
Object
  • Object
show all
Defined in:
lib/bijou/minicgi.rb

Overview

MiniCGI provides some of the functionality of the Ruby CGI class in cgi.rb in a way that makes it accessible from the HttpRequest interface. The Ruby CGI class is multipurpose and provides featurs such as HTML rendering, which isn’t useful within the Bijou framework (in part, because it would bypass the Bijou output rendering mechanism).

One significant difference is that MiniCGI always preserves the query string values separately from any posted values. This is similar to the way that PHP and ASP work. Ruby CGI merges all paramters together, regardless of their origin. This feature is also useful, so MiniCGI provides a merged parameter set as well, which is also available via the HttpRequest interface.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(single = true) ⇒ MiniCGI

Set single to true to have request parameters with single values be converted from an array to a single value. Otherwise, all parameter values will be arrays.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bijou/minicgi.rb', line 30

def initialize(single = true)
  @single = single

  # Defines @params, @cookies, @multipart
  extend ::CGI::QueryExtension
  @multipart = false

  # NOTE: We use alternate names for these variables (e.g., get instead
  # of query_string) because CGI::QueryExtension takes the liberty of
  # defining methods on this class for a large set of HTTP-oriented
  # environment variables.
  @get = nil
  @post = nil
  @server = nil
  @method = nil # from REQUEST_METHOD

  initialize_params()
  initialize_server()
end

Instance Attribute Details

#getObject (readonly)

Returns the value of attribute get.



50
51
52
# File 'lib/bijou/minicgi.rb', line 50

def get
  @get
end

#methodObject (readonly)

Returns the value of attribute method.



50
51
52
# File 'lib/bijou/minicgi.rb', line 50

def method
  @method
end

#postObject (readonly)

Returns the value of attribute post.



50
51
52
# File 'lib/bijou/minicgi.rb', line 50

def post
  @post
end

#serverObject (readonly)

Returns the value of attribute server.



50
51
52
# File 'lib/bijou/minicgi.rb', line 50

def server
  @server
end

Class Method Details

.singularize(hash) ⇒ Object

Converts a hash of arrays to a hash of arrays and values whereby any single item arrays in the original hash become values in the result hash.



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/bijou/minicgi.rb', line 145

def self.singularize(hash)
  result = {}

  # Extract any single-item values from their arrays.
  hash.each {|k,v|
    if v.length == 1
      result[k] = v[0]
    else
      result[k] = v
    end
  }
  return result
end

Instance Method Details

#env_tableObject



52
53
54
# File 'lib/bijou/minicgi.rb', line 52

def env_table 
  ENV
end

#initialize_paramsObject

Initialize the data from the query.

NOTE: This overridden version of the CGI equivalent reads data into separate query_string and form tables and, optionally, changes the storage format of parameter values.

Handles multipart forms (in particular, forms that involve file uploads). Reads query parameters into the @params field, and cookies into @cookies.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/bijou/minicgi.rb', line 74

def initialize_params()
  @method = env_table['REQUEST_METHOD']
  @multipart = false

  qs_params = ''
  form_params = ''

  case @method
  when "GET", "HEAD", "POST"
    if defined?(MOD_RUBY)
      qs_params = (Apache::request.args or '')
    else
      qs_params = (env_table['QUERY_STRING'] or '')
    end

    if @method == 'POST'
      if %r|\Amultipart/form-data.*boundary=\"?([^\";,]+)\"?|n.match(env_table['CONTENT_TYPE'])
        boundary = $1.dup
        @multipart = true
        form_params = read_multipart(boundary, Integer(env_table['CONTENT_LENGTH']))
      else
        stdinput.binmode if defined? stdinput.binmode
        form_params = (stdinput.read(Integer(env_table['CONTENT_LENGTH'])) or '')
      end
    end
  else
    qs_params = read_from_cmdline
  end

  query_string = ::CGI::parse(qs_params) || {}
  form         = ::CGI::parse(form_params) || {}
  cookies      = ::CGI::Cookie::parse((env_table['HTTP_COOKIE'] or 
                                       env_table['COOKIE']))

  # If requested, convert all single-element arrays to their values.
  if @single
    @get = Bijou::MiniCGI.singularize(query_string)
    @post = Bijou::MiniCGI.singularize(form)
    @cookies = Bijou::MiniCGI.singularize(cookies)
  else
    @get = query_string
    @post = form
    @cookies = cookies
  end

  @params = @post.clone
  @params.merge!(@get)
  @params.merge!(@cookies)
end

#initialize_serverObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/bijou/minicgi.rb', line 124

def initialize_server
  @server = {}

   %w[ AUTH_TYPE CONTENT_TYPE GATEWAY_INTERFACE PATH_INFO
       PATH_TRANSLATED QUERY_STRING REMOTE_ADDR REMOTE_HOST
       REMOTE_IDENT REMOTE_USER REQUEST_METHOD SCRIPT_NAME
       SERVER_NAME SERVER_PROTOCOL SERVER_SOFTWARE

       REQUEST_URI

       HTTP_ACCEPT HTTP_ACCEPT_CHARSET HTTP_ACCEPT_ENCODING
       HTTP_ACCEPT_LANGUAGE HTTP_CACHE_CONTROL HTTP_FROM HTTP_HOST
       HTTP_NEGOTIATE HTTP_PRAGMA HTTP_REFERER HTTP_USER_AGENT 
   ].each { |env|
     @server[env] = env_table[env]
   }
end

#stdinputObject



56
57
58
# File 'lib/bijou/minicgi.rb', line 56

def stdinput
  $stdin
end

#stdoutputObject



60
61
62
# File 'lib/bijou/minicgi.rb', line 60

def stdoutput
  $DEFAULT_OUTPUT
end