Skip to content

Commit 42f7d5e

Browse files
authored
Merge pull request #790 from skipkayhil/hm-fix-rails-7-compat
Fix header casing compatibility with Rails 7
2 parents d0de178 + 5d795a7 commit 42f7d5e

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Get upgrade notes from Sprockets 3.x to 4.x at https://github.com/rails/sprockets/blob/master/UPGRADING.md
44

55
- Fix for precompile issues when multiple extensions map to the same MIME type (eg. `.jpeg` / `.jpg`). [#781](https://github.com/rails/sprockets/pull/781)
6+
- Fix compatibility with Rack 2 applications. [#790](https://github.com/rails/sprockets/pull/790)
67

78
## 4.2.0
89

lib/sprockets/server.rb

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22
require 'set'
33
require 'time'
4-
require 'rack/utils'
4+
require 'rack'
55

66
module Sprockets
77
# `Server` is a concern mixed into `Environment` and
@@ -11,6 +11,16 @@ module Server
1111
# Supported HTTP request methods.
1212
ALLOWED_REQUEST_METHODS = ['GET', 'HEAD'].to_set.freeze
1313

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+
1424
# `call` implements the Rack 1.x specification which accepts an
1525
# `env` Hash and returns a three item tuple with the status code,
1626
# headers, and body.
@@ -148,39 +158,39 @@ def not_modified_response(env, etag)
148158
# Returns a 400 Forbidden response tuple
149159
def bad_request_response(env)
150160
if head_request?(env)
151-
[ 400, { "content-type" => "text/plain", "content-length" => "0" }, [] ]
161+
[ 400, { Rack::CONTENT_TYPE => "text/plain", Rack::CONTENT_LENGTH => "0" }, [] ]
152162
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" ] ]
154164
end
155165
end
156166

157167
# Returns a 403 Forbidden response tuple
158168
def forbidden_response(env)
159169
if head_request?(env)
160-
[ 403, { "content-type" => "text/plain", "content-length" => "0" }, [] ]
170+
[ 403, { Rack::CONTENT_TYPE => "text/plain", Rack::CONTENT_LENGTH => "0" }, [] ]
161171
else
162-
[ 403, { "content-type" => "text/plain", "content-length" => "9" }, [ "Forbidden" ] ]
172+
[ 403, { Rack::CONTENT_TYPE => "text/plain", Rack::CONTENT_LENGTH => "9" }, [ "Forbidden" ] ]
163173
end
164174
end
165175

166176
# Returns a 404 Not Found response tuple
167177
def not_found_response(env)
168178
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" }, [] ]
170180
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" ] ]
172182
end
173183
end
174184

175185
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" ] ]
177187
end
178188

179189
def precondition_failed_response(env)
180190
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" }, [] ]
182192
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" ] ]
184194
end
185195
end
186196

@@ -189,7 +199,7 @@ def precondition_failed_response(env)
189199
def javascript_exception_response(exception)
190200
err = "#{exception.class.name}: #{exception.message}\n (in #{exception.backtrace[0]})"
191201
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 ] ]
193203
end
194204

195205
# Returns a CSS response that hides all elements on the page and
@@ -242,7 +252,7 @@ def css_exception_response(exception)
242252
}
243253
CSS
244254

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 ] ]
246256
end
247257

248258
# Escape special characters for use inside a CSS content("...") string
@@ -258,18 +268,18 @@ def cache_headers(env, etag)
258268
headers = {}
259269

260270
# Set caching headers
261-
headers["cache-control"] = +"public"
262-
headers["etag"] = %("#{etag}")
271+
headers[Rack::CACHE_CONTROL] = +"public"
272+
headers[Rack::ETAG] = %("#{etag}")
263273

264274
# If the request url contains a fingerprint, set a long
265275
# expires on the response
266276
if path_fingerprint(env["PATH_INFO"])
267-
headers["cache-control"] << ", max-age=31536000, immutable"
277+
headers[Rack::CACHE_CONTROL] << ", max-age=31536000, immutable"
268278

269279
# Otherwise set `must-revalidate` since the asset could be modified.
270280
else
271-
headers["cache-control"] << ", must-revalidate"
272-
headers["vary"] = "Accept-Encoding"
281+
headers[Rack::CACHE_CONTROL] << ", must-revalidate"
282+
headers[VARY] = "Accept-Encoding"
273283
end
274284

275285
headers
@@ -279,15 +289,15 @@ def headers(env, asset, length)
279289
headers = {}
280290

281291
# Set content length header
282-
headers["content-length"] = length.to_s
292+
headers[Rack::CONTENT_LENGTH] = length.to_s
283293

284294
# Set content type header
285295
if type = asset.content_type
286296
# Set charset param for text/* mime types
287297
if type.start_with?("text/") && asset.charset
288298
type += "; charset=#{asset.charset}"
289299
end
290-
headers["content-type"] = type
300+
headers[Rack::CONTENT_TYPE] = type
291301
end
292302

293303
headers.merge(cache_headers(env, asset.etag))

0 commit comments

Comments
 (0)