GitHub user hirthwork opened a pull request: https://github.com/apache/httpcore/pull/8
Set EmptyInputStream in entity instead of ContentLengthInputStream(in, 0) # Problem description 1. If HttpClient receives response with `Content-Length: 0` the connection won't be returned to connection pool until `entity.getContent().close()` is called (it is usually done by `EntityUtils.consume()`) or any read operation performed on `entity.getContent()`. 2. If CloseableHttpResponse.close() is called on such response without entity consuming, the connection will be closed, despite of any keep-alive strategies. Here the [sample](https://gist.github.com/hirthwork/6f4ebb047e77a61f644e) illustrating first problem. Trying to execute two sequential requests with maxPerRoute set to 1 we will hit TimeoutException because connection from first response (which has empty body) won't be returned to pool. # Problem analysis Current BHttpConnectionBase contract is to set input stream regardless of `Content-Length:` header value. The only way to distinct empty response entities from regular ones is to set entity content to some pre-defined value which can indicate that entity is not streaming, so `MainClientExec.execute()` can return connection to pool immediately. # Solution Introduce `EmptyInputStream` singleton which will be set as BasicHttpEntity content on zero length responses. `isStreaming()` will check content for nullness and equality with `EmptyInputStream.INSTANCE`. As alternative, suggested solution can be applied to 4.4.x branch, while for 5.0 branch contract can be broken and `prepareInput()` will return null for zero-length responses. # Test Aforementioned sample can be used for this solution testing. You can merge this pull request into a Git repository by running: $ git pull https://github.com/hirthwork/httpcore trunk Alternatively you can review and apply these changes as the patch at: https://github.com/apache/httpcore/pull/8.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #8 ---- commit f52e25482c1b4d5b518656122188c9cab99ac5ed Author: Dmitry Potapov <dpota...@yandex-team.ru> Date: 2014-11-12T16:58:45Z Set EmptyInputStream in entity instead of ContentLengthInputStream(inbuffer, 0) ---- --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@hc.apache.org For additional commands, e-mail: dev-h...@hc.apache.org