Skip to content

Commit b6e70b2

Browse files
committed
HttpClient: send AddVariable params as query string on GETs and read them on the server
Two related fixes for AddVariable usage on GET requests. Client side (HttpClientJavaLib + GXHttpClient): On GET, variables added with AddVariable were serialized as a multipart body. Servers, proxies and CDNs ignore the body of a GET so the values never reached the destination. They now go to the URL query string, URL-encoded in UTF-8. POST/PUT/DELETE keep using the body as before. Server side (HttpContextWeb): HttpRequest.GetVariable() consulted only the request body. On a GET this body is empty so values sent via the query string were invisible. Now, only on GET, if the body parsed empty we fall back to the query string. POST and any other verb are unchanged: the body still has precedence and the query string is not consulted. (cherry picked from commit 85180bc)
1 parent 5c18694 commit b6e70b2

3 files changed

Lines changed: 60 additions & 13 deletions

File tree

common/src/main/java/com/genexus/internet/GXHttpClient.java

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import javax.xml.parsers.ParserConfigurationException;
1515
import java.io.*;
1616
import java.net.URISyntaxException;
17+
import java.net.URLEncoder;
1718
import java.util.Hashtable;
1819
import java.util.Vector;
1920

@@ -570,21 +571,51 @@ protected String setPathUrl(String url) {
570571
}
571572

572573
boolean firstMultiPart;
573-
@SuppressWarnings("unchecked")
574+
574575
protected byte[] getData()
576+
{
577+
return getData(true);
578+
}
579+
580+
protected String getQueryStringFromVariables()
581+
{
582+
if (getVariablesToSend() == null || getVariablesToSend().isEmpty())
583+
return "";
584+
StringBuilder sb = new StringBuilder();
585+
boolean first = true;
586+
for (Object key : getVariablesToSend().keySet())
587+
{
588+
if (!first) sb.append('&');
589+
try
590+
{
591+
sb.append(URLEncoder.encode((String) key, "UTF-8"))
592+
.append('=')
593+
.append(URLEncoder.encode((String) getVariablesToSend().get(key), "UTF-8"));
594+
}
595+
catch (UnsupportedEncodingException ignored) { /* UTF-8 always supported */ }
596+
first = false;
597+
}
598+
return sb.toString();
599+
}
600+
601+
@SuppressWarnings("unchecked")
602+
protected byte[] getData(boolean includeVariablesAsMultipart)
575603
{
576604
byte[] out = new byte[0];
577605

578606
firstMultiPart = false;
579-
int variablesCount = getVariablesToSend().size();
580-
int count = 1;
581-
for (Object key: getVariablesToSend().keySet())
607+
if (includeVariablesAsMultipart)
582608
{
583-
if (count == variablesCount)
584-
firstMultiPart = true;
585-
String value = getMultipartTemplate().getFormDataTemplate((String)key, (String)getVariablesToSend().get(key));
586-
getContentToSend().add(0, value); //Variables al principio
587-
count++;
609+
int variablesCount = getVariablesToSend().size();
610+
int count = 1;
611+
for (Object key: getVariablesToSend().keySet())
612+
{
613+
if (count == variablesCount)
614+
firstMultiPart = true;
615+
String value = getMultipartTemplate().getFormDataTemplate((String)key, (String)getVariablesToSend().get(key));
616+
getContentToSend().add(0, value); //Variables al principio
617+
count++;
618+
}
588619
}
589620

590621
for (int idx = 0; idx < getContentToSend().size(); idx++)

java/src/main/java/com/genexus/internet/HttpClientJavaLib.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -531,9 +531,17 @@ public void execute(String method, String url) {
531531
try (CloseableHttpClient httpClient = this.httpClientBuilder.build()) {
532532

533533
if (method.equalsIgnoreCase("GET")) {
534-
byte[] data = getData();
534+
// Variables added via addVariable are appended to the query string,
535+
// not serialized as a multipart body (which servers ignore on GETs).
536+
String queryFromVars = getQueryStringFromVariables();
537+
String finalUrl = url;
538+
if (!queryFromVars.isEmpty()) {
539+
finalUrl = url + (url.contains("?") ? "&" : "?") + queryFromVars;
540+
}
541+
542+
byte[] data = getData(false); // body only from addBytes/addString, no variables
535543
if (data.length > 0) {
536-
HttpGetWithBody httpGetWithBody = new HttpGetWithBody(url.trim());
544+
HttpGetWithBody httpGetWithBody = new HttpGetWithBody(finalUrl.trim());
537545
httpGetWithBody.setConfig(reqConfig);
538546
Set<String> keys = getheadersToSend().keySet();
539547
for (String header : keys) {
@@ -543,7 +551,7 @@ public void execute(String method, String url) {
543551
response = httpClient.execute(httpGetWithBody, httpClientContext);
544552
}
545553
else {
546-
HttpGet httpget = new HttpGet(url.trim());
554+
HttpGet httpget = new HttpGet(finalUrl.trim());
547555
httpget.setConfig(reqConfig);
548556
Set<String> keys = getheadersToSend().keySet();
549557
for (String header : keys) {

java/src/main/java/com/genexus/webpanels/HttpContextWeb.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,15 @@ static private Hashtable<String, String[]> parseMultipartPostData(FileItemCollec
12411241
static public Hashtable<String, String[]> parsePostData(IHttpServletRequest request, IServletInputStream in) {
12421242
try {
12431243
// Nuestra versión del parsePostData utiliza UTF-8
1244-
return com.genexus.webpanels.HttpUtils.parsePostData(in);
1244+
Hashtable<String, String[]> postData = com.genexus.webpanels.HttpUtils.parsePostData(in);
1245+
// On GET requests, if the body yielded no variables, fall back to the query string.
1246+
// POST/PUT/DELETE/... behavior is unchanged: only the body is read.
1247+
if ((postData == null || postData.isEmpty())
1248+
&& request != null
1249+
&& "GET".equalsIgnoreCase(request.getMethod())) {
1250+
return com.genexus.webpanels.HttpUtils.parsePostData(request);
1251+
}
1252+
return postData;
12451253
} catch (IllegalArgumentException e) {
12461254
return com.genexus.webpanels.HttpUtils.parsePostData(request);
12471255
}

0 commit comments

Comments
 (0)