Skip to content

Commit ff2f829

Browse files
authored
Use declared scheme even if it does not match the transport (#160)
Fixes #159
1 parent 9b71290 commit ff2f829

3 files changed

Lines changed: 14 additions & 9 deletions

File tree

lib/bandit/pipeline.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,8 @@ defmodule Bandit.Pipeline do
5252
defp determine_scheme({secure?, _, _, _}, {scheme, _, _, _}) do
5353
case {scheme, secure?} do
5454
{nil, true} -> {:ok, "https"}
55-
{"https", true} -> {:ok, "https"}
5655
{nil, false} -> {:ok, "http"}
57-
{"http", false} -> {:ok, "http"}
58-
_ -> {:error, "request target scheme does not agree with transport"}
56+
{scheme, _} -> {:ok, scheme}
5957
end
6058
end
6159

test/bandit/http1/request_test.exs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,11 @@ defmodule HTTP1RequestTest do
201201
end
202202

203203
@tag capture_log: true
204-
test "returns 400 if URI scheme does not match the transport", context do
204+
test "uses request-line scheme even if it does not match the transport", context do
205205
client = SimpleHTTP1Client.tcp_client(context)
206206
SimpleHTTP1Client.send(client, "GET", "https://banana/echo_components")
207-
assert {:ok, "400 Bad Request", _headers, <<>>} = SimpleHTTP1Client.recv_reply(client)
207+
assert {:ok, "200 OK", _headers, body} = SimpleHTTP1Client.recv_reply(client)
208+
assert Jason.decode!(body)["scheme"] == "https"
208209
end
209210

210211
test "derives host from the URI, even if it differs from host header", context do

test/bandit/http2/protocol_test.exs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,7 +1661,7 @@ defmodule HTTP2ProtocolTest do
16611661
end
16621662

16631663
@tag capture_log: true
1664-
test "resets stream if scheme does not match transport", context do
1664+
test "uses :scheme even if it does not match transport", context do
16651665
socket = SimpleH2Client.setup_connection(context)
16661666

16671667
headers = [
@@ -1672,7 +1672,10 @@ defmodule HTTP2ProtocolTest do
16721672
]
16731673

16741674
SimpleH2Client.send_headers(socket, 1, true, headers)
1675-
assert SimpleH2Client.recv_rst_stream(socket) == {:ok, 1, 1}
1675+
1676+
assert SimpleH2Client.successful_response?(socket, 1, false)
1677+
{:ok, 1, true, body} = SimpleH2Client.recv_body(socket)
1678+
assert Jason.decode!(body)["scheme"] == "http"
16761679
end
16771680

16781681
test "derives host from host header", context do
@@ -1938,7 +1941,7 @@ defmodule HTTP2ProtocolTest do
19381941
end
19391942

19401943
@tag capture_log: true
1941-
test "resets stream if scheme does not match transport", context do
1944+
test "uses :scheme even if it does not match transport", context do
19421945
socket = SimpleH2Client.setup_connection(context)
19431946

19441947
headers = [
@@ -1949,7 +1952,10 @@ defmodule HTTP2ProtocolTest do
19491952
]
19501953

19511954
SimpleH2Client.send_headers(socket, 1, true, headers)
1952-
assert SimpleH2Client.recv_rst_stream(socket) == {:ok, 1, 1}
1955+
1956+
assert SimpleH2Client.successful_response?(socket, 1, false)
1957+
{:ok, 1, true, body} = SimpleH2Client.recv_body(socket)
1958+
assert Jason.decode!(body)["scheme"] == "http"
19531959
end
19541960

19551961
test "derives host from :authority header, even if it differs from host header", context do

0 commit comments

Comments
 (0)