Skip to content

Commit a898855

Browse files
author
Stephane Landelle
committed
Port missing AsyncHttpClient#540 on master
1 parent a2d323e commit a898855

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

api/src/main/java/org/asynchttpclient/RequestBuilderBase.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -634,8 +634,7 @@ public T setConnectionPoolKeyStrategy(ConnectionPoolKeyStrategy connectionPoolKe
634634
return derived.cast(this);
635635
}
636636

637-
public Request build() {
638-
637+
private void executeSignatureCalculator() {
639638
/* Let's first calculate and inject signature, before finalizing actual build
640639
* (order does not matter with current implementation but may in future)
641640
*/
@@ -647,8 +646,28 @@ public Request build() {
647646
url = url.substring(0, i);
648647
}
649648
signatureCalculator.calculateAndAddSignature(url, request, this);
649+
}
650+
}
651+
652+
private void computeRequestCharset() {
653+
if (request.charset != null) {
654+
try {
655+
final String contentType = request.headers.getFirstValue("Content-Type");
656+
if (contentType != null) {
657+
final String charset = AsyncHttpProviderUtils.parseCharset(contentType);
658+
if (charset != null) {
659+
// ensure that if charset is provided with the Content-Type header,
660+
// we propagate that down to the charset of the Request object
661+
request.charset = charset;
662+
}
663+
}
664+
} catch (Throwable e) {
665+
// NoOp -- we can't fix the Content-Type or charset from here
666+
}
650667
}
651-
668+
}
669+
670+
private void computeRequestContentLength() {
652671
if (request.length < 0 && request.streamData == null) {
653672
// can't concatenate content-length
654673
String contentLength = null;
@@ -664,6 +683,14 @@ public Request build() {
664683
}
665684
}
666685
}
686+
}
687+
688+
public Request build() {
689+
690+
executeSignatureCalculator();
691+
computeRequestCharset();
692+
computeRequestContentLength();
693+
667694
if (request.cookies != null) {
668695
request.cookies = Collections.unmodifiableCollection(request.cookies);
669696
}

api/src/test/java/org/asynchttpclient/async/RequestBuilderTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,12 @@ public void testPercentageEncodedUserInfo() {
112112
assertEquals(req.getMethod(), "GET");
113113
assertEquals(req.getUrl(), "http://hello:wor%[email protected]");
114114
}
115+
116+
@Test(groups = {"standalone", "default_provider"})
117+
public void testContentTypeCharsetToBodyEncoding() {
118+
final Request req = new RequestBuilder("GET").setHeader("Content-Type", "application/json; charset=utf-8").build();
119+
assertEquals(req.getBodyEncoding(), "utf-8");
120+
final Request req2 = new RequestBuilder("GET").setHeader("Content-Type", "application/json; charset=\"utf-8\"").build();
121+
assertEquals(req2.getBodyEncoding(), "utf-8");
122+
}
115123
}

0 commit comments

Comments
 (0)