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

andy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/jena.git

commit 0a2644face0052ea2e0130522ec69114099e93ab
Author: Andy Seaborne <[email protected]>
AuthorDate: Fri Apr 11 15:02:47 2025 +0100

    Ensure response is consumed and closed
---
 .../main/java/org/apache/jena/http/HttpLib.java    | 29 ++++++++++++----------
 .../jena/sparql/exec/http/QueryExecHTTP.java       |  2 +-
 2 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/jena-arq/src/main/java/org/apache/jena/http/HttpLib.java 
b/jena-arq/src/main/java/org/apache/jena/http/HttpLib.java
index 820d2307dc..37fc7542d9 100644
--- a/jena-arq/src/main/java/org/apache/jena/http/HttpLib.java
+++ b/jena-arq/src/main/java/org/apache/jena/http/HttpLib.java
@@ -46,6 +46,7 @@ import java.util.zip.InflaterInputStream;
 import org.apache.jena.atlas.RuntimeIOException;
 import org.apache.jena.atlas.io.IO;
 import org.apache.jena.atlas.lib.IRILib;
+import org.apache.jena.atlas.lib.InternalErrorException;
 import org.apache.jena.atlas.logging.FmtLog;
 import org.apache.jena.atlas.web.HttpException;
 import org.apache.jena.atlas.web.TypedInputStream;
@@ -175,31 +176,31 @@ public class HttpLib {
     public static void handleHttpStatusCode(HttpResponse<InputStream> 
response) {
         int httpStatusCode = response.statusCode();
         // There is no status message in HTTP/2.
-        if ( ! inRange(httpStatusCode, 100, 599) )
+        if ( ! inRange(httpStatusCode, 100, 599) ) {
             throw new HttpException("Status code out of range: 
"+httpStatusCode);
-        else if ( inRange(httpStatusCode, 100, 199) ) {
+        }
+        if ( inRange(httpStatusCode, 100, 199) ) {
             // Informational
+            return;
         }
-        else if ( inRange(httpStatusCode, 200, 299) ) {
+        if ( inRange(httpStatusCode, 200, 299) ) {
             // Success. Continue processing.
+            return;
         }
         if ( inRange(httpStatusCode, 300, 399) ) {
             // We had "follow redirects" on (default client) so it's 
http->https,
             // or the application passed in a HttpClient with redirects off.
             // Either way, we should not continue processing.
-            try {
-                finish(response);
-            } catch (Exception ex) {
-                throw new HttpException("Error discarding body of 
"+httpStatusCode , ex);
-            }
-            throw new HttpException(httpStatusCode, 
HttpSC.getMessage(httpStatusCode));
+            throw exception(response, httpStatusCode);
         }
-        else if ( inRange(httpStatusCode, 400, 499) ) {
+        if ( inRange(httpStatusCode, 400, 499) ) {
             throw exception(response, httpStatusCode);
         }
-        else if ( inRange(httpStatusCode, 500, 599) ) {
+        if ( inRange(httpStatusCode, 500, 599) ) {
             throw exception(response, httpStatusCode);
         }
+
+        throw new InternalErrorException("Unknown status code: 
"+httpStatusCode);
     }
 
     /**
@@ -270,7 +271,6 @@ public class HttpLib {
      * This consumes the response body.
      */
     static HttpException exception(HttpResponse<InputStream> response, int 
httpStatusCode) {
-        URI uri = response.request().uri();
         InputStream in = response.body();
         if ( in == null )
             return new HttpException(httpStatusCode, 
HttpSC.getMessage(httpStatusCode));
@@ -305,7 +305,10 @@ public class HttpLib {
      *  See {@link BodySubscribers#ofInputStream()}.
      */
     private static void finish(HttpResponse<InputStream> response) {
-        finish(response.body());
+        InputStream input = response.body();
+        if ( input == null )
+            return;
+        finish(input);
     }
 
     /** Read to end of {@link InputStream}.
diff --git 
a/jena-arq/src/main/java/org/apache/jena/sparql/exec/http/QueryExecHTTP.java 
b/jena-arq/src/main/java/org/apache/jena/sparql/exec/http/QueryExecHTTP.java
index 1d5e08f59a..94811368fc 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/exec/http/QueryExecHTTP.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/exec/http/QueryExecHTTP.java
@@ -308,7 +308,7 @@ public class QueryExecHTTP implements QueryExec {
         try {
             RDFDataMgr.read(graph, in, lang);
         } catch (RiotException ex) {
-            HttpLib.finish(in);
+            finish(in);
             throw ex;
         }
         return graph;

Reply via email to