Skip to content
Newer
Older
100644 258 lines (215 sloc) 8.54 KB
d2ed32d @jeremy Parse url-encoded and multipart requests ourselves instead of delegat…
jeremy authored May 18, 2007
1 require 'tempfile'
2 require 'stringio'
3 require 'strscan'
4
e8550ee @jeremy Cherry-pick core extensions
jeremy authored May 13, 2009
5 require 'active_support/core_ext/hash/indifferent_access'
e1b5e3c Break up inflector to reduce the dependency burden on dependency-les …
Yehuda Katz authored Nov 7, 2009
6 require 'active_support/core_ext/string/access'
d446392 @pixeltrix Add additional HTTP request methods from the following RFCs:
pixeltrix authored Nov 2, 2010
7 require 'active_support/inflector'
018dafe @josh Allow autoloads to opt out of eager loading
josh authored Dec 12, 2009
8 require 'action_dispatch/http/headers'
525fd3a @vatrai TODO fix explicitly loading exceptations, autoload removed
vatrai authored Jun 29, 2011
9 require 'action_controller/metal/exceptions'
0a9bc59 @technoweenie Raise UnknownHttpMethod exception for unknown HTTP methods. Closes #1…
technoweenie authored Nov 29, 2007
10
319ae46 @josh Move HTTP libs and middleware into ActionDispatch component
josh authored Jan 27, 2009
11 module ActionDispatch
e1f73aa @josh Inherit ActionController::Request from Rack::Request
josh authored Jan 9, 2009
12 class Request < Rack::Request
92f49b5 @josevalim Split ActionDispatch http in smaller chunks.
josevalim authored Jan 16, 2010
13 include ActionDispatch::Http::Cache::Request
14 include ActionDispatch::Http::MimeNegotiation
15 include ActionDispatch::Http::Parameters
31fddf2 @josevalim Tidy up new filter_parameters implementation.
josevalim authored Jan 21, 2010
16 include ActionDispatch::Http::FilterParameters
92f49b5 @josevalim Split ActionDispatch http in smaller chunks.
josevalim authored Jan 16, 2010
17 include ActionDispatch::Http::Upload
18 include ActionDispatch::Http::URL
293bb02 @lifo Unify ActionController::AbstractRequest and ActionController::Request
lifo authored Dec 23, 2008
19
5b88014 @dhh Only show dump of regular env methods on exception screen (not all th…
dhh authored May 4, 2011
20 LOCALHOST = [/^127\.0\.0\.\d{1,3}$/, "::1", /^0:0:0:0:0:0:0:1(%.*)?$/].freeze
21 ENV_METHODS = %w[ AUTH_TYPE GATEWAY_INTERFACE
293bb02 @lifo Unify ActionController::AbstractRequest and ActionController::Request
lifo authored Dec 23, 2008
22 PATH_TRANSLATED REMOTE_HOST
e1f73aa @josh Inherit ActionController::Request from Rack::Request
josh authored Jan 9, 2009
23 REMOTE_IDENT REMOTE_USER REMOTE_ADDR
293bb02 @lifo Unify ActionController::AbstractRequest and ActionController::Request
lifo authored Dec 23, 2008
24 SERVER_NAME SERVER_PROTOCOL
25
26 HTTP_ACCEPT HTTP_ACCEPT_CHARSET HTTP_ACCEPT_ENCODING
27 HTTP_ACCEPT_LANGUAGE HTTP_CACHE_CONTROL HTTP_FROM
5b88014 @dhh Only show dump of regular env methods on exception screen (not all th…
dhh authored May 5, 2011
28 HTTP_NEGOTIATE HTTP_PRAGMA ].freeze
d182b6e @joshk removed deprecated methods, and related tests, from ActionPack
joshk authored May 24, 2011
29
5b88014 @dhh Only show dump of regular env methods on exception screen (not all th…
dhh authored May 5, 2011
30 ENV_METHODS.each do |env|
92f49b5 @josevalim Split ActionDispatch http in smaller chunks.
josevalim authored Jan 16, 2010
31 class_eval <<-METHOD, __FILE__, __LINE__ + 1
d8c77fa @vijaydev document meta method
vijaydev authored Jul 29, 2011
32 def #{env.sub(/^HTTP_/n, '').downcase} # def accept_charset
33 @env["#{env}"] # @env["HTTP_ACCEPT_CHARSET"]
34 end # end
92f49b5 @josevalim Split ActionDispatch http in smaller chunks.
josevalim authored Jan 16, 2010
35 METHOD
293bb02 @lifo Unify ActionController::AbstractRequest and ActionController::Request
lifo authored Dec 23, 2008
36 end
37
38 def key?(key)
39 @env.key?(key)
40 end
41
d446392 @pixeltrix Add additional HTTP request methods from the following RFCs:
pixeltrix authored Nov 2, 2010
42 # List of HTTP request methods from the following RFCs:
43 # Hypertext Transfer Protocol -- HTTP/1.1 (http://www.ietf.org/rfc/rfc2616.txt)
44 # HTTP Extensions for Distributed Authoring -- WEBDAV (http://www.ietf.org/rfc/rfc2518.txt)
45 # Versioning Extensions to WebDAV (http://www.ietf.org/rfc/rfc3253.txt)
46 # Ordered Collections Protocol (WebDAV) (http://www.ietf.org/rfc/rfc3648.txt)
47 # Web Distributed Authoring and Versioning (WebDAV) Access Control Protocol (http://www.ietf.org/rfc/rfc3744.txt)
48 # Web Distributed Authoring and Versioning (WebDAV) SEARCH (http://www.ietf.org/rfc/rfc5323.txt)
49 # PATCH Method for HTTP (http://www.ietf.org/rfc/rfc5789.txt)
50 RFC2616 = %w(OPTIONS GET HEAD POST PUT DELETE TRACE CONNECT)
51 RFC2518 = %w(PROPFIND PROPPATCH MKCOL COPY MOVE LOCK UNLOCK)
52 RFC3253 = %w(VERSION-CONTROL REPORT CHECKOUT CHECKIN UNCHECKOUT MKWORKSPACE UPDATE LABEL MERGE BASELINE-CONTROL MKACTIVITY)
53 RFC3648 = %w(ORDERPATCH)
54 RFC3744 = %w(ACL)
55 RFC5323 = %w(SEARCH)
56 RFC5789 = %w(PATCH)
57
58 HTTP_METHODS = RFC2616 + RFC2518 + RFC3253 + RFC3648 + RFC3744 + RFC5323 + RFC5789
59 HTTP_METHOD_LOOKUP = Hash.new { |h, m| h[m] = m.underscore.to_sym if HTTP_METHODS.include?(m) }
b7529ed @jeremy Simplifying usage of ETags and Last-Modified and conditional GET requ…
jeremy authored Aug 7, 2008
60
ab8bf9e @wycats * Change the object used in routing constraints to be an instance of
wycats authored Apr 3, 2010
61 # Returns the HTTP \method that the application should see.
62 # In the case where the \method was overridden by a middleware
63 # (for instance, if a HEAD request was converted to a GET,
64 # or if a _method parameter was used to determine the \method
65 # the application should use), this \method returns the overridden
66 # value, not the original.
0a9bc59 @technoweenie Raise UnknownHttpMethod exception for unknown HTTP methods. Closes #1…
technoweenie authored Nov 29, 2007
67 def request_method
78ac9c2 @tenderlove dry up method checking in the request object
tenderlove authored Sep 29, 2010
68 @request_method ||= check_method(env["REQUEST_METHOD"])
ab8bf9e @wycats * Change the object used in routing constraints to be an instance of
wycats authored Apr 4, 2010
69 end
70
71 # Returns a symbol form of the #request_method
72 def request_method_symbol
73 HTTP_METHOD_LOOKUP[request_method]
0a9bc59 @technoweenie Raise UnknownHttpMethod exception for unknown HTTP methods. Closes #1…
technoweenie authored Nov 29, 2007
74 end
75
ab8bf9e @wycats * Change the object used in routing constraints to be an instance of
wycats authored Apr 4, 2010
76 # Returns the original value of the environment's REQUEST_METHOD,
77 # even if it was overridden by middleware. See #request_method for
78 # more information.
db045db @dhh Initial
dhh authored Nov 24, 2004
79 def method
78ac9c2 @tenderlove dry up method checking in the request object
tenderlove authored Sep 29, 2010
80 @method ||= check_method(env["rack.methodoverride.original_method"] || env['REQUEST_METHOD'])
ab8bf9e @wycats * Change the object used in routing constraints to be an instance of
wycats authored Apr 4, 2010
81 end
82
83 # Returns a symbol form of the #method
84 def method_symbol
85 HTTP_METHOD_LOOKUP[method]
db045db @dhh Initial
dhh authored Nov 24, 2004
86 end
87
ab8bf9e @wycats * Change the object used in routing constraints to be an instance of
wycats authored Apr 4, 2010
88 # Is this a GET (or HEAD) request?
237272e @travisp Fix ActionDispatch::Request method explanations
travisp authored Nov 28, 2011
89 # Equivalent to <tt>request.request_method_symbol == :get</tt>.
db045db @dhh Initial
dhh authored Nov 24, 2004
90 def get?
ab8bf9e @wycats * Change the object used in routing constraints to be an instance of
wycats authored Apr 4, 2010
91 HTTP_METHOD_LOOKUP[request_method] == :get
db045db @dhh Initial
dhh authored Nov 24, 2004
92 end
93
ab8bf9e @wycats * Change the object used in routing constraints to be an instance of
wycats authored Apr 4, 2010
94 # Is this a POST request?
237272e @travisp Fix ActionDispatch::Request method explanations
travisp authored Nov 28, 2011
95 # Equivalent to <tt>request.request_method_symbol == :post</tt>.
db045db @dhh Initial
dhh authored Nov 24, 2004
96 def post?
ab8bf9e @wycats * Change the object used in routing constraints to be an instance of
wycats authored Apr 4, 2010
97 HTTP_METHOD_LOOKUP[request_method] == :post
db045db @dhh Initial
dhh authored Nov 24, 2004
98 end
99
ab8bf9e @wycats * Change the object used in routing constraints to be an instance of
wycats authored Apr 4, 2010
100 # Is this a PUT request?
237272e @travisp Fix ActionDispatch::Request method explanations
travisp authored Nov 28, 2011
101 # Equivalent to <tt>request.request_method_symbol == :put</tt>.
db045db @dhh Initial
dhh authored Nov 24, 2004
102 def put?
ab8bf9e @wycats * Change the object used in routing constraints to be an instance of
wycats authored Apr 4, 2010
103 HTTP_METHOD_LOOKUP[request_method] == :put
db045db @dhh Initial
dhh authored Nov 24, 2004
104 end
105
ab8bf9e @wycats * Change the object used in routing constraints to be an instance of
wycats authored Apr 4, 2010
106 # Is this a DELETE request?
237272e @travisp Fix ActionDispatch::Request method explanations
travisp authored Nov 28, 2011
107 # Equivalent to <tt>request.request_method_symbol == :delete</tt>.
db045db @dhh Initial
dhh authored Nov 24, 2004
108 def delete?
ab8bf9e @wycats * Change the object used in routing constraints to be an instance of
wycats authored Apr 4, 2010
109 HTTP_METHOD_LOOKUP[request_method] == :delete
db045db @dhh Initial
dhh authored Nov 24, 2004
110 end
111
ab8bf9e @wycats * Change the object used in routing constraints to be an instance of
wycats authored Apr 4, 2010
112 # Is this a HEAD request?
237272e @travisp Fix ActionDispatch::Request method explanations
travisp authored Nov 28, 2011
113 # Equivalent to <tt>request.method_symbol == :head</tt>.
9492129 @dhh Added indifference to whether @headers["Content-Type"], @headers["Con…
dhh authored Dec 7, 2004
114 def head?
ab8bf9e @wycats * Change the object used in routing constraints to be an instance of
wycats authored Apr 4, 2010
115 HTTP_METHOD_LOOKUP[method] == :head
9492129 @dhh Added indifference to whether @headers["Content-Type"], @headers["Con…
dhh authored Dec 7, 2004
116 end
db11abb @dhh Added support for POST data in form of YAML or XML, which is controll…
dhh authored May 14, 2005
117
0432d15 @lifo Merge with docrails.
lifo authored Jul 16, 2008
118 # Provides access to the request's HTTP headers, for example:
6ef3546 @lifo Merge docrails
lifo authored Sep 3, 2008
119 #
120 # request.headers["Content-Type"] # => "text/plain"
2e55095 @dhh Added that rendering will automatically insert the etag header on 200…
dhh authored Feb 17, 2007
121 def headers
319ae46 @josh Move HTTP libs and middleware into ActionDispatch component
josh authored Jan 28, 2009
122 Http::Headers.new(@env)
2e55095 @dhh Added that rendering will automatically insert the etag header on 200…
dhh authored Feb 17, 2007
123 end
124
79b12a0 @drogus Add original_fullpath and original_url methods to Request
drogus authored Dec 20, 2011
125 def original_fullpath
126 @original_fullpath ||= (env["ORIGINAL_FULLPATH"] || fullpath)
127 end
128
ab1407c @wycats Improve performance of commonly used request methods
wycats authored Jun 4, 2010
129 def fullpath
130 @fullpath ||= super
131 end
132
79b12a0 @drogus Add original_fullpath and original_url methods to Request
drogus authored Dec 20, 2011
133 def original_url
134 base_url + original_fullpath
135 end
136
b69da86 @josh Remove vendored version of Rack
josh authored Apr 25, 2009
137 def media_type
77a2a3d @wycats Request#content_type exists in Rack::Request, and other parts of Rack…
wycats authored Mar 28, 2010
138 content_mime_type.to_s
b69da86 @josh Remove vendored version of Rack
josh authored Apr 25, 2009
139 end
140
92f49b5 @josevalim Split ActionDispatch http in smaller chunks.
josevalim authored Jan 16, 2010
141 # Returns the content length of the request as an integer.
142 def content_length
143 super.to_i
12cf8f3 @NZKoz Move template_format logic out to the request so it's alongside the '…
NZKoz authored Jun 27, 2008
144 end
145
0aa66f0 @fxn gets rid of a double negation, no need to force exactly true/false in…
fxn authored Sep 12, 2010
146 # Returns true if the "X-Requested-With" header contains "XMLHttpRequest"
147 # (case-insensitive). All major JavaScript libraries send this header with
148 # every Ajax request.
0367317 @dhh Deprecated redirect_to_path and redirect_to_url in favor of letting r…
dhh authored May 22, 2005
149 def xml_http_request?
0aa66f0 @fxn gets rid of a double negation, no need to force exactly true/false in…
fxn authored Sep 11, 2010
150 @env['HTTP_X_REQUESTED_WITH'] =~ /XMLHttpRequest/i
0367317 @dhh Deprecated redirect_to_path and redirect_to_url in favor of letting r…
dhh authored May 22, 2005
151 end
3e8ba61 @josevalim Refactor even more Responder. Move mime negotiation to request and ad…
josevalim authored Jul 28, 2009
152 alias :xhr? :xml_http_request?
0367317 @dhh Deprecated redirect_to_path and redirect_to_url in favor of letting r…
dhh authored May 22, 2005
153
ab1407c @wycats Improve performance of commonly used request methods
wycats authored Jun 4, 2010
154 def ip
155 @ip ||= super
156 end
157
9432163 @indirect refactor RemoteIp middleware
indirect authored Nov 11, 2011
158 # Originating IP address, usually set by the RemoteIp middleware.
db045db @dhh Initial
dhh authored Nov 24, 2004
159 def remote_ip
ab1407c @wycats Improve performance of commonly used request methods
wycats authored Jun 4, 2010
160 @remote_ip ||= (@env["action_dispatch.remote_ip"] || ip).to_s
db045db @dhh Initial
dhh authored Nov 24, 2004
161 end
162
afde6fd @dhh Added X-Request-Id tracking and TaggedLogging to easily log that and …
dhh authored Oct 19, 2011
163 # Returns the unique request id, which is based off either the X-Request-Id header that can
164 # be generated by a firewall, load balancer, or web server or by the RequestId middleware
165 # (which sets the action_dispatch.request_id environment variable).
166 #
167 # This unique ID is useful for tracing a request from end-to-end as part of logging or debugging.
168 # This relies on the rack variable set by the ActionDispatch::RequestId middleware.
169 def uuid
170 @uuid ||= env["action_dispatch.request_id"]
171 end
172
e0179c1 @dhh Added Request#url that returns the complete URL used for the request …
dhh authored Mar 4, 2007
173 # Returns the lowercase name of the HTTP server software.
174 def server_software
175 (@env['SERVER_SOFTWARE'] && /^([a-zA-Z]+)/ =~ @env['SERVER_SOFTWARE']) ? $1.downcase : nil
176 end
177
6ef3546 @lifo Merge docrails
lifo authored Sep 3, 2008
178 # Read the request \body. This is useful for web services that need to
4bd3e3a @jeremy Set RAW_POST_DATA when request parameters are parsed.
jeremy authored May 23, 2007
179 # work with raw requests directly.
e0179c1 @dhh Added Request#url that returns the complete URL used for the request …
dhh authored Mar 4, 2007
180 def raw_post
ff0a267 @josh Build query string and POST params parser on top of Rack::Request. Al…
josh authored Jan 17, 2009
181 unless @env.include? 'RAW_POST_DATA'
182 @env['RAW_POST_DATA'] = body.read(@env['CONTENT_LENGTH'].to_i)
183 body.rewind if body.respond_to?(:rewind)
184 end
185 @env['RAW_POST_DATA']
61960e7 @dhh Added Request#port_string to get something like ":8080" back on 8080 …
dhh authored Dec 18, 2004
186 end
187
ff0a267 @josh Build query string and POST params parser on top of Rack::Request. Al…
josh authored Jan 18, 2009
188 # The request body is an IO input stream. If the RAW_POST_DATA environment
189 # variable is already set, wrap it in a StringIO.
b7529ed @jeremy Simplifying usage of ETags and Last-Modified and conditional GET requ…
jeremy authored Aug 8, 2008
190 def body
ff0a267 @josh Build query string and POST params parser on top of Rack::Request. Al…
josh authored Jan 18, 2009
191 if raw_post = @env['RAW_POST_DATA']
192 raw_post.force_encoding(Encoding::BINARY) if raw_post.respond_to?(:force_encoding)
193 StringIO.new(raw_post)
194 else
195 @env['rack.input']
196 end
197 end
198
199 def form_data?
77a2a3d @wycats Request#content_type exists in Rack::Request, and other parts of Rack…
wycats authored Mar 28, 2010
200 FORM_DATA_MEDIA_TYPES.include?(content_mime_type.to_s)
b7529ed @jeremy Simplifying usage of ETags and Last-Modified and conditional GET requ…
jeremy authored Aug 8, 2008
201 end
540d005 @jeremy Improved AbstractRequest documentation. Closes #1483.
jeremy authored Jun 24, 2005
202
b7529ed @jeremy Simplifying usage of ETags and Last-Modified and conditional GET requ…
jeremy authored Aug 8, 2008
203 def body_stream #:nodoc:
293bb02 @lifo Unify ActionController::AbstractRequest and ActionController::Request
lifo authored Dec 23, 2008
204 @env['rack.input']
db045db @dhh Initial
dhh authored Nov 24, 2004
205 end
206
a12b76b @josevalim Just reading flash messages should not create a session if one does n…
josevalim authored Jun 25, 2010
207 # TODO This should be broken apart into AD::Request::Session and probably
208 # be included by the session middleware.
0494909 @josh Inherit TestSession from Session::AbstractStore and add indifferent a…
josh authored Apr 26, 2009
209 def reset_session
653acac @josevalim Solve some warnings and a failing test.
josevalim authored Oct 3, 2010
210 session.destroy if session && session.respond_to?(:destroy)
0494909 @josh Inherit TestSession from Session::AbstractStore and add indifferent a…
josh authored Apr 26, 2009
211 self.session = {}
a12b76b @josevalim Just reading flash messages should not create a session if one does n…
josevalim authored Jun 25, 2010
212 @env['action_dispatch.request.flash_hash'] = nil
db045db @dhh Initial
dhh authored Nov 24, 2004
213 end
214
7654082 @jeremy Major components cleanup and speedup. Closes #3527.
jeremy authored Feb 9, 2006
215 def session=(session) #:nodoc:
db619c4 @wycats Sync 'rails/rails/master'
wycats authored Jan 5, 2009
216 @env['rack.session'] = session
7654082 @jeremy Major components cleanup and speedup. Closes #3527.
jeremy authored Feb 9, 2006
217 end
218
293bb02 @lifo Unify ActionController::AbstractRequest and ActionController::Request
lifo authored Dec 23, 2008
219 def session_options=(options)
220 @env['rack.session.options'] = options
221 end
222
92f49b5 @josevalim Split ActionDispatch http in smaller chunks.
josevalim authored Jan 16, 2010
223 # Override Rack's GET method to support indifferent access
224 def GET
3eff729 make sure request parameters are accessible after rack throws an exce…
Miles Egan authored Jul 18, 2010
225 @env["action_dispatch.request.query_parameters"] ||= (normalize_parameters(super) || {})
92f49b5 @josevalim Split ActionDispatch http in smaller chunks.
josevalim authored Jan 16, 2010
226 end
227 alias :query_parameters :GET
228
229 # Override Rack's POST method to support indifferent access
230 def POST
3eff729 make sure request parameters are accessible after rack throws an exce…
Miles Egan authored Jul 18, 2010
231 @env["action_dispatch.request.request_parameters"] ||= (normalize_parameters(super) || {})
92f49b5 @josevalim Split ActionDispatch http in smaller chunks.
josevalim authored Jan 16, 2010
232 end
233 alias :request_parameters :POST
234
235
cf9d6a9 @dhh Added ActionDispatch::Request#authorization to access the http authen…
dhh authored Dec 20, 2009
236 # Returns the authorization header regardless of whether it was specified directly or through one of the
237 # proxy alternatives.
238 def authorization
239 @env['HTTP_AUTHORIZATION'] ||
240 @env['X-HTTP_AUTHORIZATION'] ||
241 @env['X_HTTP_AUTHORIZATION'] ||
242 @env['REDIRECT_X_HTTP_AUTHORIZATION']
243 end
9198885 @spastorino Moves local_request? to require.local?
spastorino authored Aug 13, 2010
244
245 # True if the request came from localhost, 127.0.0.1.
246 def local?
247 LOCALHOST.any? { |local_ip| local_ip === remote_addr && local_ip === remote_ip }
248 end
78ac9c2 @tenderlove dry up method checking in the request object
tenderlove authored Sep 29, 2010
249
250 private
251
252 def check_method(name)
253 HTTP_METHOD_LOOKUP[name] || raise(ActionController::UnknownHttpMethod, "#{name}, accepted HTTP methods are #{HTTP_METHODS.to_sentence(:locale => :en)}")
254 name
255 end
52ca5da @jeremy Use StringIO and Tempfile subclasses instead of defining singleton me…
jeremy authored Oct 6, 2007
256 end
0ee1cb2 @jeremy Ruby 1.9 compat, consistent load paths
jeremy authored Oct 2, 2007
257 end
Something went wrong with that request. Please try again.