mirror of
https://github.com/silkimen/cordova-plugin-advanced-http.git
synced 2026-04-24 00:00:03 +08:00
58 lines
1.9 KiB
Objective-C
58 lines
1.9 KiB
Objective-C
#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 |