Skip to content

Commit 56be654

Browse files
committed
1084: remove remove duplicated code with HttpUtils
1 parent 6fa3f29 commit 56be654

File tree

5 files changed

+81
-91
lines changed

5 files changed

+81
-91
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.amazonaws.serverless.proxy.internal;
2+
3+
import org.apache.commons.io.Charsets;
4+
5+
import java.nio.charset.Charset;
6+
import java.nio.charset.StandardCharsets;
7+
import java.nio.charset.UnsupportedCharsetException;
8+
9+
public final class HttpUtils {
10+
11+
static final String HEADER_KEY_VALUE_SEPARATOR = "=";
12+
static final String HEADER_VALUE_SEPARATOR = ";";
13+
static final String ENCODING_VALUE_KEY = "charset";
14+
15+
16+
static public Charset parseCharacterEncoding(String contentTypeHeader,Charset defaultCharset) {
17+
// we only look at content-type because content-encoding should only be used for
18+
// "binary" requests such as gzip/deflate.
19+
if (contentTypeHeader == null) {
20+
return defaultCharset;
21+
}
22+
23+
String[] contentTypeValues = contentTypeHeader.split(HEADER_VALUE_SEPARATOR);
24+
if (contentTypeValues.length <= 1) {
25+
return defaultCharset;
26+
}
27+
28+
for (String contentTypeValue : contentTypeValues) {
29+
if (contentTypeValue.trim().startsWith(ENCODING_VALUE_KEY)) {
30+
String[] encodingValues = contentTypeValue.split(HEADER_KEY_VALUE_SEPARATOR);
31+
if (encodingValues.length <= 1) {
32+
return defaultCharset;
33+
}
34+
try {
35+
return Charsets.toCharset(encodingValues[1]);
36+
} catch (UnsupportedCharsetException ex) {
37+
return defaultCharset;
38+
}
39+
}
40+
}
41+
return defaultCharset;
42+
}
43+
44+
45+
static public String appendCharacterEncoding(String currentContentType, String newEncoding) {
46+
if (currentContentType == null || currentContentType.trim().isEmpty()) {
47+
return null;
48+
}
49+
50+
if (currentContentType.contains(HEADER_VALUE_SEPARATOR)) {
51+
String[] contentTypeValues = currentContentType.split(HEADER_VALUE_SEPARATOR);
52+
StringBuilder contentType = new StringBuilder(contentTypeValues[0]);
53+
54+
for (int i = 1; i < contentTypeValues.length; i++) {
55+
String contentTypeValue = contentTypeValues[i];
56+
String contentTypeString = HEADER_VALUE_SEPARATOR + " " + contentTypeValue;
57+
if (contentTypeValue.trim().startsWith(ENCODING_VALUE_KEY)) {
58+
contentTypeString = HEADER_VALUE_SEPARATOR + " " + ENCODING_VALUE_KEY + HEADER_KEY_VALUE_SEPARATOR + newEncoding;
59+
}
60+
contentType.append(contentTypeString);
61+
}
62+
63+
return contentType.toString();
64+
} else {
65+
return currentContentType + HEADER_VALUE_SEPARATOR + " " + ENCODING_VALUE_KEY + HEADER_KEY_VALUE_SEPARATOR + newEncoding;
66+
}
67+
}
68+
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package com.amazonaws.serverless.proxy.internal.servlet;
1414

15+
import com.amazonaws.serverless.proxy.internal.HttpUtils;
1516
import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler;
1617
import com.amazonaws.serverless.proxy.internal.SecurityUtils;
1718
import com.amazonaws.serverless.proxy.model.ContainerConfig;
@@ -32,6 +33,7 @@
3233
import java.io.StringReader;
3334
import java.io.UnsupportedEncodingException;
3435
import java.net.URLDecoder;
36+
import java.nio.charset.Charset;
3537
import java.security.Principal;
3638
import java.time.Instant;
3739
import java.time.ZonedDateTime;
@@ -232,7 +234,8 @@ public String getCharacterEncoding() {
232234
if (headers == null) {
233235
return config.getDefaultContentCharset();
234236
}
235-
return parseCharacterEncoding(headers.getFirst(HttpHeaders.CONTENT_TYPE));
237+
Charset charset = HttpUtils.parseCharacterEncoding(headers.getFirst(HttpHeaders.CONTENT_TYPE),null);
238+
return charset != null ? charset.name() : null;
236239
}
237240

238241
@Override
@@ -242,7 +245,7 @@ public void setCharacterEncoding(String s) throws UnsupportedEncodingException {
242245
return;
243246
}
244247
String currentContentType = headers.getFirst(HttpHeaders.CONTENT_TYPE);
245-
headers.putSingle(HttpHeaders.CONTENT_TYPE, appendCharacterEncoding(currentContentType, s));
248+
headers.putSingle(HttpHeaders.CONTENT_TYPE, HttpUtils.appendCharacterEncoding(currentContentType, s));
246249
}
247250

