diff options
| author | PliablePixels <pliablepixels@gmail.com> | 2015-06-24 18:47:42 -0400 |
|---|---|---|
| committer | PliablePixels <pliablepixels@gmail.com> | 2015-06-24 18:47:42 -0400 |
| commit | 855a0e8ddc273b58066530a1b55a946021dfc56e (patch) | |
| tree | 26550033e855a31a265fc2da4da3df0cc2733dc1 /plugins/com.synconset.cordovaHTTP/src/ios/TextResponseSerializer.m | |
| parent | d442629aa825aab6bc55ab6be19e3aba060867fe (diff) | |
Cleaned up code, commented, preparing for HTTPS via CordovaHTTP
Diffstat (limited to 'plugins/com.synconset.cordovaHTTP/src/ios/TextResponseSerializer.m')
| -rw-r--r-- | plugins/com.synconset.cordovaHTTP/src/ios/TextResponseSerializer.m | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/plugins/com.synconset.cordovaHTTP/src/ios/TextResponseSerializer.m b/plugins/com.synconset.cordovaHTTP/src/ios/TextResponseSerializer.m new file mode 100644 index 00000000..39d4080b --- /dev/null +++ b/plugins/com.synconset.cordovaHTTP/src/ios/TextResponseSerializer.m @@ -0,0 +1,58 @@ +#import "TextResponseSerializer.h" + +static BOOL AFErrorOrUnderlyingErrorHasCode(NSError *error, NSInteger code) { + if (error.code == code) { + return YES; + } else if (error.userInfo[NSUnderlyingErrorKey]) { + return AFErrorOrUnderlyingErrorHasCode(error.userInfo[NSUnderlyingErrorKey], code); + } + + return NO; +} + +@implementation TextResponseSerializer + ++ (instancetype)serializer { + TextResponseSerializer *serializer = [[self alloc] init]; + return serializer; +} + +- (instancetype)init { + self = [super init]; + if (!self) { + return nil; + } + + self.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", @"text/html", @"text/json", @"application/json", @"text/xml", @"application/xml", @"text/css", nil]; + + return self; +} + +#pragma mark - AFURLResponseSerialization + +- (id)responseObjectForResponse:(NSURLResponse *)response + data:(NSData *)data + error:(NSError *__autoreleasing *)error +{ + if (![self validateResponse:(NSHTTPURLResponse *)response data:data error:error]) { + if (AFErrorOrUnderlyingErrorHasCode(*error, NSURLErrorCannotDecodeContentData)) { + return nil; + } + } + + // Workaround for behavior of Rails to return a single space for `head :ok` (a workaround for a bug in Safari), which is not interpreted as valid input by NSJSONSerialization. + // See https://github.com/rails/rails/issues/1742 + NSStringEncoding stringEncoding = self.stringEncoding; + if (response.textEncodingName) { + CFStringEncoding encoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)response.textEncodingName); + if (encoding != kCFStringEncodingInvalidId) { + stringEncoding = CFStringConvertEncodingToNSStringEncoding(encoding); + } + } + + NSString *responseString = [[NSString alloc] initWithData:data encoding:stringEncoding]; + + return responseString; +} + +@end
\ No newline at end of file |
