Skip to content

Commit fe57bd2

Browse files
committed
Lower case the link header
Since rack/rack@1fbcf54 the early hints will be checked against Rack::Link that requires all headers to be lower cased. Fixes rails#51961.
1 parent 157f1df commit fe57bd2

File tree

7 files changed

+29
-29
lines changed

7 files changed

+29
-29
lines changed

actionpack/lib/action_dispatch/http/request.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,11 @@ def headers
230230
# start making preparations for processing the final response.
231231
#
232232
# If the env contains `rack.early_hints` then the server accepts HTTP2 push for
233-
# Link headers.
233+
# link headers.
234234
#
235235
# The `send_early_hints` method accepts a hash of links as follows:
236236
#
237-
# send_early_hints("Link" => "</style.css>; rel=preload; as=style\n</script.js>; rel=preload")
237+
# send_early_hints("link" => "</style.css>; rel=preload; as=style\n</script.js>; rel=preload")
238238
#
239239
# If you are using `javascript_include_tag` or `stylesheet_link_tag` the Early
240240
# Hints headers are included by default if supported.

actionpack/test/dispatch/request_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,8 +1412,8 @@ def setup
14121412
end
14131413

14141414
test "when early hints is set in the env link headers are sent" do
1415-
early_hints = @request.send_early_hints("Link" => "</style.css>; rel=preload; as=style\n</script.js>; rel=preload")
1416-
expected_hints = { "Link" => "</style.css>; rel=preload; as=style\n</script.js>; rel=preload" }
1415+
early_hints = @request.send_early_hints("link" => "</style.css>; rel=preload; as=style\n</script.js>; rel=preload")
1416+
expected_hints = { "link" => "</style.css>; rel=preload; as=style\n</script.js>; rel=preload" }
14171417

14181418
assert_equal expected_hints, early_hints
14191419
end

actionview/lib/action_view/helpers/asset_tag_helper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,11 +657,11 @@ def send_preload_links_header(preload_links, max_header_size: MAX_HEADER_SIZE)
657657
return if response_present && response.sending?
658658

659659
if respond_to?(:request) && request
660-
request.send_early_hints("Link" => preload_links.join("\n"))
660+
request.send_early_hints("link" => preload_links.join("\n"))
661661
end
662662

663663
if response_present
664-
header = +response.headers["Link"].to_s
664+
header = +response.headers["link"].to_s
665665
preload_links.each do |link|
666666
break if header.bytesize + link.bytesize > max_header_size
667667

@@ -672,7 +672,7 @@ def send_preload_links_header(preload_links, max_header_size: MAX_HEADER_SIZE)
672672
end
673673
end
674674

675-
response.headers["Link"] = header
675+
response.headers["link"] = header
676676
end
677677
end
678678
end

actionview/test/template/asset_tag_helper_test.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -634,23 +634,23 @@ def test_should_set_preload_links
634634
stylesheet_link_tag("http://example.com/style.css")
635635
javascript_include_tag("http://example.com/all.js")
636636
expected = "<http://example.com/style.css>; rel=preload; as=style; nopush,<http://example.com/all.js>; rel=preload; as=script; nopush"
637-
assert_equal expected, @response.headers["Link"]
637+
assert_equal expected, @response.headers["link"]
638638
end
639639
end
640640

641641
def test_should_not_set_preload_links_for_data_url
642642
with_preload_links_header do
643643
stylesheet_link_tag("data:text/css;base64,YWxlcnQoIkhlbGxvIik7")
644644
javascript_include_tag("data:text/javascript;base64,YWxlcnQoIkhlbGxvIik7")
645-
assert_nil @response.headers["Link"]
645+
assert_nil @response.headers["link"]
646646
end
647647
end
648648

649649
def test_should_not_set_preload_links_if_opted_out_at_invokation
650650
with_preload_links_header do
651651
stylesheet_link_tag("http://example.com/style.css", preload_links_header: false)
652652
javascript_include_tag("http://example.com/all.js", preload_links_header: false)
653-
assert_nil @response.headers["Link"]
653+
assert_nil @response.headers["link"]
654654
end
655655
end
656656