248251
@Override

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

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -369,53 +369,9 @@ protected StringBuffer generateRequestURL(String requestPath) {
369369
return new StringBuffer(getScheme() + "://" + url);
370370
}
371371

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

379-
String[] contentTypeValues = contentTypeHeader.split(HEADER_VALUE_SEPARATOR);
380-
if (contentTypeValues.length <= 1) {
381-
return null;
382-
}
383373

384-
for (String contentTypeValue : contentTypeValues) {
385-
if (contentTypeValue.trim().startsWith(ENCODING_VALUE_KEY)) {
386-
String[] encodingValues = contentTypeValue.split(HEADER_KEY_VALUE_SEPARATOR);
387-
if (encodingValues.length <= 1) {
388-
return null;
389-
}
390-
return encodingValues[1];
391-
}
392-
}
393-
return null;
394-
}
395374

396-
protected String appendCharacterEncoding(String currentContentType, String newEncoding) {
397-
if (currentContentType == null || currentContentType.trim().isEmpty()) {
398-
return null;
399-
}
400-
401-
if (currentContentType.contains(HEADER_VALUE_SEPARATOR)) {
402-
String[] contentTypeValues = currentContentType.split(HEADER_VALUE_SEPARATOR);
403-
StringBuilder contentType = new StringBuilder(contentTypeValues[0]);
404-
405-
for (int i = 1; i < contentTypeValues.length; i++) {
406-
String contentTypeValue = contentTypeValues[i];
407-
String contentTypeString = HEADER_VALUE_SEPARATOR + " " + contentTypeValue;
408-
if (contentTypeValue.trim().startsWith(ENCODING_VALUE_KEY)) {
409-
contentTypeString = HEADER_VALUE_SEPARATOR + " " + ENCODING_VALUE_KEY + HEADER_KEY_VALUE_SEPARATOR + newEncoding;
410-
}
411-
contentType.append(contentTypeString);
412-
}
413-
414-
return contentType.toString();
415-
} else {
416-
return currentContentType + HEADER_VALUE_SEPARATOR + " " + ENCODING_VALUE_KEY + HEADER_KEY_VALUE_SEPARATOR + newEncoding;
417-
}
418-
}
419375

