rack 1.6.13 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rack might be problematic. Click here for more details.

Files changed (144) hide show
  1. checksums.yaml +5 -5
  2. data/COPYING +1 -1
  3. data/HISTORY.md +138 -8
  4. data/README.rdoc +17 -25
  5. data/Rakefile +6 -14
  6. data/SPEC +8 -9
  7. data/contrib/rack_logo.svg +164 -111
  8. data/example/protectedlobster.rb +1 -1
  9. data/example/protectedlobster.ru +1 -1
  10. data/lib/rack/auth/abstract/request.rb +5 -1
  11. data/lib/rack/auth/digest/params.rb +2 -3
  12. data/lib/rack/auth/digest/request.rb +1 -1
  13. data/lib/rack/body_proxy.rb +14 -9
  14. data/lib/rack/builder.rb +3 -3
  15. data/lib/rack/chunked.rb +5 -5
  16. data/lib/rack/{commonlogger.rb → common_logger.rb} +3 -3
  17. data/lib/rack/{conditionalget.rb → conditional_get.rb} +0 -0
  18. data/lib/rack/content_length.rb +2 -2
  19. data/lib/rack/deflater.rb +4 -4
  20. data/lib/rack/directory.rb +66 -54
  21. data/lib/rack/etag.rb +4 -3
  22. data/lib/rack/events.rb +154 -0
  23. data/lib/rack/file.rb +63 -39
  24. data/lib/rack/handler/cgi.rb +15 -16
  25. data/lib/rack/handler/fastcgi.rb +13 -14
  26. data/lib/rack/handler/lsws.rb +11 -11
  27. data/lib/rack/handler/scgi.rb +15 -15
  28. data/lib/rack/handler/thin.rb +3 -0
  29. data/lib/rack/handler/webrick.rb +22 -24
  30. data/lib/rack/handler.rb +3 -25
  31. data/lib/rack/head.rb +15 -17
  32. data/lib/rack/lint.rb +38 -38
  33. data/lib/rack/lobster.rb +1 -1
  34. data/lib/rack/lock.rb +6 -10
  35. data/lib/rack/logger.rb +2 -2
  36. data/lib/rack/media_type.rb +38 -0
  37. data/lib/rack/{methodoverride.rb → method_override.rb} +4 -11
  38. data/lib/rack/mime.rb +18 -5
  39. data/lib/rack/mock.rb +35 -53
  40. data/lib/rack/multipart/generator.rb +5 -5
  41. data/lib/rack/multipart/parser.rb +272 -158
  42. data/lib/rack/multipart/uploaded_file.rb +1 -2
  43. data/lib/rack/multipart.rb +35 -6
  44. data/lib/rack/{nulllogger.rb → null_logger.rb} +1 -1
  45. data/lib/rack/query_parser.rb +192 -0
  46. data/lib/rack/recursive.rb +8 -8
  47. data/lib/rack/request.rb +383 -307
  48. data/lib/rack/response.rb +129 -56
  49. data/lib/rack/rewindable_input.rb +1 -12
  50. data/lib/rack/runtime.rb +10 -18
  51. data/lib/rack/sendfile.rb +5 -7
  52. data/lib/rack/server.rb +31 -25
  53. data/lib/rack/session/abstract/id.rb +95 -135
  54. data/lib/rack/session/cookie.rb +26 -28
  55. data/lib/rack/session/memcache.rb +8 -14
  56. data/lib/rack/session/pool.rb +14 -21
  57. data/lib/rack/show_exceptions.rb +386 -0
  58. data/lib/rack/{showstatus.rb → show_status.rb} +3 -3
  59. data/lib/rack/static.rb +30 -5
  60. data/lib/rack/tempfile_reaper.rb +2 -2
  61. data/lib/rack/urlmap.rb +15 -14
  62. data/lib/rack/utils.rb +135 -210
  63. data/lib/rack.rb +70 -21
  64. data/rack.gemspec +7 -5
  65. data/test/builder/an_underscore_app.rb +5 -0
  66. data/test/builder/options.ru +1 -1
  67. data/test/cgi/test.fcgi +1 -0
  68. data/test/cgi/test.gz +0 -0
  69. data/test/helper.rb +34 -0
  70. data/test/multipart/filename_with_encoded_words +7 -0
  71. data/test/multipart/{filename_with_null_byte → filename_with_single_quote} +1 -1
  72. data/test/multipart/quoted +15 -0
  73. data/test/multipart/rack-logo.png +0 -0
  74. data/test/multipart/unity3d_wwwform +11 -0
  75. data/test/registering_handler/rack/handler/registering_myself.rb +1 -1
  76. data/test/spec_auth_basic.rb +27 -19
  77. data/test/spec_auth_digest.rb +47 -46
  78. data/test/spec_body_proxy.rb +27 -27
  79. data/test/spec_builder.rb +51 -41
  80. data/test/spec_cascade.rb +24 -22
  81. data/test/spec_cgi.rb +49 -67
  82. data/test/spec_chunked.rb +36 -34
  83. data/test/{spec_commonlogger.rb → spec_common_logger.rb} +23 -21
  84. data/test/{spec_conditionalget.rb → spec_conditional_get.rb} +29 -28
  85. data/test/spec_config.rb +3 -2
  86. data/test/spec_content_length.rb +18 -17
  87. data/test/spec_content_type.rb +13 -12
  88. data/test/spec_deflater.rb +66 -40
  89. data/test/spec_directory.rb +87 -27
  90. data/test/spec_etag.rb +32 -31
  91. data/test/spec_events.rb +133 -0
  92. data/test/spec_fastcgi.rb +50 -72
  93. data/test/spec_file.rb +107 -77
  94. data/test/spec_handler.rb +19 -34
  95. data/test/spec_head.rb +15 -14
  96. data/test/spec_lint.rb +162 -197
  97. data/test/spec_lobster.rb +24 -23
  98. data/test/spec_lock.rb +69 -39
  99. data/test/spec_logger.rb +4 -3
  100. data/test/spec_media_type.rb +42 -0
  101. data/test/spec_method_override.rb +83 -0
  102. data/test/spec_mime.rb +19 -19
  103. data/test/spec_mock.rb +196 -151
  104. data/test/spec_multipart.rb +317 -201
  105. data/test/{spec_nulllogger.rb → spec_null_logger.rb} +5 -4
  106. data/test/spec_recursive.rb +17 -14
  107. data/test/spec_request.rb +768 -607
  108. data/test/spec_response.rb +214 -111
  109. data/test/spec_rewindable_input.rb +50 -40
  110. data/test/spec_runtime.rb +11 -10
  111. data/test/spec_sendfile.rb +30 -35
  112. data/test/spec_server.rb +78 -52
  113. data/test/spec_session_abstract_id.rb +11 -33
  114. data/test/spec_session_abstract_session_hash.rb +28 -0
  115. data/test/spec_session_cookie.rb +97 -65
  116. data/test/spec_session_memcache.rb +63 -101
  117. data/test/spec_session_pool.rb +48 -84
  118. data/test/spec_show_exceptions.rb +80 -0
  119. data/test/{spec_showstatus.rb → spec_show_status.rb} +36 -35
  120. data/test/spec_static.rb +71 -32
  121. data/test/spec_tempfile_reaper.rb +11 -10
  122. data/test/spec_thin.rb +55 -50
  123. data/test/spec_urlmap.rb +79 -78
  124. data/test/spec_utils.rb +441 -346
  125. data/test/spec_version.rb +2 -8
  126. data/test/spec_webrick.rb +91 -67
  127. data/test/static/foo.html +1 -0
  128. data/test/testrequest.rb +1 -1
  129. data/test/unregistered_handler/rack/handler/unregistered.rb +1 -1
  130. data/test/unregistered_handler/rack/handler/unregistered_long_one.rb +1 -1
  131. metadata +103 -69
  132. data/KNOWN-ISSUES +0 -44
  133. data/lib/rack/backports/uri/common_18.rb +0 -56
  134. data/lib/rack/backports/uri/common_192.rb +0 -52
  135. data/lib/rack/backports/uri/common_193.rb +0 -29
  136. data/lib/rack/handler/evented_mongrel.rb +0 -8
  137. data/lib/rack/handler/mongrel.rb +0 -106
  138. data/lib/rack/handler/swiftiplied_mongrel.rb +0 -8
  139. data/lib/rack/showexceptions.rb +0 -387
  140. data/lib/rack/utils/okjson.rb +0 -600
  141. data/test/spec_methodoverride.rb +0 -111
  142. data/test/spec_mongrel.rb +0 -182
  143. data/test/spec_session_persisted_secure_secure_session_hash.rb +0 -73
  144. data/test/spec_showexceptions.rb +0 -98
