diff --git a/plugin.xml b/plugin.xml index c8ef95f..9a976ef 100644 --- a/plugin.xml +++ b/plugin.xml @@ -24,6 +24,7 @@ + @@ -33,6 +34,7 @@ + @@ -64,4 +66,4 @@ - \ No newline at end of file + diff --git a/src/android/com/synconset/cordovahttp/CordovaHttp.java b/src/android/com/synconset/cordovahttp/CordovaHttp.java index 20c0a72..d11e72e 100644 --- a/src/android/com/synconset/cordovahttp/CordovaHttp.java +++ b/src/android/com/synconset/cordovahttp/CordovaHttp.java @@ -144,8 +144,7 @@ abstract class CordovaHttp { if (new String("json").equals(this.getSerializerName())) { request.contentType(request.CONTENT_TYPE_JSON, request.CHARSET_UTF8); request.send(this.getParamsObject().toString()); - } else if (new String("raw").equals(this.getSerializerName())) { - // request.contentType(request.CONTENT_TYPE_JSON, request.CHARSET_UTF8); + } else if (new String("utf8").equals(this.getSerializerName())) { request.send(this.getParamsObject().toString()); } else { diff --git a/src/ios/AFNetworking/AFURLRequestSerialization.h b/src/ios/AFNetworking/AFURLRequestSerialization.h index 8548a3e..694696b 100644 --- a/src/ios/AFNetworking/AFURLRequestSerialization.h +++ b/src/ios/AFNetworking/AFURLRequestSerialization.h @@ -375,19 +375,6 @@ forHTTPHeaderField:(NSString *)field; @end - -#pragma mark - - -/** - `AFRAWRequestSerializer` is a subclass of `AFHTTPRequestSerializer` - */ -@interface AFRAWRequestSerializer : AFHTTPRequestSerializer - - -+ (instancetype)serializer; - -@end - #pragma mark - /** diff --git a/src/ios/AFNetworking/AFURLRequestSerialization.m b/src/ios/AFNetworking/AFURLRequestSerialization.m index 036b80f..a47e2e6 100644 --- a/src/ios/AFNetworking/AFURLRequestSerialization.m +++ b/src/ios/AFNetworking/AFURLRequestSerialization.m @@ -1195,62 +1195,6 @@ typedef enum { @end - -#pragma mark - - -@implementation AFRAWRequestSerializer - -+ (instancetype)serializer -{ - AFRAWRequestSerializer *serializer = [[self alloc] init]; - return serializer; -} - -#pragma mark - AFURLRequestSerialization - -- (NSURLRequest *)requestBySerializingRequest:(NSURLRequest *)request - withParameters:(id)parameters - error:(NSError *__autoreleasing *)error -{ - NSParameterAssert(request); - - if ([self.HTTPMethodsEncodingParametersInURI containsObject:[[request HTTPMethod] uppercaseString]]) { - return [super requestBySerializingRequest:request withParameters:parameters error:error]; - } - - NSMutableURLRequest *mutableRequest = [request mutableCopy]; - - [self.HTTPRequestHeaders enumerateKeysAndObjectsUsingBlock:^(id field, id value, BOOL * __unused stop) { - if (![request valueForHTTPHeaderField:field]) { - [mutableRequest setValue:value forHTTPHeaderField:field]; - } - }]; - - if (parameters) { -// if (![mutableRequest valueForHTTPHeaderField:@"Content-Type"]) { -// [mutableRequest setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"]; -// } - - [mutableRequest setHTTPBody: [parameters dataUsingEncoding:NSUTF8StringEncoding]]; - } - - return mutableRequest; -} - -#pragma mark - NSSecureCoding - -- (instancetype)initWithCoder:(NSCoder *)decoder { - self = [super initWithCoder:decoder]; - if (!self) { - return nil; - } - - return self; -} - -@end - - #pragma mark - @implementation AFJSONRequestSerializer diff --git a/src/ios/CordovaHttpPlugin.m b/src/ios/CordovaHttpPlugin.m index 8fafed0..6f34c8b 100644 --- a/src/ios/CordovaHttpPlugin.m +++ b/src/ios/CordovaHttpPlugin.m @@ -1,6 +1,7 @@ #import "CordovaHttpPlugin.h" #import "CDVFile.h" #import "TextResponseSerializer.h" +#import "TextRequestSerializer.h" #import "AFHTTPSessionManager.h" @interface CordovaHttpPlugin() @@ -28,11 +29,9 @@ - (void)setRequestSerializer:(NSString*)serializerName forManager:(AFHTTPSessionManager*)manager { if ([serializerName isEqualToString:@"json"]) { manager.requestSerializer = [AFJSONRequestSerializer serializer]; - } else - if ([serializerName isEqualToString:@"raw"]) { - manager.requestSerializer = [AFRAWRequestSerializer serializer]; - } else - { + } else if ([serializerName isEqualToString:@"utf8"]) { + manager.requestSerializer = [TextRequestSerializer serializer]; + } else { manager.requestSerializer = [AFHTTPRequestSerializer serializer]; } } @@ -77,6 +76,17 @@ } } +- (void)handleException:(NSException*)exception withCommand:(CDVInvokedUrlCommand*)command { + CordovaHttpPlugin* __weak weakSelf = self; + + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + [dictionary setValue:exception.userInfo forKey:@"error"]; + [dictionary setObject:[NSNumber numberWithInt:-1] forKey:@"status"]; + + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; +} + - (NSNumber*)getStatusCode:(NSError*) error { switch ([error code]) { case -1001: @@ -163,19 +173,25 @@ CordovaHttpPlugin* __weak weakSelf = self; manager.responseSerializer = [TextResponseSerializer serializer]; - [manager POST:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) { - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:responseObject]; - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; - [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } failure:^(NSURLSessionTask *task, NSError *error) { - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error]; + @try { + [manager POST:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + [self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:responseObject]; - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; - [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - }]; + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } failure:^(NSURLSessionTask *task, NSError *error) { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + [self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error]; + + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; + } + @catch (NSException *exception) { + [self handleException:exception withCommand:command]; + } } - (void)get:(CDVInvokedUrlCommand*)command { @@ -194,21 +210,26 @@ [self setRedirect: manager]; CordovaHttpPlugin* __weak weakSelf = self; - manager.responseSerializer = [TextResponseSerializer serializer]; - [manager GET:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) { - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:responseObject]; - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; - [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } failure:^(NSURLSessionTask *task, NSError *error) { - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error]; + @try { + [manager GET:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + [self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:responseObject]; - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; - [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - }]; + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } failure:^(NSURLSessionTask *task, NSError *error) { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + [self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error]; + + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; + } + @catch (NSException *exception) { + [self handleException:exception withCommand:command]; + } } - (void)put:(CDVInvokedUrlCommand*)command { @@ -228,19 +249,25 @@ CordovaHttpPlugin* __weak weakSelf = self; manager.responseSerializer = [TextResponseSerializer serializer]; - [manager PUT:url parameters:parameters success:^(NSURLSessionTask *task, id responseObject) { - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:responseObject]; - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; - [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } failure:^(NSURLSessionTask *task, NSError *error) { - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error]; + @try { + [manager PUT:url parameters:parameters success:^(NSURLSessionTask *task, id responseObject) { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + [self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:responseObject]; - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; - [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - }]; + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } failure:^(NSURLSessionTask *task, NSError *error) { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + [self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error]; + + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; + } + @catch (NSException *exception) { + [self handleException:exception withCommand:command]; + } } - (void)patch:(CDVInvokedUrlCommand*)command { @@ -260,19 +287,25 @@ CordovaHttpPlugin* __weak weakSelf = self; manager.responseSerializer = [TextResponseSerializer serializer]; - [manager PATCH:url parameters:parameters success:^(NSURLSessionTask *task, id responseObject) { - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:responseObject]; - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; - [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } failure:^(NSURLSessionTask *task, NSError *error) { - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error]; + @try { + [manager PATCH:url parameters:parameters success:^(NSURLSessionTask *task, id responseObject) { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + [self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:responseObject]; - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; - [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - }]; + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } failure:^(NSURLSessionTask *task, NSError *error) { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + [self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error]; + + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; + } + @catch (NSException *exception) { + [self handleException:exception withCommand:command]; + } } - (void)delete:(CDVInvokedUrlCommand*)command { @@ -290,21 +323,26 @@ [self setRedirect: manager]; CordovaHttpPlugin* __weak weakSelf = self; - manager.responseSerializer = [TextResponseSerializer serializer]; - [manager DELETE:url parameters:parameters success:^(NSURLSessionTask *task, id responseObject) { - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:responseObject]; - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; - [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } failure:^(NSURLSessionTask *task, NSError *error) { - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error]; + @try { + [manager DELETE:url parameters:parameters success:^(NSURLSessionTask *task, id responseObject) { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + [self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:responseObject]; - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; - [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - }]; + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } failure:^(NSURLSessionTask *task, NSError *error) { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + [self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error]; + + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; + } + @catch (NSException *exception) { + [self handleException:exception withCommand:command]; + } } - (void)head:(CDVInvokedUrlCommand*)command { @@ -320,22 +358,27 @@ [self setRedirect: manager]; CordovaHttpPlugin* __weak weakSelf = self; - manager.responseSerializer = [AFHTTPResponseSerializer serializer]; - [manager HEAD:url parameters:parameters success:^(NSURLSessionTask *task) { - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - // no 'body' for HEAD request, omitting 'data' - [self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:nil]; - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; - [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } failure:^(NSURLSessionTask *task, NSError *error) { - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error]; + @try { + [manager HEAD:url parameters:parameters success:^(NSURLSessionTask *task) { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + // no 'body' for HEAD request, omitting 'data' + [self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:nil]; - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; - [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - }]; + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } failure:^(NSURLSessionTask *task, NSError *error) { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + [self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error]; + + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; + } + @catch (NSException *exception) { + [self handleException:exception withCommand:command]; + } } - (void)uploadFile:(CDVInvokedUrlCommand*)command { @@ -357,30 +400,36 @@ CordovaHttpPlugin* __weak weakSelf = self; manager.responseSerializer = [TextResponseSerializer serializer]; - [manager POST:url parameters:parameters constructingBodyWithBlock:^(id formData) { - NSError *error; - [formData appendPartWithFileURL:fileURL name:name error:&error]; - if (error) { + + @try { + [manager POST:url parameters:parameters constructingBodyWithBlock:^(id formData) { + NSError *error; + [formData appendPartWithFileURL:fileURL name:name error:&error]; + if (error) { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + [dictionary setObject:[NSNumber numberWithInt:500] forKey:@"status"]; + [dictionary setObject:@"Could not add file to post body." forKey:@"error"]; + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + return; + } + } progress:nil success:^(NSURLSessionTask *task, id responseObject) { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [dictionary setObject:[NSNumber numberWithInt:500] forKey:@"status"]; - [dictionary setObject:@"Could not add file to post body." forKey:@"error"]; + [self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:responseObject]; + + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } failure:^(NSURLSessionTask *task, NSError *error) { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + [self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error]; + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - return; - } - } progress:nil success:^(NSURLSessionTask *task, id responseObject) { - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:responseObject]; - - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; - [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } failure:^(NSURLSessionTask *task, NSError *error) { - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error]; - - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; - [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - }]; + }]; + } + @catch (NSException *exception) { + [self handleException:exception withCommand:command]; + } } @@ -404,70 +453,76 @@ CordovaHttpPlugin* __weak weakSelf = self; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; - [manager GET:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) { - /* - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - * Modified by Andrew Stephan for Sync OnSet - * - */ - // Download response is okay; begin streaming output to file - NSString* parentPath = [filePath stringByDeletingLastPathComponent]; - // create parent directories if needed - NSError *error; - if ([[NSFileManager defaultManager] createDirectoryAtPath:parentPath withIntermediateDirectories:YES attributes:nil error:&error] == NO) { - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [dictionary setObject:[NSNumber numberWithInt:500] forKey:@"status"]; - if (error) { - [dictionary setObject:[NSString stringWithFormat:@"Could not create path to save downloaded file: %@", [error localizedDescription]] forKey:@"error"]; - } else { - [dictionary setObject:@"Could not create path to save downloaded file" forKey:@"error"]; + @try { + [manager GET:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) { + /* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * Modified by Andrew Stephan for Sync OnSet + * + */ + // Download response is okay; begin streaming output to file + NSString* parentPath = [filePath stringByDeletingLastPathComponent]; + + // create parent directories if needed + NSError *error; + if ([[NSFileManager defaultManager] createDirectoryAtPath:parentPath withIntermediateDirectories:YES attributes:nil error:&error] == NO) { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + [dictionary setObject:[NSNumber numberWithInt:500] forKey:@"status"]; + if (error) { + [dictionary setObject:[NSString stringWithFormat:@"Could not create path to save downloaded file: %@", [error localizedDescription]] forKey:@"error"]; + } else { + [dictionary setObject:@"Could not create path to save downloaded file" forKey:@"error"]; + } + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + return; } - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; - [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - return; - } - NSData *data = (NSData *)responseObject; - if (![data writeToFile:filePath atomically:YES]) { + NSData *data = (NSData *)responseObject; + if (![data writeToFile:filePath atomically:YES]) { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + [dictionary setObject:[NSNumber numberWithInt:500] forKey:@"status"]; + [dictionary setObject:@"Could not write the data to the given filePath." forKey:@"error"]; + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + return; + } + + id filePlugin = [self.commandDelegate getCommandInstance:@"File"]; NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [dictionary setObject:[NSNumber numberWithInt:500] forKey:@"status"]; - [dictionary setObject:@"Could not write the data to the given filePath." forKey:@"error"]; + [self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:nil]; + [dictionary setObject:[filePlugin getDirectoryEntry:filePath isDirectory:NO] forKey:@"file"]; + + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } failure:^(NSURLSessionTask *task, NSError *error) { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + [self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error]; + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - return; - } - - id filePlugin = [self.commandDelegate getCommandInstance:@"File"]; - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [self handleSuccess:dictionary withResponse:(NSHTTPURLResponse*)task.response andData:nil]; - [dictionary setObject:[filePlugin getDirectoryEntry:filePath isDirectory:NO] forKey:@"file"]; - - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; - [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } failure:^(NSURLSessionTask *task, NSError *error) { - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; - [self handleError:dictionary withResponse:(NSHTTPURLResponse*)task.response error:error]; - - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; - [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - }]; + }]; + } + @catch (NSException *exception) { + [self handleException:exception withCommand:command]; + } } @end diff --git a/src/ios/TextRequestSerializer.h b/src/ios/TextRequestSerializer.h new file mode 100644 index 0000000..56ddad4 --- /dev/null +++ b/src/ios/TextRequestSerializer.h @@ -0,0 +1,8 @@ +#import +#import "AFURLRequestSerialization.h" + +@interface TextRequestSerializer : AFHTTPRequestSerializer + ++ (instancetype)serializer; + +@end diff --git a/src/ios/TextRequestSerializer.m b/src/ios/TextRequestSerializer.m new file mode 100644 index 0000000..31c1c2e --- /dev/null +++ b/src/ios/TextRequestSerializer.m @@ -0,0 +1,53 @@ +#import "TextRequestSerializer.h" + +@implementation TextRequestSerializer + ++ (instancetype)serializer +{ + TextRequestSerializer *serializer = [[self alloc] init]; + return serializer; +} + +#pragma mark - AFURLRequestSerialization + +- (NSURLRequest *)requestBySerializingRequest:(NSURLRequest *)request + withParameters:(id)parameters + error:(NSError *__autoreleasing *)error +{ + NSParameterAssert(request); + + if ([self.HTTPMethodsEncodingParametersInURI containsObject:[[request HTTPMethod] uppercaseString]]) { + return [super requestBySerializingRequest:request withParameters:parameters error:error]; + } + + NSMutableURLRequest *mutableRequest = [request mutableCopy]; + + [self.HTTPRequestHeaders enumerateKeysAndObjectsUsingBlock:^(id field, id value, BOOL * __unused stop) { + if (![request valueForHTTPHeaderField:field]) { + [mutableRequest setValue:value forHTTPHeaderField:field]; + } + }]; + + if (parameters) { + if (![mutableRequest valueForHTTPHeaderField:@"Content-Type"]) { + [mutableRequest setValue:@"text/plain; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; + } + + [mutableRequest setHTTPBody: [[parameters valueForKey:@"text"] dataUsingEncoding:NSUTF8StringEncoding]]; + } + + return mutableRequest; +} + +#pragma mark - NSSecureCoding + +- (instancetype)initWithCoder:(NSCoder *)decoder { + self = [super initWithCoder:decoder]; + if (!self) { + return nil; + } + + return self; +} + +@end diff --git a/test/app-test-definitions.js b/test/app-test-definitions.js index 1c687a3..36f5c43 100644 --- a/test/app-test-definitions.js +++ b/test/app-test-definitions.js @@ -8,6 +8,7 @@ const hooks = { const helpers = { acceptAllCerts: function(done) { cordova.plugin.http.acceptAllCerts(true, done, done); }, setJsonSerializer: function(done) { done(cordova.plugin.http.setDataSerializer('json')); }, + setUtf8StringSerializer: function(done) { done(cordova.plugin.http.setDataSerializer('utf8')); }, setUrlEncodedSerializer: function(done) { done(cordova.plugin.http.setDataSerializer('urlencoded')); }, getWithXhr: function(done, url) { var xhr = new XMLHttpRequest(); @@ -382,6 +383,17 @@ const tests = [ cookies.myCookie.should.be.equal('myValue'); cookies.mySecondCookie.should.be.equal('mySecondValue'); } + },{ + description: 'should send UTF-8 encoded raw string correctly (POST)', + expected: 'resolved: {"status": 200, "data": "{\\"data\\": \\"this is a test string\\"...', + before: helpers.setUtf8StringSerializer, + func: function(resolve, reject) { + cordova.plugin.http.post('http://httpbin.org/anything', { text: 'this is a test string' }, {}, resolve, reject); + }, + validationFunc: function(driver, result) { + result.type.should.be.equal('resolved'); + JSON.parse(result.data.data).data.should.eql('this is a test string'); + } } ]; diff --git a/www/advanced-http.js b/www/advanced-http.js index 5119032..16ab2be 100644 --- a/www/advanced-http.js +++ b/www/advanced-http.js @@ -20,12 +20,7 @@ * under the License. * * Modified by Andrew Stephan for Sync OnSet - * Modified by Sefa Ilkimen: - * - added configurable params serializer - * - added put and delete methods - * - using cordova www module pattern - * - some minor improvements - * + * Modified by Sefa Ilkimen */ /* @@ -33,7 +28,7 @@ */ var pluginId = module.id.slice(0, module.id.indexOf('.')); -var validSerializers = ['urlencoded', 'json', 'raw' ]; +var validSerializers = ['urlencoded', 'json', 'utf8' ]; var exec = require('cordova/exec'); var angularIntegration = require(pluginId +'.angular-integration');