You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by "Robert Wang (Jira)" <ji...@apache.org> on 2022/02/26 05:29:00 UTC

[jira] [Created] (HTTPCLIENT-2206) fluent: Response#saveContent(File) does not dispose response upon throwing

Robert Wang created HTTPCLIENT-2206:
---------------------------------------

             Summary: fluent: Response#saveContent(File) does not dispose response upon throwing
                 Key: HTTPCLIENT-2206
                 URL: https://issues.apache.org/jira/browse/HTTPCLIENT-2206
             Project: HttpComponents HttpClient
          Issue Type: Bug
          Components: Fluent HC
            Reporter: Robert Wang


 

When throwing HttpResponseException, it does not dispose the underlying entity. This can cause conn pool to be stuck, imagine a normal retry loop using the same HttpClient. Perhaps it should be wrapped inside #handleResponse.
{code:java}
    public void saveContent(final File file) throws IOException {
        assertNotConsumed();
        final int status = response.getCode();
        if (status >= HttpStatus.SC_REDIRECTION) {
            throw new HttpResponseException(status, response.getReasonPhrase());
        }
        try (FileOutputStream out = new FileOutputStream(file)) {
            final HttpEntity entity = this.response.getEntity();
            if (entity != null) {
                entity.writeTo(out);
            }
        } finally {
            this.consumed = true;
        }
    }
{code}
An alternative may be calling response.close(), which its behavior is documented here. I'm not sure how to check the "if supported" part though.

 
{code:java}
    /**
     * Returns a content stream of the entity.
...omitted...
     * <p>
     * If this entity belongs to an incoming HTTP message, calling
     * {@link InputStream#close()} on the returned {@code InputStream} will
     * try to consume the complete entity content to keep the connection
     * alive. In cases where this is undesired, e.g. when only a small part
     * of the content is relevant and consuming the complete entity content
     * would be too inefficient, <i>only</i> the HTTP message from which
     * this entity was obtained should be closed (if supported).
     * </p>
...omitted...
     */
    InputStream getContent() throws IOException, UnsupportedOperationException; {code}
 

 

 



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org