An HTTP request class.
A Hash that plugins can use to pass data amongst themselves. The missing-key callback is set to auto-create nested sub-hashes. If you create an HTTPResponse via response, the response's notes will be shared with its request.
The parameters hash parsed from the request
The HTTP verb of the request (as a Symbol)
Override the type of response returned by this request type.
# File lib/strelka/httprequest.rb, line 31
def self::response_class
return Strelka::HTTPResponse
end
Return the unescaped portion of the Request's path relative to the request's route.
# For a handler with a route of '/user', for the request: # "GET /user/1/profile HTTP/1.1" request.app_path # => "/1/profile"
# File lib/strelka/httprequest.rb, line 120
def app_path
rval = URI.unescape( self.uri.path )
rval.slice!( 0, self.route.bytesize )
return rval
end
Return a URI object for the base of the app being run. This is the uri with the app_path and any query string removed.
# For a handler with a route of '/user', for the request: # "GET /user/1/profile HTTP/1.1" request.base_uri # => #<URI::HTTP:0x007fe34d16b2e0 URL:http://localhost:8080/user>
# File lib/strelka/httprequest.rb, line 93
def base_uri
rval = self.uri
rval.path = self.headers.pattern
rval.query = nil
return rval
end
Parse the request parameters and return them as a Hash. For GET requests, these are taken from the query arguments. For requests that commonly contain an entity-body, this method will attempt to parse that.
# For a handler with a route of '/user', for the request: # "GET /user/1/profile?checkbox=1&checkbox=2&text=foo HTTP/1.1" # r.params # => {"checkbox"=>["1", "2"], "text"=>"foo"}
If the request body is not a Hash, an empty Hash with the body's value as the default value will be returned instead.
# File lib/strelka/httprequest.rb, line 138
def params
unless @params
value = nil
case self.verb
when :GET, :HEAD
value = decode_www_form( self.uri.query )
when :POST, :PUT
value = self.parse_body
when :TRACE
self.log.debug "No parameters for a TRACE request."
else
value = self.parse_body if self.content_type
end
value = Hash.new( value ) unless value.is_a?( Hash )
@params = value
end
return @params
rescue => err
self.log.error "%p while parsing the request body: %s" % [ err.class, err.message ]
self.log.debug " %s" % [ err.backtrace.join("\n ") ]
finish_with( HTTP::BAD_REQUEST, "Malformed request body or missing content type." )
end
Parse the request body if it's a representation of a complex data structure.
# File lib/strelka/httprequest.rb, line 171
def parse_body
mimetype = self.content_type or
raise ArgumentError, "Malformed request (no content type?)"
self.body.rewind
case mimetype.split( ';' ).first
when 'application/x-www-form-urlencoded'
return decode_www_form( self.body.read )
when 'application/json', 'text/javascript'
return Yajl.load( self.body )
when 'text/x-yaml', 'application/x-yaml'
return nil if self.body.eof?
return YAML.load( self.body, safe: true )
when 'multipart/form-data'
boundary = self.content_type[ /\bboundary=(\S+)/, 1 ] or
raise Strelka::ParseError, "no boundary found for form data: %p" %
[ self.content_type ]
boundary = dequote( boundary )
parser = Strelka::MultipartParser.new( self.body, boundary )
return parser.parse
else
self.log.debug "don't know how to parse a %p request" % [ self.content_type ]
return {}
end
end
A convenience method for redirecting a request to another URI.
# File lib/strelka/httprequest.rb, line 214
def redirect( uri, perm=false )
code = perm ? HTTP::MOVED_PERMANENTLY : HTTP::MOVED_TEMPORARILY
finish_with( code, "redirect from #{self.uri.path} to #{uri}", :location => uri )
end
Return the unescaped portion of the Request's path that was routed by Mongrel2. This and the app_path make up the path.
# For a handler with a route of '/user', for the request: # "GET /user/1/profile HTTP/1.1" request.route # => "/user"
# File lib/strelka/httprequest.rb, line 108
def route
return URI.unescape( self.headers.pattern )
end
Return a URI object parsed from the URI of the request.
# "GET /user/1/profile HTTP/1.1" request.uri # => #<URI::HTTP:0x007fe34d16b2e0 URL:http://localhost:8080/user/1/profile>
# File lib/strelka/httprequest.rb, line 72
def uri
unless @uri
uri = "%s://%s%s" % [
self.scheme,
self.headers.host,
self.headers.uri
]
@uri = URI( uri )
end
return @uri
end
Initialize some additional stuff for Strelka requests.
# File lib/strelka/httprequest.rb, line 41
def initialize( * ) # :notnew:
super
@uri = nil
@verb = self.headers[:method].to_sym
@params = nil
@notes = Hash.new {|h,k| h[k] = {} }
@cookies = nil
end