HTTPRequest

class
Superclass
Mongrel2::HTTPRequest
Included Modules
Strelka::Constants
Strelka::ResponseHelpers
Strelka::DataUtilities
Extended With
Loggability

An HTTP request class.

Attributes

notes[R]

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.

params[W]

The parameters hash parsed from the request

verb[RW]

The HTTP verb of the request (as a Symbol)

Public Class Methods

anchor
response_class()

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

Public Instance Methods

anchor
app_path()

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
anchor
base_uri()

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
anchor
cookies()

Fetch any cookies that accompanied the request as a Strelka::CookieSet, creating it if necessary.

# File lib/strelka/httprequest.rb, line 202
def cookies
        @cookies = Strelka::CookieSet.parse( self ) unless @cookies
        return @cookies
rescue => err
        self.log.error "%p while parsing cookies: %s" % [ err.class, err.message ]
        self.log.debug "  %s" % [ err.backtrace.join("\n  ") ]

        finish_with( HTTP::BAD_REQUEST, "Malformed cookie header." )
end
anchor
params()

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
anchor
parse_body()

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
anchor
pattern()
Alias for: route
anchor
redirect( uri, perm=false )

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
Also aliased as: redirect_to
anchor
redirect_to( uri, perm=false )
Alias for: redirect
anchor
route()

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
Also aliased as: pattern
anchor
uri()

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

Protected Instance Methods

anchor
initialize( * )

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