Skip to content

Commit d54ba54

Browse files
authored
Fail requests pipelined behind a 101 response (#529)
After a 101 response the connection stayed open with the queued requests in place, so bytes arriving later, which belong to the protocol the server switched to (RFC 9110 15.2.2), were parsed as HTTP/1 responses to those requests. Queued requests now fail with the new :connection_upgraded error when the 101 response completes. Bytes arriving later are unexpected data as long as no new request is made on the connection. A failed request that was streaming its body can't stream the rest of it. :unprocessed isn't used because the pipelined request bytes reached the server as part of the new protocol, so the request might have been processed. The bytes after the 101 response in the same packet are still delivered as data for the upgrade request.
1 parent af2836f commit d54ba54

2 files changed

Lines changed: 93 additions & 1 deletion

File tree

‎lib/mint/http1.ex‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ defmodule Mint.HTTP1 do
5656
`Mint.TransportError` with reason `:closed` instead, since the server might have
5757
processed them.
5858
59+
* `:connection_upgraded` - when a pipelined request gets no response because the
60+
server answered a previous request with a `101` response. The connection then
61+
uses another protocol, and the server might have received the request as part
62+
of it.
63+
5964
* `{:unexpected_data, data}` - when unexpected data is received from the server.
6065
6166
* `:invalid_status_line` - when the HTTP/1 status line is invalid.
@@ -894,9 +899,12 @@ defmodule Mint.HTTP1 do
894899
decode(:status, conn, data, responses)
895900
end
896901

902+
# A 101 response switches the connection to another protocol (RFC 9110
903+
# 15.2.2), so the bytes after it belong to that protocol.
897904
defp decode_body(:single, conn, data, _request_ref, responses) do
898905
{conn, responses} = add_body(conn, data, responses)
899906
{conn, responses} = request_done(conn, responses)
907+
{conn, responses} = fail_requests(conn, responses, wrap_error(:connection_upgraded))
900908
{:ok, conn, responses}
901909
end
902910

@@ -1205,14 +1213,28 @@ defmodule Mint.HTTP1 do
12051213
# Requests pipelined behind a response that closes the connection never get a
12061214
# response of their own.
12071215
defp close_after_response(conn, responses, error) do
1216+
{conn, responses} = fail_requests(conn, responses, error)
1217+
{internal_close(conn), responses}
1218+
end
1219+
1220+
# Returns an error response with the given error for the current request and
1221+
# every queued one, and removes them from the connection. If one of them is
1222+
# streaming its body, the streaming state is cleared so the rest of the body
1223+
# can't be sent.
1224+
defp fail_requests(conn, responses, error) do
12081225
requests = if conn.request, do: [conn.request | :queue.to_list(conn.requests)], else: []
12091226

12101227
responses =
12111228
Enum.reduce(requests, responses, fn request, responses ->
12121229
[{:error, request.ref, error} | responses]
12131230
end)
12141231

1215-
{internal_close(%{conn | request: nil, requests: :queue.new()}), responses}
1232+
# No request can be made while one is streaming its body, so a streaming
1233+
# request is the last one and it's among the failed ones unless it's done.
1234+
streaming_request = if requests == [], do: conn.streaming_request
1235+
1236+
conn = %{conn | request: nil, requests: :queue.new(), streaming_request: streaming_request}
1237+
{conn, responses}
12161238
end
12171239

12181240
defp pop_request(conn) do
@@ -1433,6 +1455,11 @@ defmodule Mint.HTTP1 do
14331455
"\"connection: close\", so it's safe to retry on a new connection"
14341456
end
14351457

1458+
def format_error(:connection_upgraded) do
1459+
"request got no response because the server switched the connection to another " <>
1460+
"protocol in response to a previous request"
1461+
end
1462+
14361463
def format_error(:request_body_is_streaming) do
14371464
"a request body is currently streaming, so no new requests can be issued"
14381465
end

‎test/mint/http1/conn_test.exs‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,71 @@ defmodule Mint.HTTP1Test do
12401240
assert conn.buffer == <<>>
12411241
end
12421242

1243+
test "a request pipelined behind a successful CONNECT gets its response from the tunnel",
1244+
%{conn: conn} do
1245+
{:ok, conn, ref1} = HTTP1.request(conn, "CONNECT", "example.com:80", [], nil)
1246+
{:ok, conn, ref2} = HTTP1.request(conn, "GET", "/", [], nil)
1247+
1248+
assert {:ok, conn, [{:status, ^ref1, 200}, {:headers, ^ref1, []}, {:done, ^ref1}]} =
1249+
HTTP1.stream(
1250+
conn,
1251+
{:tcp, conn.socket, "HTTP/1.1 200 Connection Established\r\n\r\n"}
1252+
)
1253+
1254+
response = "HTTP/1.1 200 OK\r\ncontent-length: 2\r\n\r\nok"
1255+
1256+
assert {:ok, conn,
1257+
[{:status, ^ref2, 200}, {:headers, ^ref2, _}, {:data, ^ref2, "ok"}, {:done, ^ref2}]} =
1258+
HTTP1.stream(conn, {:tcp, conn.socket, response})
1259+
1260+
assert HTTP1.open?(conn)
1261+
end
1262+
1263+
test "requests pipelined behind a 101 response get no response", %{conn: conn} do
1264+
{:ok, conn, ref1} = HTTP1.request(conn, "GET", "/", [], nil)
1265+
{:ok, conn, ref2} = HTTP1.request(conn, "GET", "/", [], nil)
1266+
1267+
response = "HTTP/1.1 101 Switching Protocols\r\nupgrade: foo\r\nconnection: upgrade\r\n\r\n"
1268+
1269+
assert {:ok, conn, responses} = HTTP1.stream(conn, {:tcp, conn.socket, response})
1270+
1271+
assert [
1272+
{:status, ^ref1, 101},
1273+
{:headers, ^ref1, _},
1274+
{:done, ^ref1},
1275+
{:error, ^ref2, %HTTPError{reason: :connection_upgraded}}
1276+
] = responses
1277+
1278+
assert HTTP1.open?(conn)
1279+
assert HTTP1.open_request_count(conn) == 0
1280+
1281+
data = "HTTP/1.1 200 OK\r\ncontent-length: 2\r\n\r\nok"
1282+
1283+
assert {:error, _conn, %HTTPError{reason: {:unexpected_data, ^data}}, []} =
1284+
HTTP1.stream(conn, {:tcp, conn.socket, data})
1285+
end
1286+
1287+
test "a streaming request pipelined behind a 101 response can't stream its body",
1288+
%{conn: conn} do
1289+
{:ok, conn, ref1} = HTTP1.request(conn, "GET", "/", [], nil)
1290+
{:ok, conn, ref2} = HTTP1.request(conn, "POST", "/", [], :stream)
1291+
1292+
response = "HTTP/1.1 101 Switching Protocols\r\nupgrade: foo\r\nconnection: upgrade\r\n\r\n"
1293+
1294+
assert {:ok, conn,
1295+
[
1296+
{:status, ^ref1, 101},
1297+
{:headers, ^ref1, _},
1298+
{:done, ^ref1},
1299+
{:error, ^ref2, %HTTPError{reason: :connection_upgraded}}
1300+
]} = HTTP1.stream(conn, {:tcp, conn.socket, response})
1301+
1302+
assert {:error, _conn, %HTTPError{reason: :unknown_request_to_stream}} =
1303+
HTTP1.stream_request_body(conn, ref2, "hello")
1304+
1305+
assert_raise ArgumentError, fn -> HTTP1.request_body_window(conn, ref2) end
1306+
end
1307+
12431308
test "unallowed trailer headers are removed from the trailer headers", %{conn: conn} do
12441309
{:ok, conn, ref} = HTTP1.request(conn, "GET", "/", [], nil)
12451310

0 commit comments

Comments
 (0)