data/lib/rack/request.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'rack/utils'
2
+ require 'rack/media_type'
2
3
 
3
4
  module Rack
4
5
  # Rack::Request provides a convenient interface to a Rack
@@ -10,371 +11,444 @@ module Rack
10
11
  # req.params["data"]
11
12
 
12
13
  class Request
13
- # The environment of the request.
14
- attr_reader :env
15
-
16
- SCHEME_WHITELIST = %w(https http).freeze
17
-
18
14
  def initialize(env)
19
- @env = env
15
+ @params = nil
16
+ super(env)
20
17
  end
21
18
 
22
- def body; @env["rack.input"] end
23
- def script_name; @env[SCRIPT_NAME].to_s end
24
- def path_info; @env[PATH_INFO].to_s end
25
- def request_method; @env["REQUEST_METHOD"] end
26
- def query_string; @env[QUERY_STRING].to_s end
27
- def content_length; @env['CONTENT_LENGTH'] end
28
-
29
- def content_type
30
- content_type = @env['CONTENT_TYPE']
31
- content_type.nil? || content_type.empty? ? nil : content_type
19
+ def params
20
+ @params ||= super
32
21
  end
33
22
 
34
- def session; @env['rack.session'] ||= {} end
35
- def session_options; @env['rack.session.options'] ||= {} end
36
- def logger; @env['rack.logger'] end
37
-
38
- # The media type (type/subtype) portion of the CONTENT_TYPE header
39
- # without any media type parameters. e.g., when CONTENT_TYPE is
40
- # "text/plain;charset=utf-8", the media-type is "text/plain".
41
- #
42
- # For more information on the use of media types in HTTP, see:
43
- # http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7
44
- def media_type
45
- content_type && content_type.split(/\s*[;,]\s*/, 2).first.downcase
23
+ def update_param(k, v)
24
+ super
25
+ @params = nil
46
26
  end
