1
1
# frozen_string_literal: true
2
2
require 'set'
3
3
require 'time'
4
- require 'rack/utils '
4
+ require 'rack'
5
5
6
6
module Sprockets
7
7
# `Server` is a concern mixed into `Environment` and
@@ -11,6 +11,16 @@ module Server
11
11
# Supported HTTP request methods.
12
12
ALLOWED_REQUEST_METHODS = [ 'GET' , 'HEAD' ] . to_set . freeze
13
13
14
+ # :stopdoc:
15
+ if Gem ::Version . new ( Rack ::RELEASE ) < Gem ::Version . new ( "3" )
16
+ X_CASCADE = "X-Cascade"
17
+ VARY = "Vary"
18
+ else
19
+ X_CASCADE = "x-cascade"
20
+ VARY = "vary"
21
+ end
22
+ # :startdoc:
23
+
14
24
# `call` implements the Rack 1.x specification which accepts an
15
25
# `env` Hash and returns a three item tuple with the status code,
16
26
# headers, and body.
@@ -148,39 +158,39 @@ def not_modified_response(env, etag)
148
158
# Returns a 400 Forbidden response tuple
149
159
def bad_request_response ( env )
150
160
if head_request? ( env )
151
- [ 400 , { "content-type" => "text/plain" , "content-length" => "0" } , [ ] ]
161
+ [ 400 , { Rack :: CONTENT_TYPE => "text/plain" , Rack :: CONTENT_LENGTH => "0" } , [ ] ]
152
162
else
153
- [ 400 , { "content-type" => "text/plain" , "content-length" => "11" } , [ "Bad Request" ] ]
163
+ [ 400 , { Rack :: CONTENT_TYPE => "text/plain" , Rack :: CONTENT_LENGTH => "11" } , [ "Bad Request" ] ]
154
164
end
155
165
end
156
166
157
167
# Returns a 403 Forbidden response tuple
158
168
def forbidden_response ( env )
159
169
if head_request? ( env )
160
- [ 403 , { "content-type" => "text/plain" , "content-length" => "0" } , [ ] ]
170
+ [ 403 , { Rack :: CONTENT_TYPE => "text/plain" , Rack :: CONTENT_LENGTH => "0" } , [ ] ]
161
171
else
162
- [ 403 , { "content-type" => "text/plain" , "content-length" => "9" } , [ "Forbidden" ] ]
172
+ [ 403 , { Rack :: CONTENT_TYPE => "text/plain" , Rack :: CONTENT_LENGTH => "9" } , [ "Forbidden" ] ]
163
173
end
164
174
end
165
175
166
176
# Returns a 404 Not Found response tuple
167
177
def not_found_response ( env )
168
178
if head_request? ( env )
169
- [ 404 , { "content-type" => "text/plain" , "content-length" => "0" , "x-cascade" => "pass" } , [ ] ]
179
+ [ 404 , { Rack :: CONTENT_TYPE => "text/plain" , Rack :: CONTENT_LENGTH => "0" , X_CASCADE => "pass" } , [ ] ]
170
180
else
171
- [ 404 , { "content-type" => "text/plain" , "content-length" => "9" , "x-cascade" => "pass" } , [ "Not found" ] ]
181
+ [ 404 , { Rack :: CONTENT_TYPE => "text/plain" , Rack :: CONTENT_LENGTH => "9" , X_CASCADE => "pass" } , [ "Not found" ] ]
172
182
end
173
183
end
174
184
175
185
def method_not_allowed_response
176
- [ 405 , { "content-type" => "text/plain" , "content-length" => "18" } , [ "Method Not Allowed" ] ]
186
+ [ 405 , { Rack :: CONTENT_TYPE => "text/plain" , Rack :: CONTENT_LENGTH => "18" } , [ "Method Not Allowed" ] ]
177
187
end
178
188
179
189
def precondition_failed_response ( env )
180
190
if head_request? ( env )
181
- [ 412 , { "content-type" => "text/plain" , "content-length" => "0" , "x-cascade" => "pass" } , [ ] ]
191
+ [ 412 , { Rack :: CONTENT_TYPE => "text/plain" , Rack :: CONTENT_LENGTH => "0" , X_CASCADE => "pass" } , [ ] ]
182
192
else
183
- [ 412 , { "content-type" => "text/plain" , "content-length" => "19" , "x-cascade" => "pass" } , [ "Precondition Failed" ] ]
193
+ [ 412 , { Rack :: CONTENT_TYPE => "text/plain" , Rack :: CONTENT_LENGTH => "19" , X_CASCADE => "pass" } , [ "Precondition Failed" ] ]
184
194
end
185
195
end
186
196
@@ -189,7 +199,7 @@ def precondition_failed_response(env)
189
199
def javascript_exception_response ( exception )
190
200
err = "#{ exception . class . name } : #{ exception . message } \n (in #{ exception . backtrace [ 0 ] } )"
191
201
body = "throw Error(#{ err . inspect } )"
192
- [ 200 , { "content-type" => "application/javascript" , "content-length" => body . bytesize . to_s } , [ body ] ]
202
+ [ 200 , { Rack :: CONTENT_TYPE => "application/javascript" , Rack :: CONTENT_LENGTH => body . bytesize . to_s } , [ body ] ]
193
203
end
194
204
195
205
# Returns a CSS response that hides all elements on the page and
@@ -242,7 +252,7 @@ def css_exception_response(exception)
242
252
}
243
253
CSS
244
254
245
- [ 200 , { "content-type" => "text/css; charset=utf-8" , "content-length" => body . bytesize . to_s } , [ body ] ]
255
+ [ 200 , { Rack :: CONTENT_TYPE => "text/css; charset=utf-8" , Rack :: CONTENT_LENGTH => body . bytesize . to_s } , [ body ] ]
246
256
end
247
257
248
258
# Escape special characters for use inside a CSS content("...") string
@@ -258,18 +268,18 @@ def cache_headers(env, etag)
258
268
headers = { }
259
269
260
270
# Set caching headers
261
- headers [ "cache-control" ] = +"public"
262
- headers [ "etag" ] = %("#{ etag } ")
271
+ headers [ Rack :: CACHE_CONTROL ] = +"public"
272
+ headers [ Rack :: ETAG ] = %("#{ etag } ")
263
273
264
274
# If the request url contains a fingerprint, set a long
265
275
# expires on the response
266
276
if path_fingerprint ( env [ "PATH_INFO" ] )
267
- headers [ "cache-control" ] << ", max-age=31536000, immutable"
277
+ headers [ Rack :: CACHE_CONTROL ] << ", max-age=31536000, immutable"
268
278
269
279
# Otherwise set `must-revalidate` since the asset could be modified.
270
280
else
271
- headers [ "cache-control" ] << ", must-revalidate"
272
- headers [ "vary" ] = "Accept-Encoding"
281
+ headers [ Rack :: CACHE_CONTROL ] << ", must-revalidate"
282
+ headers [ VARY ] = "Accept-Encoding"
273
283
end
274
284
275
285
headers
@@ -279,15 +289,15 @@ def headers(env, asset, length)
279
289
headers = { }
280
290
281
291
# Set content length header
282
- headers [ "content-length" ] = length . to_s
292
+ headers [ Rack :: CONTENT_LENGTH ] = length . to_s
283
293
284
294
# Set content type header
285
295
if type = asset . content_type
286
296
# Set charset param for text/* mime types
287
297
if type . start_with? ( "text/" ) && asset . charset
288
298
type += "; charset=#{ asset . charset } "
289
299
end
290
- headers [ "content-type" ] = type
300
+ headers [ Rack :: CONTENT_TYPE ] = type
291
301
end
292
302
293
303
headers . merge ( cache_headers ( env , asset . etag ) )
0 commit comments