Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions lib/mint/http2.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1515,7 +1515,6 @@ defmodule Mint.HTTP2 do
defp headers_to_encoded_frames(conn, stream_id, hbf, enabled_flags) do
if IO.iodata_length(hbf) > conn.server_settings.max_frame_size do
hbf
|> IO.iodata_to_binary()
|> split_payload_in_chunks(conn.server_settings.max_frame_size)
|> split_hbf_to_encoded_frames(stream_id, enabled_flags)
else
Expand Down Expand Up @@ -1571,9 +1570,7 @@ defmodule Mint.HTTP2 do
# on the max frame size.
data_size > conn.server_settings.max_frame_size ->
{chunks, last_chunk} =
data
|> IO.iodata_to_binary()
|> split_payload_in_chunks(conn.server_settings.max_frame_size)
split_payload_in_chunks(data, conn.server_settings.max_frame_size)

{encoded_chunks, conn} =
Enum.map_reduce(chunks, conn, fn chunk, acc ->
Expand Down Expand Up @@ -1606,18 +1603,35 @@ defmodule Mint.HTTP2 do
{conn, Frame.encode(frame)}
end

defp split_payload_in_chunks(binary, chunk_size),
do: split_payload_in_chunks(binary, chunk_size, [])
# Splits iodata into chunks of exactly chunk_size bytes plus a non-empty last
# chunk. While the head of the list is a binary, we split it into sub-binaries
# to avoid copying the payload. Anything else is converted to an iovec.
defp split_payload_in_chunks(iodata, chunk_size),
do: split_payload_in_chunks(iodata, chunk_size, chunk_size, [], [])

defp split_payload_in_chunks(chunk, chunk_size, acc) when byte_size(chunk) <= chunk_size do
{Enum.reverse(acc), chunk}
defp split_payload_in_chunks([], _size, _left, chunk, chunks),
do: {Enum.reverse(chunks), Enum.reverse(chunk)}

defp split_payload_in_chunks([binary | rest], size, left, chunk, chunks)
when byte_size(binary) <= left do
split_payload_in_chunks(rest, size, left - byte_size(binary), [binary | chunk], chunks)
end

# The current chunk is full and there is more data, so we start a new one.
defp split_payload_in_chunks([binary | _] = iodata, size, 0, chunk, chunks)
when is_binary(binary) do
split_payload_in_chunks(iodata, size, size, [], [Enum.reverse(chunk) | chunks])
end

defp split_payload_in_chunks(binary, chunk_size, acc) do
{chunk, rest} = :erlang.split_binary(binary, chunk_size)
split_payload_in_chunks(rest, chunk_size, [chunk | acc])
defp split_payload_in_chunks([binary | rest], size, left, chunk, chunks)
when is_binary(binary) do
{part, binary} = :erlang.split_binary(binary, left)
split_payload_in_chunks([binary | rest], size, 0, [part | chunk], chunks)
end

defp split_payload_in_chunks(iodata, size, left, chunk, chunks),
do: split_payload_in_chunks(:erlang.iolist_to_iovec(iodata), size, left, chunk, chunks)

defp send_ping(conn, payload) do
frame = Frame.ping(stream_id: 0, opaque_data: payload)
conn = send!(conn, Frame.encode(frame))
Expand Down
7 changes: 4 additions & 3 deletions test/mint/http2/conn_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3425,7 +3425,8 @@ defmodule Mint.HTTP2Test do
%{conn: conn} do
max_frame_size = HTTP2.get_server_setting(conn, :max_frame_size)

body = :binary.copy(<<0>>, max_frame_size + 1)
# The nested list is converted to an iovec which is then split across frames.
body = [:binary.copy(<<0>>, max_frame_size - 1), [<<1>>, <<2>>]]
{conn, _ref} = open_request(conn, body)

assert_recv_frames [
Expand All @@ -3435,10 +3436,10 @@ defmodule Mint.HTTP2Test do
]

assert flags1 == set_flags(:data, [])
assert data1 == :binary.copy(<<0>>, max_frame_size)
assert data1 == :binary.copy(<<0>>, max_frame_size - 1) <> <<1>>

assert flags2 == set_flags(:data, [:end_stream])
assert data2 == <<0>>
assert data2 == <<2>>

assert HTTP2.open?(conn)
end
Expand Down
Loading