mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 04:41:36 +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();
|
||||||
@ -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();
|
||||||
}
|
}
|
||||||
|
238
tns-core-modules/http/http.d.ts
vendored
238
tns-core-modules/http/http.d.ts
vendored
@ -2,144 +2,148 @@
|
|||||||
* Allows you to send web requests and receive the responses.
|
* Allows you to send web requests and receive the responses.
|
||||||
*/
|
*/
|
||||||
declare module "http" {
|
declare module "http" {
|
||||||
import image = require("image-source");
|
import image = require("image-source");
|
||||||
import fs = require("file-system");
|
import fs = require("file-system");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Downloads the content from the specified URL as a string.
|
* Downloads the content from the specified URL as a string.
|
||||||
* @param url The URL to request from.
|
* @param url The URL to request from.
|
||||||
*/
|
*/
|
||||||
export function getString(url: string): Promise<string>
|
export function getString(url: string): Promise<string>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Downloads the content from the specified URL as a string.
|
* Downloads the content from the specified URL as a string.
|
||||||
* @param options An object that specifies various request options.
|
* @param options An object that specifies various request options.
|
||||||
*/
|
*/
|
||||||
export function getString(options: HttpRequestOptions): Promise<string>
|
export function getString(options: HttpRequestOptions): Promise<string>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Downloads the content from the specified URL as a string and returns its JSON.parse representation.
|
* Downloads the content from the specified URL as a string and returns its JSON.parse representation.
|
||||||
* @param url The URL to request from.
|
* @param url The URL to request from.
|
||||||
*/
|
*/
|
||||||
export function getJSON<T>(url: string): Promise<T>
|
export function getJSON<T>(url: string): Promise<T>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Downloads the content from the specified URL as a string and returns its JSON.parse representation.
|
* Downloads the content from the specified URL as a string and returns its JSON.parse representation.
|
||||||
* @param options An object that specifies various request options.
|
* @param options An object that specifies various request options.
|
||||||
*/
|
*/
|
||||||
export function getJSON<T>(options: HttpRequestOptions): Promise<T>
|
export function getJSON<T>(options: HttpRequestOptions): Promise<T>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Downloads the content from the specified URL and attempts to decode it as an image.
|
* Downloads the content from the specified URL and attempts to decode it as an image.
|
||||||
* @param url The URL to request from.
|
* @param url The URL to request from.
|
||||||
*/
|
*/
|
||||||
export function getImage(url: string): Promise<image.ImageSource>
|
export function getImage(url: string): Promise<image.ImageSource>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Downloads the content from the specified URL and attempts to decode it as an image.
|
* Downloads the content from the specified URL and attempts to decode it as an image.
|
||||||
* @param options An object that specifies various request options.
|
* @param options An object that specifies various request options.
|
||||||
*/
|
*/
|
||||||
export function getImage(options: HttpRequestOptions): Promise<image.ImageSource>
|
export function getImage(options: HttpRequestOptions): Promise<image.ImageSource>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Downloads the content from the specified URL and attempts to save it as file.
|
||||||
|
* @param url The URL to request from.
|
||||||
|
* @param destinationFilePath Optional. The downloaded file path.
|
||||||
|
*/
|
||||||
|
export function getFile(url: string, destinationFilePath?: string): Promise<fs.File>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Downloads the content from the specified URL and attempts to save it as file.
|
||||||
|
* @param options An object that specifies various request options.
|
||||||
|
* @param destinationFilePath Optional. The downloaded file path.
|
||||||
|
*/
|
||||||
|
export function getFile(options: HttpRequestOptions, destinationFilePath?: string): Promise<fs.File>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Makes a generic http request using the provided options and returns a HttpResponse Object.
|
||||||
|
* @param options An object that specifies various request options.
|
||||||
|
*/
|
||||||
|
export function request(options: HttpRequestOptions): Promise<HttpResponse>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides options for the http requests.
|
||||||
|
*/
|
||||||
|
export interface HttpRequestOptions {
|
||||||
|
/**
|
||||||
|
* Gets or sets the request url.
|
||||||
|
*/
|
||||||
|
url: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Downloads the content from the specified URL and attempts to save it as file.
|
* Gets or sets the request method.
|
||||||
* @param url The URL to request from.
|
|
||||||
* @param destinationFilePath Optional. The downloaded file path.
|
|
||||||
*/
|
*/
|
||||||
export function getFile(url: string, destinationFilePath?: string): Promise<fs.File>
|
method: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Downloads the content from the specified URL and attempts to save it as file.
|
* Gets or sets the request headers in JSON format.
|
||||||
* @param options An object that specifies various request options.
|
|
||||||
* @param destinationFilePath Optional. The downloaded file path.
|
|
||||||
*/
|
*/
|
||||||
export function getFile(options: HttpRequestOptions, destinationFilePath?: string): Promise<fs.File>
|
headers?: any;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Makes a generic http request using the provided options and returns a HttpResponse Object.
|
* Gets or sets the request body.
|
||||||
* @param options An object that specifies various request options.
|
*/
|
||||||
*/
|
content?: string | FormData;
|
||||||
export function request(options: HttpRequestOptions): Promise<HttpResponse>;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides options for the http requests.
|
* Gets or sets the request timeout in milliseconds.
|
||||||
*/
|
*/
|
||||||
export interface HttpRequestOptions {
|
timeout?: number;
|
||||||
/**
|
}
|
||||||
* Gets or sets the request url.
|
|
||||||
*/
|
|
||||||
url: string;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets or sets the request method.
|
* Encapsulates HTTP-response information from an HTTP-request.
|
||||||
*/
|
*/
|
||||||
method: string;
|
export interface HttpResponse {
|
||||||
|
/**
|
||||||
|
* Gets the response status code.
|
||||||
|
*/
|
||||||
|
statusCode: number;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets or sets the request headers in JSON format.
|
* Gets the response headers.
|
||||||
*/
|
*/
|
||||||
headers?: any;
|
headers: Headers;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets or sets the request body.
|
* Gets the response content.
|
||||||
*/
|
*/
|
||||||
content?: string | FormData;
|
content?: HttpContent;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
export type Headers = { [key: string]: string | string[] };
|
||||||
* Gets or sets the request timeout in milliseconds.
|
|
||||||
*/
|
|
||||||
timeout?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
export const enum HttpResponseEncoding {
|
||||||
* Encapsulates HTTP-response information from an HTTP-request.
|
UTF8,
|
||||||
*/
|
GBK
|
||||||
export interface HttpResponse {
|
}
|
||||||
/**
|
/**
|
||||||
* Gets the response status code.
|
* Encapsulates the content of an HttpResponse.
|
||||||
*/
|
*/
|
||||||
statusCode: number;
|
export interface HttpContent {
|
||||||
|
/**
|
||||||
|
* Gets the response body as raw data.
|
||||||
|
*/
|
||||||
|
raw: any;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the response headers.
|
* Gets the response body as string.
|
||||||
*/
|
*/
|
||||||
headers: Headers;
|
toString: (encoding?: HttpResponseEncoding) => string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the response content.
|
* Gets the response body as JSON object.
|
||||||
*/
|
*/
|
||||||
content?: HttpContent;
|
toJSON: (encoding?: HttpResponseEncoding) => any;
|
||||||
}
|
|
||||||
|
|
||||||
export type Headers = { [key: string]: string | string[] };
|
/**
|
||||||
|
* Gets the response body as ImageSource.
|
||||||
|
*/
|
||||||
|
toImage: () => Promise<image.ImageSource>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encapsulates the content of an HttpResponse.
|
* Gets the response body as file.
|
||||||
*/
|
*/
|
||||||
export interface HttpContent {
|
toFile: (destinationFilePath?: string) => fs.File;
|
||||||
/**
|
}
|
||||||
* Gets the response body as raw data.
|
|
||||||
*/
|
|
||||||
raw: any;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the response body as string.
|
|
||||||
*/
|
|
||||||
toString: () => string;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the response body as JSON object.
|
|
||||||
*/
|
|
||||||
toJSON: () => any;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the response body as ImageSource.
|
|
||||||
*/
|
|
||||||
toImage: () => Promise<image.ImageSource>;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the response body as file.
|
|
||||||
*/
|
|
||||||
toFile: (destinationFilePath?: string) => fs.File;
|
|
||||||
}
|
|
||||||
}
|
}
|
Reference in New Issue
Block a user