GBK encoding support for http (#3165)

* http response add GBK charset support

* default encode do not change

* change compare response encode `==`  to `===`
This commit is contained in:
Vladimir Enchev
2016-11-24 13:53:43 +02:00
committed by GitHub
parent 2d640ddeda
commit 3f4a5beca9
3 changed files with 159 additions and 131 deletions

View File

@ -66,7 +66,7 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
var pair: org.nativescript.widgets.Async.Http.KeyValuePair;
for (i = 0; i < length; i++) {
pair = jHeaders.get(i);
(<any>http).addHeader(headers, pair.key, pair.value);
}
}
@ -74,16 +74,28 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
callbacks.resolveCallback({
content: {
raw: result.raw,
toString: () => {
if (types.isString(result.responseAsString)) {
return result.responseAsString;
toString: (encoding?: http.HttpResponseEncoding) => {
let str: string;
if (encoding) {
str = decodeResponse(result.raw, encoding);
} else {
str = result.responseAsString;
}
if (types.isString(str)) {
return str;
} else {
throw new Error("Response content may not be converted to string");
}
},
toJSON: () => {
toJSON: (encoding?: http.HttpResponseEncoding) => {
ensureUtils();
return utils.parseJSON(result.responseAsString);
let str: string;
if (encoding) {
str = decodeResponse(result.raw, encoding);
} else {
str = result.responseAsString;
}
return utils.parseJSON(str);
},
toImage: () => {
ensureImageSource();
@ -195,3 +207,11 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
}
});
}
function decodeResponse(raw: any, encoding?: http.HttpResponseEncoding) {
let charsetName = "UTF-8";
if (encoding === http.HttpResponseEncoding.GBK) {
charsetName = 'GBK';
}
return raw.toString(charsetName)
}