Skip to content

Commit

Permalink
Changed form parameter parser method to use request input stream inst…
Browse files Browse the repository at this point in the history
…ead of the raw API Gateway event body to address #141.
  • Loading branch information
sapessi committed Apr 6, 2018
1 parent 8b8741a commit 6cd0ab7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.NullInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -756,7 +757,12 @@ private Map<String, List<String>> getFormUrlEncodedParametersMap() {
return urlEncodedFormParameters;
}
Timer.start("SERVLET_REQUEST_GET_FORM_PARAMS");
String rawBodyContent = request.getBody();
String rawBodyContent = null;
try {
rawBodyContent = IOUtils.toString(getInputStream());
} catch (IOException e) {
throw new RuntimeException(e);
}

urlEncodedFormParameters = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
for (String parameter : rawBodyContent.split(FORM_DATA_SEPARATOR)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import javax.ws.rs.core.MediaType;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Base64;
import java.util.Map;
import java.util.Random;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -102,4 +105,18 @@ public void multipart_getParts_binary() {
fail(e.getMessage());
}
}

@Test
public void postForm_getParamsBase64Encoded_expectAllParams() {
AwsProxyRequest proxyRequest = new AwsProxyRequestBuilder("/form", "POST")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED).build();
proxyRequest.setBody(Base64.getEncoder().encodeToString(ENCODED_FORM_ENTITY.getBytes(Charset.defaultCharset())));
proxyRequest.setIsBase64Encoded(true);

HttpServletRequest request = new AwsProxyHttpServletRequest(proxyRequest, null, null);
Map<String, String[]> params = request.getParameterMap();
assertNotNull(params);
assertEquals(2, params.size());
assertEquals(true, params.containsKey(PART_KEY_1));
}
}

0 comments on commit 6cd0ab7

Please sign in to comment.