mirror of
https://github.com/silkimen/cordova-plugin-advanced-http.git
synced 2026-04-24 00:00:03 +08:00
add response type "arraybuffer" support for iOS
This commit is contained in:
@@ -28,6 +28,7 @@
|
|||||||
</feature>
|
</feature>
|
||||||
</config-file>
|
</config-file>
|
||||||
<header-file src="src/ios/CordovaHttpPlugin.h"/>
|
<header-file src="src/ios/CordovaHttpPlugin.h"/>
|
||||||
|
<header-file src="src/ios/BinaryResponseSerializer.h"/>
|
||||||
<header-file src="src/ios/TextResponseSerializer.h"/>
|
<header-file src="src/ios/TextResponseSerializer.h"/>
|
||||||
<header-file src="src/ios/TextRequestSerializer.h"/>
|
<header-file src="src/ios/TextRequestSerializer.h"/>
|
||||||
<header-file src="src/ios/AFNetworking/AFHTTPSessionManager.h"/>
|
<header-file src="src/ios/AFNetworking/AFHTTPSessionManager.h"/>
|
||||||
@@ -39,6 +40,7 @@
|
|||||||
<header-file src="src/ios/AFNetworking/AFURLSessionManager.h"/>
|
<header-file src="src/ios/AFNetworking/AFURLSessionManager.h"/>
|
||||||
<header-file src="src/ios/SDNetworkActivityIndicator/SDNetworkActivityIndicator.h"/>
|
<header-file src="src/ios/SDNetworkActivityIndicator/SDNetworkActivityIndicator.h"/>
|
||||||
<source-file src="src/ios/CordovaHttpPlugin.m"/>
|
<source-file src="src/ios/CordovaHttpPlugin.m"/>
|
||||||
|
<source-file src="src/ios/BinaryResponseSerializer.m"/>
|
||||||
<source-file src="src/ios/TextResponseSerializer.m"/>
|
<source-file src="src/ios/TextResponseSerializer.m"/>
|
||||||
<source-file src="src/ios/TextRequestSerializer.m"/>
|
<source-file src="src/ios/TextRequestSerializer.m"/>
|
||||||
<source-file src="src/ios/AFNetworking/AFHTTPSessionManager.m"/>
|
<source-file src="src/ios/AFNetworking/AFHTTPSessionManager.m"/>
|
||||||
|
|||||||
@@ -308,4 +308,11 @@ FOUNDATION_EXPORT NSString * const AFNetworkingOperationFailingURLResponseErrorK
|
|||||||
|
|
||||||
FOUNDATION_EXPORT NSString * const AFNetworkingOperationFailingURLResponseDataErrorKey;
|
FOUNDATION_EXPORT NSString * const AFNetworkingOperationFailingURLResponseDataErrorKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
`AFNetworkingOperationFailingURLResponseBodyErrorKey`
|
||||||
|
The corresponding value is an `NSString` containing the decoded error message.
|
||||||
|
*/
|
||||||
|
|
||||||
|
FOUNDATION_EXPORT NSString * const AFNetworkingOperationFailingURLResponseBodyErrorKey;
|
||||||
|
|
||||||
NS_ASSUME_NONNULL_END
|
NS_ASSUME_NONNULL_END
|
||||||
|
|||||||
@@ -34,6 +34,7 @@
|
|||||||
NSString * const AFURLResponseSerializationErrorDomain = @"com.alamofire.error.serialization.response";
|
NSString * const AFURLResponseSerializationErrorDomain = @"com.alamofire.error.serialization.response";
|
||||||
NSString * const AFNetworkingOperationFailingURLResponseErrorKey = @"com.alamofire.serialization.response.error.response";
|
NSString * const AFNetworkingOperationFailingURLResponseErrorKey = @"com.alamofire.serialization.response.error.response";
|
||||||
NSString * const AFNetworkingOperationFailingURLResponseDataErrorKey = @"com.alamofire.serialization.response.error.data";
|
NSString * const AFNetworkingOperationFailingURLResponseDataErrorKey = @"com.alamofire.serialization.response.error.data";
|
||||||
|
NSString * const AFNetworkingOperationFailingURLResponseBodyErrorKey = @"com.alamofire.serialization.response.error.body";
|
||||||
|
|
||||||
static NSError * AFErrorWithUnderlyingError(NSError *error, NSError *underlyingError) {
|
static NSError * AFErrorWithUnderlyingError(NSError *error, NSError *underlyingError) {
|
||||||
if (!error) {
|
if (!error) {
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#import <Foundation/Foundation.h>
|
||||||
|
#import "AFURLResponseSerialization.h"
|
||||||
|
|
||||||
|
@interface BinaryResponseSerializer : AFHTTPResponseSerializer
|
||||||
|
|
||||||
|
+ (instancetype)serializer;
|
||||||
|
|
||||||
|
@end
|
||||||
@@ -0,0 +1,126 @@
|
|||||||
|
#import "BinaryResponseSerializer.h"
|
||||||
|
|
||||||
|
static NSError * AFErrorWithUnderlyingError(NSError *error, NSError *underlyingError) {
|
||||||
|
if (!error) {
|
||||||
|
return underlyingError;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!underlyingError || error.userInfo[NSUnderlyingErrorKey]) {
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
NSMutableDictionary *mutableUserInfo = [error.userInfo mutableCopy];
|
||||||
|
mutableUserInfo[NSUnderlyingErrorKey] = underlyingError;
|
||||||
|
|
||||||
|
return [[NSError alloc] initWithDomain:error.domain code:error.code userInfo:mutableUserInfo];
|
||||||
|
}
|
||||||
|
|
||||||
|
static BOOL AFErrorOrUnderlyingErrorHasCodeInDomain(NSError *error, NSInteger code, NSString *domain) {
|
||||||
|
if ([error.domain isEqualToString:domain] && error.code == code) {
|
||||||
|
return YES;
|
||||||
|
} else if (error.userInfo[NSUnderlyingErrorKey]) {
|
||||||
|
return AFErrorOrUnderlyingErrorHasCodeInDomain(error.userInfo[NSUnderlyingErrorKey], code, domain);
|
||||||
|
}
|
||||||
|
|
||||||
|
return NO;
|
||||||
|
}
|
||||||
|
|
||||||
|
@implementation BinaryResponseSerializer
|
||||||
|
|
||||||
|
+ (instancetype)serializer {
|
||||||
|
BinaryResponseSerializer *serializer = [[self alloc] init];
|
||||||
|
return serializer;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (instancetype)init {
|
||||||
|
self = [super init];
|
||||||
|
|
||||||
|
if (!self) {
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.acceptableContentTypes = nil;
|
||||||
|
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSString*)decodeResponseData:(NSData*)rawResponseData withEncoding:(CFStringEncoding)cfEncoding {
|
||||||
|
NSStringEncoding nsEncoding;
|
||||||
|
NSString* decoded = nil;
|
||||||
|
|
||||||
|
if (cfEncoding != kCFStringEncodingInvalidId) {
|
||||||
|
nsEncoding = CFStringConvertEncodingToNSStringEncoding(cfEncoding);
|
||||||
|
}
|
||||||
|
|
||||||
|
NSStringEncoding supportedEncodings[6] = {
|
||||||
|
NSUTF8StringEncoding, NSWindowsCP1252StringEncoding, NSISOLatin1StringEncoding,
|
||||||
|
NSISOLatin2StringEncoding, NSASCIIStringEncoding, NSUnicodeStringEncoding
|
||||||
|
};
|
||||||
|
|
||||||
|
for (int i = 0; i < sizeof(supportedEncodings) / sizeof(NSStringEncoding) && !decoded; ++i) {
|
||||||
|
if (cfEncoding == kCFStringEncodingInvalidId || nsEncoding == supportedEncodings[i]) {
|
||||||
|
decoded = [[NSString alloc] initWithData:rawResponseData encoding:supportedEncodings[i]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return decoded;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (CFStringEncoding) getEncoding:(NSURLResponse *)response {
|
||||||
|
CFStringEncoding encoding = kCFStringEncodingInvalidId;
|
||||||
|
|
||||||
|
if (response.textEncodingName) {
|
||||||
|
encoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)response.textEncodingName);
|
||||||
|
}
|
||||||
|
|
||||||
|
return encoding;
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma mark -
|
||||||
|
|
||||||
|
- (BOOL)validateResponse:(NSHTTPURLResponse *)response
|
||||||
|
data:(NSData *)data
|
||||||
|
error:(NSError * __autoreleasing *)error
|
||||||
|
{
|
||||||
|
if (response && [response isKindOfClass:[NSHTTPURLResponse class]]) {
|
||||||
|
if (self.acceptableStatusCodes && ![self.acceptableStatusCodes containsIndex:(NSUInteger)response.statusCode] && [response URL]) {
|
||||||
|
NSMutableDictionary *mutableUserInfo = [@{
|
||||||
|
NSLocalizedDescriptionKey: [NSString stringWithFormat:NSLocalizedStringFromTable(@"Request failed: %@ (%ld)", @"AFNetworking", nil), [NSHTTPURLResponse localizedStringForStatusCode:response.statusCode], (long)response.statusCode],
|
||||||
|
NSURLErrorFailingURLErrorKey: [response URL],
|
||||||
|
AFNetworkingOperationFailingURLResponseErrorKey: response,
|
||||||
|
} mutableCopy];
|
||||||
|
|
||||||
|
if (data) {
|
||||||
|
mutableUserInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] = data;
|
||||||
|
|
||||||
|
// trying to decode error message in body
|
||||||
|
mutableUserInfo[AFNetworkingOperationFailingURLResponseBodyErrorKey] = [self decodeResponseData:data withEncoding:[self getEncoding:response]];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
*error = [NSError errorWithDomain:AFURLResponseSerializationErrorDomain code:NSURLErrorBadServerResponse userInfo:mutableUserInfo];
|
||||||
|
}
|
||||||
|
|
||||||
|
return NO;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return YES;
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma mark - AFURLResponseSerialization
|
||||||
|
|
||||||
|
- (id)responseObjectForResponse:(NSURLResponse *)response
|
||||||
|
data:(NSData *)data
|
||||||
|
error:(NSError *__autoreleasing *)error
|
||||||
|
{
|
||||||
|
if (![self validateResponse:(NSHTTPURLResponse *)response data:data error:error]) {
|
||||||
|
if (!error || AFErrorOrUnderlyingErrorHasCodeInDomain(*error, NSURLErrorCannotDecodeContentData, AFURLResponseSerializationErrorDomain)) {
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [data base64EncodedStringWithOptions:0];
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
+26
-10
@@ -1,5 +1,6 @@
|
|||||||
#import "CordovaHttpPlugin.h"
|
#import "CordovaHttpPlugin.h"
|
||||||
#import "CDVFile.h"
|
#import "CDVFile.h"
|
||||||
|
#import "BinaryResponseSerializer.h"
|
||||||
#import "TextResponseSerializer.h"
|
#import "TextResponseSerializer.h"
|
||||||
#import "TextRequestSerializer.h"
|
#import "TextRequestSerializer.h"
|
||||||
#import "AFHTTPSessionManager.h"
|
#import "AFHTTPSessionManager.h"
|
||||||
@@ -57,6 +58,15 @@
|
|||||||
[manager.requestSerializer setTimeoutInterval:timeout];
|
[manager.requestSerializer setTimeoutInterval:timeout];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)setResponseSerializer:(NSString*)responseType forManager:(AFHTTPSessionManager*)manager {
|
||||||
|
if ([responseType isEqualToString: @"text"]) {
|
||||||
|
manager.responseSerializer = [TextResponseSerializer serializer];
|
||||||
|
} else {
|
||||||
|
manager.responseSerializer = [BinaryResponseSerializer serializer];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
- (void)handleSuccess:(NSMutableDictionary*)dictionary withResponse:(NSHTTPURLResponse*)response andData:(id)data {
|
- (void)handleSuccess:(NSMutableDictionary*)dictionary withResponse:(NSHTTPURLResponse*)response andData:(id)data {
|
||||||
if (response != nil) {
|
if (response != nil) {
|
||||||
[dictionary setValue:response.URL.absoluteString forKey:@"url"];
|
[dictionary setValue:response.URL.absoluteString forKey:@"url"];
|
||||||
@@ -74,8 +84,8 @@
|
|||||||
[dictionary setValue:response.URL.absoluteString forKey:@"url"];
|
[dictionary setValue:response.URL.absoluteString forKey:@"url"];
|
||||||
[dictionary setObject:[NSNumber numberWithInt:(int)response.statusCode] forKey:@"status"];
|
[dictionary setObject:[NSNumber numberWithInt:(int)response.statusCode] forKey:@"status"];
|
||||||
[dictionary setObject:[self copyHeaderFields:response.allHeaderFields] forKey:@"headers"];
|
[dictionary setObject:[self copyHeaderFields:response.allHeaderFields] forKey:@"headers"];
|
||||||
if (error.userInfo[AFNetworkingOperationFailingURLResponseBodyKey]) {
|
if (error.userInfo[AFNetworkingOperationFailingURLResponseBodyErrorKey]) {
|
||||||
[dictionary setObject:error.userInfo[AFNetworkingOperationFailingURLResponseBodyKey] forKey:@"error"];
|
[dictionary setObject:error.userInfo[AFNetworkingOperationFailingURLResponseBodyErrorKey] forKey:@"error"];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
[dictionary setObject:[self getStatusCode:error] forKey:@"status"];
|
[dictionary setObject:[self getStatusCode:error] forKey:@"status"];
|
||||||
@@ -161,14 +171,15 @@
|
|||||||
NSDictionary *headers = [command.arguments objectAtIndex:1];
|
NSDictionary *headers = [command.arguments objectAtIndex:1];
|
||||||
NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:2] doubleValue];
|
NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:2] doubleValue];
|
||||||
bool followRedirect = [[command.arguments objectAtIndex:3] boolValue];
|
bool followRedirect = [[command.arguments objectAtIndex:3] boolValue];
|
||||||
|
NSString *responseType = [command.arguments objectAtIndex:4];
|
||||||
|
|
||||||
[self setRequestSerializer: @"default" forManager: manager];
|
[self setRequestSerializer: @"default" forManager: manager];
|
||||||
[self setRequestHeaders: headers forManager: manager];
|
[self setRequestHeaders: headers forManager: manager];
|
||||||
[self setTimeout:timeoutInSeconds forManager:manager];
|
[self setTimeout:timeoutInSeconds forManager:manager];
|
||||||
[self setRedirect:followRedirect forManager:manager];
|
[self setRedirect:followRedirect forManager:manager];
|
||||||
|
[self setResponseSerializer:responseType forManager:manager];
|
||||||
|
|
||||||
CordovaHttpPlugin* __weak weakSelf = self;
|
CordovaHttpPlugin* __weak weakSelf = self;
|
||||||
manager.responseSerializer = [TextResponseSerializer serializer];
|
|
||||||
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
|
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
|
||||||
|
|
||||||
@try {
|
@try {
|
||||||
@@ -197,6 +208,7 @@
|
|||||||
- (void)head:(CDVInvokedUrlCommand*)command {
|
- (void)head:(CDVInvokedUrlCommand*)command {
|
||||||
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
|
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
|
||||||
manager.securityPolicy = securityPolicy;
|
manager.securityPolicy = securityPolicy;
|
||||||
|
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
|
||||||
|
|
||||||
NSString *url = [command.arguments objectAtIndex:0];
|
NSString *url = [command.arguments objectAtIndex:0];
|
||||||
NSDictionary *headers = [command.arguments objectAtIndex:1];
|
NSDictionary *headers = [command.arguments objectAtIndex:1];
|
||||||
@@ -208,7 +220,6 @@
|
|||||||
[self setRedirect:followRedirect forManager:manager];
|
[self setRedirect:followRedirect forManager:manager];
|
||||||
|
|
||||||
CordovaHttpPlugin* __weak weakSelf = self;
|
CordovaHttpPlugin* __weak weakSelf = self;
|
||||||
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
|
|
||||||
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
|
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
|
||||||
|
|
||||||
@try {
|
@try {
|
||||||
@@ -243,14 +254,15 @@
|
|||||||
NSDictionary *headers = [command.arguments objectAtIndex:1];
|
NSDictionary *headers = [command.arguments objectAtIndex:1];
|
||||||
NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:2] doubleValue];
|
NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:2] doubleValue];
|
||||||
bool followRedirect = [[command.arguments objectAtIndex:3] boolValue];
|
bool followRedirect = [[command.arguments objectAtIndex:3] boolValue];
|
||||||
|
NSString *responseType = [command.arguments objectAtIndex:4];
|
||||||
|
|
||||||
[self setRequestSerializer: @"default" forManager: manager];
|
[self setRequestSerializer: @"default" forManager: manager];
|
||||||
[self setRequestHeaders: headers forManager: manager];
|
[self setRequestHeaders: headers forManager: manager];
|
||||||
[self setTimeout:timeoutInSeconds forManager:manager];
|
[self setTimeout:timeoutInSeconds forManager:manager];
|
||||||
[self setRedirect:followRedirect forManager:manager];
|
[self setRedirect:followRedirect forManager:manager];
|
||||||
|
[self setResponseSerializer:responseType forManager:manager];
|
||||||
|
|
||||||
CordovaHttpPlugin* __weak weakSelf = self;
|
CordovaHttpPlugin* __weak weakSelf = self;
|
||||||
manager.responseSerializer = [TextResponseSerializer serializer];
|
|
||||||
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
|
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
|
||||||
|
|
||||||
@try {
|
@try {
|
||||||
@@ -286,14 +298,15 @@
|
|||||||
NSDictionary *headers = [command.arguments objectAtIndex:3];
|
NSDictionary *headers = [command.arguments objectAtIndex:3];
|
||||||
NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:4] doubleValue];
|
NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:4] doubleValue];
|
||||||
bool followRedirect = [[command.arguments objectAtIndex:5] boolValue];
|
bool followRedirect = [[command.arguments objectAtIndex:5] boolValue];
|
||||||
|
NSString *responseType = [command.arguments objectAtIndex:6];
|
||||||
|
|
||||||
[self setRequestSerializer: serializerName forManager: manager];
|
[self setRequestSerializer: serializerName forManager: manager];
|
||||||
[self setRequestHeaders: headers forManager: manager];
|
[self setRequestHeaders: headers forManager: manager];
|
||||||
[self setTimeout:timeoutInSeconds forManager:manager];
|
[self setTimeout:timeoutInSeconds forManager:manager];
|
||||||
[self setRedirect:followRedirect forManager:manager];
|
[self setRedirect:followRedirect forManager:manager];
|
||||||
|
[self setResponseSerializer:responseType forManager:manager];
|
||||||
|
|
||||||
CordovaHttpPlugin* __weak weakSelf = self;
|
CordovaHttpPlugin* __weak weakSelf = self;
|
||||||
manager.responseSerializer = [TextResponseSerializer serializer];
|
|
||||||
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
|
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
|
||||||
|
|
||||||
@try {
|
@try {
|
||||||
@@ -329,14 +342,15 @@
|
|||||||
NSDictionary *headers = [command.arguments objectAtIndex:3];
|
NSDictionary *headers = [command.arguments objectAtIndex:3];
|
||||||
NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:4] doubleValue];
|
NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:4] doubleValue];
|
||||||
bool followRedirect = [[command.arguments objectAtIndex:5] boolValue];
|
bool followRedirect = [[command.arguments objectAtIndex:5] boolValue];
|
||||||
|
NSString *responseType = [command.arguments objectAtIndex:6];
|
||||||
|
|
||||||
[self setRequestSerializer: serializerName forManager: manager];
|
[self setRequestSerializer: serializerName forManager: manager];
|
||||||
[self setRequestHeaders: headers forManager: manager];
|
[self setRequestHeaders: headers forManager: manager];
|
||||||
[self setTimeout:timeoutInSeconds forManager:manager];
|
[self setTimeout:timeoutInSeconds forManager:manager];
|
||||||
[self setRedirect:followRedirect forManager:manager];
|
[self setRedirect:followRedirect forManager:manager];
|
||||||
|
[self setResponseSerializer:responseType forManager:manager];
|
||||||
|
|
||||||
CordovaHttpPlugin* __weak weakSelf = self;
|
CordovaHttpPlugin* __weak weakSelf = self;
|
||||||
manager.responseSerializer = [TextResponseSerializer serializer];
|
|
||||||
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
|
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
|
||||||
|
|
||||||
@try {
|
@try {
|
||||||
@@ -372,14 +386,15 @@
|
|||||||
NSDictionary *headers = [command.arguments objectAtIndex:3];
|
NSDictionary *headers = [command.arguments objectAtIndex:3];
|
||||||
NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:4] doubleValue];
|
NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:4] doubleValue];
|
||||||
bool followRedirect = [[command.arguments objectAtIndex:5] boolValue];
|
bool followRedirect = [[command.arguments objectAtIndex:5] boolValue];
|
||||||
|
NSString *responseType = [command.arguments objectAtIndex:6];
|
||||||
|
|
||||||
[self setRequestSerializer: serializerName forManager: manager];
|
[self setRequestSerializer: serializerName forManager: manager];
|
||||||
[self setRequestHeaders: headers forManager: manager];
|
[self setRequestHeaders: headers forManager: manager];
|
||||||
[self setTimeout:timeoutInSeconds forManager:manager];
|
[self setTimeout:timeoutInSeconds forManager:manager];
|
||||||
[self setRedirect:followRedirect forManager:manager];
|
[self setRedirect:followRedirect forManager:manager];
|
||||||
|
[self setResponseSerializer:responseType forManager:manager];
|
||||||
|
|
||||||
CordovaHttpPlugin* __weak weakSelf = self;
|
CordovaHttpPlugin* __weak weakSelf = self;
|
||||||
manager.responseSerializer = [TextResponseSerializer serializer];
|
|
||||||
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
|
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
|
||||||
|
|
||||||
@try {
|
@try {
|
||||||
@@ -415,15 +430,16 @@
|
|||||||
NSString *name = [command.arguments objectAtIndex: 3];
|
NSString *name = [command.arguments objectAtIndex: 3];
|
||||||
NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:4] doubleValue];
|
NSTimeInterval timeoutInSeconds = [[command.arguments objectAtIndex:4] doubleValue];
|
||||||
bool followRedirect = [[command.arguments objectAtIndex:5] boolValue];
|
bool followRedirect = [[command.arguments objectAtIndex:5] boolValue];
|
||||||
|
NSString *responseType = [command.arguments objectAtIndex:6];
|
||||||
|
|
||||||
NSURL *fileURL = [NSURL URLWithString: filePath];
|
NSURL *fileURL = [NSURL URLWithString: filePath];
|
||||||
|
|
||||||
[self setRequestHeaders: headers forManager: manager];
|
[self setRequestHeaders: headers forManager: manager];
|
||||||
[self setTimeout:timeoutInSeconds forManager:manager];
|
[self setTimeout:timeoutInSeconds forManager:manager];
|
||||||
[self setRedirect:followRedirect forManager:manager];
|
[self setRedirect:followRedirect forManager:manager];
|
||||||
|
[self setResponseSerializer:responseType forManager:manager];
|
||||||
|
|
||||||
CordovaHttpPlugin* __weak weakSelf = self;
|
CordovaHttpPlugin* __weak weakSelf = self;
|
||||||
manager.responseSerializer = [TextResponseSerializer serializer];
|
|
||||||
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
|
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
|
||||||
|
|
||||||
@try {
|
@try {
|
||||||
@@ -464,6 +480,7 @@
|
|||||||
- (void)downloadFile:(CDVInvokedUrlCommand*)command {
|
- (void)downloadFile:(CDVInvokedUrlCommand*)command {
|
||||||
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
|
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
|
||||||
manager.securityPolicy = securityPolicy;
|
manager.securityPolicy = securityPolicy;
|
||||||
|
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
|
||||||
|
|
||||||
NSString *url = [command.arguments objectAtIndex:0];
|
NSString *url = [command.arguments objectAtIndex:0];
|
||||||
NSDictionary *headers = [command.arguments objectAtIndex:1];
|
NSDictionary *headers = [command.arguments objectAtIndex:1];
|
||||||
@@ -480,7 +497,6 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
CordovaHttpPlugin* __weak weakSelf = self;
|
CordovaHttpPlugin* __weak weakSelf = self;
|
||||||
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
|
|
||||||
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
|
[[SDNetworkActivityIndicator sharedActivityIndicator] startActivity];
|
||||||
|
|
||||||
@try {
|
@try {
|
||||||
|
|||||||
@@ -5,6 +5,4 @@
|
|||||||
|
|
||||||
+ (instancetype)serializer;
|
+ (instancetype)serializer;
|
||||||
|
|
||||||
FOUNDATION_EXPORT NSString * const AFNetworkingOperationFailingURLResponseBodyKey;
|
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
@@ -1,8 +1,5 @@
|
|||||||
#import "TextResponseSerializer.h"
|
#import "TextResponseSerializer.h"
|
||||||
|
|
||||||
NSString * const AFNetworkingOperationFailingURLResponseBodyKey = @"com.alamofire.serialization.response.error.body";
|
|
||||||
NSStringEncoding const SupportedEncodings[6] = { NSUTF8StringEncoding, NSWindowsCP1252StringEncoding, NSISOLatin1StringEncoding, NSISOLatin2StringEncoding, NSASCIIStringEncoding, NSUnicodeStringEncoding };
|
|
||||||
|
|
||||||
static NSError * AFErrorWithUnderlyingError(NSError *error, NSError *underlyingError) {
|
static NSError * AFErrorWithUnderlyingError(NSError *error, NSError *underlyingError) {
|
||||||
if (!error) {
|
if (!error) {
|
||||||
return underlyingError;
|
return underlyingError;
|
||||||
@@ -55,9 +52,14 @@ static BOOL AFErrorOrUnderlyingErrorHasCodeInDomain(NSError *error, NSInteger co
|
|||||||
nsEncoding = CFStringConvertEncodingToNSStringEncoding(cfEncoding);
|
nsEncoding = CFStringConvertEncodingToNSStringEncoding(cfEncoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < sizeof(SupportedEncodings) / sizeof(NSStringEncoding) && !decoded; ++i) {
|
NSStringEncoding supportedEncodings[6] = {
|
||||||
if (cfEncoding == kCFStringEncodingInvalidId || nsEncoding == SupportedEncodings[i]) {
|
NSUTF8StringEncoding, NSWindowsCP1252StringEncoding, NSISOLatin1StringEncoding,
|
||||||
decoded = [[NSString alloc] initWithData:rawResponseData encoding:SupportedEncodings[i]];
|
NSISOLatin2StringEncoding, NSASCIIStringEncoding, NSUnicodeStringEncoding
|
||||||
|
};
|
||||||
|
|
||||||
|
for (int i = 0; i < sizeof(supportedEncodings) / sizeof(NSStringEncoding) && !decoded; ++i) {
|
||||||
|
if (cfEncoding == kCFStringEncodingInvalidId || nsEncoding == supportedEncodings[i]) {
|
||||||
|
decoded = [[NSString alloc] initWithData:rawResponseData encoding:supportedEncodings[i]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,7 +96,7 @@ static BOOL AFErrorOrUnderlyingErrorHasCodeInDomain(NSError *error, NSInteger co
|
|||||||
NSURLErrorFailingURLErrorKey:[response URL],
|
NSURLErrorFailingURLErrorKey:[response URL],
|
||||||
AFNetworkingOperationFailingURLResponseErrorKey: response,
|
AFNetworkingOperationFailingURLResponseErrorKey: response,
|
||||||
AFNetworkingOperationFailingURLResponseDataErrorKey: data,
|
AFNetworkingOperationFailingURLResponseDataErrorKey: data,
|
||||||
AFNetworkingOperationFailingURLResponseBodyKey: @"Could not decode response data due to invalid or unknown charset encoding",
|
AFNetworkingOperationFailingURLResponseBodyErrorKey: @"Could not decode response data due to invalid or unknown charset encoding",
|
||||||
} mutableCopy];
|
} mutableCopy];
|
||||||
|
|
||||||
validationError = AFErrorWithUnderlyingError([NSError errorWithDomain:AFURLResponseSerializationErrorDomain code:NSURLErrorBadServerResponse userInfo:mutableUserInfo], validationError);
|
validationError = AFErrorWithUnderlyingError([NSError errorWithDomain:AFURLResponseSerializationErrorDomain code:NSURLErrorBadServerResponse userInfo:mutableUserInfo], validationError);
|
||||||
@@ -108,7 +110,7 @@ static BOOL AFErrorOrUnderlyingErrorHasCodeInDomain(NSError *error, NSInteger co
|
|||||||
|
|
||||||
if (data) {
|
if (data) {
|
||||||
mutableUserInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] = data;
|
mutableUserInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] = data;
|
||||||
mutableUserInfo[AFNetworkingOperationFailingURLResponseBodyKey] = *decoded;
|
mutableUserInfo[AFNetworkingOperationFailingURLResponseBodyErrorKey] = *decoded;
|
||||||
}
|
}
|
||||||
|
|
||||||
validationError = AFErrorWithUnderlyingError([NSError errorWithDomain:AFURLResponseSerializationErrorDomain code:NSURLErrorBadServerResponse userInfo:mutableUserInfo], validationError);
|
validationError = AFErrorWithUnderlyingError([NSError errorWithDomain:AFURLResponseSerializationErrorDomain code:NSURLErrorBadServerResponse userInfo:mutableUserInfo], validationError);
|
||||||
|
|||||||
+15
-1
@@ -642,7 +642,7 @@ const tests = [
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: 'should fetch binary correctly when response type "arraybuffer" is given',
|
description: 'should fetch binary correctly when response type is "arraybuffer"',
|
||||||
expected: 'resolved: {"hash":-1032603775,"byteLength":35588}',
|
expected: 'resolved: {"hash":-1032603775,"byteLength":35588}',
|
||||||
func: function (resolve, reject) {
|
func: function (resolve, reject) {
|
||||||
var url = 'https://httpbin.org/image/jpeg';
|
var url = 'https://httpbin.org/image/jpeg';
|
||||||
@@ -660,6 +660,20 @@ const tests = [
|
|||||||
result.data.hash.should.be.equal(-1032603775);
|
result.data.hash.should.be.equal(-1032603775);
|
||||||
result.data.byteLength.should.be.equal(35588);
|
result.data.byteLength.should.be.equal(35588);
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: 'should decode error body even if response type is "arraybuffer"',
|
||||||
|
expected: 'rejected: {"status": 418, ...',
|
||||||
|
func: function (resolve, reject) {
|
||||||
|
var url = 'https://httpbin.org/status/418';
|
||||||
|
var options = { method: 'get', responseType: 'arraybuffer' };
|
||||||
|
cordova.plugin.http.sendRequest(url, options, resolve, reject);
|
||||||
|
},
|
||||||
|
validationFunc: function (driver, result) {
|
||||||
|
result.type.should.be.equal('rejected');
|
||||||
|
result.data.status.should.be.equal(418);
|
||||||
|
result.data.error.should.be.equal("\n -=[ teapot ]=-\n\n _...._\n .' _ _ `.\n | .\"` ^ `\". _,\n \\_;`\"---\"`|//\n | ;/\n \\_ _/\n `\"\"\"`\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// @TODO: not ready yet
|
// @TODO: not ready yet
|
||||||
// {
|
// {
|
||||||
|
|||||||
Reference in New Issue
Block a user