You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2022/02/26 13:57:44 UTC

[httpcomponents-client] branch master updated: HTTPCLIENT-2206: Corrected resource de-allocation by fluent response objects

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git


The following commit(s) were added to refs/heads/master by this push:
     new 77d358e  HTTPCLIENT-2206: Corrected resource de-allocation by fluent response objects
77d358e is described below

commit 77d358e9c3ee49d9411073aec8590a8267e0cd82
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Sat Feb 26 14:45:43 2022 +0100

    HTTPCLIENT-2206: Corrected resource de-allocation by fluent response objects
---
 .../apache/hc/client5/http/fluent/Response.java    | 25 ++++++++++++++--------
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/httpclient5-fluent/src/main/java/org/apache/hc/client5/http/fluent/Response.java b/httpclient5-fluent/src/main/java/org/apache/hc/client5/http/fluent/Response.java
index e9faf2f..82d028d 100644
--- a/httpclient5-fluent/src/main/java/org/apache/hc/client5/http/fluent/Response.java
+++ b/httpclient5-fluent/src/main/java/org/apache/hc/client5/http/fluent/Response.java
@@ -42,6 +42,7 @@ import org.apache.hc.core5.http.HttpStatus;
 import org.apache.hc.core5.http.io.HttpClientResponseHandler;
 import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
 import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.hc.core5.http.io.support.ClassicResponseBuilder;
 
 /**
  * HTTP response used by the fluent facade.
@@ -64,7 +65,7 @@ public class Response {
         }
     }
 
-    private void dispose() {
+    private void dispose() throws IOException {
         if (this.consumed) {
             return;
         }
@@ -76,9 +77,9 @@ public class Response {
                     content.close();
                 }
             }
-        } catch (final Exception ignore) {
         } finally {
             this.consumed = true;
+            this.response.close();
         }
     }
 
@@ -86,7 +87,10 @@ public class Response {
      * Discards response content and deallocates all resources associated with it.
      */
     public void discardContent() {
-        dispose();
+        try {
+            dispose();
+        } catch (final Exception ignore) {
+        }
     }
 
     /**
@@ -111,14 +115,16 @@ public class Response {
         assertNotConsumed();
         try {
             final HttpEntity entity = this.response.getEntity();
-            if (entity != null) {
-                final ByteArrayEntity byteArrayEntity = new ByteArrayEntity(
-                        EntityUtils.toByteArray(entity), ContentType.parse(entity.getContentType()));
-                this.response.setEntity(byteArrayEntity);
-            }
-            return this.response;
+            return ClassicResponseBuilder.copy(response)
+                    .setEntity(entity != null ?
+                            new ByteArrayEntity(
+                                    EntityUtils.toByteArray(entity),
+                                    ContentType.parse(entity.getContentType()))
+                            : null)
+                    .build();
         } finally {
             this.consumed = true;
+            this.response.close();
         }
     }
 
@@ -135,6 +141,7 @@ public class Response {
             }
         } finally {
             this.consumed = true;
+            this.response.close();
         }
     }