Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use charset declaration to decode body and use utf-8 as default json charset #430

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions AndroidAsync/src/com/koushikdutta/async/http/HttpUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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"));
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,7 +17,7 @@
public class JSONObjectParser implements AsyncParser<JSONObject> {
@Override
public Future<JSONObject> parse(DataEmitter emitter) {
return new StringParser().parse(emitter)
return new StringParser(emitter.charset() == null ? Charsets.UTF_8 : null ).parse(emitter)
.then(new TransformFuture<JSONObject, String>() {
@Override
protected void transform(String result) throws Exception {
Expand Down