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

BinaryResponseSerializer.m 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #import "BinaryResponseSerializer.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 BinaryResponseSerializer
  22. + (instancetype)serializer {
  23. BinaryResponseSerializer *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. error:(NSError * __autoreleasing *)error
  62. {
  63. if (response && [response isKindOfClass:[NSHTTPURLResponse class]]) {
  64. if (self.acceptableStatusCodes && ![self.acceptableStatusCodes containsIndex:(NSUInteger)response.statusCode] && [response URL]) {
  65. NSMutableDictionary *mutableUserInfo = [@{
  66. NSLocalizedDescriptionKey: [NSString stringWithFormat:NSLocalizedStringFromTable(@"Request failed: %@ (%ld)", @"AFNetworking", nil), [NSHTTPURLResponse localizedStringForStatusCode:response.statusCode], (long)response.statusCode],
  67. NSURLErrorFailingURLErrorKey: [response URL],
  68. AFNetworkingOperationFailingURLResponseErrorKey: response,
  69. } mutableCopy];
  70. if (data) {
  71. mutableUserInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] = data;
  72. // trying to decode error message in body
  73. mutableUserInfo[AFNetworkingOperationFailingURLResponseBodyErrorKey] = [self decodeResponseData:data withEncoding:[self getEncoding:response]];
  74. }
  75. if (error) {
  76. *error = [NSError errorWithDomain:AFURLResponseSerializationErrorDomain code:NSURLErrorBadServerResponse userInfo:mutableUserInfo];
  77. }
  78. return NO;
  79. }
  80. }
  81. return YES;
  82. }
  83. #pragma mark - AFURLResponseSerialization
  84. - (id)responseObjectForResponse:(NSURLResponse *)response
  85. data:(NSData *)data
  86. error:(NSError *__autoreleasing *)error
  87. {
  88. if (![self validateResponse:(NSHTTPURLResponse *)response data:data error:error]) {
  89. if (!error || AFErrorOrUnderlyingErrorHasCodeInDomain(*error, NSURLErrorCannotDecodeContentData, AFURLResponseSerializationErrorDomain)) {
  90. return nil;
  91. }
  92. }
  93. return [data base64EncodedStringWithOptions:0];
  94. }
  95. @end