47
27
 
48
- # The media type parameters provided in CONTENT_TYPE as a Hash, or
49
- # an empty Hash if no CONTENT_TYPE or media-type parameters were
50
- # provided. e.g., when the CONTENT_TYPE is "text/plain;charset=utf-8",
51
- # this method responds with the following Hash:
52
- # { 'charset' => 'utf-8' }
53
- def media_type_params
54
- return {} if content_type.nil?
55
- Hash[*content_type.split(/\s*[;,]\s*/)[1..-1].
56
- collect { |s| s.split('=', 2) }.
57
- map { |k,v| [k.downcase, strip_doublequotes(v)] }.flatten]
28
+ def delete_param(k)
29
+ v = super
30
+ @params = nil
31
+ v
58
32
  end
59
33
 
60
- # The character set of the request body if a "charset" media type
61
- # parameter was given, or nil if no "charset" was specified. Note
62
- # that, per RFC2616, text/* media types that specify no explicit
63
- # charset are to be considered ISO-8859-1.
64
- def content_charset
65
- media_type_params['charset']
66
- end
34
+ module Env
35
+ # The environment of the request.
36
+ attr_reader :env
67
37
 
68
- def scheme
69
- if @env['HTTPS'] == 'on'
70
- 'https'
71
- elsif @env['HTTP_X_FORWARDED_SSL'] == 'on'
72
- 'https'
73
- elsif forwarded_scheme
74
- forwarded_scheme
75
- else
76
- @env["rack.url_scheme"]
38
+ def initialize(env)
39
+ @env = env
40
+ super()
77
41
  end
78
- end
79
42
 
80
- def ssl?
81
- scheme == 'https'
82
- end
43
+ # Predicate method to test to see if `name` has been set as request
44
+ # specific data
45
+ def has_header?(name)
46
+ @env.key? name
47
+ end
83
48
 
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']}"
49
+ # Get a request specific value for `name`.
50
+ def get_header(name)
51
+ @env[name]
89
52
  end
90
- end
91
53
 
92
- def port
93
- if port = host_with_port.split(/:/)[1]
94
- port.to_i
95
- elsif port = @env['HTTP_X_FORWARDED_PORT']
96
- port.to_i
97
- elsif @env.has_key?("HTTP_X_FORWARDED_HOST")
98
- DEFAULT_PORTS[scheme]
99
- elsif @env.has_key?("HTTP_X_FORWARDED_PROTO")
100
- DEFAULT_PORTS[@env['HTTP_X_FORWARDED_PROTO'].split(',')[0]]
101
- else
102
- @env["SERVER_PORT"].to_i
54
+ # If a block is given, it yields to the block if the value hasn't been set
55
+ # on the request.
56
+ def fetch_header(name, &block)
57
+ @env.fetch(name, &block)
103
58
  end
104
- end
105
59
 
106
- def host
107
- # Remove port number.
108
- host_with_port.to_s.sub(/:\d+\z/, '')
109
- end
60
+ # Loops through each key / value pair in the request specific data.
61
+ def each_header(&block)
62
+ @env.each(&block)
63
+ end
110
64
 
111
- def script_name=(s); @env["SCRIPT_NAME"] = s.to_s end
112
- def path_info=(s); @env["PATH_INFO"] = s.to_s end
65
+ # Set a request specific value for `name` to `v`
66
+ def set_header(name, v)
67
+ @env[name] = v
68
+ end
69
+
70
+ # Add a header that may have multiple values.
71
+ #
72
+ # Example:
73
+ # request.add_header 'Accept', 'image/png'
74
+ # request.add_header 'Accept', '*/*'
75
+ #
76
+ # assert_equal 'image/png,*/*', request.get_header('Accept')
77
+ #
78
+ # http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
79
+ def add_header key, v
80
+ if v.nil?
81
+ get_header key
82
+ elsif has_header? key
83
+ set_header key, "#{get_header key},#{v}"
84
+ else
85
+ set_header key, v
86
+ end
87
+ end
113
88
 
