Skip to content

Commit

Permalink
Merge pull request #85 from wymsee/ios-errors
Browse files Browse the repository at this point in the history
updated AFNetworking and edited requests to send back the body for errors
  • Loading branch information
allie-wake-up committed May 2, 2016
2 parents ba37200 + b6976be commit d726998
Show file tree
Hide file tree
Showing 17 changed files with 83 additions and 90 deletions.
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
# Changelog

## v1.1.0

- Fixed the body of errors not being returned in iOS
- Updated AFNetworking to 3.1.0

### Potentially Breaking Changes

- Disable encoding get() URLS in android (Thanks to devgeeks)

## v1.0.3

- Fixed version number in plugin.xml

## v1.0.2

- Fixed bug using useBasicAuth and setHeader from angular

## v1.0.1

- updated README

## v1.0.0

- Added getBasicAuthHeader function
Expand Down
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,11 @@ This plugin utilizes some awesome open source networking libraries. These are b

We made a few modifications to http-request. They can be found in a separate repo here: https://github.com/wymsee/http-request

## Current Limitations
## Cookies

This plugin isn't equivalent to using XMLHttpRequest or Ajax calls in Javascript.
For instance, the following features are currently not supported:
- a cookie set by a request isn't sent in subsequent requests

- cookies support (a cookie set by a request isn't sent in subsequent requests)
- read content of error responses (only the HTTP status code and message are returned)

Take this into account when using this plugin into your application.
Take this into account when using this plugin in your application.

## License

Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-plugin-http"
version="1.0.3">
version="1.1.0">

<name>SSL Pinning</name>

Expand Down
14 changes: 7 additions & 7 deletions src/ios/AFNetworking/AFHTTPSessionManager.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// AFHTTPSessionManager.h
// Copyright (c) 2011–2016 Alamofire Software Foundation (http://alamofire.org/)
// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -150,15 +150,15 @@ NS_ASSUME_NONNULL_BEGIN
@param URLString The URL string used to create the request URL.
@param parameters The parameters to be encoded according to the client request serializer.
@param progress A block object to be executed when the download progress is updated. Note this block is called on the session queue, not the main queue.
@param downloadProgress A block object to be executed when the download progress is updated. Note this block is called on the session queue, not the main queue.
@param success A block object to be executed when the task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer.
@param failure A block object to be executed when the task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the data task and the error describing the network or parsing error that occurred.
@see -dataTaskWithRequest:uploadProgress:downloadProgress:completionHandler:
*/
- (nullable NSURLSessionDataTask *)GET:(NSString *)URLString
parameters:(nullable id)parameters
progress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgress
progress:(nullable void (^)(NSProgress *downloadProgress))downloadProgress
success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;

Expand Down Expand Up @@ -197,15 +197,15 @@ NS_ASSUME_NONNULL_BEGIN
@param URLString The URL string used to create the request URL.
@param parameters The parameters to be encoded according to the client request serializer.
@param progress A block object to be executed when the upload progress is updated. Note this block is called on the session queue, not the main queue.
@param uploadProgress A block object to be executed when the upload progress is updated. Note this block is called on the session queue, not the main queue.
@param success A block object to be executed when the task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer.
@param failure A block object to be executed when the task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the data task and the error describing the network or parsing error that occurred.
@see -dataTaskWithRequest:uploadProgress:downloadProgress:completionHandler:
*/
- (nullable NSURLSessionDataTask *)POST:(NSString *)URLString
parameters:(nullable id)parameters
progress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress
progress:(nullable void (^)(NSProgress *uploadProgress))uploadProgress
success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;

Expand All @@ -232,7 +232,7 @@ NS_ASSUME_NONNULL_BEGIN
@param URLString The URL string used to create the request URL.
@param parameters The parameters to be encoded according to the client request serializer.
@param block A block that takes a single argument and appends data to the HTTP body. The block argument is an object adopting the `AFMultipartFormData` protocol.
@param progress A block object to be executed when the upload progress is updated. Note this block is called on the session queue, not the main queue.
@param uploadProgress A block object to be executed when the upload progress is updated. Note this block is called on the session queue, not the main queue.
@param success A block object to be executed when the task finishes successfully. This block has no return value and takes two arguments: the data task, and the response object created by the client response serializer.
@param failure A block object to be executed when the task finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a two arguments: the data task and the error describing the network or parsing error that occurred.
Expand All @@ -241,7 +241,7 @@ NS_ASSUME_NONNULL_BEGIN
- (nullable NSURLSessionDataTask *)POST:(NSString *)URLString
parameters:(nullable id)parameters
constructingBodyWithBlock:(nullable void (^)(id <AFMultipartFormData> formData))block
progress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress
progress:(nullable void (^)(NSProgress *uploadProgress))uploadProgress
success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;

Expand Down
8 changes: 1 addition & 7 deletions src/ios/AFNetworking/AFHTTPSessionManager.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// AFHTTPSessionManager.m
// Copyright (c) 2011–2016 Alamofire Software Foundation (http://alamofire.org/)
// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -186,12 +186,9 @@ - (NSURLSessionDataTask *)POST:(NSString *)URLString
NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters constructingBodyWithBlock:block error:&serializationError];
if (serializationError) {
if (failure) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgnu"
dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{
failure(nil, serializationError);
});
#pragma clang diagnostic pop
}

