Skip to content

Commit 2ce7593

Browse files
committed
Rename "trailing" to "trailer" headers
See also: - wojtekmach/req#230 - https://www.rfc-editor.org/rfc/rfc9110#section-6.5
1 parent c9b9e6a commit 2ce7593

File tree

11 files changed

+149
-147
lines changed

11 files changed

+149
-147
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
* Fix a small bug with double "wrapping" of some `Mint.TransportError`s.
116116
* Prevent unnecessary buffer allocations in the connections (less memory waste!).
117117
* Add support for chunked transfer-encoding in HTTP/1 requests when you don't use `content-encoding`/`transfer-encoding` yourself.
118-
* Add support for trailing headers in HTTP/* requests through `stream_request_body/3`.
118+
* Add support for trailer headers in HTTP/* requests through `stream_request_body/3`.
119119
* Add a page about decompressing responses in the guides.
120120

121121
## v0.3.0
@@ -136,7 +136,7 @@
136136

137137
* Add `Mint.HTTP.controlling_process/2`, `Mint.HTTP1.controlling_process/2`, and `Mint.HTTP2.controlling_process/2` to change the controlling process of a connection.
138138

139-
* Support trailing response headers in HTTP/2 connections.
139+
* Support trailer response headers in HTTP/2 connections.
140140

141141
## v0.2.1
142142

lib/mint/core/conn.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ defmodule Mint.Core.Conn do
3030
@callback stream_request_body(
3131
conn(),
3232
Types.request_ref(),
33-
body_chunk :: iodata() | :eof | {:eof, trailing_headers :: Types.headers()}
33+
body_chunk :: iodata() | :eof | {:eof, trailer_headers :: Types.headers()}
3434
) ::
3535
{:ok, conn()} | {:error, conn(), Types.error()}
3636

lib/mint/core/util.ex

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
11
defmodule Mint.Core.Util do
22
@moduledoc false
33

4-
@unallowed_trailing_headers MapSet.new([
5-
"content-encoding",
6-
"content-length",
7-
"content-range",
8-
"content-type",
9-
"trailer",
10-
"transfer-encoding",
11-
12-
# Control headers (https://svn.tools.ietf.org/svn/wg/httpbis/specs/rfc7231.html#rfc.section.5.1)
13-
"cache-control",
14-
"expect",
15-
"host",
16-
"max-forwards",
17-
"pragma",
18-
"range",
19-
"te",
20-
21-
# Conditionals (https://svn.tools.ietf.org/svn/wg/httpbis/specs/rfc7231.html#rfc.section.5.2)
22-
"if-match",
23-
"if-none-match",
24-
"if-modified-since",
25-
"if-unmodified-since",
26-
"if-range",
27-
28-
# Authentication/authorization (https://tools.ietf.org/html/rfc7235#section-5.3)
29-
"authorization",
30-
"proxy-authenticate",
31-
"proxy-authorization",
32-
"www-authenticate",
33-
34-
# Cookie management (https://tools.ietf.org/html/rfc6265)
35-
"cookie",
36-
"set-cookie",
37-
38-
# Control data (https://svn.tools.ietf.org/svn/wg/httpbis/specs/rfc7231.html#rfc.section.7.1)
39-
"age",
40-
"cache-control",
41-
"expires",
42-
"date",
43-
"location",
44-
"retry-after",
45-
"vary",
46-
"warning"
47-
])
4+
@unallowed_trailer_headers MapSet.new([
5+
"content-encoding",
6+
"content-length",
7+
"content-range",
8+
"content-type",
9+
"trailer",
10+
"transfer-encoding",
11+
12+
# Control headers (https://svn.tools.ietf.org/svn/wg/httpbis/specs/rfc7231.html#rfc.section.5.1)
13+
"cache-control",
14+
"expect",
15+
"host",
16+
"max-forwards",
17+
"pragma",
18+
"range",
19+
"te",
20+
21+
# Conditionals (https://svn.tools.ietf.org/svn/wg/httpbis/specs/rfc7231.html#rfc.section.5.2)
22+
"if-match",
23+
"if-none-match",
24+
"if-modified-since",
25+
"if-unmodified-since",
26+
"if-range",
27+
28+
# Authentication/authorization (https://tools.ietf.org/html/rfc7235#section-5.3)
29+
"authorization",
30+
"proxy-authenticate",
31+
"proxy-authorization",
32+
"www-authenticate",
33+
34+
# Cookie management (https://tools.ietf.org/html/rfc6265)
35+
"cookie",
36+
"set-cookie",
37+
38+
# Control data (https://svn.tools.ietf.org/svn/wg/httpbis/specs/rfc7231.html#rfc.section.7.1)
39+
"age",
40+
"cache-control",
41+
"expires",
42+
"date",
43+
"location",
44+
"retry-after",
45+
"vary",
46+
"warning"
47+
])
4848

4949
def hostname(opts, address) when is_list(opts) do
5050
case Keyword.fetch(opts, :hostname) do
@@ -114,11 +114,11 @@ defmodule Mint.Core.Util do
114114
def maybe_concat(<<>>, data), do: data
115115
def maybe_concat(buffer, data) when is_binary(buffer), do: buffer <> data
116116

117-
def find_unallowed_trailing_header(headers) do
118-
Enum.find(headers, fn {name, _value} -> name in @unallowed_trailing_headers end)
117+
def find_unallowed_trailer_header(headers) do
118+
Enum.find(headers, fn {name, _value} -> name in @unallowed_trailer_headers end)
119119
end
120120

121-
def remove_unallowed_trailing_headers(headers) do
122-
Enum.reject(headers, fn {name, _value} -> name in @unallowed_trailing_headers end)
121+
def remove_unallowed_trailer_headers(headers) do
122+
Enum.reject(headers, fn {name, _value} -> name in @unallowed_trailer_headers end)
123123
end
124124
end

lib/mint/http.ex

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -606,41 +606,43 @@ defmodule Mint.HTTP do
606606
* `:eof` - signals the end of the streaming of the request body for the given
607607
request. Usually the server won't send any reply until this is sent.
608608
609-
* `{:eof, trailing_headers}` - sends **trailing headers** and signals the end
609+
* `{:eof, trailer_headers}` - sends **trailer headers** and signals the end
610610
of the streaming of the request body for the given request. This behaves the
611-
same way as `:eof` but first sends the trailing headers. See the "Trailing headers"
612-
section below.
611+
same way as `:eof` but first sends the trailer headers. See the
612+
[*Trailer headers*](#module-trailer-headers) section below.
613613
614614
This function always returns an updated connection to be stored over the old connection.
615615
616616
For information about transfer encoding and content length in HTTP/1, see
617617
`Mint.HTTP1.stream_request_body/3`.
618618
619-
## Trailing headers
619+
## Trailer headers
620620
621-
HTTP trailing headers can be sent after the body of a request. The behaviour is slightly
622-
different for HTTP/1 and HTTP/2.
621+
HTTP trailer headers can be sent after the body of a request. trailer headers are described
622+
[in RFC 9110](https://www.rfc-editor.org/rfc/rfc9110#section-6.5).
623623
624-
In HTTP/1, trailing headers are only supported if the transfer encoding is set to
625-
`chunked`. See `Mint.HTTP1.stream_request_body/3` for more information on chunked
626-
transfer encoding.
624+
The behaviour is slightly different for HTTP/1 and HTTP/2:
627625
628-
In HTTP/2, trailing headers behave like normal headers. You don't need to care
629-
about the transfer encoding.
626+
* In HTTP/1, trailer headers are only supported if the transfer encoding is set to
627+
`chunked`. See `Mint.HTTP1.stream_request_body/3` for more information on chunked
628+
transfer encoding.
629+
630+
* In HTTP/2, trailer headers behave like normal headers. You don't need to care
631+
about the transfer encoding.
630632
631633
### The `trailer` header
632634
633635
As specified in [section 4.4 of RFC 7230](https://tools.ietf.org/html/rfc7230#section-4.4),
634-
in HTTP/1 you need to specify which headers you're going to send as trailing
636+
in HTTP/1 you need to specify which headers you're going to send as traoler
635637
headers using the `trailer` header. The `trailer` header applies to both HTTP/1
636638
and HTTP/2. See the examples below for more information.
637639
638640
### The `te` header
639641
640642
As specified in [section 4.3 of RFC 7230](https://tools.ietf.org/html/rfc7230#section-4.3),
641643
the `te` (or `TE`) header is used to specify which transfer-encodings the client
642-
is willing to accept (besides `chunked`). Mint supports decoding of trailing headers,
643-
but if you want to notify the server that you are accepting trailing headers,
644+
is willing to accept (besides `chunked`). Mint supports decoding of trailer headers,
645+
but if you want to notify the server that you are accepting trailer headers,
644646
use the `trailers` value in the `te` header. For example:
645647
646648
Mint.HTTP.request(conn, "GET", "/", [{"te", "trailers"}], "some body")
@@ -659,22 +661,22 @@ defmodule Mint.HTTP do
659661
{:ok, conn} = Mint.HTTP.stream_request_body(conn, request_ref, "}")
660662
{:ok, conn} = Mint.HTTP.stream_request_body(conn, request_ref, :eof)
661663
662-
Here's an example of sending trailing headers:
664+
Here's an example of sending trailer headers:
663665
664666
headers = [{"content-type", "application/json"}, {"trailer", "my-trailer, x-expires"}]
665667
{:ok, conn, request_ref} = Mint.HTTP.request(conn, "POST", "/", headers, :stream)
666668
667669
{:ok, conn} = Mint.HTTP.stream_request_body(conn, request_ref, "{}")
668670
669-
trailing_headers = [{"my-trailer", "xxx"}, {"x-expires", "10 days"}]
670-
{:ok, conn} = Mint.HTTP.stream_request_body(conn, request_ref, {:eof, trailing_headers})
671+
trailer_headers = [{"my-trailer", "xxx"}, {"x-expires", "10 days"}]
672+
{:ok, conn} = Mint.HTTP.stream_request_body(conn, request_ref, {:eof, trailer_headers})
671673
672674
"""
673675
@impl true
674676
@spec stream_request_body(
675677
t(),
676678
Types.request_ref(),
677-
iodata() | :eof | {:eof, trailing_headers :: Types.headers()}
679+
iodata() | :eof | {:eof, trailer_headers :: Types.headers()}
678680
) ::
679681
{:ok, t()} | {:error, t(), Types.error()}
680682
def stream_request_body(conn, ref, body),
@@ -743,7 +745,7 @@ defmodule Mint.HTTP do
743745
with a list of headers. Headers are in the form `{header_name, header_value}`
744746
with `header_name` and `header_value` being strings. A single `:headers` response
745747
will come after the `:status` response. A single `:headers` response may come
746-
after all the `:data` responses if **trailing headers** are present.
748+
after all the `:data` responses if **trailer headers** are present.
747749
748750
* `{:data, request_ref, binary}` - returned when the server replied with
749751
a chunk of response body (as a binary). The request shouldn't be considered done

lib/mint/http1.ex

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ defmodule Mint.HTTP1 do
7777
* `{:invalid_token_list, string}` - when a header that is supposed to contain a list
7878
of tokens (such as the `connection` header) contains a malformed list of tokens.
7979
80-
* `:trailing_headers_but_not_chunked_encoding` - when you try to send trailing
80+
* `:trailing_headers_but_not_chunked_encoding` - when you try to send trailer
8181
headers through `stream_request_body/3` but the transfer encoding of the request
8282
was not `chunked`.
8383
@@ -320,7 +320,7 @@ defmodule Mint.HTTP1 do
320320
@spec stream_request_body(
321321
t(),
322322
Types.request_ref(),
323-
iodata() | :eof | {:eof, trailing_headers :: Types.headers()}
323+
iodata() | :eof | {:eof, trailer_headers :: Types.headers()}
324324
) ::
325325
{:ok, t()} | {:error, t(), Types.error()}
326326
def stream_request_body(
@@ -336,7 +336,7 @@ defmodule Mint.HTTP1 do
336336
def stream_request_body(
337337
%__MODULE__{streaming_request: %{state: {:stream_request, :identity}, ref: ref}} = conn,
338338
ref,
339-
{:eof, _trailing_headers}
339+
{:eof, _trailer_headers}
340340
) do
341341
{:error, conn, wrap_error(:trailing_headers_but_not_chunked_encoding)}
342342
end
@@ -371,7 +371,7 @@ defmodule Mint.HTTP1 do
371371
conn = enqueue_request(%__MODULE__{conn | streaming_request: nil}, request)
372372
{:ok, conn}
373373

374-
{:eof, _trailing_headers} ->
374+
{:eof, _trailer_headers} ->
375375
request = %{conn.streaming_request | state: :status}
376376
conn = enqueue_request(%__MODULE__{conn | streaming_request: nil}, request)
377377
{:ok, conn}
@@ -391,10 +391,10 @@ defmodule Mint.HTTP1 do
391391
end
392392
end
393393

394-
defp validate_chunk({:eof, trailing_headers}) do
395-
headers = lower_header_keys(trailing_headers)
394+
defp validate_chunk({:eof, trailer_headers}) do
395+
headers = lower_header_keys(trailer_headers)
396396

397-
if unallowed_header = find_unallowed_trailing_header(headers) do
397+
if unallowed_header = find_unallowed_trailer_header(headers) do
398398
{:error, wrap_error({:unallowed_trailing_header, unallowed_header})}
399399
else
400400
{:ok, {:eof, headers}}
@@ -791,11 +791,11 @@ defmodule Mint.HTTP1 do
791791
decode_trailer_headers(conn, rest, responses, headers)
792792

793793
{:ok, :eof, rest} ->
794-
headers = Util.remove_unallowed_trailing_headers(headers)
794+
headers = Util.remove_unallowed_trailer_headers(headers)
795795

796796
responses = [
797797
{:done, conn.request.ref}
798-
| add_trailing_headers(headers, conn.request.ref, responses)
798+
| add_trailer_headers(headers, conn.request.ref, responses)
799799
]
800800

801801
conn = request_done(conn)
@@ -821,9 +821,9 @@ defmodule Mint.HTTP1 do
821821
decode(:status, %{conn | state: :status}, data, responses)
822822
end
823823

824-
defp add_trailing_headers([], _request_ref, responses), do: responses
824+
defp add_trailer_headers([], _request_ref, responses), do: responses
825825

826-
defp add_trailing_headers(headers, request_ref, responses),
826+
defp add_trailer_headers(headers, request_ref, responses),
827827
do: [{:headers, request_ref, Enum.reverse(headers)} | responses]
828828

829829
defp add_body(conn, data, responses) do
@@ -1095,10 +1095,10 @@ defmodule Mint.HTTP1 do
10951095
end
10961096

10971097
def format_error(:trailing_headers_but_not_chunked_encoding) do
1098-
"trailing headers can only be sent when using chunked transfer-encoding"
1098+
"trailer headers can only be sent when using chunked transfer-encoding"
10991099
end
11001100

11011101
def format_error({:unallowed_trailing_header, {name, value}}) do
1102-
"header #{inspect(name)} (with value #{inspect(value)}) is not allowed as a trailing header"
1102+
"header #{inspect(name)} (with value #{inspect(value)}) is not allowed as a trailer header"
11031103
end
11041104
end

lib/mint/http2.ex

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ defmodule Mint.HTTP2 do
542542
@spec stream_request_body(
543543
t(),
544544
Types.request_ref(),
545-
iodata() | :eof | {:eof, trailing_headers :: Types.headers()}
545+
iodata() | :eof | {:eof, trailer_headers :: Types.headers()}
546546
) :: {:ok, t()} | {:error, t(), Types.error()}
547547
def stream_request_body(conn, request_ref, chunk)
548548

@@ -1097,15 +1097,15 @@ defmodule Mint.HTTP2 do
10971097
encode_data(conn, stream_id, "", [:end_stream])
10981098
end
10991099

1100-
defp encode_stream_body_request_payload(conn, stream_id, {:eof, trailing_headers}) do
1101-
lowered_headers = downcase_header_names(trailing_headers)
1100+
defp encode_stream_body_request_payload(conn, stream_id, {:eof, trailer_headers}) do
1101+
lowered_headers = downcase_header_names(trailer_headers)
11021102

1103-
if unallowed_trailing_header = Util.find_unallowed_trailing_header(lowered_headers) do
1104-
error = wrap_error({:unallowed_trailing_header, unallowed_trailing_header})
1103+
if unallowed_trailer_header = Util.find_unallowed_trailer_header(lowered_headers) do
1104+
error = wrap_error({:unallowed_trailing_header, unallowed_trailer_header})
11051105
throw({:mint, conn, error})
11061106
end
11071107

1108-
encode_headers(conn, stream_id, trailing_headers, [:end_headers, :end_stream])
1108+
encode_headers(conn, stream_id, trailer_headers, [:end_headers, :end_stream])
11091109
end
11101110

11111111
defp encode_stream_body_request_payload(conn, stream_id, iodata) do
@@ -1680,24 +1680,24 @@ defmodule Mint.HTTP2 do
16801680
{conn, new_responses}
16811681
end
16821682

1683-
# Trailing headers. We don't care about the :status header here.
1683+
# Trailer headers. We don't care about the :status header here.
16841684
headers when received_first_headers? ->
16851685
if end_stream? do
16861686
conn = close_stream!(conn, stream.id, :no_error)
1687-
headers = headers |> Util.remove_unallowed_trailing_headers() |> join_cookie_headers()
1687+
headers = headers |> Util.remove_unallowed_trailer_headers() |> join_cookie_headers()
16881688
{conn, [{:done, ref}, {:headers, ref, headers} | responses]}
16891689
else
1690-
# Trailing headers must set the END_STREAM flag because they're
1690+
# Trailer headers must set the END_STREAM flag because they're
16911691
# the last thing allowed on the stream (other than RST_STREAM and
16921692
# the usual frames).
16931693
conn = close_stream!(conn, stream.id, :protocol_error)
1694-
debug_data = "trailing headers didn't set the END_STREAM flag"
1694+
debug_data = "trailer headers didn't set the END_STREAM flag"
16951695
error = wrap_error({:protocol_error, debug_data})
16961696
responses = [{:error, stream.ref, error} | responses]
16971697
{conn, responses}
16981698
end
16991699

1700-
# Non-trailing headers need to have a :status header, otherwise
1700+
# Non-trailer headers need to have a :status header, otherwise
17011701
# it's a protocol error.
17021702
_headers ->
17031703
conn = close_stream!(conn, stream.id, :protocol_error)
@@ -2216,7 +2216,7 @@ defmodule Mint.HTTP2 do
22162216
end
22172217

22182218
def format_error({:unallowed_trailing_header, {name, value}}) do
2219-
"header #{inspect(name)} (with value #{inspect(value)}) is not allowed as a trailing header"
2219+
"header #{inspect(name)} (with value #{inspect(value)}) is not allowed as a trailer header"
22202220
end
22212221

22222222
def format_error(:missing_status_header) do

lib/mint/tunnel_proxy.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ defmodule Mint.TunnelProxy do
138138
"expected tunnel proxy to return a status between 200 and 299, got: #{inspect(status)}"
139139

140140
{:unexpected_trailing_responses, responses} ->
141-
"tunnel proxy returned unexpected trailing responses: #{inspect(responses)}"
141+
"tunnel proxy returned unexpected trailer responses: #{inspect(responses)}"
142142

143143
http_reason ->
144144
"error when establishing the tunnel proxy connection: " <>

0 commit comments

Comments
 (0)