From 44888b6e26b6472f3beaee9f7b86a19c3c1761e9 Mon Sep 17 00:00:00 2001 From: abasile Date: Fri, 11 Mar 2016 17:15:39 +0100 Subject: [PATCH 1/2] add charset to DataEmitter on BodyDecoder --- .../com/koushikdutta/async/http/HttpUtil.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/AndroidAsync/src/com/koushikdutta/async/http/HttpUtil.java b/AndroidAsync/src/com/koushikdutta/async/http/HttpUtil.java index b7178ee6c..6a57ead1d 100644 --- a/AndroidAsync/src/com/koushikdutta/async/http/HttpUtil.java +++ b/AndroidAsync/src/com/koushikdutta/async/http/HttpUtil.java @@ -14,6 +14,8 @@ import com.koushikdutta.async.http.filter.GZIPInputFilter; import com.koushikdutta.async.http.filter.InflaterInputFilter; +import java.nio.charset.Charset; + public class HttpUtil { public static AsyncHttpRequestBody getBody(DataEmitter emitter, CompletedCallback reporter, Headers headers) { String contentType = headers.get("Content-Type"); @@ -59,7 +61,7 @@ public void run() { } } - public static DataEmitter getBodyDecoder(DataEmitter emitter, Protocol protocol, Headers headers, boolean server) { + public static DataEmitter getBodyDecoder(DataEmitter emitter, Protocol protocol, final Headers headers, boolean server) { long _contentLength; try { _contentLength = Long.parseLong(headers.get("Content-Length")); @@ -81,7 +83,19 @@ public static DataEmitter getBodyDecoder(DataEmitter emitter, Protocol protocol, emitter = ender; return emitter; } - ContentLengthFilter contentLengthWatcher = new ContentLengthFilter(contentLength); + String charset = null; + Multimap mm = Multimap.parseSemicolonDelimited(headers.get("Content-Type")); + String cs; + if (mm != null && null != (cs = mm.getString("charset")) && Charset.isSupported(cs)) { + charset = cs; + } + final String finalCharset = charset; + final ContentLengthFilter contentLengthWatcher = new ContentLengthFilter(contentLength){ + @Override + public String charset() { + return finalCharset; + } + }; contentLengthWatcher.setDataEmitter(emitter); emitter = contentLengthWatcher; } From befd05c062b81e914ca1c7b57918846250566ab6 Mon Sep 17 00:00:00 2001 From: abasile Date: Mon, 14 Mar 2016 15:10:19 +0100 Subject: [PATCH 2/2] set utf-8 as defaut charset for json decoding --- .../src/com/koushikdutta/async/parser/JSONObjectParser.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/AndroidAsync/src/com/koushikdutta/async/parser/JSONObjectParser.java b/AndroidAsync/src/com/koushikdutta/async/parser/JSONObjectParser.java index e02db2c2e..d12d3f17e 100644 --- a/AndroidAsync/src/com/koushikdutta/async/parser/JSONObjectParser.java +++ b/AndroidAsync/src/com/koushikdutta/async/parser/JSONObjectParser.java @@ -5,6 +5,8 @@ import com.koushikdutta.async.callback.CompletedCallback; import com.koushikdutta.async.future.Future; import com.koushikdutta.async.future.TransformFuture; +import com.koushikdutta.async.util.Charsets; + import org.json.JSONObject; import java.lang.reflect.Type; @@ -15,7 +17,7 @@ public class JSONObjectParser implements AsyncParser { @Override public Future parse(DataEmitter emitter) { - return new StringParser().parse(emitter) + return new StringParser(emitter.charset() == null ? Charsets.UTF_8 : null ).parse(emitter) .then(new TransformFuture() { @Override protected void transform(String result) throws Exception {