return nil;
Expand Down Expand Up @@ -262,12 +259,9 @@ - (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:&serializationError];
if (serializationError) {
if (failure) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgnu"
dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{
failure(nil, serializationError);
});
#pragma clang diagnostic pop
}

return nil;
Expand Down
4 changes: 2 additions & 2 deletions src/ios/AFNetworking/AFNetworkReachabilityManager.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// AFNetworkReachabilityManager.h
// Copyright (c) 2011–2016 Alamofire Software Foundation (http://alamofire.org/)
// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -38,7 +38,7 @@ NS_ASSUME_NONNULL_BEGIN
Reachability can be used to determine background information about why a network operation failed, or to trigger a network operation retrying when a connection is established. It should not be used to prevent a user from initiating a network request, as it's possible that an initial request may be required to establish reachability.
See Apple's Reachability Sample Code (https://developer.apple.com/library/ios/samplecode/reachability/)
See Apple's Reachability Sample Code ( https://developer.apple.com/library/ios/samplecode/reachability/ )
@warning Instances of `AFNetworkReachabilityManager` must be started with `-startMonitoring` before reachability status can be determined.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/ios/AFNetworking/AFNetworkReachabilityManager.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// AFNetworkReachabilityManager.m
// Copyright (c) 2011–2016 Alamofire Software Foundation (http://alamofire.org/)
// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion src/ios/AFNetworking/AFSecurityPolicy.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// AFSecurityPolicy.h
// Copyright (c) 2011–2016 Alamofire Software Foundation (http://alamofire.org/)
// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion src/ios/AFNetworking/AFSecurityPolicy.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// AFSecurityPolicy.m
// Copyright (c) 2011–2016 Alamofire Software Foundation (http://alamofire.org/)
// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion src/ios/AFNetworking/AFURLRequestSerialization.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// AFURLRequestSerialization.h
// Copyright (c) 2011–2016 Alamofire Software Foundation (http://alamofire.org/)
// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
25 changes: 2 additions & 23 deletions src/ios/AFNetworking/AFURLRequestSerialization.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// AFURLRequestSerialization.m
// Copyright (c) 2011–2016 Alamofire Software Foundation (http://alamofire.org/)
// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -60,10 +60,7 @@
NSMutableString *escaped = @"".mutableCopy;

while (index < string.length) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wgnu"
NSUInteger length = MIN(string.length - index, batchSize);
#pragma GCC diagnostic pop
NSRange range = NSMakeRange(index, length);

