Skip to content

Commit c6f96b4

Browse files
committed
Fix for empty header values as well as empty query string values (#247)
1 parent 4fcde7d commit c6f96b4

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/AwsHttpServletRequest.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,12 @@ protected String generateQueryString(MultiValuedTreeMap<String, String> paramete
320320
queryStringBuilder.append(key);
321321
}
322322
queryStringBuilder.append("=");
323-
if (encode) {
324-
queryStringBuilder.append(URLEncoder.encode(val, encodeCharset));
325-
} else {
326-
queryStringBuilder.append(val);
323+
if (val != null) {
324+
if (encode) {
325+
queryStringBuilder.append(URLEncoder.encode(val, encodeCharset));
326+
} else {
327+
queryStringBuilder.append(val);
328+
}
327329
}
328330
}
329331
}
@@ -342,7 +344,7 @@ protected String generateQueryString(MultiValuedTreeMap<String, String> paramete
342344
* @param headerValue The value to be parsed
343345
* @return A list of SimpleMapEntry objects with all of the possible values for the header.
344346
*/
345-
protected List<HeaderValue> parseHeaderValue(String headerValue) {
347+
protected List<HeaderValue> parseHeaderValue(String headerValue) {
346348
return parseHeaderValue(headerValue, HEADER_VALUE_SEPARATOR, HEADER_QUALIFIER_SEPARATOR);
347349
}
348350

@@ -377,16 +379,21 @@ protected List<HeaderValue> parseHeaderValue(String headerValue, String valueSep
377379
// contains key/value pairs and it's not a base64-encoded value.
378380
if (q.contains(HEADER_KEY_VALUE_SEPARATOR) && !q.trim().endsWith("==")) {
379381
String[] kv = q.split(HEADER_KEY_VALUE_SEPARATOR);
382+
String key = kv[0].trim();
383+
String val = null;
384+
if (kv.length > 1) {
385+
val = kv[1].trim();
386+
}
380387
// TODO: Should we concatenate the rest of the values?
381388
if (newValue.getValue() == null) {
382-
newValue.setKey(kv[0].trim());
383-
newValue.setValue(kv[1].trim());
389+
newValue.setKey(key);
390+
newValue.setValue(val);
384391
} else {
385392
// special case for quality q=
386-
if ("q".equals(kv[0].trim())) {
387-
curPreference = Float.parseFloat(kv[1].trim());
393+
if ("q".equals(key)) {
394+
curPreference = Float.parseFloat(val);
388395
} else {
389-
newValue.addAttribute(kv[0].trim(), kv[1].trim());
396+
newValue.addAttribute(key, val);
390397
}
391398
}
392399
} else {

aws-serverless-java-container-core/src/test/java/com/amazonaws/serverless/proxy/internal/servlet/AwsHttpServletRequestTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class AwsHttpServletRequestTest {
2828
.header(HttpHeaders.ACCEPT, "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8").build();
2929
private static final AwsProxyRequest queryString = new AwsProxyRequestBuilder("/test", "GET")
3030
.queryString("one", "two").queryString("three", "four").build();
31+
private static final AwsProxyRequest queryStringNullValue = new AwsProxyRequestBuilder("/test", "GET")
32+
.queryString("one", "two").queryString("three", null).build();
3133
private static final AwsProxyRequest encodedQueryString = new AwsProxyRequestBuilder("/test", "GET")
3234
.queryString("one", "two").queryString("json", "{\"name\":\"faisal\"}").build();
3335
private static final AwsProxyRequest multipleParams = new AwsProxyRequestBuilder("/test", "GET")
@@ -104,6 +106,19 @@ public void queryString_generateQueryString_validQuery() {
104106
assertTrue(parsedString.contains("&") && parsedString.indexOf("&") > 0 && parsedString.indexOf("&") < parsedString.length());
105107
}
106108

109+
@Test
110+
public void queryString_generateQueryString_nullParameterIsEmpty() {
111+
AwsProxyHttpServletRequest request = new AwsProxyHttpServletRequest(queryStringNullValue, mockContext, null, config);String parsedString = null;
112+
try {
113+
parsedString = request.generateQueryString(request.getAwsProxyRequest().getMultiValueQueryStringParameters(), true, config.getUriEncoding());
114+
} catch (ServletException e) {
115+
e.printStackTrace();
116+
fail("Could not generate query string");
117+
}
118+
119+
assertTrue(parsedString.endsWith("three="));
120+
}
121+
107122
@Test
108123
public void queryStringWithEncodedParams_generateQueryString_validQuery() {
109124
AwsProxyHttpServletRequest request = new AwsProxyHttpServletRequest(encodedQueryString, mockContext, null, config);

0 commit comments

Comments
 (0)