[ 
https://issues.apache.org/jira/browse/HTTPCORE-677?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17341542#comment-17341542
 ] 

Pierre N. commented on HTTPCORE-677:
------------------------------------

The exception is also triggered if you send a connection: close, example with 
curl :
{code:java}
curl -v -H 'Connection: close' localhost:8888
*   Trying 127.0.0.1:8888...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8888 (#0)
> GET / HTTP/1.1
> Host: localhost:8888
> User-Agent: curl/7.68.0
> Accept: */*
> Connection: close
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Sun, 09 May 2021 15:14:11 GMT
< Server: Apache-HttpCore/5.1.1 (Java/16)
< Content-Length: 4
< Content-Type: text/plain; charset=ISO-8859-1
< Connection: close
< 
* Closing connection 0
{code}
{quote}This is an abnormal situation in my opinion because the client is 
supposed to communicate ins intent to close the connection by sending 
`Connection: close` header.
{quote}
The connection close header is sent when you know you are going to close the 
connection after the request. However, in keep-alive mode, most of the time the 
client doesn't know if it is the last request so it will never send this 
connection close header. It will just close the connection after a certain 
delay or at the end of the session.

Most of the client will be in this situation, by example, all browser consuming 
your API, and even API client like apache http-client which maintains 
connection by default (unless you close it/configure it).
{quote}It is much neater from the API perspective to signal this event through 
an exception instead of returning a null request object.
{quote}
I don't understand why you would need such null request object but I didn't dig 
into the code. However, as an API user I think an optional lifecycle/connection 
listener (defaulted to noop) would be more adapted for this use case with event 
like ConnectionClosed, ConnectionCreated, ...
{quote}What I could do is catch ConnectionClosedException here [1] and quietly 
discard it
{quote}
Why even throw the exception if it is to catch it and discard it immediately ? 
Creating and throwing an exception has a performance cost. And the exception 
may be usefull when approriate (when the close is really unexpected).

But anyway it is just a suggestion to improve the API, I can just ignore the 
ConnectionCloseException in the listener myself (but the drawback is I won't 
know when the connection is really abnormally closed by the client).

> Suggestion: Don't throw ConnectionClosedException when connection close is 
> expected
> -----------------------------------------------------------------------------------
>
>                 Key: HTTPCORE-677
>                 URL: https://issues.apache.org/jira/browse/HTTPCORE-677
>             Project: HttpComponents HttpCore
>          Issue Type: Improvement
>          Components: HttpCore
>    Affects Versions: 5.1
>            Reporter: Pierre N.
>            Priority: Major
>             Fix For: 5.1.1
>
>
> I'm testing apache core to replace Undertow in my web app so maybe I 
> configured badly but I noticed the server is throwing an exception in 
> ExceptionListener every time a client connection is closed.
> Is it necessary ? Is it really exceptional ? Do we really need to build a 
> full stacktrace every time a socket is closed because it happens a lot.
> The connection is expected to be closed at some point, I understand an 
> exception could be thrown if it occurs will reading the request or the 
> request body, but if the request has been read, I think it is not exceptional 
> and it shouldn't throw.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to