89
+ # Delete a request specific value for `name`.
90
+ def delete_header(name)
91
+ @env.delete name
92
+ end
114
93
 
115
- # Checks the HTTP request method (or verb) to see if it was of type DELETE
116
- def delete?; request_method == "DELETE" end
94
+ def initialize_copy(other)
95
+ @env = other.env.dup
96
+ end
97
+ end
117
98
 
118
- # Checks the HTTP request method (or verb) to see if it was of type GET
119
- def get?; request_method == GET end
99
+ module Helpers
100
+ # The set of form-data media-types. Requests that do not indicate
101
+ # one of the media types presents in this list will not be eligible
102
+ # for form-data / param parsing.
103
+ FORM_DATA_MEDIA_TYPES = [
104
+ 'application/x-www-form-urlencoded',
105
+ 'multipart/form-data'
106
+ ]
120
107
 
121
- # Checks the HTTP request method (or verb) to see if it was of type HEAD
122
- def head?; request_method == HEAD end
108
+ # The set of media-types. Requests that do not indicate
109
+ # one of the media types presents in this list will not be eligible
110
+ # for param parsing like soap attachments or generic multiparts
111
+ PARSEABLE_DATA_MEDIA_TYPES = [
112
+ 'multipart/related',
113
+ 'multipart/mixed'
114
+ ]
123
115
 
124
- # Checks the HTTP request method (or verb) to see if it was of type OPTIONS
125
- def options?; request_method == "OPTIONS" end
116
+ # Default ports depending on scheme. Used to decide whether or not
117
+ # to include the port in a generated URI.
118
+ DEFAULT_PORTS = { 'http' => 80, 'https' => 443, 'coffee' => 80 }
119
+
120
+ HTTP_X_FORWARDED_SCHEME = 'HTTP_X_FORWARDED_SCHEME'.freeze
121
+ HTTP_X_FORWARDED_PROTO = 'HTTP_X_FORWARDED_PROTO'.freeze
122
+ HTTP_X_FORWARDED_HOST = 'HTTP_X_FORWARDED_HOST'.freeze
123
+ HTTP_X_FORWARDED_PORT = 'HTTP_X_FORWARDED_PORT'.freeze
124
+ HTTP_X_FORWARDED_SSL = 'HTTP_X_FORWARDED_SSL'.freeze
125
+
126
+ def body; get_header(RACK_INPUT) end
127
+ def script_name; get_header(SCRIPT_NAME).to_s end
128
+ def script_name=(s); set_header(SCRIPT_NAME, s.to_s) end
129
+
130
+ def path_info; get_header(PATH_INFO).to_s end
131
+ def path_info=(s); set_header(PATH_INFO, s.to_s) end
132
+
133
+ def request_method; get_header(REQUEST_METHOD) end
134
+ def query_string; get_header(QUERY_STRING).to_s end
135
+ def content_length; get_header('CONTENT_LENGTH') end
136
+ def logger; get_header(RACK_LOGGER) end
137
+ def user_agent; get_header('HTTP_USER_AGENT') end
138
+ def multithread?; get_header(RACK_MULTITHREAD) end
139
+
140
+ # the referer of the client
141
+ def referer; get_header('HTTP_REFERER') end
142
+ alias referrer referer
143
+
144
+ def session
145
+ fetch_header(RACK_SESSION) do |k|
146
+ set_header RACK_SESSION, default_session
147
+ end
148
+ end
126
149
 
127
- # Checks the HTTP request method (or verb) to see if it was of type LINK
128
- def link?; request_method == "LINK" end
150
+ def session_options
151
+ fetch_header(RACK_SESSION_OPTIONS) do |k|
152
+ set_header RACK_SESSION_OPTIONS, {}
153
+ end
154
+ end
129
155
 
130
- # Checks the HTTP request method (or verb) to see if it was of type PATCH
131
- def patch?; request_method == "PATCH" end
156
+ # Checks the HTTP request method (or verb) to see if it was of type DELETE
157
+ def delete?; request_method == DELETE end
132
158
 
133
- # Checks the HTTP request method (or verb) to see if it was of type POST
134
- def post?; request_method == "POST" end
159
+ # Checks the HTTP request method (or verb) to see if it was of type GET
160
+ def get?; request_method == GET end
135
161
 
136
- # Checks the HTTP request method (or verb) to see if it was of type PUT
137
- def put?; request_method == "PUT" end
162
+ # Checks the HTTP request method (or verb) to see if it was of type HEAD
163
+ def head?; request_method == HEAD end
138
164
 
