Skip to content

Commit d6bc919

Browse files
authored
HttpClient: send AddVariable params as query string on GETs and read them on the server (#1107)
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.
1 parent 62c0298 commit d6bc919

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
@@ -15,6 +15,7 @@
1515
import javax.xml.parsers.DocumentBuilderFactory;
1616
import javax.xml.parsers.ParserConfigurationException;
1717
import java.io.*;
18+
import java.net.URLEncoder;
1819
import java.util.Hashtable;
1920
import java.util.Vector;
2021

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

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

579607
firstMultiPart = false;
580-
int variablesCount = getVariablesToSend().size();
581-
int count = 1;
582-
for (Object key: getVariablesToSend().keySet())
608+
if (includeVariablesAsMultipart)
583609
{
584-
if (count == variablesCount)
585-
firstMultiPart = true;
586-
String value = getMultipartTemplate().getFormDataTemplate((String)key, (String)getVariablesToSend().get(key));
587-
getContentToSend().add(0, value); //Variables al principio
588-
count++;
610+
int variablesCount = getVariablesToSend().size();
611+
int count = 1;
612+
for (Object key: getVariablesToSend().keySet())
613+
{
614+
if (count == variablesCount)
615+
firstMultiPart = true;
616+
String value = getMultipartTemplate().getFormDataTemplate((String)key, (String)getVariablesToSend().get(key));
617+
getContentToSend().add(0, value); //Variables al principio
618+
count++;
619+
}
589620
}
590621

591622
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
@@ -547,9 +547,17 @@ public void execute(String method, String url) {
547547

548548
try (CloseableHttpClient httpClient = this.httpClientBuilder.build()) {
549549
if (method.equalsIgnoreCase("GET")) {
550-
byte[] data = getData();
550+
// Variables added via addVariable are appended to the query string,
551+
// not serialized as a multipart body (which servers ignore on GETs).
552+
String queryFromVars = getQueryStringFromVariables();
553+
String finalUrl = url;
554+
if (!queryFromVars.isEmpty()) {
555+
finalUrl = url + (url.contains("?") ? "&" : "?") + queryFromVars;
556+
}
557+
558+
byte[] data = getData(false); // body only from addBytes/addString, no variables
551559
if (data.length > 0) {
552-
HttpGetWithBody httpGetWithBody = new HttpGetWithBody(url.trim());
560+
HttpGetWithBody httpGetWithBody = new HttpGetWithBody(finalUrl.trim());
553561
httpGetWithBody.setConfig(reqConfig);
554562
Set<String> keys = getheadersToSend().keySet();
555563
for (String header : keys) {
@@ -559,7 +567,7 @@ public void execute(String method, String url) {
559567
response = httpClient.execute(httpGetWithBody, httpClientContext);
560568
}
561569
else {
562-
HttpGet httpget = new HttpGet(url.trim());
570+
HttpGet httpget = new HttpGet(finalUrl.trim());
563571
httpget.setConfig(reqConfig);
564572
Set<String> keys = getheadersToSend().keySet();
565573
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
@@ -1242,7 +1242,15 @@ static private Hashtable<String, String[]> parseMultipartPostData(FileItemCollec
12421242
static public Hashtable<String, String[]> parsePostData(IHttpServletRequest request, IServletInputStream in) {
12431243
try {
12441244
// Nuestra versión del parsePostData utiliza UTF-8
1245-
return com.genexus.webpanels.HttpUtils.parsePostData(in);
1245+
Hashtable<String, String[]> postData = com.genexus.webpanels.HttpUtils.parsePostData(in);
1246+
// On GET requests, if the body yielded no variables, fall back to the query string.
1247+
// POST/PUT/DELETE/... behavior is unchanged: only the body is read.
1248+
if ((postData == null || postData.isEmpty())
1249+
&& request != null
1250+
&& "GET".equalsIgnoreCase(request.getMethod())) {
1251+
return com.genexus.webpanels.HttpUtils.parsePostData(request);
1252+
}
1253+
return postData;
12461254
} catch (IllegalArgumentException e) {
12471255
return com.genexus.webpanels.HttpUtils.parsePostData(request);
12481256
}

0 commit comments

Comments
 (0)