Class: Strelka::HTTPRequest

Inherits:
Mongrel2::HTTPRequest
  • Object
show all
Extended by:
Loggability
Includes:
Constants, DataUtilities, ResponseHelpers
Defined in:
lib/strelka/httprequest.rb,
lib/strelka/httprequest/acceptparams.rb

Overview

An HTTP request class.

Defined Under Namespace

Modules: Auth, Negotiation, Session Classes: AcceptParam, Charset, Encoding, Language, MediaType

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ResponseHelpers

finish_with

Methods included from DataUtilities

autovivify, deep_copy, stringify_keys, symbolify_keys

Constructor Details

#initializeHTTPRequest

Initialize some additional stuff for Strelka requests.



42
43
44
45
46
47
48
49
# File 'lib/strelka/httprequest.rb', line 42

def initialize( * ) # :notnew:
	super
	@uri     = nil
	@verb    = self.headers[:method].to_sym
	@params  = nil
	@notes   = Hash.new {|h,k| h[k] = {} }
	@cookies = nil
end

Instance Attribute Details

#notesObject (readonly)

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.



65
66
67
# File 'lib/strelka/httprequest.rb', line 65

def notes
  @notes
end

#paramsObject

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.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/strelka/httprequest.rb', line 139

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

#verbObject

The HTTP verb of the request (as a Symbol)



57
58
59
# File 'lib/strelka/httprequest.rb', line 57

def verb
  @verb
end

Class Method Details

.response_classObject

Override the type of response returned by this request type.



32
33
34
# File 'lib/strelka/httprequest.rb', line 32

def self::response_class
	return Strelka::HTTPResponse
end

Instance Method Details

#app_pathObject

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"


121
122
123
124
125
# File 'lib/strelka/httprequest.rb', line 121

def app_path
	rval = URI.decode_www_form_component( self.uri.path )
	rval.slice!( 0, self.route.bytesize )
	return rval
end

#base_uriObject

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>


94
95
96
97
98
99
# File 'lib/strelka/httprequest.rb', line 94

def base_uri
	rval = self.uri.dup
	rval.path = self.headers.pattern
	rval.query = nil
	return rval
end

#cookiesObject

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



203
204
205
206
207
208
209
210
211
# File 'lib/strelka/httprequest.rb', line 203

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

#parse_bodyObject

Parse the request body if it’s a representation of a complex data structure.



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/strelka/httprequest.rb', line 172

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

#redirect(uri, perm = false) ⇒ Object Also known as: redirect_to

A convenience method for redirecting a request to another URI.



215
216
217
218
# File 'lib/strelka/httprequest.rb', line 215

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

#routeObject Also known as: pattern

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"


109
110
111
# File 'lib/strelka/httprequest.rb', line 109

def route
	return URI.decode_www_form_component( self.headers.pattern )
end

#uriObject

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>


73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/strelka/httprequest.rb', line 73

def uri
	unless @uri
		uri = "%s://%s%s" % [
			self.scheme,
			self.headers.host,
			self.headers.uri
		]
		@uri = URI( uri )
	end

	return @uri
end