Found related issues:
And, I’d like to quote words from @mtrudel.
Where does this error come from?
This is a wholly normal error; one that comes from the client closing the connection before the server has fully sent its response.
Can i ignore this error?
Unless you control the client, you can safely ignore these. They correspond to client errors, disconnections, and the like.
Which configuration can i use to control this behaviour?
It is configurable within Bandit via the http_errors.log_protocol_errors
configuration point
I’ll try the mentioned configuration and report the result at here later.
Edit
In short, it works.
1. Reproduce the error
As mtrudel said, The error (Bandit.HTTPError) Header read socket error: :closed
comes from a client-side issue. But, it’s hard to reproduce it using command like curl
. Therefore, I attempted another method to simulate a client-side issue.
# build a malformed request
$ curl 127.0.0.1:4000 -H 'Host:'
Within a project generated by mix phx.new --adapter bandit ...
, it will report an error:
[error] ** (Bandit.HTTPError) Unable to obtain host and port: No host header
2. Set the option
After setting log_protocol_errors: false
:
config :demo, DemoWeb.Endpoint,
http: [
http_options: [log_protocol_errors: false],
# ...
],
# ...
3. Result
The error won’t be reported anymore.
4. (extra) How does Cowboy adapter handle malformed requests?
I also generated a project using cowboy adapter.
When I sent a malformed request to the server, the server doesn’t report anything.
5. (extra) Investigate ‘Is it safe to suppress this kind of error’?
I noticed the changelog said:
Add log_protocol_errors
option to optionally quell console logging of 4xx errors generated by Bandit. Defaults to true
for now; may switch to false
in the future based on adoption
As it said that log_protocol_errors
is for suppressing 4xx errors - which are client errors, IMO, it’s safe to do that.