1
1
#import " TextResponseSerializer.h"
2
2
3
+ NSString * const AFNetworkingOperationFailingURLResponseBodyKey = @" com.alamofire.serialization.response.error.body" ;
4
+ NSStringEncoding const SupportedEncodings[6 ] = { NSUTF8StringEncoding, NSWindowsCP1252StringEncoding, NSISOLatin1StringEncoding, NSISOLatin2StringEncoding, NSASCIIStringEncoding, NSUnicodeStringEncoding };
5
+
6
+ static NSError * AFErrorWithUnderlyingError (NSError *error, NSError *underlyingError) {
7
+ if (!error) {
8
+ return underlyingError;
9
+ }
10
+
11
+ if (!underlyingError || error.userInfo [NSUnderlyingErrorKey ]) {
12
+ return error;
13
+ }
14
+
15
+ NSMutableDictionary *mutableUserInfo = [error.userInfo mutableCopy ];
16
+ mutableUserInfo[NSUnderlyingErrorKey ] = underlyingError;
17
+
18
+ return [[NSError alloc ] initWithDomain: error.domain code: error.code userInfo: mutableUserInfo];
19
+ }
20
+
3
21
static BOOL AFErrorOrUnderlyingErrorHasCodeInDomain (NSError *error, NSInteger code, NSString *domain) {
4
- if ([error.domain isEqualToString: domain] && error.code == code) {
5
- return YES ;
6
- } else if (error.userInfo [NSUnderlyingErrorKey ]) {
7
- return AFErrorOrUnderlyingErrorHasCodeInDomain (error.userInfo [NSUnderlyingErrorKey ], code, domain);
8
- }
22
+ if ([error.domain isEqualToString: domain] && error.code == code) {
23
+ return YES ;
24
+ } else if (error.userInfo [NSUnderlyingErrorKey ]) {
25
+ return AFErrorOrUnderlyingErrorHasCodeInDomain (error.userInfo [NSUnderlyingErrorKey ], code, domain);
26
+ }
9
27
10
- return NO ;
28
+ return NO ;
11
29
}
12
30
13
31
@implementation TextResponseSerializer
14
32
15
33
+ (instancetype )serializer {
16
- TextResponseSerializer *serializer = [[self alloc ] init ];
17
- return serializer;
34
+ TextResponseSerializer *serializer = [[self alloc ] init ];
35
+ return serializer;
18
36
}
19
37
20
38
- (instancetype )init {
21
- self = [super init ];
39
+ self = [super init ];
40
+
41
+ if (!self) {
42
+ return nil ;
43
+ }
44
+
45
+ self.acceptableContentTypes = nil ;
46
+
47
+ return self;
48
+ }
49
+
50
+ - (NSString *)decodeResponseData : (NSData *)rawResponseData withEncoding : (CFStringEncoding)cfEncoding {
51
+ NSStringEncoding nsEncoding;
52
+ NSString * decoded = nil ;
53
+
54
+ if (cfEncoding != kCFStringEncodingInvalidId ) {
55
+ nsEncoding = CFStringConvertEncodingToNSStringEncoding (cfEncoding);
56
+ }
57
+
58
+ for (int i = 0 ; i < sizeof (SupportedEncodings) / sizeof (NSStringEncoding ) && !decoded; ++i) {
59
+ if (cfEncoding == kCFStringEncodingInvalidId || nsEncoding == SupportedEncodings[i]) {
60
+ decoded = [[NSString alloc ] initWithData: rawResponseData encoding: SupportedEncodings[i]];
61
+ }
62
+ }
22
63
23
- if (!self) {
24
- return nil ;
64
+ if (!decoded) {
65
+ decoded = @" Could not decode response data due to invalid or unknown charset encoding" ;
66
+ }
67
+
68
+ return decoded;
69
+ }
70
+
71
+ - (CFStringEncoding) getEncoding : (NSURLResponse *)response {
72
+ CFStringEncoding encoding = kCFStringEncodingInvalidId ;
73
+
74
+ if (response.textEncodingName ) {
75
+ encoding = CFStringConvertIANACharSetNameToEncoding ((CFStringRef)response.textEncodingName );
76
+ }
77
+
78
+ return encoding;
79
+ }
80
+
81
+ #pragma mark -
82
+
83
+ - (BOOL )validateResponse : (NSHTTPURLResponse *)response
84
+ data : (NSData *)data
85
+ error : (NSError * __autoreleasing *)error
86
+ {
87
+ BOOL responseIsValid = YES ;
88
+ NSError *validationError = nil ;
89
+
90
+ if (response && [response isKindOfClass: [NSHTTPURLResponse class ]]) {
91
+ if (self.acceptableStatusCodes && ![self .acceptableStatusCodes containsIndex: (NSUInteger )response.statusCode] && [response URL ]) {
92
+ NSMutableDictionary *mutableUserInfo = [@{
93
+ NSLocalizedDescriptionKey : [NSString stringWithFormat: NSLocalizedStringFromTable(@" Request failed: %@ (%ld )" , @" AFNetworking" , nil ), [NSHTTPURLResponse localizedStringForStatusCode: response.statusCode], (long )response.statusCode],
94
+ NSURLErrorFailingURLErrorKey :[response URL ],
95
+ AFNetworkingOperationFailingURLResponseErrorKey: response,
96
+ } mutableCopy];
97
+
98
+ if (data) {
99
+ mutableUserInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] = data;
100
+ mutableUserInfo[AFNetworkingOperationFailingURLResponseBodyKey] = [self decodeResponseData: data withEncoding: [self getEncoding: response]];
101
+ }
102
+
103
+ validationError = AFErrorWithUnderlyingError ([NSError errorWithDomain: AFURLResponseSerializationErrorDomain code: NSURLErrorBadServerResponse userInfo: mutableUserInfo], validationError);
104
+ responseIsValid = NO ;
25
105
}
106
+ }
26
107
27
- self.acceptableContentTypes = nil ;
108
+ if (error && !responseIsValid) {
109
+ *error = validationError;
110
+ }
28
111
29
- return self ;
112
+ return responseIsValid ;
30
113
}
31
114
32
115
#pragma mark - AFURLResponseSerialization
@@ -35,25 +118,13 @@ - (id)responseObjectForResponse:(NSURLResponse *)response
35
118
data : (NSData *)data
36
119
error : (NSError *__autoreleasing *)error
37
120
{
38
- if (![self validateResponse: (NSHTTPURLResponse *)response data: data error: error]) {
39
- if (!error || AFErrorOrUnderlyingErrorHasCodeInDomain (*error, NSURLErrorCannotDecodeContentData, AFURLResponseSerializationErrorDomain)) {
40
- return nil ;
41
- }
121
+ if (![self validateResponse: (NSHTTPURLResponse *)response data: data error: error]) {
122
+ if (!error || AFErrorOrUnderlyingErrorHasCodeInDomain (*error, NSURLErrorCannotDecodeContentData, AFURLResponseSerializationErrorDomain)) {
123
+ return nil ;
42
124
}
125
+ }
43
126
44
- // 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.
45
- // See https://github.com/rails/rails/issues/1742
46
- NSStringEncoding stringEncoding = self.stringEncoding ;
47
- if (response.textEncodingName ) {
48
- CFStringEncoding encoding = CFStringConvertIANACharSetNameToEncoding ((CFStringRef)response.textEncodingName );
49
- if (encoding != kCFStringEncodingInvalidId ) {
50
- stringEncoding = CFStringConvertEncodingToNSStringEncoding (encoding);
51
- }
52
- }
53
-
54
- NSString *responseString = [[NSString alloc ] initWithData: data encoding: stringEncoding];
55
-
56
- return responseString;
127
+ return [self decodeResponseData: data withEncoding: [self getEncoding: response]];
57
128
}
58
129
59
130
@end
0 commit comments