// To avoid breaking up character sequences such as 👴🏻👮🏽
Expand Down Expand Up @@ -219,8 +216,6 @@ - (instancetype)init {
[self setValue:[acceptLanguagesComponents componentsJoinedByString:@", "] forHTTPHeaderField:@"Accept-Language"];

NSString *userAgent = nil;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgnu"
#if TARGET_OS_IOS
// User-Agent Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43
userAgent = [NSString stringWithFormat:@"%@/%@ (%@; iOS %@; Scale/%0.2f)", [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleExecutableKey] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleIdentifierKey], [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleVersionKey], [[UIDevice currentDevice] model], [[UIDevice currentDevice] systemVersion], [[UIScreen mainScreen] scale]];
Expand All @@ -230,7 +225,6 @@ - (instancetype)init {
#elif defined(__MAC_OS_X_VERSION_MIN_REQUIRED)
userAgent = [NSString stringWithFormat:@"%@/%@ (Mac OS X %@)", [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleExecutableKey] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleIdentifierKey], [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleVersionKey], [[NSProcessInfo processInfo] operatingSystemVersionString]];
#endif
#pragma clang diagnostic pop
if (userAgent) {
if (![userAgent canBeConvertedToEncoding:NSASCIIStringEncoding]) {
NSMutableString *mutableUserAgent = [userAgent mutableCopy];
Expand Down Expand Up @@ -500,7 +494,7 @@ - (NSURLRequest *)requestBySerializingRequest:(NSURLRequest *)request
}

if ([self.HTTPMethodsEncodingParametersInURI containsObject:[[request HTTPMethod] uppercaseString]]) {
if (query) {
if (query && query.length > 0) {
mutableRequest.URL = [NSURL URLWithString:[[mutableRequest.URL absoluteString] stringByAppendingFormat:mutableRequest.URL.query ? @"&%@" : @"?%@", query]];
}
} else {
Expand Down Expand Up @@ -835,14 +829,11 @@ @interface AFMultipartBodyStream () <NSCopying>
@end

@implementation AFMultipartBodyStream
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wimplicit-atomic-properties"
#if (defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000) || (defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 1100)
@synthesize delegate;
#endif
@synthesize streamStatus;
@synthesize streamError;
#pragma clang diagnostic pop

- (instancetype)initWithStringEncoding:(NSStringEncoding)encoding {
self = [super init];
Expand Down Expand Up @@ -888,8 +879,6 @@ - (NSInteger)read:(uint8_t *)buffer

NSInteger totalNumberOfBytesRead = 0;

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgnu"
while ((NSUInteger)totalNumberOfBytesRead < MIN(length, self.numberOfBytesInPacket)) {
if (!self.currentHTTPBodyPart || ![self.currentHTTPBodyPart hasBytesAvailable]) {
if (!(self.currentHTTPBodyPart = [self.HTTPBodyPartEnumerator nextObject])) {
Expand All @@ -910,7 +899,6 @@ - (NSInteger)read:(uint8_t *)buffer
}
}
}
#pragma clang diagnostic pop

return totalNumberOfBytesRead;
}
Expand Down Expand Up @@ -1091,8 +1079,6 @@ - (BOOL)hasBytesAvailable {
return YES;
}

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wcovered-switch-default"
switch (self.inputStream.streamStatus) {
case NSStreamStatusNotOpen:
case NSStreamStatusOpening:
Expand All @@ -1106,7 +1092,6 @@ - (BOOL)hasBytesAvailable {
default:
return NO;
}
#pragma clang diagnostic pop
}

- (NSInteger)read:(uint8_t *)buffer
Expand Down Expand Up @@ -1151,11 +1136,8 @@ - (NSInteger)readData:(NSData *)data
intoBuffer:(uint8_t *)buffer
maxLength:(NSUInteger)length
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgnu"
NSRange range = NSMakeRange((NSUInteger)_phaseReadOffset, MIN([data length] - ((NSUInteger)_phaseReadOffset), length));
[data getBytes:buffer range:range];
#pragma clang diagnostic pop

_phaseReadOffset += range.length;

Expand All @@ -1174,8 +1156,6 @@ - (BOOL)transitionToNextPhase {
return YES;
}

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wcovered-switch-default"
switch (_phase) {
case AFEncapsulationBoundaryPhase:
_phase = AFHeaderPhase;
Expand All @@ -1195,7 +1175,6 @@ - (BOOL)transitionToNextPhase {
break;
}
_phaseReadOffset = 0;
#pragma clang diagnostic pop

return YES;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ios/AFNetworking/AFURLResponseSerialization.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// AFURLResponseSerialization.h
// Copyright (c) 2011–2016 Alamofire Software Foundation (http://alamofire.org/)
// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 4 additions & 2 deletions src/ios/AFNetworking/AFURLResponseSerialization.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// AFURLResponseSerialization.m
// Copyright (c) 2011–2016 Alamofire Software Foundation (http://alamofire.org/)
// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -115,7 +115,9 @@ - (BOOL)validateResponse:(NSHTTPURLResponse *)response
NSError *validationError = nil;

if (response && [response isKindOfClass:[NSHTTPURLResponse class]]) {
if (self.acceptableContentTypes && ![self.acceptableContentTypes containsObject:[response MIMEType]]) {
if (self.acceptableContentTypes && ![self.acceptableContentTypes containsObject:[response MIMEType]] &&
!([response MIMEType] == nil && [data length] == 0)) {

if ([data length] > 0 && [response URL]) {
NSMutableDictionary *mutableUserInfo = [@{
NSLocalizedDescriptionKey: [NSString stringWithFormat:NSLocalizedStringFromTable(@"Request failed: unacceptable content-type: %@", @"AFNetworking", nil), [response MIMEType]],
Expand Down
Loading

0 comments on commit d726998

Please sign in to comment.