139
- # Checks the HTTP request method (or verb) to see if it was of type TRACE
140
- def trace?; request_method == "TRACE" end
165
+ # Checks the HTTP request method (or verb) to see if it was of type OPTIONS
166
+ def options?; request_method == OPTIONS end
141
167
 
142
- # Checks the HTTP request method (or verb) to see if it was of type UNLINK
143
- def unlink?; request_method == "UNLINK" end
168
+ # Checks the HTTP request method (or verb) to see if it was of type LINK
169
+ def link?; request_method == LINK end
144
170
 
171
+ # Checks the HTTP request method (or verb) to see if it was of type PATCH
172
+ def patch?; request_method == PATCH end
145
173
 
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
- ]
174
+ # Checks the HTTP request method (or verb) to see if it was of type POST
175
+ def post?; request_method == POST end
153
176
 
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'
160
- ]
177
+ # Checks the HTTP request method (or verb) to see if it was of type PUT
178
+ def put?; request_method == PUT end
161
179
 
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 }
180
+ # Checks the HTTP request method (or verb) to see if it was of type TRACE
181
+ def trace?; request_method == TRACE end
165
182
 
166
- # Determine whether the request body contains form-data by checking
167
- # the request Content-Type for one of the media-types:
168
- # "application/x-www-form-urlencoded" or "multipart/form-data". The
169
- # list of form-data media types can be modified through the
170
- # +FORM_DATA_MEDIA_TYPES+ array.
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.
174
- def form_data?
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)
178
- end
183
+ # Checks the HTTP request method (or verb) to see if it was of type UNLINK
184
+ def unlink?; request_method == UNLINK end
179
185
 
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
186
+ def scheme
187
+ if get_header(HTTPS) == 'on'
188
+ 'https'
189
+ elsif get_header(HTTP_X_FORWARDED_SSL) == 'on'
190
+ 'https'
191
+ elsif get_header(HTTP_X_FORWARDED_SCHEME)
192
+ get_header(HTTP_X_FORWARDED_SCHEME)
193
+ elsif get_header(HTTP_X_FORWARDED_PROTO)
194
+ get_header(HTTP_X_FORWARDED_PROTO).split(',')[0]
195
+ else
196
+ get_header(RACK_URL_SCHEME)
197
+ end
198
+ end
185
199
 
186
- # Returns the data received in the query string.
187
- def GET
188
- if @env["rack.request.query_string"] == query_string
189
- @env["rack.request.query_hash"]
190
- else
191
- p = parse_query({ :query => query_string, :separator => '&;' })
192
- @env["rack.request.query_string"] = query_string
193
- @env["rack.request.query_hash"] = p
200
+ def authority
201
+ get_header(SERVER_NAME) + ':' + get_header(SERVER_PORT)
194
202
  end
195
- end
196
203
 
197
- # Returns the data received in the request body.
198
- #
199
- # This method support both application/x-www-form-urlencoded and
200
- # multipart/form-data.
201
- def POST
202
- if @env["rack.input"].nil?
203
- raise "Missing rack.input"
204
- elsif @env["rack.request.form_input"].equal? @env["rack.input"]
205
- @env["rack.request.form_hash"]
206
- elsif form_data? || parseable_data?
207
- unless @env["rack.request.form_hash"] = parse_multipart(env)
208
- form_vars = @env["rack.input"].read
209
-
210
- # Fix for Safari Ajax postings that always append \0
211
- # form_vars.sub!(/\0\z/, '') # performance replacement:
212
- form_vars.slice!(-1) if form_vars[-1] == ?\0
213
-
214
- @env["rack.request.form_vars"] = form_vars
215
- @env["rack.request.form_hash"] = parse_query({ :query => form_vars, :separator => '&' })
216
-
217
- @env["rack.input"].rewind
204
+ def cookies
205
+ hash = fetch_header(RACK_REQUEST_COOKIE_HASH) do |k|
206
+ set_header(k, {})
218
207
  end
219
- @env["rack.request.form_input"] = @env["rack.input"]
220
- @env["rack.request.form_hash"]
221
- else
222
- {}
208
+ string = get_header HTTP_COOKIE
209
+
210
+ return hash if string == get_header(RACK_REQUEST_COOKIE_STRING)
211
+ hash.replace Utils.parse_cookies_header get_header HTTP_COOKIE
212
+ set_header(RACK_REQUEST_COOKIE_STRING, string)
213
+ hash
223
214
  end
224
- end
225
215
 
226
- # The union of GET and POST data.
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.
229
- def params
230
- @params ||= self.GET.merge(self.POST)
231
- rescue EOFError
232
- self.GET.dup
233
- end
216
+ def content_type
217
+ content_type = get_header('CONTENT_TYPE')
218
+ content_type.nil? || content_type.empty? ? nil : content_type
219
+ end
234
220
 
