mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 12:57:42 +08:00
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:
@ -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)
|
||||||
|
}
|
||||||
|
@ -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();
|
||||||
@ -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();
|
||||||
}
|
}
|
||||||
|
8
tns-core-modules/http/http.d.ts
vendored
8
tns-core-modules/http/http.d.ts
vendored
@ -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.
|
||||||
|
Reference in New Issue
Block a user