@@ -659,7 +659,7 @@ def test_should_set_preload_links_if_opted_in_at_invokation
659659
stylesheet_link_tag("http://example.com/style.css", preload_links_header: true)
660660
javascript_include_tag("http://example.com/all.js", preload_links_header: true)
661661
expected = "<http://example.com/style.css>; rel=preload; as=style; nopush,<http://example.com/all.js>; rel=preload; as=script; nopush"
662-
assert_equal expected, @response.headers["Link"]
662+
assert_equal expected, @response.headers["link"]
663663
end
664664
end
665665

@@ -669,15 +669,15 @@ def test_should_generate_links_under_the_max_size
669669
stylesheet_link_tag("http://example.com/style.css?#{i}")
670670
javascript_include_tag("http://example.com/all.js?#{i}")
671671
end
672-
links = @response.headers["Link"].count(",")
672+
links = @response.headers["link"].count(",")
673673
assert_equal 14, links
674674
end
675675
end
676676

677677
def test_should_not_preload_links_with_defer
678678
with_preload_links_header do
679679
javascript_include_tag("http://example.com/all.js", defer: true)
680-
assert_nil @response.headers["Link"]
680+
assert_nil @response.headers["link"]
681681
end
682682
end
683683

@@ -686,7 +686,7 @@ def test_should_allow_caller_to_remove_nopush
686686
stylesheet_link_tag("http://example.com/style.css", nopush: false)
687687
javascript_include_tag("http://example.com/all.js", nopush: false)
688688
expected = "<http://example.com/style.css>; rel=preload; as=style,<http://example.com/all.js>; rel=preload; as=script"
689-
assert_equal expected, @response.headers["Link"]
689+
assert_equal expected, @response.headers["link"]
690690
end
691691
end
692692

@@ -695,23 +695,23 @@ def test_should_set_preload_links_with_cross_origin
695695
stylesheet_link_tag("http://example.com/style.css", crossorigin: "use-credentials")
696696
javascript_include_tag("http://example.com/all.js", crossorigin: true)
697697
expected = "<http://example.com/style.css>; rel=preload; as=style; crossorigin=use-credentials; nopush,<http://example.com/all.js>; rel=preload; as=script; crossorigin=anonymous; nopush"
698-
assert_equal expected, @response.headers["Link"]
698+
assert_equal expected, @response.headers["link"]
699699
end
700700
end
701701

702702
def test_should_set_preload_links_with_rel_modulepreload
703703
with_preload_links_header do
704704
javascript_include_tag("http://example.com/all.js", type: "module")
705705
expected = "<http://example.com/all.js>; rel=modulepreload; as=script; nopush"
706-
assert_equal expected, @response.headers["Link"]
706+
assert_equal expected, @response.headers["link"]
707707
end
708708
end
709709

710710
def test_should_set_preload_early_hints_with_rel_modulepreload
711711
with_preload_links_header do
712712
preload_link_tag("http://example.com/all.js", type: "module")
713713
expected = "<http://example.com/all.js>; rel=modulepreload; as=script; type=module"
714-
assert_equal expected, @response.headers["Link"]
714+
assert_equal expected, @response.headers["link"]
715715
end
716716
end
717717

@@ -720,15 +720,15 @@ def test_should_set_preload_links_with_integrity_hashes
720720
stylesheet_link_tag("http://example.com/style.css", integrity: "sha256-AbpHGcgLb+kRsJGnwFEktk7uzpZOCcBY74+YBdrKVGs")
721721
javascript_include_tag("http://example.com/all.js", integrity: "sha256-AbpHGcgLb+kRsJGnwFEktk7uzpZOCcBY74+YBdrKVGs")
722722
expected = "<http://example.com/style.css>; rel=preload; as=style; integrity=sha256-AbpHGcgLb+kRsJGnwFEktk7uzpZOCcBY74+YBdrKVGs; nopush,<http://example.com/all.js>; rel=preload; as=script; integrity=sha256-AbpHGcgLb+kRsJGnwFEktk7uzpZOCcBY74+YBdrKVGs; nopush"
723-
assert_equal expected, @response.headers["Link"]
723+
assert_equal expected, @response.headers["link"]
724724
end
725725
end
726726

727727
def test_should_not_preload_links_when_disabled
728728
with_preload_links_header(false) do
729729
stylesheet_link_tag("http://example.com/style.css")
730730
javascript_include_tag("http://example.com/all.js")
731-
assert_nil @response.headers["Link"]
731+
assert_nil @response.headers["link"]
732732
end
733733
end
734734

