Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

TextResponseSerializer.m 4.9KB

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