mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
chore: default tabs
This commit is contained in:
@@ -8,8 +8,8 @@ import * as domainDebugger from '../../debugger';
|
||||
import { getFilenameFromUrl } from './http-request-common';
|
||||
|
||||
export enum HttpResponseEncoding {
|
||||
UTF8,
|
||||
GBK,
|
||||
UTF8,
|
||||
GBK,
|
||||
}
|
||||
|
||||
const currentDevice = UIDevice.currentDevice;
|
||||
@@ -23,214 +23,214 @@ const sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration;
|
||||
const queue = NSOperationQueue.mainQueue;
|
||||
|
||||
function parseJSON(source: string): any {
|
||||
const src = source.trim();
|
||||
if (src.lastIndexOf(')') === src.length - 1) {
|
||||
return JSON.parse(src.substring(src.indexOf('(') + 1, src.lastIndexOf(')')));
|
||||
}
|
||||
const src = source.trim();
|
||||
if (src.lastIndexOf(')') === src.length - 1) {
|
||||
return JSON.parse(src.substring(src.indexOf('(') + 1, src.lastIndexOf(')')));
|
||||
}
|
||||
|
||||
return JSON.parse(src);
|
||||
return JSON.parse(src);
|
||||
}
|
||||
|
||||
class NSURLSessionTaskDelegateImpl extends NSObject implements NSURLSessionTaskDelegate {
|
||||
public static ObjCProtocols = [NSURLSessionTaskDelegate];
|
||||
public URLSessionTaskWillPerformHTTPRedirectionNewRequestCompletionHandler(session: NSURLSession, task: NSURLSessionTask, response: NSHTTPURLResponse, request: NSURLRequest, completionHandler: (p1: NSURLRequest) => void): void {
|
||||
completionHandler(null);
|
||||
}
|
||||
public static ObjCProtocols = [NSURLSessionTaskDelegate];
|
||||
public URLSessionTaskWillPerformHTTPRedirectionNewRequestCompletionHandler(session: NSURLSession, task: NSURLSessionTask, response: NSHTTPURLResponse, request: NSURLRequest, completionHandler: (p1: NSURLRequest) => void): void {
|
||||
completionHandler(null);
|
||||
}
|
||||
}
|
||||
const sessionTaskDelegateInstance: NSURLSessionTaskDelegateImpl = <NSURLSessionTaskDelegateImpl>NSURLSessionTaskDelegateImpl.new();
|
||||
|
||||
let defaultSession;
|
||||
function ensureDefaultSession() {
|
||||
if (!defaultSession) {
|
||||
defaultSession = NSURLSession.sessionWithConfigurationDelegateDelegateQueue(sessionConfig, null, queue);
|
||||
}
|
||||
if (!defaultSession) {
|
||||
defaultSession = NSURLSession.sessionWithConfigurationDelegateDelegateQueue(sessionConfig, null, queue);
|
||||
}
|
||||
}
|
||||
|
||||
let sessionNotFollowingRedirects;
|
||||
function ensureSessionNotFollowingRedirects() {
|
||||
if (!sessionNotFollowingRedirects) {
|
||||
sessionNotFollowingRedirects = NSURLSession.sessionWithConfigurationDelegateDelegateQueue(sessionConfig, sessionTaskDelegateInstance, queue);
|
||||
}
|
||||
if (!sessionNotFollowingRedirects) {
|
||||
sessionNotFollowingRedirects = NSURLSession.sessionWithConfigurationDelegateDelegateQueue(sessionConfig, sessionTaskDelegateInstance, queue);
|
||||
}
|
||||
}
|
||||
|
||||
let imageSource: typeof imageSourceModule;
|
||||
function ensureImageSource() {
|
||||
if (!imageSource) {
|
||||
imageSource = require('../../image-source');
|
||||
}
|
||||
if (!imageSource) {
|
||||
imageSource = require('../../image-source');
|
||||
}
|
||||
}
|
||||
|
||||
let fs: typeof fsModule;
|
||||
function ensureFileSystem() {
|
||||
if (!fs) {
|
||||
fs = require('../../file-system');
|
||||
}
|
||||
if (!fs) {
|
||||
fs = require('../../file-system');
|
||||
}
|
||||
}
|
||||
|
||||
export function request(options: httpModule.HttpRequestOptions): Promise<httpModule.HttpResponse> {
|
||||
return new Promise<httpModule.HttpResponse>((resolve, reject) => {
|
||||
if (!options.url) {
|
||||
reject(new Error('Request url was empty.'));
|
||||
return new Promise<httpModule.HttpResponse>((resolve, reject) => {
|
||||
if (!options.url) {
|
||||
reject(new Error('Request url was empty.'));
|
||||
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const network = domainDebugger.getNetwork();
|
||||
const debugRequest = network && network.create();
|
||||
try {
|
||||
const network = domainDebugger.getNetwork();
|
||||
const debugRequest = network && network.create();
|
||||
|
||||
const urlRequest = NSMutableURLRequest.requestWithURL(NSURL.URLWithString(options.url));
|
||||
const urlRequest = NSMutableURLRequest.requestWithURL(NSURL.URLWithString(options.url));
|
||||
|
||||
urlRequest.HTTPMethod = types.isDefined(options.method) ? options.method : GET;
|
||||
urlRequest.HTTPMethod = types.isDefined(options.method) ? options.method : GET;
|
||||
|
||||
urlRequest.setValueForHTTPHeaderField(USER_AGENT, USER_AGENT_HEADER);
|
||||
urlRequest.setValueForHTTPHeaderField(USER_AGENT, USER_AGENT_HEADER);
|
||||
|
||||
if (options.headers) {
|
||||
for (let header in options.headers) {
|
||||
urlRequest.setValueForHTTPHeaderField(options.headers[header] + '', header);
|
||||
}
|
||||
}
|
||||
if (options.headers) {
|
||||
for (let header in options.headers) {
|
||||
urlRequest.setValueForHTTPHeaderField(options.headers[header] + '', header);
|
||||
}
|
||||
}
|
||||
|
||||
if (types.isString(options.content) || options.content instanceof FormData) {
|
||||
urlRequest.HTTPBody = NSString.stringWithString(options.content.toString()).dataUsingEncoding(4);
|
||||
} else if (options.content instanceof ArrayBuffer) {
|
||||
const buffer = options.content as ArrayBuffer;
|
||||
urlRequest.HTTPBody = NSData.dataWithData(buffer as any);
|
||||
}
|
||||
if (types.isString(options.content) || options.content instanceof FormData) {
|
||||
urlRequest.HTTPBody = NSString.stringWithString(options.content.toString()).dataUsingEncoding(4);
|
||||
} else if (options.content instanceof ArrayBuffer) {
|
||||
const buffer = options.content as ArrayBuffer;
|
||||
urlRequest.HTTPBody = NSData.dataWithData(buffer as any);
|
||||
}
|
||||
|
||||
if (types.isNumber(options.timeout)) {
|
||||
urlRequest.timeoutInterval = options.timeout / 1000;
|
||||
}
|
||||
if (types.isNumber(options.timeout)) {
|
||||
urlRequest.timeoutInterval = options.timeout / 1000;
|
||||
}
|
||||
|
||||
let session;
|
||||
if (types.isBoolean(options.dontFollowRedirects) && options.dontFollowRedirects) {
|
||||
ensureSessionNotFollowingRedirects();
|
||||
session = sessionNotFollowingRedirects;
|
||||
} else {
|
||||
ensureDefaultSession();
|
||||
session = defaultSession;
|
||||
}
|
||||
let session;
|
||||
if (types.isBoolean(options.dontFollowRedirects) && options.dontFollowRedirects) {
|
||||
ensureSessionNotFollowingRedirects();
|
||||
session = sessionNotFollowingRedirects;
|
||||
} else {
|
||||
ensureDefaultSession();
|
||||
session = defaultSession;
|
||||
}
|
||||
|
||||
const dataTask = session.dataTaskWithRequestCompletionHandler(urlRequest, function (data: NSData, response: NSHTTPURLResponse, error: NSError) {
|
||||
if (error) {
|
||||
reject(new Error(error.localizedDescription));
|
||||
} else {
|
||||
const headers: httpModule.Headers = {};
|
||||
if (response && response.allHeaderFields) {
|
||||
const headerFields = response.allHeaderFields;
|
||||
const dataTask = session.dataTaskWithRequestCompletionHandler(urlRequest, function (data: NSData, response: NSHTTPURLResponse, error: NSError) {
|
||||
if (error) {
|
||||
reject(new Error(error.localizedDescription));
|
||||
} else {
|
||||
const headers: httpModule.Headers = {};
|
||||
if (response && response.allHeaderFields) {
|
||||
const headerFields = response.allHeaderFields;
|
||||
|
||||
headerFields.enumerateKeysAndObjectsUsingBlock((key, value, stop) => {
|
||||
addHeader(headers, key, value);
|
||||
});
|
||||
}
|
||||
headerFields.enumerateKeysAndObjectsUsingBlock((key, value, stop) => {
|
||||
addHeader(headers, key, value);
|
||||
});
|
||||
}
|
||||
|
||||
if (debugRequest) {
|
||||
debugRequest.mimeType = response.MIMEType;
|
||||
debugRequest.data = data;
|
||||
const debugResponse = {
|
||||
url: options.url,
|
||||
status: response.statusCode,
|
||||
statusText: NSHTTPURLResponse.localizedStringForStatusCode(response.statusCode),
|
||||
headers: headers,
|
||||
mimeType: response.MIMEType,
|
||||
fromDiskCache: false,
|
||||
};
|
||||
debugRequest.responseReceived(debugResponse);
|
||||
debugRequest.loadingFinished();
|
||||
}
|
||||
if (debugRequest) {
|
||||
debugRequest.mimeType = response.MIMEType;
|
||||
debugRequest.data = data;
|
||||
const debugResponse = {
|
||||
url: options.url,
|
||||
status: response.statusCode,
|
||||
statusText: NSHTTPURLResponse.localizedStringForStatusCode(response.statusCode),
|
||||
headers: headers,
|
||||
mimeType: response.MIMEType,
|
||||
fromDiskCache: false,
|
||||
};
|
||||
debugRequest.responseReceived(debugResponse);
|
||||
debugRequest.loadingFinished();
|
||||
}
|
||||
|
||||
resolve({
|
||||
content: {
|
||||
raw: data,
|
||||
toArrayBuffer: () => interop.bufferFromData(data),
|
||||
toString: (encoding?: any) => {
|
||||
const str = NSDataToString(data, encoding);
|
||||
if (typeof str === 'string') {
|
||||
return str;
|
||||
} else {
|
||||
throw new Error('Response content may not be converted to string');
|
||||
}
|
||||
},
|
||||
toJSON: (encoding?: any) => parseJSON(NSDataToString(data, encoding)),
|
||||
toImage: () => {
|
||||
ensureImageSource();
|
||||
resolve({
|
||||
content: {
|
||||
raw: data,
|
||||
toArrayBuffer: () => interop.bufferFromData(data),
|
||||
toString: (encoding?: any) => {
|
||||
const str = NSDataToString(data, encoding);
|
||||
if (typeof str === 'string') {
|
||||
return str;
|
||||
} else {
|
||||
throw new Error('Response content may not be converted to string');
|
||||
}
|
||||
},
|
||||
toJSON: (encoding?: any) => parseJSON(NSDataToString(data, encoding)),
|
||||
toImage: () => {
|
||||
ensureImageSource();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
(<any>UIImage).tns_decodeImageWithDataCompletion(data, (image) => {
|
||||
if (image) {
|
||||
resolve(new imageSource.ImageSource(image));
|
||||
} else {
|
||||
reject(new Error('Response content may not be converted to an Image'));
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
toFile: (destinationFilePath?: string) => {
|
||||
ensureFileSystem();
|
||||
return new Promise((resolve, reject) => {
|
||||
(<any>UIImage).tns_decodeImageWithDataCompletion(data, (image) => {
|
||||
if (image) {
|
||||
resolve(new imageSource.ImageSource(image));
|
||||
} else {
|
||||
reject(new Error('Response content may not be converted to an Image'));
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
toFile: (destinationFilePath?: string) => {
|
||||
ensureFileSystem();
|
||||
|
||||
if (!destinationFilePath) {
|
||||
destinationFilePath = getFilenameFromUrl(options.url);
|
||||
}
|
||||
if (data instanceof NSData) {
|
||||
// ensure destination path exists by creating any missing parent directories
|
||||
const file = fs.File.fromPath(destinationFilePath);
|
||||
if (!destinationFilePath) {
|
||||
destinationFilePath = getFilenameFromUrl(options.url);
|
||||
}
|
||||
if (data instanceof NSData) {
|
||||
// ensure destination path exists by creating any missing parent directories
|
||||
const file = fs.File.fromPath(destinationFilePath);
|
||||
|
||||
data.writeToFileAtomically(destinationFilePath, true);
|
||||
data.writeToFileAtomically(destinationFilePath, true);
|
||||
|
||||
return file;
|
||||
} else {
|
||||
reject(new Error(`Cannot save file with path: ${destinationFilePath}.`));
|
||||
}
|
||||
},
|
||||
},
|
||||
statusCode: response.statusCode,
|
||||
headers: headers,
|
||||
});
|
||||
}
|
||||
});
|
||||
return file;
|
||||
} else {
|
||||
reject(new Error(`Cannot save file with path: ${destinationFilePath}.`));
|
||||
}
|
||||
},
|
||||
},
|
||||
statusCode: response.statusCode,
|
||||
headers: headers,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
if (options.url && debugRequest) {
|
||||
const request = {
|
||||
url: options.url,
|
||||
method: 'GET',
|
||||
headers: options.headers,
|
||||
};
|
||||
debugRequest.requestWillBeSent(request);
|
||||
}
|
||||
if (options.url && debugRequest) {
|
||||
const request = {
|
||||
url: options.url,
|
||||
method: 'GET',
|
||||
headers: options.headers,
|
||||
};
|
||||
debugRequest.requestWillBeSent(request);
|
||||
}
|
||||
|
||||
dataTask.resume();
|
||||
} catch (ex) {
|
||||
reject(ex);
|
||||
}
|
||||
});
|
||||
dataTask.resume();
|
||||
} catch (ex) {
|
||||
reject(ex);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function NSDataToString(data: any, encoding?: HttpResponseEncoding): string {
|
||||
let code = NSUTF8StringEncoding; // long:4
|
||||
let code = NSUTF8StringEncoding; // long:4
|
||||
|
||||
if (encoding === HttpResponseEncoding.GBK) {
|
||||
code = CFStringEncodings.kCFStringEncodingGB_18030_2000; // long:1586
|
||||
}
|
||||
if (encoding === HttpResponseEncoding.GBK) {
|
||||
code = CFStringEncodings.kCFStringEncodingGB_18030_2000; // long:1586
|
||||
}
|
||||
|
||||
let encodedString = NSString.alloc().initWithDataEncoding(data, code);
|
||||
let encodedString = NSString.alloc().initWithDataEncoding(data, code);
|
||||
|
||||
// If UTF8 string encoding fails try with ISO-8859-1
|
||||
if (!encodedString) {
|
||||
code = NSISOLatin1StringEncoding; // long:5
|
||||
encodedString = NSString.alloc().initWithDataEncoding(data, code);
|
||||
}
|
||||
// If UTF8 string encoding fails try with ISO-8859-1
|
||||
if (!encodedString) {
|
||||
code = NSISOLatin1StringEncoding; // long:5
|
||||
encodedString = NSString.alloc().initWithDataEncoding(data, code);
|
||||
}
|
||||
|
||||
return encodedString.toString();
|
||||
return encodedString.toString();
|
||||
}
|
||||
|
||||
export function addHeader(headers: httpModule.Headers, key: string, value: string): void {
|
||||
if (!headers[key]) {
|
||||
headers[key] = value;
|
||||
} else if (Array.isArray(headers[key])) {
|
||||
(<string[]>headers[key]).push(value);
|
||||
} else {
|
||||
const values: string[] = [<string>headers[key]];
|
||||
values.push(value);
|
||||
headers[key] = values;
|
||||
}
|
||||
if (!headers[key]) {
|
||||
headers[key] = value;
|
||||
} else if (Array.isArray(headers[key])) {
|
||||
(<string[]>headers[key]).push(value);
|
||||
} else {
|
||||
const values: string[] = [<string>headers[key]];
|
||||
values.push(value);
|
||||
headers[key] = values;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user