guides/source/6_1_release_notes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ Please refer to the [Changelog][action-view] for detailed changes.
176176

177177
* Make `locals` argument required on `ActionView::Template#initialize`.
178178

179-
* The `javascript_include_tag` and `stylesheet_link_tag` asset helpers generate a `Link` header that gives hints to modern browsers about preloading assets. This can be disabled by setting `config.action_view.preload_links_header` to `false`.
179+
* The `javascript_include_tag` and `stylesheet_link_tag` asset helpers generate a `link` header that gives hints to modern browsers about preloading assets. This can be disabled by setting `config.action_view.preload_links_header` to `false`.
180180

181181
Action Mailer
182182
-------------

guides/source/configuring.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2219,7 +2219,7 @@ Determines whether to annotate rendered view with template file names. This defa
22192219
22202220
#### `config.action_view.preload_links_header`
22212221
2222-
Determines whether `javascript_include_tag` and `stylesheet_link_tag` will generate a `Link` header that preload assets.
2222+
Determines whether `javascript_include_tag` and `stylesheet_link_tag` will generate a `link` header that preload assets.
22232223
22242224
The default value depends on the `config.load_defaults` target version:
22252225

railties/test/application/configuration_test.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3188,7 +3188,7 @@ class ::DummyDestroyAssociationAsyncJob; end
31883188
assert_equal false, ActionView::Helpers::AssetTagHelper.apply_stylesheet_media_default
31893189
end
31903190

3191-
test "stylesheet_link_tag sets the Link header by default" do
3191+
test "stylesheet_link_tag sets the link header by default" do
31923192
app_file "app/controllers/pages_controller.rb", <<-RUBY
31933193
class PagesController < ApplicationController
31943194
def index
@@ -3207,10 +3207,10 @@ def index
32073207

32083208
get "/"
32093209
assert_match %r[<link rel="stylesheet" href="/application.css" />], last_response.body
3210-
assert_equal "</application.css>; rel=preload; as=style; nopush", last_response.headers["Link"]
3210+
assert_equal "</application.css>; rel=preload; as=style; nopush", last_response.headers["link"]
32113211
end
32123212

3213-
test "stylesheet_link_tag doesn't set the Link header when disabled" do
3213+
test "stylesheet_link_tag doesn't set the link header when disabled" do
32143214
app_file "config/initializers/action_view.rb", <<-RUBY
32153215
Rails.application.config.action_view.preload_links_header = false
32163216
RUBY
@@ -3233,10 +3233,10 @@ def index
32333233

32343234
get "/"
32353235
assert_match %r[<link rel="stylesheet" href="/application.css" />], last_response.body
3236-
assert_nil last_response.headers["Link"]
3236+
assert_nil last_response.headers["link"]
32373237
end
32383238

3239-
test "javascript_include_tag sets the Link header by default" do
3239+
test "javascript_include_tag sets the link header by default" do
32403240
app_file "app/controllers/pages_controller.rb", <<-RUBY
32413241
class PagesController < ApplicationController
32423242
def index
@@ -3255,10 +3255,10 @@ def index
32553255

32563256
get "/"
32573257
assert_match %r[<script src="/application.js"></script>], last_response.body
3258-
assert_equal "</application.js>; rel=preload; as=script; nopush", last_response.headers["Link"]
3258+
assert_equal "</application.js>; rel=preload; as=script; nopush", last_response.headers["link"]
32593259
end
32603260

3261-
test "javascript_include_tag doesn't set the Link header when disabled" do
3261+
test "javascript_include_tag doesn't set the link header when disabled" do
32623262
app_file "config/initializers/action_view.rb", <<-RUBY
32633263
Rails.application.config.action_view.preload_links_header = false
32643264
RUBY
@@ -3281,7 +3281,7 @@ def index
32813281

32823282
get "/"
32833283
assert_match %r[<script src="/application.js"></script>], last_response.body
3284-
assert_nil last_response.headers["Link"]
3284+
assert_nil last_response.headers["link"]
32853285
end
32863286

32873287
test "ActiveJob::Base.retry_jitter is 0.15 by default for new apps" do

0 commit comments

Comments
 (0)