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

@ -74,16 +74,28 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
callbacks.resolveCallback({ callbacks.resolveCallback({
content: { content: {
raw: result.raw, raw: result.raw,
toString: () => { toString: (encoding?: http.HttpResponseEncoding) => {
if (types.isString(result.responseAsString)) { let str: string;
return result.responseAsString; if (encoding) {
str = decodeResponse(result.raw, encoding);
} else {
str = result.responseAsString;
}
if (types.isString(str)) {
return str;
} else { } else {
throw new Error("Response content may not be converted to string"); throw new Error("Response content may not be converted to string");
} }
}, },
toJSON: () => { toJSON: (encoding?: http.HttpResponseEncoding) => {
ensureUtils(); 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: () => { toImage: () => {
ensureImageSource(); 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)
}

View File

@ -89,9 +89,9 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
resolve({ resolve({
content: { content: {
raw: data, raw: data,
toString: () => { return NSDataToString(data); }, toString: (encoding?: http.HttpResponseEncoding) => { return NSDataToString(data, encoding); },
toJSON: () => { toJSON: (encoding?: http.HttpResponseEncoding) => {
return utils.parseJSON(NSDataToString(data)); return utils.parseJSON(NSDataToString(data, encoding));
}, },
toImage: () => { toImage: () => {
ensureImageSource(); ensureImageSource();
@ -125,7 +125,7 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
} }
}); });
if(options.url && debugRequest) { if (options.url && debugRequest) {
var request = { var request = {
url: options.url, url: options.url,
method: "GET", method: "GET",
@ -141,6 +141,10 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
}); });
} }
function NSDataToString(data: any): string { function NSDataToString(data: any, encoding?: http.HttpResponseEncoding): string {
return NSString.alloc().initWithDataEncoding(data, 4).toString(); let code = 4; //UTF8
if (encoding === http.HttpResponseEncoding.GBK) {
code = 1586;
}
return NSString.alloc().initWithDataEncoding(data, code).toString();
} }

View File

@ -113,6 +113,10 @@ declare module "http" {
export type Headers = { [key: string]: string | string[] }; export type Headers = { [key: string]: string | string[] };
export const enum HttpResponseEncoding {
UTF8,
GBK
}
/** /**
* Encapsulates the content of an HttpResponse. * Encapsulates the content of an HttpResponse.
*/ */
@ -125,12 +129,12 @@ declare module "http" {
/** /**
* Gets the response body as string. * Gets the response body as string.
*/ */
toString: () => string; toString: (encoding?: HttpResponseEncoding) => string;
/** /**
* Gets the response body as JSON object. * Gets the response body as JSON object.
*/ */
toJSON: () => any; toJSON: (encoding?: HttpResponseEncoding) => any;
/** /**
* Gets the response body as ImageSource. * Gets the response body as ImageSource.