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)
}

View File

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

View File

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