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
30 changes: 30 additions & 0 deletions lib/plug/cowboy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,36 @@ defmodule Plug.Cowboy do
end

@doc false
def handle_event(
[:cowboy, :request, :early_error],
_,
%{
resp_status: 414,
reason: {:connection_error, :limit_reached, specific_reason},
partial_req: partial_req
},
_
) do
Logger.error("""
Cowboy returned 414 because the request path was too long.

The more specific reason is:

#{inspect(specific_reason)}

You can customize those limits when configuring your http/https
server. The configuration option and default values are shown below:

protocol_options: [
max_request_line_length: 50_000
]

Request info:

peer: #{format_peer(partial_req.peer)}
""")
end

def handle_event(
[:cowboy, :request, :early_error],
_,
Expand Down
27 changes: 27 additions & 0 deletions test/plug/cowboy/conn_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,33 @@
:telemetry.detach(:early_error_test)
end

test "emits telemetry events for cowboy early_error for paths that are too long" do
:telemetry.attach(
:early_error_test,
[:cowboy, :request, :early_error],
&__MODULE__.telemetry_send/4,
self()
)

assert capture_log(fn ->
# Send a request line that's too long (exceeds max_request_line_length)
long_path = String.duplicate("a", 10_000)
response = request(:get, "/#{long_path}")
assert match?({414, _, _}, response) or match?({:error, :closed}, response)
end) =~ "Cowboy returned 414 because the request path was too long"

assert_receive {:telemetry, [:cowboy, :request, :early_error],
%{
system_time: _
},
%{
reason: {:connection_error, :limit_reached, _},
partial_req: %{}
}}

:telemetry.detach(:early_error_test)
end

def send_200(conn) do
assert conn.state == :unset
assert conn.resp_body == nil
Expand Down Expand Up @@ -427,7 +454,7 @@

def push_or_raise(conn) do
conn
|> push!("/static/assets.css")

Check warning on line 457 in test/plug/cowboy/conn_test.exs

View workflow job for this annotation

GitHub Actions / test (1.14, 24.2)

Plug.Conn.push!/2 is deprecated. Most browsers and clients have removed push support
|> send_resp(200, "push or raise")
end

Expand Down Expand Up @@ -614,7 +641,7 @@
case conn.query_string do
"noinfer" <> _ ->
conn
|> push("/static/assets.css", [{"accept", "text/plain"}])

Check warning on line 644 in test/plug/cowboy/conn_test.exs

View workflow job for this annotation

GitHub Actions / test (1.14, 24.2)

Plug.Conn.push/3 is deprecated. Most browsers and clients have removed push support
|> send_resp(200, Atom.to_string(get_http_protocol(conn)))

"earlyhints" <> _ ->
Expand Down
Loading