summaryrefslogtreecommitdiff
path: root/plugins/com.synconset.cordovaHTTP/src/ios/TextResponseSerializer.m
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/com.synconset.cordovaHTTP/src/ios/TextResponseSerializer.m')
-rw-r--r--plugins/com.synconset.cordovaHTTP/src/ios/TextResponseSerializer.m58
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