Skip to content
This repository
Newer
Older
100644 393 lines (327 sloc) 12.833 kb
7ed819ad » chneukirchen
2007-02-16 Add Rack::Response and Rack::Utils
1 require 'rack/utils'
2
5f13e3d0 » chneukirchen
2007-02-16 Add Rack::Request
3 module Rack
376fa1e3 » chneukirchen
2007-03-01 Add RDocs
4 # Rack::Request provides a convenient interface to a Rack
5 # environment. It is stateless, the environment +env+ passed to the
6 # constructor will be directly modified.
7 #
8 # req = Rack::Request.new(env)
9 # req.post?
10 # req.params["data"]
230d62c7 » chneukirchen
2007-05-16 Fix trailing whitespace. Sigh.
11
5f13e3d0 » chneukirchen
2007-02-16 Add Rack::Request
12 class Request
376fa1e3 » chneukirchen
2007-03-01 Add RDocs
13 # The environment of the request.
05ab0f90 » chneukirchen
2007-02-18 More convenience for Rack::Request
14 attr_reader :env
230d62c7 » chneukirchen
2007-05-16 Fix trailing whitespace. Sigh.
15
5f13e3d0 » chneukirchen
2007-02-16 Add Rack::Request
16 def initialize(env)
17 @env = env
18 end
19
71030b9d » josh
2009-08-05 Revert "Add common HTTP strings to Rack::Const"
20 def body; @env["rack.input"] end
21 def script_name; @env["SCRIPT_NAME"].to_s end
22 def path_info; @env["PATH_INFO"].to_s end
23 def request_method; @env["REQUEST_METHOD"] end
24 def query_string; @env["QUERY_STRING"].to_s end
25 def content_length; @env['CONTENT_LENGTH'] end
8810d690 » richmeyers
2010-08-12 Treat an empty content type as nil content type (issue #40)
26
27 def content_type
28 content_type = @env['CONTENT_TYPE']
29 content_type.nil? || content_type.empty? ? nil : content_type
30 end
31
71030b9d » josh
2009-08-05 Revert "Add common HTTP strings to Rack::Const"
32 def session; @env['rack.session'] ||= {} end
33 def session_options; @env['rack.session.options'] ||= {} end
df22480a » josh
2009-12-11 rack.logger specification
34 def logger; @env['rack.logger'] end
fe40ec21 » chneukirchen
2008-03-15 Fix that Request assumes form-data even when Content-Type says otherwise
35
36 # The media type (type/subtype) portion of the CONTENT_TYPE header
37 # without any media type parameters. e.g., when CONTENT_TYPE is
38 # "text/plain;charset=utf-8", the media-type is "text/plain".
39 #
40 # For more information on the use of media types in HTTP, see:
41 # http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7
42 def media_type
939fa7d2 » visionmedia
2009-03-18 Refactored Rack::Request#media_type
43 content_type && content_type.split(/\s*[;,]\s*/, 2).first.downcase
fe40ec21 » chneukirchen
2008-03-15 Fix that Request assumes form-data even when Content-Type says otherwise
44 end
45
46 # The media type parameters provided in CONTENT_TYPE as a Hash, or
47 # an empty Hash if no CONTENT_TYPE or media-type parameters were
48 # provided. e.g., when the CONTENT_TYPE is "text/plain;charset=utf-8",
49 # this method responds with the following Hash:
50 # { 'charset' => 'utf-8' }
51 def media_type_params
52 return {} if content_type.nil?
d3893c27 » raggi
2010-10-03 186 bro :'(
53 Hash[*content_type.split(/\s*[;,]\s*/)[1..-1].
fe40ec21 » chneukirchen
2008-03-15 Fix that Request assumes form-data even when Content-Type says otherwise
54 collect { |s| s.split('=', 2) }.
d3893c27 » raggi
2010-10-03 186 bro :'(
55 map { |k,v| [k.downcase, v] }.flatten]
fe40ec21 » chneukirchen
2008-03-15 Fix that Request assumes form-data even when Content-Type says otherwise
56 end
57
58 # The character set of the request body if a "charset" media type
59 # parameter was given, or nil if no "charset" was specified. Note
60 # that, per RFC2616, text/* media types that specify no explicit
61 # charset are to be considered ISO-8859-1.
62 def content_charset
63 media_type_params['charset']
64 end
a18fc325 » chneukirchen
2007-02-18 Rename Request#method to #request_method to not confuse stdlibs
65
dda892d6 » josh
2010-10-26 Request#scheme returns 'https' if forwarded protocol headers are set
66 def scheme
67 if @env['HTTPS'] == 'on'
68 'https'
35bb5ba6 » stephencelis
2010-12-09 Rack::Request support for HTTP_X_FORWARDED_{PORT,SSL}
69 elsif @env['HTTP_X_FORWARDED_SSL'] == 'on'
70 'https'
2598035d » raggi
2011-12-27 Add support for HTTP_X_FORWARDED_SCHEME. From pull request 292, reimp…
71 elsif @env['HTTP_X_FORWARDED_SCHEME']
72 @env['HTTP_X_FORWARDED_SCHEME']
dda892d6 » josh
2010-10-26 Request#scheme returns 'https' if forwarded protocol headers are set
73 elsif @env['HTTP_X_FORWARDED_PROTO']
85ca454e » martoche
2010-11-02 HTTP_X_FORWARDED_PROTO can be a list of chained values, eg 'https, ht…
74 @env['HTTP_X_FORWARDED_PROTO'].split(',')[0]
dda892d6 » josh
2010-10-26 Request#scheme returns 'https' if forwarded protocol headers are set
75 else
76 @env["rack.url_scheme"]
77 end
78 end
79
80 def ssl?
81 scheme == 'https'
82 end
83
fb32ff74 » josh
2009-12-20 Request#host knows about forwared host [#77 state:resolved]
84 def host_with_port
85 if forwarded = @env["HTTP_X_FORWARDED_HOST"]
86 forwarded.split(/,\s?/).last
87 else
88 @env['HTTP_HOST'] || "#{@env['SERVER_NAME'] || @env['SERVER_ADDR']}:#{@env['SERVER_PORT']}"
89 end
90 end
35bb5ba6 » stephencelis
2010-12-09 Rack::Request support for HTTP_X_FORWARDED_{PORT,SSL}
91
4bfe1d74 » brasten
2010-06-29 Modified Rack::Request#port to respect HTTP_X_FORWARDED_HOST port inf…
92 def port
35bb5ba6 » stephencelis
2010-12-09 Rack::Request support for HTTP_X_FORWARDED_{PORT,SSL}
93 if port = host_with_port.split(/:/)[1]
94 port.to_i
95 elsif port = @env['HTTP_X_FORWARDED_PORT']
96 port.to_i
1c271577 » jonleighton
2011-01-24 If there is an X-Forwarded-Host header, we should take the absence of…
97 elsif @env.has_key?("HTTP_X_FORWARDED_HOST")
fe431e75 » rkh
2012-12-12 better support for coffee protocol
98 DEFAULT_PORTS[scheme]
da7efac4 » manveru
2013-04-19 correct Request#port for lighttpd2 proxy case
99 elsif @env.has_key?("HTTP_X_FORWARDED_PROTO")
100 DEFAULT_PORTS[@env['HTTP_X_FORWARDED_PROTO']]
35bb5ba6 » stephencelis
2010-12-09 Rack::Request support for HTTP_X_FORWARDED_{PORT,SSL}
101 else
102 @env["SERVER_PORT"].to_i
103 end
4bfe1d74 » brasten
2010-06-29 Modified Rack::Request#port to respect HTTP_X_FORWARDED_HOST port inf…
104 end
fb32ff74 » josh
2009-12-20 Request#host knows about forwared host [#77 state:resolved]
105
c95b7516 » chneukirchen
2007-02-28 Remove the port number of HTTP_HOST and SERVER_NAME
106 def host
107 # Remove port number.
f6673c77 » benpickles
2013-08-20 Use `sub` as we only need to substitute once.
108 host_with_port.to_s.sub(/:\d+\z/, '')
c95b7516 » chneukirchen
2007-02-28 Remove the port number of HTTP_HOST and SERVER_NAME
109 end
110
71030b9d » josh
2009-08-05 Revert "Add common HTTP strings to Rack::Const"
111 def script_name=(s); @env["SCRIPT_NAME"] = s.to_s end
112 def path_info=(s); @env["PATH_INFO"] = s.to_s end
a18fc325 » chneukirchen
2007-02-18 Rename Request#method to #request_method to not confuse stdlibs
113
d7decc46 » yb66
2011-09-13 added some comments to HTTP verb checks, for the rdoc
114
115 # Checks the HTTP request method (or verb) to see if it was of type DELETE
320d8009 » manveru
2010-06-11 Add Request.trace? and Request.options?
116 def delete?; request_method == "DELETE" end
d0fa93d2 » rkh
2012-12-12 whitespace removal
117
d7decc46 » yb66
2011-09-13 added some comments to HTTP verb checks, for the rdoc
118 # Checks the HTTP request method (or verb) to see if it was of type GET
320d8009 » manveru
2010-06-11 Add Request.trace? and Request.options?
119 def get?; request_method == "GET" end
d0fa93d2 » rkh
2012-12-12 whitespace removal
120
d7decc46 » yb66
2011-09-13 added some comments to HTTP verb checks, for the rdoc
121 # Checks the HTTP request method (or verb) to see if it was of type HEAD
320d8009 » manveru
2010-06-11 Add Request.trace? and Request.options?
122 def head?; request_method == "HEAD" end
d0fa93d2 » rkh
2012-12-12 whitespace removal
123
d7decc46 » yb66
2011-09-13 added some comments to HTTP verb checks, for the rdoc
124 # Checks the HTTP request method (or verb) to see if it was of type OPTIONS
320d8009 » manveru
2010-06-11 Add Request.trace? and Request.options?
125 def options?; request_method == "OPTIONS" end
d0fa93d2 » rkh
2012-12-12 whitespace removal
126
a81b0f97 » jamiehodge
2013-06-15 add #link? and #unlink?
127 # Checks the HTTP request method (or verb) to see if it was of type LINK
128 def link?; request_method == "LINK" end
129
d7decc46 » yb66
2011-09-13 added some comments to HTTP verb checks, for the rdoc
130 # Checks the HTTP request method (or verb) to see if it was of type PATCH
828e2ab0 » rkh
2011-03-18 add Rack::Request#patch?
131 def patch?; request_method == "PATCH" end
d0fa93d2 » rkh
2012-12-12 whitespace removal
132
d7decc46 » yb66
2011-09-13 added some comments to HTTP verb checks, for the rdoc
133 # Checks the HTTP request method (or verb) to see if it was of type POST
320d8009 » manveru
2010-06-11 Add Request.trace? and Request.options?
134 def post?; request_method == "POST" end
d0fa93d2 » rkh
2012-12-12 whitespace removal
135
d7decc46 » yb66
2011-09-13 added some comments to HTTP verb checks, for the rdoc
136 # Checks the HTTP request method (or verb) to see if it was of type PUT
320d8009 » manveru
2010-06-11 Add Request.trace? and Request.options?
137 def put?; request_method == "PUT" end
d0fa93d2 » rkh
2012-12-12 whitespace removal
138
d7decc46 » yb66
2011-09-13 added some comments to HTTP verb checks, for the rdoc
139 # Checks the HTTP request method (or verb) to see if it was of type TRACE
320d8009 » manveru
2010-06-11 Add Request.trace? and Request.options?
140 def trace?; request_method == "TRACE" end
3a8bd8bb » craic
2013-11-25 Add accept_language method to request to extract HTTP_ACCEPT_LANGUAGE…
141
a81b0f97 » jamiehodge
2013-06-15 add #link? and #unlink?
142 # Checks the HTTP request method (or verb) to see if it was of type UNLINK
143 def unlink?; request_method == "UNLINK" end
fe40ec21 » chneukirchen
2008-03-15 Fix that Request assumes form-data even when Content-Type says otherwise
144
d7decc46 » yb66
2011-09-13 added some comments to HTTP verb checks, for the rdoc
145
fe40ec21 » chneukirchen
2008-03-15 Fix that Request assumes form-data even when Content-Type says otherwise
146 # The set of form-data media-types. Requests that do not indicate
147 # one of the media types presents in this list will not be eligible
148 # for form-data / param parsing.
149 FORM_DATA_MEDIA_TYPES = [
150 'application/x-www-form-urlencoded',
151 'multipart/form-data'
152 ]
153
6674f365 » eTM
2009-04-11 better multipart handling
154 # The set of media-types. Requests that do not indicate
155 # one of the media types presents in this list will not be eligible
156 # for param parsing like soap attachments or generic multiparts
157 PARSEABLE_DATA_MEDIA_TYPES = [
158 'multipart/related',
159 'multipart/mixed'
df22480a » josh
2009-12-11 rack.logger specification
160 ]
6674f365 » eTM
2009-04-11 better multipart handling
161
fe431e75 » rkh
2012-12-12 better support for coffee protocol
162 # Default ports depending on scheme. Used to decide whether or not
163 # to include the port in a generated URI.
164 DEFAULT_PORTS = { 'http' => 80, 'https' => 443, 'coffee' => 80 }
165
fe40ec21 » chneukirchen
2008-03-15 Fix that Request assumes form-data even when Content-Type says otherwise
166 # Determine whether the request body contains form-data by checking
aea2d608 » rtomayko
2009-01-15 Use Content-Type to determine POST params parsing [#20]
167 # the request Content-Type for one of the media-types:
168 # "application/x-www-form-urlencoded" or "multipart/form-data". The
fe40ec21 » chneukirchen
2008-03-15 Fix that Request assumes form-data even when Content-Type says otherwise
169 # list of form-data media types can be modified through the
170 # +FORM_DATA_MEDIA_TYPES+ array.
aea2d608 » rtomayko
2009-01-15 Use Content-Type to determine POST params parsing [#20]
171 #
172 # A request body is also assumed to contain form-data when no
173 # Content-Type header is provided and the request_method is POST.
fe40ec21 » chneukirchen
2008-03-15 Fix that Request assumes form-data even when Content-Type says otherwise
174 def form_data?
aea2d608 » rtomayko
2009-01-15 Use Content-Type to determine POST params parsing [#20]
175 type = media_type
176 meth = env["rack.methodoverride.original_method"] || env['REQUEST_METHOD']
177 (meth == 'POST' && type.nil?) || FORM_DATA_MEDIA_TYPES.include?(type)
fe40ec21 » chneukirchen
2008-03-15 Fix that Request assumes form-data even when Content-Type says otherwise
178 end
5f13e3d0 » chneukirchen
2007-02-16 Add Rack::Request
179
6674f365 » eTM
2009-04-11 better multipart handling
180 # Determine whether the request body contains data by checking
181 # the request media_type against registered parse-data media-types
182 def parseable_data?
183 PARSEABLE_DATA_MEDIA_TYPES.include?(media_type)
184 end
185
89a88a12 » jm3
2011-12-28 ...except after c.
186 # Returns the data received in the query string.
5f13e3d0 » chneukirchen
2007-02-16 Add Rack::Request
187 def GET
71030b9d » josh
2009-08-05 Revert "Add common HTTP strings to Rack::Const"
188 if @env["rack.request.query_string"] == query_string
189 @env["rack.request.query_hash"]
6c80c6cf » chneukirchen
2007-02-19 Cache the parsed things in Rack::Request
190 else
5a5aee36 » vspy
2013-10-23 correct handling for incomplete hash/array parameters
191 p = parse_query(query_string)
71030b9d » josh
2009-08-05 Revert "Add common HTTP strings to Rack::Const"
192 @env["rack.request.query_string"] = query_string
5a5aee36 » vspy
2013-10-23 correct handling for incomplete hash/array parameters
193 @env["rack.request.query_hash"] = p
6c80c6cf » chneukirchen
2007-02-19 Cache the parsed things in Rack::Request
194 end
5f13e3d0 » chneukirchen
2007-02-16 Add Rack::Request
195 end
196
89a88a12 » jm3
2011-12-28 ...except after c.
197 # Returns the data received in the request body.
376fa1e3 » chneukirchen
2007-03-01 Add RDocs
198 #
199 # This method support both application/x-www-form-urlencoded and
200 # multipart/form-data.
5f13e3d0 » chneukirchen
2007-02-16 Add Rack::Request
201 def POST
71030b9d » josh
2009-08-05 Revert "Add common HTTP strings to Rack::Const"
202 if @env["rack.input"].nil?
01c17705 » jeremy
2009-05-14 Rack::Request#POST raises if rack.input is missing instead of returni…
203 raise "Missing rack.input"
b0593078 » statianzo
2013-07-18 Use equal? to compare form_input to rack.input
204 elsif @env["rack.request.form_input"].equal? @env["rack.input"]
71030b9d » josh
2009-08-05 Revert "Add common HTTP strings to Rack::Const"
205 @env["rack.request.form_hash"]
6674f365 » eTM
2009-04-11 better multipart handling
206 elsif form_data? || parseable_data?
d8ccbf37 » josh
2009-12-20 Extract parse_query and parse_multipart in Request so subclasses
207 unless @env["rack.request.form_hash"] = parse_multipart(env)
71030b9d » josh
2009-08-05 Revert "Add common HTTP strings to Rack::Const"
208 form_vars = @env["rack.input"].read
0f081dde » josh
2009-01-18 Clean up Safari's ajax POST body
209
210 # Fix for Safari Ajax postings that always append \0
51fc8649 » raggi
2011-05-02 slice is about double the speed on MRI, and slightly faster on 1.9
211 # form_vars.sub!(/\0\z/, '') # performance replacement:
212 form_vars.slice!(-1) if form_vars[-1] == ?\0
0f081dde » josh
2009-01-18 Clean up Safari's ajax POST body
213
71030b9d » josh
2009-08-05 Revert "Add common HTTP strings to Rack::Const"
214 @env["rack.request.form_vars"] = form_vars
d8ccbf37 » josh
2009-12-20 Extract parse_query and parse_multipart in Request so subclasses
215 @env["rack.request.form_hash"] = parse_query(form_vars)
3513f4fd » josh
2009-01-13 Add tests for multipart uploads. Also ensure multipart parser tries t…
216
71030b9d » josh
2009-08-05 Revert "Add common HTTP strings to Rack::Const"
217 @env["rack.input"].rewind
4fe5360a » chneukirchen
2007-02-28 Make Rack::Request read multipart form data
218 end
8d468ee7 » jdelStrother
2013-05-21 Ensure request.POST always raises EOFError on bad input
219 @env["rack.request.form_input"] = @env["rack.input"]
71030b9d » josh
2009-08-05 Revert "Add common HTTP strings to Rack::Const"
220 @env["rack.request.form_hash"]
fe40ec21 » chneukirchen
2008-03-15 Fix that Request assumes form-data even when Content-Type says otherwise
221 else
222 {}
6c80c6cf » chneukirchen
2007-02-19 Cache the parsed things in Rack::Request
223 end
5f13e3d0 » chneukirchen
2007-02-16 Add Rack::Request
224 end
225
376fa1e3 » chneukirchen
2007-03-01 Add RDocs
226 # The union of GET and POST data.
02c4ecd2 » seamusabshere
2012-05-01 provide Rack::Request#update_param and Rack::Request#delete_param
227 #
228 # Note that modifications will not be persisted in the env. Use update_param or delete_param if you want to destructively modify params.
5f13e3d0 » chneukirchen
2007-02-16 Add Rack::Request
229 def params
91bbcea2 » skade
2011-05-03 Keep #params from merging POST into GET
230 @params ||= self.GET.merge(self.POST)
cbcae3a2 » tenderlove
2011-01-14 fixing unused variable warnings in 1.9.3
231 rescue EOFError
02c4ecd2 » seamusabshere
2012-05-01 provide Rack::Request#update_param and Rack::Request#delete_param
232 self.GET.dup
233 end
234
235 # Destructively update a parameter, whether it's in GET and/or POST. Returns nil.
236 #
a5ba82b0 » seamusabshere
2012-05-02 only update param where it was previously defined
237 # The parameter is updated wherever it was previous defined, so GET, POST, or both. If it wasn't previously defined, it's inserted into GET.
238 #
239 # env['rack.input'] is not touched.
02c4ecd2 » seamusabshere
2012-05-01 provide Rack::Request#update_param and Rack::Request#delete_param
240 def update_param(k, v)
a5ba82b0 » seamusabshere
2012-05-02 only update param where it was previously defined
241 found = false
242 if self.GET.has_key?(k)
243 found = true
244 self.GET[k] = v
245 end
246 if self.POST.has_key?(k)
247 found = true
248 self.POST[k] = v
249 end
250 unless found
251 self.GET[k] = v
252 end
02c4ecd2 » seamusabshere
2012-05-01 provide Rack::Request#update_param and Rack::Request#delete_param
253 @params = nil
254 nil
255 end
256
257 # Destructively delete a parameter, whether it's in GET or POST. Returns the value of the deleted parameter.
258 #
259 # If the parameter is in both GET and POST, the POST value takes precedence since that's how #params works.
a5ba82b0 » seamusabshere
2012-05-02 only update param where it was previously defined
260 #
261 # env['rack.input'] is not touched.
02c4ecd2 » seamusabshere
2012-05-01 provide Rack::Request#update_param and Rack::Request#delete_param
262 def delete_param(k)
263 v = [ self.POST.delete(k), self.GET.delete(k) ].compact.first
264 @params = nil
265 v
5f13e3d0 » chneukirchen
2007-02-16 Add Rack::Request
266 end
267
de7411c5 » manveru
2007-05-15 add some features to Request and the corresponding tests for them
268 # shortcut for request.params[key]
269 def [](key)
270 params[key.to_s]
271 end
272
273 # shortcut for request.params[key] = value
02c4ecd2 » seamusabshere
2012-05-01 provide Rack::Request#update_param and Rack::Request#delete_param
274 #
275 # Note that modifications will not be persisted in the env. Use update_param or delete_param if you want to destructively modify params.
de7411c5 » manveru
2007-05-15 add some features to Request and the corresponding tests for them
276 def []=(key, value)
277 params[key.to_s] = value
278 end
279
280 # like Hash#values_at
281 def values_at(*keys)
282 keys.map{|key| params[key] }
283 end
284
aa7ce77c » ghazel
2010-09-17 do not default referer to '/'
285 # the referer of the client
de7411c5 » manveru
2007-05-15 add some features to Request and the corresponding tests for them
286 def referer
aa7ce77c » ghazel
2010-09-17 do not default referer to '/'
287 @env['HTTP_REFERER']
de7411c5 » manveru
2007-05-15 add some features to Request and the corresponding tests for them
288 end
289 alias referrer referer
290
e2a1a644 » josh
2009-12-20 Request#user_agent
291 def user_agent
292 @env['HTTP_USER_AGENT']
293 end
de7411c5 » manveru
2007-05-15 add some features to Request and the corresponding tests for them
294
5f13e3d0 » chneukirchen
2007-02-16 Add Rack::Request
295 def cookies
84a9d3ee » rkh
2011-08-23 refactor Request#cookies, fixes #225
296 hash = @env["rack.request.cookie_hash"] ||= {}
297 string = @env["HTTP_COOKIE"]
298
299 return hash if string == @env["rack.request.cookie_string"]
02209e2e » lawrencepit
2011-09-03 Request should return cookies, also when initially not set.
300 hash.clear
84a9d3ee » rkh
2011-08-23 refactor Request#cookies, fixes #225
301
302 # According to RFC 2109:
303 # If multiple cookies satisfy the criteria above, they are ordered in
304 # the Cookie header such that those with more specific Path attributes
305 # precede those with less specific. Ordering with respect to other
306 # attributes (e.g., Domain) is unspecified.
8adaab4b » raggi
2012-12-29 Do not fail on cookies that are not URI escaped
307 cookies = Utils.parse_query(string, ';,') { |s| Rack::Utils.unescape(s) rescue s }
308 cookies.each { |k,v| hash[k] = Array === v ? v.first : v }
84a9d3ee » rkh
2011-08-23 refactor Request#cookies, fixes #225
309 @env["rack.request.cookie_string"] = string
310 hash
5f13e3d0 » chneukirchen
2007-02-16 Add Rack::Request
311 end
312
313 def xhr?
71030b9d » josh
2009-08-05 Revert "Add common HTTP strings to Rack::Const"
314 @env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest"
5f13e3d0 » chneukirchen
2007-02-16 Add Rack::Request
315 end
fd51d363 » chneukirchen
2007-02-21 Add Request#url
316
d7a3bb21 » josh
2011-01-03 Extract Request#base_url
317 def base_url
fe431e75 » rkh
2012-12-12 better support for coffee protocol
318 url = "#{scheme}://#{host}"
319 url << ":#{port}" if port != DEFAULT_PORTS[scheme]
fd51d363 » chneukirchen
2007-02-21 Add Request#url
320 url
321 end
df22480a » josh
2009-12-11 rack.logger specification
322
d7a3bb21 » josh
2011-01-03 Extract Request#base_url
323 # Tries to return a remake of the original request URL as a string.
324 def url
325 base_url + fullpath
326 end
327
3a544a7a » visionmedia
2009-03-18 Added Rack::Request#path
328 def path
329 script_name + path_info
330 end
df22480a » josh
2009-12-11 rack.logger specification
331
234c062f » chneukirchen
2007-03-30 Add Request#fullpath
332 def fullpath
3a544a7a » visionmedia
2009-03-18 Added Rack::Request#path
333 query_string.empty? ? path : "#{path}?#{query_string}"
234c062f » chneukirchen
2007-03-30 Add Request#fullpath
334 end
0f2dab57 » qerub
2008-07-01 Added support for Accept-Encoding (via Request#accept_encoding and Ut…
335
336 def accept_encoding
9798ae21 » craic
2013-12-05 add parse_http_accept_header utility method to simplify accept_encodi…
337 parse_http_accept_header(@env["HTTP_ACCEPT_ENCODING"])
0f2dab57 » qerub
2008-07-01 Added support for Accept-Encoding (via Request#accept_encoding and Ut…
338 end
a48eb513 » manveru
2008-08-30 Add Request#ip and corresponding spec
339
3a8bd8bb » craic
2013-11-25 Add accept_language method to request to extract HTTP_ACCEPT_LANGUAGE…
340 def accept_language
9798ae21 » craic
2013-12-05 add parse_http_accept_header utility method to simplify accept_encodi…
341 parse_http_accept_header(@env["HTTP_ACCEPT_LANGUAGE"])
3a8bd8bb » craic
2013-11-25 Add accept_language method to request to extract HTTP_ACCEPT_LANGUAGE…
342 end
343
a9fae37b » martoche
2011-06-20 moved the trusted_proxy regex into a separate method to allow overrid…
344 def trusted_proxy?(ip)
5c8a5b7b » raggi
2013-02-02 Request#trusted_proxy? no longer accepts lines
345 ip =~ /\A127\.0\.0\.1\Z|\A(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\.|\A::1\Z|\Afd[0-9a-f]{2}:.+|\Alocalhost\Z|\Aunix\Z|\Aunix:/i
a9fae37b » martoche
2011-06-20 moved the trusted_proxy regex into a separate method to allow overrid…
346 end
347
a48eb513 » manveru
2008-08-30 Add Request#ip and corresponding spec
348 def ip
42f4b951 » Rodrigo Flores
2012-01-10 Refactoring ip method
349 remote_addrs = split_ip_addresses(@env['REMOTE_ADDR'])
ab637df5 » Rodrigo Flores
2012-01-10 Improving a method name
350 remote_addrs = reject_trusted_ip_addresses(remote_addrs)
d0fa93d2 » rkh
2012-12-12 whitespace removal
351
250cde15 » martoche
2011-06-20 support for trusted proxies and IPv6 for Request#ip
352 return remote_addrs.first if remote_addrs.any?
353
42f4b951 » Rodrigo Flores
2012-01-10 Refactoring ip method
354 forwarded_ips = split_ip_addresses(@env['HTTP_X_FORWARDED_FOR'])
250cde15 » martoche
2011-06-20 support for trusted proxies and IPv6 for Request#ip
355
356 if client_ip = @env['HTTP_CLIENT_IP']
357 # If forwarded_ips doesn't include the client_ip, it might be an
358 # ip spoofing attempt, so we ignore HTTP_CLIENT_IP
359 return client_ip if forwarded_ips.include?(client_ip)
a48eb513 » manveru
2008-08-30 Add Request#ip and corresponding spec
360 end
250cde15 » martoche
2011-06-20 support for trusted proxies and IPv6 for Request#ip
361
ab637df5 » Rodrigo Flores
2012-01-10 Improving a method name
362 return reject_trusted_ip_addresses(forwarded_ips).last || @env["REMOTE_ADDR"]
a48eb513 » manveru
2008-08-30 Add Request#ip and corresponding spec
363 end
d8ccbf37 » josh
2009-12-20 Extract parse_query and parse_multipart in Request so subclasses
364
365 protected
42f4b951 » Rodrigo Flores
2012-01-10 Refactoring ip method
366 def split_ip_addresses(ip_addresses)
367 ip_addresses ? ip_addresses.strip.split(/[,\s]+/) : []
368 end
369
ab637df5 » Rodrigo Flores
2012-01-10 Improving a method name
370 def reject_trusted_ip_addresses(ip_addresses)
42f4b951 » Rodrigo Flores
2012-01-10 Refactoring ip method
371 ip_addresses.reject { |ip| trusted_proxy?(ip) }
372 end
373
d8ccbf37 » josh
2009-12-20 Extract parse_query and parse_multipart in Request so subclasses
374 def parse_query(qs)
375 Utils.parse_nested_query(qs)
376 end
377
378 def parse_multipart(env)
297f6382 » raggi
2011-05-02 Autoloads, requires, and using new api
379 Rack::Multipart.parse_multipart(env)
d8ccbf37 » josh
2009-12-20 Extract parse_query and parse_multipart in Request so subclasses
380 end
9798ae21 » craic
2013-12-05 add parse_http_accept_header utility method to simplify accept_encodi…
381
382 def parse_http_accept_header(header)
383 header.to_s.split(/\s*,\s*/).map do |part|
384 attribute, parameters = part.split(/\s*;\s*/, 2)
385 quality = 1.0
386 if parameters and /\Aq=([\d.]+)/ =~ parameters
387 quality = $1.to_f
388 end
389 [attribute, quality]
390 end
391 end
5f13e3d0 » chneukirchen
2007-02-16 Add Rack::Request
392 end
393 end
Something went wrong with that request. Please try again.