235
- # Destructively update a parameter, whether it's in GET and/or POST. Returns nil.
236
- #
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.
240
- def update_param(k, v)
241
- found = false
242
- if self.GET.has_key?(k)
243
- found = true
244
- self.GET[k] = v
221
+ def xhr?
222
+ get_header("HTTP_X_REQUESTED_WITH") == "XMLHttpRequest"
245
223
  end
246
- if self.POST.has_key?(k)
247
- found = true
248
- self.POST[k] = v
224
+
225
+ def host_with_port
226
+ if forwarded = get_header(HTTP_X_FORWARDED_HOST)
227
+ forwarded.split(/,\s?/).last
228
+ else
229
+ get_header(HTTP_HOST) || "#{get_header(SERVER_NAME) || get_header(SERVER_ADDR)}:#{get_header(SERVER_PORT)}"
230
+ end
249
231
  end
250
- unless found
251
- self.GET[k] = v
232
+
233
+ def host
234
+ # Remove port number.
235
+ host_with_port.to_s.sub(/:\d+\z/, '')
252
236
  end
253
- @params = nil
254
- nil
255
- end
256
237
 
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.
260
- #
261
- # env['rack.input'] is not touched.
262
- def delete_param(k)
263
- v = [ self.POST.delete(k), self.GET.delete(k) ].compact.first
264
- @params = nil
265
- v
266
- end
238
+ def port
239
+ if port = host_with_port.split(/:/)[1]
240
+ port.to_i
241
+ elsif port = get_header(HTTP_X_FORWARDED_PORT)
242
+ port.to_i
243
+ elsif has_header?(HTTP_X_FORWARDED_HOST)
244
+ DEFAULT_PORTS[scheme]
245
+ elsif has_header?(HTTP_X_FORWARDED_PROTO)
246
+ DEFAULT_PORTS[get_header(HTTP_X_FORWARDED_PROTO).split(',')[0]]
247
+ else
248
+ get_header(SERVER_PORT).to_i
249
+ end
250
+ end
267
251
 
268
- # shortcut for request.params[key]
269
- def [](key)
270
- params[key.to_s]
271
- end
252
+ def ssl?
253
+ scheme == 'https'
254
+ end
272
255
 
273
- # shortcut for request.params[key] = value
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.
276
- def []=(key, value)
277
- params[key.to_s] = value
278
- end
256
+ def ip
257
+ remote_addrs = split_ip_addresses(get_header('REMOTE_ADDR'))
258
+ remote_addrs = reject_trusted_ip_addresses(remote_addrs)
279
259
 
280
- # like Hash#values_at
281
- def values_at(*keys)
282
- keys.map{|key| params[key] }
283
- end
260
+ return remote_addrs.first if remote_addrs.any?
284
261
 
285
- # the referer of the client
286
- def referer
287
- @env['HTTP_REFERER']
288
- end
289
- alias referrer referer
262
+ forwarded_ips = split_ip_addresses(get_header('HTTP_X_FORWARDED_FOR'))
290
263
 
291
- def user_agent
292
- @env['HTTP_USER_AGENT']
293
- end
264
+ return reject_trusted_ip_addresses(forwarded_ips).last || get_header("REMOTE_ADDR")
265
+ end
294
266
 
295
- def cookies
296
- hash = @env["rack.request.cookie_hash"] ||= {}
297
- string = @env["HTTP_COOKIE"]
298
-
299
- return hash if string == @env["rack.request.cookie_string"]
300
- hash.clear
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.
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 }
309
- @env["rack.request.cookie_string"] = string
310
- hash
311
- end
267
+ # The media type (type/subtype) portion of the CONTENT_TYPE header
268
+ # without any media type parameters. e.g., when CONTENT_TYPE is
269
+ # "text/plain;charset=utf-8", the media-type is "text/plain".
270
+ #
271
+ # For more information on the use of media types in HTTP, see:
272
+ # http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7
273
+ def media_type
274
+ MediaType.type(content_type)
275
+ end
312
276
 
313
- def xhr?
314
- @env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest"
315
- end
277
+ # The media type parameters provided in CONTENT_TYPE as a Hash, or
278
+ # an empty Hash if no CONTENT_TYPE or media-type parameters were
279
+ # provided. e.g., when the CONTENT_TYPE is "text/plain;charset=utf-8",
280
+ # this method responds with the following Hash:
281
+ # { 'charset' => 'utf-8' }
282
+ def media_type_params
283
+ MediaType.params(content_type)
284
+ end
316
285
 
317
- def base_url
318
- url = "#{scheme}://#{host}"
319
- url << ":#{port}" if port != DEFAULT_PORTS[scheme]
320
- url
321
- end
286
+ # The character set of the request body if a "charset" media type
287
+ # parameter was given, or nil if no "charset" was specified. Note
288
+ # that, per RFC2616, text/* media types that specify no explicit
289
+ # charset are to be considered ISO-8859-1.
290
+ def content_charset
291
+ media_type_params['charset']
292
+ end
322
293
 
