Skip to content

Commit

Permalink
aws#1084: remove remove duplicated code with HttpUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
npeters committed Nov 21, 2024
1 parent 6fa3f29 commit 8e79023
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.amazonaws.serverless.proxy.internal;

import org.apache.commons.io.Charsets;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;

public final class HttpUtils {

static final String HEADER_KEY_VALUE_SEPARATOR = "=";
static final String HEADER_VALUE_SEPARATOR = ";";
static final String ENCODING_VALUE_KEY = "charset";


static public Charset parseCharacterEncoding(String contentTypeHeader,Charset defaultCharset) {
// we only look at content-type because content-encoding should only be used for
// "binary" requests such as gzip/deflate.
if (contentTypeHeader == null) {
return defaultCharset;
}

String[] contentTypeValues = contentTypeHeader.split(HEADER_VALUE_SEPARATOR);
if (contentTypeValues.length <= 1) {
return defaultCharset;
}

for (String contentTypeValue : contentTypeValues) {
if (contentTypeValue.trim().startsWith(ENCODING_VALUE_KEY)) {
String[] encodingValues = contentTypeValue.split(HEADER_KEY_VALUE_SEPARATOR);
if (encodingValues.length <= 1) {
return defaultCharset;
}
try {
return Charsets.toCharset(encodingValues[1]);
} catch (UnsupportedCharsetException ex) {
return defaultCharset;
}
}
}
return defaultCharset;
}


static public String appendCharacterEncoding(String currentContentType, String newEncoding) {
if (currentContentType == null || currentContentType.trim().isEmpty()) {
return null;
}

if (currentContentType.contains(HEADER_VALUE_SEPARATOR)) {
String[] contentTypeValues = currentContentType.split(HEADER_VALUE_SEPARATOR);
StringBuilder contentType = new StringBuilder(contentTypeValues[0]);

for (int i = 1; i < contentTypeValues.length; i++) {
String contentTypeValue = contentTypeValues[i];
String contentTypeString = HEADER_VALUE_SEPARATOR + " " + contentTypeValue;
if (contentTypeValue.trim().startsWith(ENCODING_VALUE_KEY)) {
contentTypeString = HEADER_VALUE_SEPARATOR + " " + ENCODING_VALUE_KEY + HEADER_KEY_VALUE_SEPARATOR + newEncoding;
}
contentType.append(contentTypeString);
}

return contentType.toString();
} else {
return currentContentType + HEADER_VALUE_SEPARATOR + " " + ENCODING_VALUE_KEY + HEADER_KEY_VALUE_SEPARATOR + newEncoding;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package com.amazonaws.serverless.proxy.internal.servlet;

import com.amazonaws.serverless.proxy.internal.HttpUtils;
import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler;
import com.amazonaws.serverless.proxy.internal.SecurityUtils;
import com.amazonaws.serverless.proxy.model.ContainerConfig;
Expand All @@ -32,6 +33,7 @@
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.security.Principal;
import java.time.Instant;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -232,7 +234,8 @@ public String getCharacterEncoding() {
if (headers == null) {
return config.getDefaultContentCharset();
}
return parseCharacterEncoding(headers.getFirst(HttpHeaders.CONTENT_TYPE));
Charset charset = HttpUtils.parseCharacterEncoding(headers.getFirst(HttpHeaders.CONTENT_TYPE),null);
return charset != null ? charset.name() : null;
}

@Override
Expand All @@ -242,7 +245,7 @@ public void setCharacterEncoding(String s) throws UnsupportedEncodingException {
return;
}
String currentContentType = headers.getFirst(HttpHeaders.CONTENT_TYPE);
headers.putSingle(HttpHeaders.CONTENT_TYPE, appendCharacterEncoding(currentContentType, s));
headers.putSingle(HttpHeaders.CONTENT_TYPE, HttpUtils.appendCharacterEncoding(currentContentType, s));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,53 +369,9 @@ protected StringBuffer generateRequestURL(String requestPath) {
return new StringBuffer(getScheme() + "://" + url);
}

protected String parseCharacterEncoding(String contentTypeHeader) {
// we only look at content-type because content-encoding should only be used for
// "binary" requests such as gzip/deflate.
if (contentTypeHeader == null) {
return null;
}

String[] contentTypeValues = contentTypeHeader.split(HEADER_VALUE_SEPARATOR);
if (contentTypeValues.length <= 1) {
return null;
}

for (String contentTypeValue : contentTypeValues) {
if (contentTypeValue.trim().startsWith(ENCODING_VALUE_KEY)) {
String[] encodingValues = contentTypeValue.split(HEADER_KEY_VALUE_SEPARATOR);
if (encodingValues.length <= 1) {
return null;
}
return encodingValues[1];
}
}
return null;
}

protected String appendCharacterEncoding(String currentContentType, String newEncoding) {
if (currentContentType == null || currentContentType.trim().isEmpty()) {
return null;
}

if (currentContentType.contains(HEADER_VALUE_SEPARATOR)) {
String[] contentTypeValues = currentContentType.split(HEADER_VALUE_SEPARATOR);
StringBuilder contentType = new StringBuilder(contentTypeValues[0]);

for (int i = 1; i < contentTypeValues.length; i++) {
String contentTypeValue = contentTypeValues[i];
String contentTypeString = HEADER_VALUE_SEPARATOR + " " + contentTypeValue;
if (contentTypeValue.trim().startsWith(ENCODING_VALUE_KEY)) {
contentTypeString = HEADER_VALUE_SEPARATOR + " " + ENCODING_VALUE_KEY + HEADER_KEY_VALUE_SEPARATOR + newEncoding;
}
contentType.append(contentTypeString);
}

return contentType.toString();
} else {
return currentContentType + HEADER_VALUE_SEPARATOR + " " + ENCODING_VALUE_KEY + HEADER_KEY_VALUE_SEPARATOR + newEncoding;
}
}

protected ServletInputStream bodyStringToInputStream(String body, boolean isBase64Encoded) throws IOException {
if (body == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package com.amazonaws.serverless.proxy.internal.servlet;


import com.amazonaws.serverless.proxy.internal.HttpUtils;
import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler;
import com.amazonaws.serverless.proxy.internal.SecurityUtils;
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
Expand All @@ -35,6 +36,7 @@
import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.Principal;
import java.time.Instant;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -273,7 +275,8 @@ public String getCharacterEncoding() {
if (request.getMultiValueHeaders() == null) {
return config.getDefaultContentCharset();
}
return parseCharacterEncoding(request.getMultiValueHeaders().getFirst(HttpHeaders.CONTENT_TYPE));
Charset charset = HttpUtils.parseCharacterEncoding(request.getMultiValueHeaders().getFirst(HttpHeaders.CONTENT_TYPE),null);
return charset != null ? charset.name() : null;
}


Expand All @@ -284,12 +287,12 @@ public void setCharacterEncoding(String s)
request.setMultiValueHeaders(new Headers());
}
String currentContentType = request.getMultiValueHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
if (currentContentType == null || "".equals(currentContentType)) {
if (currentContentType == null || currentContentType.isEmpty()) {
log.debug("Called set character encoding to " + SecurityUtils.crlf(s) + " on a request without a content type. Character encoding will not be set");
return;
}

request.getMultiValueHeaders().putSingle(HttpHeaders.CONTENT_TYPE, appendCharacterEncoding(currentContentType, s));
request.getMultiValueHeaders().putSingle(HttpHeaders.CONTENT_TYPE, HttpUtils.appendCharacterEncoding(currentContentType, s));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import com.amazonaws.serverless.proxy.internal.HttpUtils;
import org.apache.commons.io.Charsets;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -168,7 +169,7 @@ private static HttpServletRequest generateRequest2(String request, Context lambd
if (v2Request.isBase64Encoded()) {
httpRequest.setContent(Base64.getMimeDecoder().decode(v2Request.getBody()));
} else {
Charset charseEncoding = parseCharacterEncoding(v2Request.getHeaders().get(HttpHeaders.CONTENT_TYPE));
Charset charseEncoding = HttpUtils.parseCharacterEncoding(v2Request.getHeaders().get(HttpHeaders.CONTENT_TYPE),StandardCharsets.UTF_8);
httpRequest.setContent(v2Request.getBody().getBytes(charseEncoding));
}
}
Expand Down Expand Up @@ -210,42 +211,12 @@ private static void populateContentAndContentType(
if (base64Encoded) {
httpRequest.setContent(Base64.getMimeDecoder().decode(body));
} else {
Charset charseEncoding = parseCharacterEncoding(contentType);
Charset charseEncoding = HttpUtils.parseCharacterEncoding(contentType,StandardCharsets.UTF_8);
httpRequest.setContent(body.getBytes(charseEncoding));
}
}
}

static final String HEADER_KEY_VALUE_SEPARATOR = "=";
static final String HEADER_VALUE_SEPARATOR = ";";
static final String ENCODING_VALUE_KEY = "charset";
static protected Charset parseCharacterEncoding(String contentTypeHeader) {
// we only look at content-type because content-encoding should only be used for
// "binary" requests such as gzip/deflate.
Charset defaultCharset = StandardCharsets.UTF_8;
if (contentTypeHeader == null) {
return defaultCharset;
}

String[] contentTypeValues = contentTypeHeader.split(HEADER_VALUE_SEPARATOR);
if (contentTypeValues.length <= 1) {
return defaultCharset;
}

for (String contentTypeValue : contentTypeValues) {
if (contentTypeValue.trim().startsWith(ENCODING_VALUE_KEY)) {
String[] encodingValues = contentTypeValue.split(HEADER_KEY_VALUE_SEPARATOR);
if (encodingValues.length <= 1) {
return defaultCharset;
}
try {
return Charsets.toCharset(encodingValues[1]);
} catch (UnsupportedCharsetException ex) {
return defaultCharset;
}
}
}
return defaultCharset;
}

}

0 comments on commit 8e79023

Please sign in to comment.