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


The following commit(s) were added to refs/heads/main by this push:
     new 9a40fb8  JENA-2257: Include query string in URI limit test
     new e7a192c  Merge pull request #1171 from afs/url-limit
9a40fb8 is described below

commit 9a40fb8e2d36098cbae533650077c44db0791811
Author: Andy Seaborne <a...@apache.org>
AuthorDate: Fri Jan 21 22:23:38 2022 +0000

    JENA-2257: Include query string in URI limit test
---
 .../main/java/org/apache/jena/http/HttpLib.java    |  2 +-
 .../org/apache/jena/sparql/exec/http/Params.java   |  6 ++-
 .../jena/sparql/exec/http/QueryExecHTTP.java       | 46 ++++++++++++++--------
 .../jena/sparql/exec/http/UpdateExecHTTP.java      |  3 --
 4 files changed, 34 insertions(+), 23 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 542c355..f0b3a41 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
@@ -400,7 +400,7 @@ public class HttpLib {
         if ( queryString == null || queryString.isEmpty() )
             // Empty string. Don't add "?"
             return url;
-        String sep =  url.contains("?") ? "&" : "?";
+        String sep = url.contains("?") ? "&" : "?";
         String requestURL = url+sep+queryString;
         return requestURL;
     }
diff --git 
a/jena-arq/src/main/java/org/apache/jena/sparql/exec/http/Params.java 
b/jena-arq/src/main/java/org/apache/jena/sparql/exec/http/Params.java
index 875faa6..4073691 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/exec/http/Params.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/exec/http/Params.java
@@ -114,7 +114,9 @@ public class Params
 
     /** URL query string, without leading "?" */
     public String httpString() {
-        return format(new StringBuilder(), paramList).toString();
+        if ( paramList.isEmpty() )
+            return "";
+        return formatEncodeHTTP(new StringBuilder(), paramList).toString();
     }
 
 //    /** URL query string, without leading "?" */
@@ -131,7 +133,7 @@ public class Params
     /**
      * Return a string that is suitable for HTTP use.
      */