420376
protected ServletInputStream bodyStringToInputStream(String body, boolean isBase64Encoded) throws IOException {
421377
if (body == null) {

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
package com.amazonaws.serverless.proxy.internal.servlet;
1414

1515

16+
import com.amazonaws.serverless.proxy.internal.HttpUtils;
1617
import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler;
1718
import com.amazonaws.serverless.proxy.internal.SecurityUtils;
1819
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
@@ -35,6 +36,7 @@
3536
import java.io.IOException;
3637
import java.io.StringReader;
3738
import java.io.UnsupportedEncodingException;
39+
import java.nio.charset.Charset;
3840
import java.security.Principal;
3941
import java.time.Instant;
4042
import java.time.ZonedDateTime;
@@ -273,7 +275,8 @@ public String getCharacterEncoding() {
273275
if (request.getMultiValueHeaders() == null) {
274276
return config.getDefaultContentCharset();
275277
}
276-
return parseCharacterEncoding(request.getMultiValueHeaders().getFirst(HttpHeaders.CONTENT_TYPE));
278+
Charset charset = HttpUtils.parseCharacterEncoding(request.getMultiValueHeaders().getFirst(HttpHeaders.CONTENT_TYPE),null);
279+
return charset != null ? charset.name() : null;
277280
}
278281

279282

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

292-
request.getMultiValueHeaders().putSingle(HttpHeaders.CONTENT_TYPE, appendCharacterEncoding(currentContentType, s));
295+
request.getMultiValueHeaders().putSingle(HttpHeaders.CONTENT_TYPE, HttpUtils.appendCharacterEncoding(currentContentType, s));
293296
}
294297

295298

aws-serverless-java-container-springboot3/src/main/java/com/amazonaws/serverless/proxy/spring/AwsSpringHttpProcessingUtils.java

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.concurrent.CountDownLatch;
1111
import java.util.concurrent.TimeUnit;
1212

13+
import com.amazonaws.serverless.proxy.internal.HttpUtils;
1314
import org.apache.commons.io.Charsets;
1415
import org.apache.commons.logging.Log;
1516
import org.apache.commons.logging.LogFactory;
@@ -161,17 +162,6 @@ private static HttpServletRequest generateRequest2(String request, Context lambd
161162
httpRequest
162163
);
163164

164-
if (StringUtils.hasText(v2Request.getBody())) {
165-
if (v2Request.getHeaders().get(HttpHeaders.CONTENT_TYPE)==null) {
166-
httpRequest.setContentType("application/json");
167-
}
168-
if (v2Request.isBase64Encoded()) {
169-
httpRequest.setContent(Base64.getMimeDecoder().decode(v2Request.getBody()));
170-
} else {
171-
Charset charseEncoding = parseCharacterEncoding(v2Request.getHeaders().get(HttpHeaders.CONTENT_TYPE));
172-
httpRequest.setContent(v2Request.getBody().getBytes(charseEncoding));
173-
}
174-
}
175165
httpRequest.setAttribute(RequestReader.HTTP_API_CONTEXT_PROPERTY, v2Request.getRequestContext());
176166
httpRequest.setAttribute(RequestReader.HTTP_API_STAGE_VARS_PROPERTY, v2Request.getStageVariables());
177167
httpRequest.setAttribute(RequestReader.HTTP_API_EVENT_PROPERTY, v2Request);
@@ -210,42 +200,12 @@ private static void populateContentAndContentType(
210200
if (base64Encoded) {
211201
httpRequest.setContent(Base64.getMimeDecoder().decode(body));
212202
} else {
213-
Charset charseEncoding = parseCharacterEncoding(contentType);
203+
Charset charseEncoding = HttpUtils.parseCharacterEncoding(contentType,StandardCharsets.UTF_8);
214204
httpRequest.setContent(body.getBytes(charseEncoding));
215205
}
216206
}
217207
}
218208

219-
static final String HEADER_KEY_VALUE_SEPARATOR = "=";
220-
static final String HEADER_VALUE_SEPARATOR = ";";
221-
static final String ENCODING_VALUE_KEY = "charset";
222-
static protected Charset parseCharacterEncoding(String contentTypeHeader) {
223-
// we only look at content-type because content-encoding should only be used for
224-
// "binary" requests such as gzip/deflate.
225-
Charset defaultCharset = StandardCharsets.UTF_8;
226-
if (contentTypeHeader == null) {
227-
return defaultCharset;
228-
}
229-
230-
String[] contentTypeValues = contentTypeHeader.split(HEADER_VALUE_SEPARATOR);
231-
if (contentTypeValues.length <= 1) {
232-
return defaultCharset;
233-
}
234209

235-
for (String contentTypeValue : contentTypeValues) {
236-
if (contentTypeValue.trim().startsWith(ENCODING_VALUE_KEY)) {
237-
String[] encodingValues = contentTypeValue.split(HEADER_KEY_VALUE_SEPARATOR);
238-
if (encodingValues.length <= 1) {
239-
return defaultCharset;
240-
}
241-
try {
242-
return Charsets.toCharset(encodingValues[1]);
243-
} catch (UnsupportedCharsetException ex) {
244-
return defaultCharset;
245-
}
246-
}
247-
}
248-
return defaultCharset;
249-
}
250210

251211
}

0 commit comments

Comments
 (0)