Class: Mongrel::HttpRequest

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

Overview

When a handler is found for a registered URI then this class is constructed and passed to your HttpHandler::process method. You should assume that one handler processes all requests. Included in the HttpRequest is a HttpRequest.params Hash that matches common CGI params, and a HttpRequest.body which is a string containing the request body (raw for now).

Mongrel really only supports small-ish request bodies right now since really huge ones have to be completely read off the wire and put into a string. Later there will be several options for efficiently handling large file uploads.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, initial_body, socket) ⇒ HttpRequest

You don’t really call this. It’s made for you. Main thing it does is hook up the params, and store any remaining body data into the HttpRequest.body attribute.



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/mongrel.rb', line 153

def initialize(params, initial_body, socket)
  @body = initial_body || ""
  @params = params
  @socket = socket

  # now, if the initial_body isn't long enough for the content length we have to fill it
  # TODO: adapt for big ass stuff by writing to a temp file
  clen = params[Const::CONTENT_LENGTH].to_i
  if @body.length < clen
    @body << @socket.read(clen - @body.length)
  end

end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



148
149
150
# File 'lib/mongrel.rb', line 148

def body
  @body
end

#paramsObject (readonly)

Returns the value of attribute params.



148
149
150
# File 'lib/mongrel.rb', line 148

def params
  @params
end

Class Method Details

.escape(s) ⇒ Object



167
168
169
170
171
# File 'lib/mongrel.rb', line 167

def self.escape(s)
  s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
    '%'+$1.unpack('H2'*$1.size).join('%').upcase
  }.tr(' ', '+') 
end

.query_parse(qs, d = '&;') ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/mongrel.rb', line 181

def self.query_parse(qs, d = '&;')
  params = {}
  (qs||'').split(/[#{d}] */n).inject(params) { |h,p|
    k, v=unescape(p).split('=',2)
    if cur = params[k]
      if cur.class == Array
        params[k] << v
      else
        params[k] = [cur, v]
      end
    else
      params[k] = v
    end
  }

  return params
end

.unescape(s) ⇒ Object



174
175
176
177
178
# File 'lib/mongrel.rb', line 174

def self.unescape(s)
  s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n){
    [$1.delete('%')].pack('H*')
  } 
end