323
- # Tries to return a remake of the original request URL as a string.
324
- def url
325
- base_url + fullpath
326
- end
294
+ # Determine whether the request body contains form-data by checking
295
+ # the request Content-Type for one of the media-types:
296
+ # "application/x-www-form-urlencoded" or "multipart/form-data". The
297
+ # list of form-data media types can be modified through the
298
+ # +FORM_DATA_MEDIA_TYPES+ array.
299
+ #
300
+ # A request body is also assumed to contain form-data when no
301
+ # Content-Type header is provided and the request_method is POST.
302
+ def form_data?
303
+ type = media_type
304
+ meth = get_header(RACK_METHODOVERRIDE_ORIGINAL_METHOD) || get_header(REQUEST_METHOD)
305
+ (meth == POST && type.nil?) || FORM_DATA_MEDIA_TYPES.include?(type)
306
+ end
327
307
 
328
- def path
329
- script_name + path_info
330
- end
308
+ # Determine whether the request body contains data by checking
309
+ # the request media_type against registered parse-data media-types
310
+ def parseable_data?
311
+ PARSEABLE_DATA_MEDIA_TYPES.include?(media_type)
312
+ end
331
313
 
332
- def fullpath
333
- query_string.empty? ? path : "#{path}?#{query_string}"
334
- end
314
+ # Returns the data received in the query string.
315
+ def GET
316
+ if get_header(RACK_REQUEST_QUERY_STRING) == query_string
317
+ get_header(RACK_REQUEST_QUERY_HASH)
318
+ else
319
+ query_hash = parse_query(query_string, '&;')
320
+ set_header(RACK_REQUEST_QUERY_STRING, query_string)
321
+ set_header(RACK_REQUEST_QUERY_HASH, query_hash)
322
+ end
323
+ end
335
324
 
336
- def accept_encoding
337
- parse_http_accept_header(@env["HTTP_ACCEPT_ENCODING"])
338
- end
325
+ # Returns the data received in the request body.
326
+ #
327
+ # This method support both application/x-www-form-urlencoded and
328
+ # multipart/form-data.
329
+ def POST
330
+ if get_header(RACK_INPUT).nil?
331
+ raise "Missing rack.input"
332
+ elsif get_header(RACK_REQUEST_FORM_INPUT) == get_header(RACK_INPUT)
333
+ get_header(RACK_REQUEST_FORM_HASH)
334
+ elsif form_data? || parseable_data?
335
+ unless set_header(RACK_REQUEST_FORM_HASH, parse_multipart)
336
+ form_vars = get_header(RACK_INPUT).read
337
+
338
+ # Fix for Safari Ajax postings that always append \0
339
+ # form_vars.sub!(/\0\z/, '') # performance replacement:
340
+ form_vars.slice!(-1) if form_vars[-1] == ?\0
341
+
342
+ set_header RACK_REQUEST_FORM_VARS, form_vars
343
+ set_header RACK_REQUEST_FORM_HASH, parse_query(form_vars, '&')
344
+
345
+ get_header(RACK_INPUT).rewind
346
+ end
347
+ set_header RACK_REQUEST_FORM_INPUT, get_header(RACK_INPUT)
348
+ get_header RACK_REQUEST_FORM_HASH
349
+ else
350
+ {}
351
+ end
352
+ end
339
353
 
340
- def accept_language
341
- parse_http_accept_header(@env["HTTP_ACCEPT_LANGUAGE"])
342
- end
354
+ # The union of GET and POST data.
355
+ #
356
+ # Note that modifications will not be persisted in the env. Use update_param or delete_param if you want to destructively modify params.
357
+ def params
358
+ self.GET.merge(self.POST)
359
+ rescue EOFError
360
+ self.GET.dup
361
+ end
343
362
 
344
- def trusted_proxy?(ip)
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
346
- end
363
+ # Destructively update a parameter, whether it's in GET and/or POST. Returns nil.
364
+ #
365
+ # 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.
366
+ #
367
+ # <tt>env['rack.input']</tt> is not touched.
368
+ def update_param(k, v)
369
+ found = false
370
+ if self.GET.has_key?(k)
371
+ found = true
372
+ self.GET[k] = v
373
+ end
374
+ if self.POST.has_key?(k)
375
+ found = true
376
+ self.POST[k] = v
377
+ end
378
+ unless found
379
+ self.GET[k] = v
380
+ end
381
+ end
382
+
383
+ # Destructively delete a parameter, whether it's in GET or POST. Returns the value of the deleted parameter.
384
+ #
385
+ # If the parameter is in both GET and POST, the POST value takes precedence since that's how #params works.
386
+ #
387
+ # <tt>env['rack.input']</tt> is not touched.
388
+ def delete_param(k)
389
+ [ self.POST.delete(k), self.GET.delete(k) ].compact.first
390
+ end
391
+
392
+ def base_url
393
+ url = "#{scheme}://#{host}"
394
+ url << ":#{port}" if port != DEFAULT_PORTS[scheme]
395
+ url
396
+ end
347
397
 