-    private static StringBuilder format(StringBuilder result, List <Param> 
parameters) {
+    private static StringBuilder formatEncodeHTTP(StringBuilder result, List 
<Param> parameters) {
         parameters.forEach(param->{
             String encodedName = encode(param.getName());
             String encodedValue = encode(param.getValue());
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 9c1b281..8adf538 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
@@ -161,7 +161,7 @@ public class QueryExecHTTP implements QueryExec {
         // Use the explicitly given header or the default selectAcceptheader
         String thisAcceptHeader = dft(appProvidedAcceptHeader, 
selectAcceptheader);
 
-        HttpResponse<InputStream> response = query(thisAcceptHeader);
+        HttpResponse<InputStream> response = performQuery(thisAcceptHeader);
         InputStream in = HttpLib.getInputStream(response);
         // Don't assume the endpoint actually gives back the content type we 
asked for
         String actualContentType = responseHeader(response, 
HttpNames.hContentType);
@@ -201,7 +201,7 @@ public class QueryExecHTTP implements QueryExec {
         checkNotClosed();
         check(QueryType.ASK);
         String thisAcceptHeader = dft(appProvidedAcceptHeader, 
askAcceptHeader);
-        HttpResponse<InputStream> response = query(thisAcceptHeader);
+        HttpResponse<InputStream> response = performQuery(thisAcceptHeader);
         InputStream in = HttpLib.getInputStream(response);
 
         String actualContentType = responseHeader(response, 
HttpNames.hContentType);
@@ -336,7 +336,7 @@ public class QueryExecHTTP implements QueryExec {
     private Pair<InputStream, Lang> execRdfWorker(String contentType, String 
ifNoContentType) {
         checkNotClosed();
         String thisAcceptHeader = dft(appProvidedAcceptHeader, contentType);
-        HttpResponse<InputStream> response = query(thisAcceptHeader);
+        HttpResponse<InputStream> response = performQuery(thisAcceptHeader);
         InputStream in = HttpLib.getInputStream(response);
 
         // Don't assume the endpoint actually gives back the content type we 
asked for
@@ -362,7 +362,7 @@ public class QueryExecHTTP implements QueryExec {
         checkNotClosed();
         check(QueryType.CONSTRUCT_JSON);
         String thisAcceptHeader = dft(appProvidedAcceptHeader, 
WebContent.contentTypeJSON);
-        HttpResponse<InputStream> response = query(thisAcceptHeader);
+        HttpResponse<InputStream> response = performQuery(thisAcceptHeader);
         InputStream in = HttpLib.getInputStream(response);
         try {
             return JSON.parseAny(in).getAsArray();
@@ -425,8 +425,8 @@ public class QueryExecHTTP implements QueryExec {
     }
 
     /**
-     * Return the query string. If this was supplied in a constructor, there 
is no
-     * guarantee this is legal SPARQL syntax.
+     * Return the query string. If this was supplied as a string,
+     * there is no guarantee this is legal SPARQL syntax.
      */
     @Override
     public String getQueryString() {
@@ -443,7 +443,7 @@ public class QueryExecHTTP implements QueryExec {
      * query execution was successful and return 200.
      * Use {@link HttpLib#getInputStream} to access the body.
      */
-    private HttpResponse<InputStream> query(String reqAcceptHeader) {
+    private HttpResponse<InputStream> performQuery(String reqAcceptHeader) {
         if (closed)
             throw new ARQException("HTTP execution already closed");
 
@@ -460,9 +460,14 @@ public class QueryExecHTTP implements QueryExec {
                 thisParams.add( HttpParams.pNamedGraph, name );
         }
 
-        // Same as UpdateExecutionHTTP
-        HttpLib.modifyByService(service, context, thisParams,  httpHeaders);
+        HttpLib.modifyByService(service, context, thisParams, httpHeaders);
 
+        HttpRequest request = makeRequest(thisParams, reqAcceptHeader);
+
+        return executeQuery(request);
+    }
+
+    private HttpRequest makeRequest(Params thisParams, String reqAcceptHeader) 
{
         QuerySendMode actualSendMode = actualSendMode();
         HttpRequest.Builder requestBuilder;
         switch(actualSendMode) {
@@ -479,8 +484,7 @@ public class QueryExecHTTP implements QueryExec {
                 // Should not happen!
                 throw new InternalErrorException("Invalid value for 
'actualSendMode' "+actualSendMode);
         }
-        HttpRequest request = requestBuilder.build();
-        return executeQuery(request);
+        return requestBuilder.build();
     }
 
     private HttpResponse<InputStream> executeQuery(HttpRequest request) {
@@ -508,19 +512,27 @@ public class QueryExecHTTP implements QueryExec {
 
         // Only QuerySendMode.asGetWithLimitBody and 
QuerySendMode.asGetWithLimitForm here.
         String requestURL = service;
-        // Don't add yet
-        //thisParams.addParam(HttpParams.pQuery, queryString);
-        String qs = params.httpString();
-        // ?query=
+        // Other params (query= has not been added at this point)
+        int paramsLength = params.httpString().length();
+        int qEncodedLength = calcEncodeStringLength(queryString);
 
         // URL Length, including service (for safety)
-        int length = 
service.length()+1+HttpParams.pQuery.length()+1+qs.length();
+        int length = service.length()
+                + /* ?query= */        1 + HttpParams.pQuery.length()
+                + /* encoded query */  qEncodedLength
+                + /* &other params*/   1 + paramsLength;
         if ( length <= thisLengthLimit )
             return QuerySendMode.asGetAlways;
-
         return (sendMode==QuerySendMode.asGetWithLimitBody) ? 
QuerySendMode.asPost : QuerySendMode.asPostForm;
     }
 
+    private static int calcEncodeStringLength(String str) {
+        // Could approximate by counting non-queryString character and adding 
that *2 to the length of the string.
+        String qs = HttpLib.urlEncodeQueryString(str);
+        int encodedLength = qs.length();
+        return encodedLength;
+    }
+
     private HttpRequest.Builder executeQueryGet(Params thisParams, String 
acceptHeader) {
         thisParams.add(HttpParams.pQuery, queryString);
         String requestURL = requestURL(service, thisParams.httpString());
diff --git 
a/jena-arq/src/main/java/org/apache/jena/sparql/exec/http/UpdateExecHTTP.java 
b/jena-arq/src/main/java/org/apache/jena/sparql/exec/http/UpdateExecHTTP.java
index 9c1a3f4..e39fac4 100644
--- 
a/jena-arq/src/main/java/org/apache/jena/sparql/exec/http/UpdateExecHTTP.java
+++ 
b/jena-arq/src/main/java/org/apache/jena/sparql/exec/http/UpdateExecHTTP.java
@@ -101,7 +101,6 @@ public class UpdateExecHTTP implements UpdateExec {
                 thisParams.add(HttpNames.paramUsingNamedGraphURI, uri);
         }
 
-        // Same as QueryExecutionHTTP
         modifyByService(service, context, thisParams, httpHeaders);
 
         switch(sendMode) {
@@ -113,8 +112,6 @@ public class UpdateExecHTTP implements UpdateExec {
     }
 
     private void executePostBody(Params thisParams) {
-        //String str = (updateString != null) ? updateString : 
update.toString();
-        // Can't be null.
         String str = updateString;
         String requestURL = service;
         if ( thisParams.count() > 0 ) {

Reply via email to