348
- def ip
349
- remote_addrs = split_ip_addresses(@env['REMOTE_ADDR'])
350
- remote_addrs = reject_trusted_ip_addresses(remote_addrs)
398
+ # Tries to return a remake of the original request URL as a string.
399
+ def url
400
+ base_url + fullpath
401
+ end
351
402
 
352
- return remote_addrs.first if remote_addrs.any?
403
+ def path
404
+ script_name + path_info
405
+ end
353
406
 
354
- forwarded_ips = split_ip_addresses(@env['HTTP_X_FORWARDED_FOR'])
407
+ def fullpath
408
+ query_string.empty? ? path : "#{path}?#{query_string}"
409
+ end
355
410
 
356
- return reject_trusted_ip_addresses(forwarded_ips).last || @env["REMOTE_ADDR"]
357
- end
411
+ def accept_encoding
412
+ parse_http_accept_header(get_header("HTTP_ACCEPT_ENCODING"))
413
+ end
358
414
 
359
- protected
360
- def split_ip_addresses(ip_addresses)
361
- ip_addresses ? ip_addresses.strip.split(/[,\s]+/) : []
415
+ def accept_language
416
+ parse_http_accept_header(get_header("HTTP_ACCEPT_LANGUAGE"))
362
417
  end
363
418
 
364
- def reject_trusted_ip_addresses(ip_addresses)
365
- ip_addresses.reject { |ip| trusted_proxy?(ip) }
419
+ def trusted_proxy?(ip)
420
+ 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
366
421
  end
367
422
 
368
- def parse_query(qs)
369
- d = '&'
370
- qs, d = qs[:query], qs[:separator] if Hash === qs
371
- Utils.parse_nested_query(qs, d)
423
+ # shortcut for <tt>request.params[key]</tt>
424
+ def [](key)
425
+ if $verbose
426
+ warn("Request#[] is deprecated and will be removed in a future version of Rack. Please use request.params[] instead")
427
+ end
428
+
429
+ params[key.to_s]
372
430
  end
373
431
 
374
- def parse_multipart(env)
375
- Rack::Multipart.parse_multipart(env)
432
+ # shortcut for <tt>request.params[key] = value</tt>
433
+ #
434
+ # Note that modifications will not be persisted in the env. Use update_param or delete_param if you want to destructively modify params.
435
+ def []=(key, value)
436
+ if $verbose
437
+ warn("Request#[]= is deprecated and will be removed in a future version of Rack. Please use request.params[]= instead")
438
+ end
439
+
440
+ params[key.to_s] = value
376
441
  end
377
442
 
443
+ # like Hash#values_at
444
+ def values_at(*keys)
445
+ keys.map { |key| params[key] }
446
+ end
447
+
448
+ private
449
+
450
+ def default_session; {}; end
451
+
378
452
  def parse_http_accept_header(header)
379
453
  header.to_s.split(/\s*,\s*/).map do |part|
380
454
  attribute, parameters = part.split(/\s*;\s*/, 2)
@@ -386,26 +460,28 @@ module Rack
386
460
  end
387
461
  end
388
462
 
389
- private
390
- def strip_doublequotes(s)
391
- if s[0] == ?" && s[-1] == ?"
392
- s[1..-2]
393
- else
394
- s
463
+ def query_parser
464
+ Utils.default_query_parser
395
465
  end
396
- end
397
466
 
398
- def forwarded_scheme
399
- scheme_headers = [
400
- @env['HTTP_X_FORWARDED_SCHEME'],
401
- @env['HTTP_X_FORWARDED_PROTO'].to_s.split(',')[0]
402
- ]
467
+ def parse_query(qs, d='&')
468
+ query_parser.parse_nested_query(qs, d)
469
+ end
470
+
471
+ def parse_multipart
472
+ Rack::Multipart.extract_multipart(self, query_parser)
473
+ end
403
474
 
404
- scheme_headers.each do |header|
405
- return header if SCHEME_WHITELIST.include?(header)
475
+ def split_ip_addresses(ip_addresses)
476
+ ip_addresses ? ip_addresses.strip.split(/[,\s]+/) : []
406
477
  end
407
478
 
408
- nil
479
+ def reject_trusted_ip_addresses(ip_addresses)
480
+ ip_addresses.reject { |ip| trusted_proxy?(ip) }
481
+ end
409
482
  end
483
+
484
+ include Env
485
+ include Helpers
410
486
  end
411
487
  end