zoukankan      html  css  js  c++  java
  • AFNetworking源码品读

    AFNetworking源码品读

    AFNetworking这个库几乎是所有苹果开发人员在使用HTTP协议的第一选择,为什么这个库会有这么大的吸引力呢?其实答案就需要问问自己,为什么会用它,而不是别的库,或者是自己去开发一个呢?作为一名开发人员,我们从内心里面会选择效率最高最节省时间的方法去解决问题,AFNetworking这个库在处理HTTP任务时,给我们提供了很多非常简单的接口,你只需要去把接口地址,参数放进去,就能够异步的得到一个回调结果,这简直太爽了,记得之前使用NSURLConnection的时候,我们需要自己去创建一个NSURLRequest然后再创建一个NSURLConnection,然后需要写一大堆的协议里面的方法,真的像给小孩换尿不湿一样,所有的事情都需要自己去处理,但是AFNetworking就好像是一个专业的baby保姆,做的又快又好,你只需要提供baby和尿不湿就可以了。这只是一个抽象的描述,至于这个库做了什么,还是需要深入的去思考的。
    目录
    打开AFNetworking的文件目录(3.0.6),先简单的说一下文件的功能,后续再具体的分析

    AFNetworking.h

    引用AFNetworking的import文件,这个文件里面包含了核心的文件名,

    NSURLSession/AFHTTPSessionManager.h

    包装了我们使用NSURLSession来进行网络请求的接口,提供一个单例来进行url等基本信息的控制

    NSURLSession/AFURLSessionManager.h

    为了让代码逻辑以及功能模块的独立性,这个模块完成最苦最累的活,也就是创建configration创建、task,处理回调回来的数据,并且提供使用block来进行回调的方法,多线程任务的封装等等任务,可以说是这个库里面最核心的代码

    Reachability/AFNetworkReachabilityManager.h

    网络状态判断和检测的文件,提供便捷的检测网络的接口,开发者可以使用block或者通知的方式来处理状态的变化

    Security/AFSecurityPolicy.h

    AFSecurityPolicy用来处理HTTP协议里面的安全策略事件,主要就是HTTPS的几种建立连接的几种策略

    Serialization/AFURLRequestSerialization.h Serialization/AFURLResponseSerialization.h

    把请求内容序列化和返回内容序列化

    另外还有UIKit文件,里面主要是UIKit文件针对使用AFNetworking的处理,处理的场景不同,具体文件具体再分析

    这些就是整个库的文件接口,非常的简洁,几个文件就能把这个库设计的如此的强大而且灵活,足以看出此库的开发者内力的深厚

    源码分析

    NSURLSession/AFHTTPSessionManager.h

    文件里面提供的是一个单例manager,不知道从什么时候开始很多单例都叫做manager,可能是因为这家伙一直都在能干很多事情,所以就叫manager,这个类是AFURLSessionManager子类。
    在这里思考为什么是用一个单例来提供服务而不是用分类或者类方法等方式来暴露接口呢?因为这个manager干了一些很重要的事情

    1. 提供基础的url信息。在一个工程里面其实很多接口都是提供几个基础的域名的请求地址,通过这个封装可以让我们后面专注于自己的接口,当接口地址发生变化或者域名迁移的时候就直接处理这个就好了
    2. 当我们的一个请求发出的时候,我们往往会请求是一次性,以后每次请求都是重新创建的,这样就让我们不想去维护这个请求,但是要知道,在代码里面我们发了一个请求之后,就去做别的事情了,但是如果没有人替我们管理这个请求的回调,那么这个请求就会被释放掉,manager正好帮我们处理了这个事情

    所以,没有使用类方法或者分类来提供接口,而是使用单例来提供接口

    来看一下这个类到底提供了那些接口

    - (nullable NSURLSessionDataTask *)GET:(NSString *)URLString
                       parameters:(nullable id)parameters
                          success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
                          failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure DEPRECATED_ATTRIBUTE;
    
    - (nullable NSURLSessionDataTask *)GET:(NSString *)URLString
                                parameters:(nullable id)parameters
                                  progress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgress
                                   success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
                                   failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
    
    - (nullable NSURLSessionDataTask *)HEAD:(NSString *)URLString
                        parameters:(nullable id)parameters
                           success:(nullable void (^)(NSURLSessionDataTask *task))success
                           failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
                           - (nullable NSURLSessionDataTask *)POST:(NSString *)URLString
                        parameters:(nullable id)parameters
                           success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
                           failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure DEPRECATED_ATTRIBUTE;
    
    - (nullable NSURLSessionDataTask *)POST:(NSString *)URLString
                                 parameters:(nullable id)parameters
                                   progress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress
                                    success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
                                    failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
    
    - (nullable NSURLSessionDataTask *)POST:(NSString *)URLString
                        parameters:(nullable id)parameters
         constructingBodyWithBlock:(nullable void (^)(id <AFMultipartFormData> formData))block
                           success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
                           failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure DEPRECATED_ATTRIBUTE;
    
    - (nullable NSURLSessionDataTask *)POST:(NSString *)URLString
                                 parameters:(nullable id)parameters
                  constructingBodyWithBlock:(nullable void (^)(id <AFMultipartFormData> formData))block
                                   progress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress
                                    success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
                                    failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
    
    - (nullable NSURLSessionDataTask *)PUT:(NSString *)URLString
                       parameters:(nullable id)parameters
                          success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
                          failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
    
    - (nullable NSURLSessionDataTask *)PATCH:(NSString *)URLString
                         parameters:(nullable id)parameters
                            success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
                            failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
    
    - (nullable NSURLSessionDataTask *)DELETE:(NSString *)URLString
                          parameters:(nullable id)parameters
                             success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
                             failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure;
    
    

    涵盖了HTTP里面所有的Method,GETPOSTHEADPUTPATCHDELETE、需要使用这些方法和服务器进行交互直接来调用接口就好了。

    AFHTTPSessionManager里面包含baseURL、requestSerializer、responseSerializer
    这个文件里面最核心的方法是

    - (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
                                           URLString:(NSString *)URLString
                                          parameters:(id)parameters
                                      uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress
                                    downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgress
                                             success:(void (^)(NSURLSessionDataTask *, id))success
                                             failure:(void (^)(NSURLSessionDataTask *, NSError *))failure
    {
        NSError *serializationError = nil;
        NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:&serializationError];
        if (serializationError) {
            if (failure) {
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wgnu"
                dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{
                    failure(nil, serializationError);
                });
    #pragma clang diagnostic pop
            }
    
            return nil;
        }
    
        __block NSURLSessionDataTask *dataTask = nil;
        dataTask = [self dataTaskWithRequest:request
                              uploadProgress:uploadProgress
                            downloadProgress:downloadProgress
                           completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) {
            if (error) {
                if (failure) {
                    failure(dataTask, error);
                }
            } else {
                if (success) {
                    success(dataTask, responseObject);
                }
            }
        }];
    
        return dataTask;
    }
    
    - (NSURLSessionDataTask *)POST:(NSString *)URLString
                        parameters:(id)parameters
         constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
                          progress:(nullable void (^)(NSProgress * _Nonnull))uploadProgress
                           success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                           failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
    {
        NSError *serializationError = nil;
        NSMutableURLRequest *request = [self.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters constructingBodyWithBlock:block error:&serializationError];
        if (serializationError) {
            if (failure) {
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wgnu"
                dispatch_async(self.completionQueue ?: dispatch_get_main_queue(), ^{
                    failure(nil, serializationError);
                });
    #pragma clang diagnostic pop
            }
    
            return nil;
        }
    
        __block NSURLSessionDataTask *task = [self uploadTaskWithStreamedRequest:request progress:uploadProgress completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) {
            if (error) {
                if (failure) {
                    failure(task, error);
                }
            } else {
                if (success) {
                    success(task, responseObject);
                }
            }
        }];
    
        [task resume];
    
        return task;
    }
    

    这两个方法主要是完成的工作就是生成NSMutableURLRequest并且把这个NSMutableURLRequest以及调用方传递进去的回调方法生成一个task,并让这个task开始运行,创建task的任务都是放在AFURLSessionManager里面完成的,创建NSMutableURLRequest的动作则是AFURLRequestSerialization来完成。


    AFURLSessionManager.h

    首先看一下这个类需要的基本的信息

    - (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)configuration {
        self = [super init];
        if (!self) {
            return nil;
        }
    
        if (!configuration) {
            configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        }
    
        self.sessionConfiguration = configuration;
    
        self.operationQueue = [[NSOperationQueue alloc] init];
        self.operationQueue.maxConcurrentOperationCount = 1;
        // 串行队列创建self.session,self.session用来创建各种task
        self.session = [NSURLSession sessionWithConfiguration:self.sessionConfiguration delegate:self delegateQueue:self.operationQueue];
    
        self.responseSerializer = [AFJSONResponseSerializer serializer];
    
        self.securityPolicy = [AFSecurityPolicy defaultPolicy];
    
    #if !TARGET_OS_WATCH
        self.reachabilityManager = [AFNetworkReachabilityManager sharedManager];
    #endif
    
        // 用来绑定task.taskIdentifier和delegte的KV字典
        self.mutableTaskDelegatesKeyedByTaskIdentifier = [[NSMutableDictionary alloc] init];
    
        // 给每个任务添加delegate的时候需要加锁,防止读写时候出现问题
        self.lock = [[NSLock alloc] init];
        self.lock.name = AFURLSessionManagerLockName;
    
        [self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
            for (NSURLSessionDataTask *task in dataTasks) {
                [self addDelegateForDataTask:task uploadProgress:nil downloadProgress:nil completionHandler:nil];
            }
    
            for (NSURLSessionUploadTask *uploadTask in uploadTasks) {
                [self addDelegateForUploadTask:uploadTask progress:nil completionHandler:nil];
            }
    
            for (NSURLSessionDownloadTask *downloadTask in downloadTasks) {
                [self addDelegateForDownloadTask:downloadTask progress:nil destination:nil completionHandler:nil];
            }
        }];
    
        return self;
    }
    

    看一下如何创建task

    - (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request
                                             fromData:(NSData *)bodyData
                                             progress:(void (^)(NSProgress *uploadProgress)) uploadProgressBlock
                                    completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler
    {
        __block NSURLSessionUploadTask *uploadTask = nil;
        // 这个地方有bug,是在ios8.0以前如果并行的去创建task可能会出现bug,就是task的id会混乱在极端的情况下回导致调用已经释放的task,
        // 我觉得应该是task的id被重复的创建了,所以苹果的哥们建议串行的创建task
        url_session_manager_create_task_safely(^{
            uploadTask = [self.session uploadTaskWithRequest:request fromData:bodyData];
        });
        
        // 给每个task都生成一个特殊的delagate,主要是用来观察task的各种状态,之所以需要这个玩意,就是为了给外接提供一个信息的出口,比如在UIKit里面去获取task的信息
        [self addDelegateForUploadTask:uploadTask progress:uploadProgressBlock completionHandler:completionHandler];
    
        return uploadTask;
    }
    

    另外这个文件处理了所有的URLsession的回调信息,并且在每个回调的协议方法里面都提前插入了用户的处理,极大的提高了方便性。

    - (void)URLSession:(__unused NSURLSession *)session
                  task:(NSURLSessionTask *)task
    didCompleteWithError:(NSError *)error
    {
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wgnu"
        __strong AFURLSessionManager *manager = self.manager;
    
        __block id responseObject = nil;
    
        __block NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
        userInfo[AFNetworkingTaskDidCompleteResponseSerializerKey] = manager.responseSerializer;
    
        //Performance Improvement from #2672
        NSData *data = nil;
        if (self.mutableData) {
            data = [self.mutableData copy];
            //We no longer need the reference, so nil it out to gain back some memory.
            self.mutableData = nil;
        }
    
        if (self.downloadFileURL) {
            userInfo[AFNetworkingTaskDidCompleteAssetPathKey] = self.downloadFileURL;
        } else if (data) {
            userInfo[AFNetworkingTaskDidCompleteResponseDataKey] = data;
        }
    
        if (error) {
            userInfo[AFNetworkingTaskDidCompleteErrorKey] = error;
    
            dispatch_group_async(manager.completionGroup ?: url_session_manager_completion_group(), manager.completionQueue ?: dispatch_get_main_queue(), ^{
                if (self.completionHandler) {
                    self.completionHandler(task.response, responseObject, error);
                }
    
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingTaskDidCompleteNotification object:task userInfo:userInfo];
                });
            });
        } else {
            dispatch_async(url_session_manager_processing_queue(), ^{
                NSError *serializationError = nil;
                // 序列化返回的结果
                responseObject = [manager.responseSerializer responseObjectForResponse:task.response data:data error:&serializationError];
    
                if (self.downloadFileURL) {
                    responseObject = self.downloadFileURL;
                }
    
                if (responseObject) {
                    userInfo[AFNetworkingTaskDidCompleteSerializedResponseKey] = responseObject;
                }
    
                if (serializationError) {
                    userInfo[AFNetworkingTaskDidCompleteErrorKey] = serializationError;
                }
    
                dispatch_group_async(manager.completionGroup ?: url_session_manager_completion_group(), manager.completionQueue ?: dispatch_get_main_queue(), ^{
                    if (self.completionHandler) {
                        self.completionHandler(task.response, responseObject, serializationError);
                    }
    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingTaskDidCompleteNotification object:task userInfo:userInfo];
                    });
                });
            });
        }
    #pragma clang diagnostic pop
    }
    
    

    另外这个文件里面有个地方在+(void)load用到runtime的swizzle方法去,用af_resume去swizzle原来的resume,主要的目的是一种面向切面AOP编程的思想,去自定义方法调用时,插入自己的行为,此处为发一个resume的通知

    + (void)swizzleResumeAndSuspendMethodForClass:(Class)theClass {
        Method afResumeMethod = class_getInstanceMethod(self, @selector(af_resume));
        Method afSuspendMethod = class_getInstanceMethod(self, @selector(af_suspend));
    
        if (af_addMethod(theClass, @selector(af_resume), afResumeMethod)) {
            af_swizzleSelector(theClass, @selector(resume), @selector(af_resume));
        }
    
        if (af_addMethod(theClass, @selector(af_suspend), afSuspendMethod)) {
            af_swizzleSelector(theClass, @selector(suspend), @selector(af_suspend));
        }
    }
    - (void)af_resume {
        NSAssert([self respondsToSelector:@selector(state)], @"Does not respond to state");
        NSURLSessionTaskState state = [self state];
        [self af_resume];
        
        if (state != NSURLSessionTaskStateRunning) {
            [[NSNotificationCenter defaultCenter] postNotificationName:AFNSURLSessionTaskDidResumeNotification object:self];
        }
    }
    

    文件里面还有一个AFURLSessionManagerTaskDelegate类,这个就是刚刚说到在创建task的时候,都创建的一个东西,这个类主要使用来观察它依赖的task的各种状态信息,包括数据进度之类的属性

    代码很精练,思路很清晰


    Reachability/AFNetworkReachabilityManager.h

    这个文件提供AFNetworkReachabilityManager这个类,如果需要使用的话,需要穿进去一个域名作为观察的地址,另外提供block和通知两种方式的回调。

    类暴露出的接口和属性分别是:

    @interface AFNetworkReachabilityManager : NSObject
    
    /**
     The current network reachability status.
     */
    @property (readonly, nonatomic, assign) AFNetworkReachabilityStatus networkReachabilityStatus;
    
    /**
     Whether or not the network is currently reachable.
     */
    @property (readonly, nonatomic, assign, getter = isReachable) BOOL reachable;
    
    /**
     Whether or not the network is currently reachable via WWAN.
     */
    @property (readonly, nonatomic, assign, getter = isReachableViaWWAN) BOOL reachableViaWWAN;
    
    /**
     Whether or not the network is currently reachable via WiFi.
     */
    @property (readonly, nonatomic, assign, getter = isReachableViaWiFi) BOOL reachableViaWiFi;
    

    满足了日常所用,接下来看一下具体的实现:

    - (void)startMonitoring {
        // 暂停之前的监控
        [self stopMonitoring];
        // 判断SCNetworkReachabilityRef是否已经设置
        if (!self.networkReachability) {
            return;
        }
        // 创建block的回调
        __weak __typeof(self)weakSelf = self;
        AFNetworkReachabilityStatusBlock callback = ^(AFNetworkReachabilityStatus status) {
            __strong __typeof(weakSelf)strongSelf = weakSelf;
    
            strongSelf.networkReachabilityStatus = status;
            if (strongSelf.networkReachabilityStatusBlock) {
                strongSelf.networkReachabilityStatusBlock(status);
            }
    
        };
    
    
        id networkReachability = self.networkReachability;
        SCNetworkReachabilityContext context = {0, (__bridge void *)callback, AFNetworkReachabilityRetainCallback, AFNetworkReachabilityReleaseCallback, NULL};
        SCNetworkReachabilitySetCallback((__bridge SCNetworkReachabilityRef)networkReachability, AFNetworkReachabilityCallback, &context);
        // 加入到主线程的runloop里面去观察网路状态,设置为kCFRunLoopCommonModes
        SCNetworkReachabilityScheduleWithRunLoop((__bridge SCNetworkReachabilityRef)networkReachability, CFRunLoopGetMain(), kCFRunLoopCommonModes);
    
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),^{
            SCNetworkReachabilityFlags flags;
            if (SCNetworkReachabilityGetFlags((__bridge SCNetworkReachabilityRef)networkReachability, &flags)) {
                AFPostReachabilityStatusChange(flags, callback);
            }
        });
    }
    

    在文件设计的时候有个细节,暴露了两个方法,细想一下,作者考虑的真的是非常的周到,平常我们只需要观察一个domain就可以了,但是也不能排除需要观察多个domain的情况,真的很贴心

    + (instancetype)sharedManager;
    + (instancetype)manager;
    

    Security/AFSecurityPolicy.h

    AFSecurityPolicy 主要作用就是验证 HTTPS 请求的证书是否有效,如果 app 中有一些敏感信息或者涉及交易信息,一定要使用 HTTPS 来保证交易或者用户信息的安全。

    typedef NS_ENUM(NSUInteger, AFSSLPinningMode) {
        AFSSLPinningModeNone,
        AFSSLPinningModePublicKey,
        AFSSLPinningModeCertificate,
    };
    

    AFSSLPinningMode

    • AFSSLPinningModeNone 是默认的认证方式,只会在系统的信任的证书列表中对服务端返回的证书进行验证
    • AFSSLPinningModeCertificate 需要客户端预先保存服务端的证书
    • AFSSLPinningModePublicKey 也需要预先保存服务端发送的证书,但是这里只会验证证书中的公钥是否正确

    在使用 AFSecurityPolicy 验证服务端是否受到信任之前,要对其进行初始化,使用初始化方法时,主要目的是设置验证服务器是否受信任的方式。

    初始化的方法为:

    + (instancetype)defaultPolicy {
        AFSecurityPolicy *securityPolicy = [[self alloc] init];
        securityPolicy.SSLPinningMode = AFSSLPinningModeNone;
    
        return securityPolicy;
    }
    
    + (instancetype)policyWithPinningMode:(AFSSLPinningMode)pinningMode {
        return [self policyWithPinningMode:pinningMode withPinnedCertificates:[self defaultPinnedCertificates]];
    }
    
    + (instancetype)policyWithPinningMode:(AFSSLPinningMode)pinningMode withPinnedCertificates:(NSSet *)pinnedCertificates {
        AFSecurityPolicy *securityPolicy = [[self alloc] init];
        securityPolicy.SSLPinningMode = pinningMode;
    
        [securityPolicy setPinnedCertificates:pinnedCertificates];
    
        return securityPolicy;
    }
    
    // 获取本地所有的证书信息
    + (NSSet *)defaultPinnedCertificates {
        static NSSet *_defaultPinnedCertificates = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            NSBundle *bundle = [NSBundle bundleForClass:[self class]];
            _defaultPinnedCertificates = [self certificatesInBundle:bundle];
        });
    
        return _defaultPinnedCertificates;
    }
    

    然后里面有一大坨获取证书信息的方法

    static id AFPublicKeyForCertificate(NSData *certificate) {
        id allowedPublicKey = nil;
        SecCertificateRef allowedCertificate;
        SecCertificateRef allowedCertificates[1];
        CFArrayRef tempCertificates = nil;
        SecPolicyRef policy = nil;
        SecTrustRef allowedTrust = nil;
        SecTrustResultType result;
    
        allowedCertificate = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)certificate);
        __Require_Quiet(allowedCertificate != NULL, _out);
    
        allowedCertificates[0] = allowedCertificate;
        tempCertificates = CFArrayCreate(NULL, (const void **)allowedCertificates, 1, NULL);
    
        policy = SecPolicyCreateBasicX509();
        __Require_noErr_Quiet(SecTrustCreateWithCertificates(tempCertificates, policy, &allowedTrust), _out);
        __Require_noErr_Quiet(SecTrustEvaluate(allowedTrust, &result), _out);
    
        allowedPublicKey = (__bridge_transfer id)SecTrustCopyPublicKey(allowedTrust);
    
    _out:
        if (allowedTrust) {
            CFRelease(allowedTrust);
        }
    
        if (policy) {
            CFRelease(policy);
        }
    
        if (tempCertificates) {
            CFRelease(tempCertificates);
        }
    
        if (allowedCertificate) {
            CFRelease(allowedCertificate);
        }
    
        return allowedPublicKey;
    }
    
    static BOOL AFServerTrustIsValid(SecTrustRef serverTrust) {
        BOOL isValid = NO;
        SecTrustResultType result;
        __Require_noErr_Quiet(SecTrustEvaluate(serverTrust, &result), _out);
    
        isValid = (result == kSecTrustResultUnspecified || result == kSecTrustResultProceed);
    
    _out:
        return isValid;
    }
    
    static NSArray * AFCertificateTrustChainForServerTrust(SecTrustRef serverTrust) {
        CFIndex certificateCount = SecTrustGetCertificateCount(serverTrust);
        NSMutableArray *trustChain = [NSMutableArray arrayWithCapacity:(NSUInteger)certificateCount];
    
        for (CFIndex i = 0; i < certificateCount; i++) {
            SecCertificateRef certificate = SecTrustGetCertificateAtIndex(serverTrust, i);
            [trustChain addObject:(__bridge_transfer NSData *)SecCertificateCopyData(certificate)];
        }
    
        return [NSArray arrayWithArray:trustChain];
    }
    
    static NSArray * AFPublicKeyTrustChainForServerTrust(SecTrustRef serverTrust) {
        SecPolicyRef policy = SecPolicyCreateBasicX509();
        CFIndex certificateCount = SecTrustGetCertificateCount(serverTrust);
        NSMutableArray *trustChain = [NSMutableArray arrayWithCapacity:(NSUInteger)certificateCount];
        for (CFIndex i = 0; i < certificateCount; i++) {
            SecCertificateRef certificate = SecTrustGetCertificateAtIndex(serverTrust, i);
    
            SecCertificateRef someCertificates[] = {certificate};
            CFArrayRef certificates = CFArrayCreate(NULL, (const void **)someCertificates, 1, NULL);
    
            SecTrustRef trust;
        // 不满足条件直接就调到标记,神奇的宏
       __Require_noErr_Quiet(SecTrustCreateWithCertificates(certificates, policy, &trust), _out);
    
            SecTrustResultType result;
            __Require_noErr_Quiet(SecTrustEvaluate(trust, &result), _out);
    
            [trustChain addObject:(__bridge_transfer id)SecTrustCopyPublicKey(trust)];
    
        _out:
            if (trust) {
                CFRelease(trust);
            }
    
            if (certificates) {
                CFRelease(certificates);
            }
    
            continue;
        }
        CFRelease(policy);
    
        return [NSArray arrayWithArray:trustChain];
    }
    
    

    验证是否可信,也是AFURLSessionManager直接调用的方法

    - (BOOL)evaluateServerTrust:(SecTrustRef)serverTrust
                      forDomain:(NSString *)domain
    {
        if (domain && self.allowInvalidCertificates && self.validatesDomainName && (self.SSLPinningMode == AFSSLPinningModeNone || [self.pinnedCertificates count] == 0)) {
            // https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/NetworkingTopics/Articles/OverridingSSLChainValidationCorrectly.html
            //  According to the docs, you should only trust your provided certs for evaluation.
            //  Pinned certificates are added to the trust. Without pinned certificates,
            //  there is nothing to evaluate against.
            //
            //  From Apple Docs:
            //          "Do not implicitly trust self-signed certificates as anchors (kSecTrustOptionImplicitAnchors).
            //           Instead, add your own (self-signed) CA certificate to the list of trusted anchors."
            // 验证个人签名证书,要通过必须使用pinning
            NSLog(@"In order to validate a domain name for self signed certificates, you MUST use pinning.");
            return NO;
        }
    
        NSMutableArray *policies = [NSMutableArray array];
        if (self.validatesDomainName) {
            [policies addObject:(__bridge_transfer id)SecPolicyCreateSSL(true, (__bridge CFStringRef)domain)];
        } else {
            [policies addObject:(__bridge_transfer id)SecPolicyCreateBasicX509()];
        }
    
        SecTrustSetPolicies(serverTrust, (__bridge CFArrayRef)policies);
    
        if (self.SSLPinningMode == AFSSLPinningModeNone) {
            // 如果允许无效证书或者serverTrust服务命名空间可信,就返回yes,这是我们最常用的
            return self.allowInvalidCertificates || AFServerTrustIsValid(serverTrust);
        } else if (!AFServerTrustIsValid(serverTrust) && !self.allowInvalidCertificates) {
            return NO;
        }
    
        switch (self.SSLPinningMode) {
            case AFSSLPinningModeNone:
            default:
                return NO;
            case AFSSLPinningModeCertificate: {
                // 验证本地是否包含服务器的证书和当前验证的证书是一个证书
                NSMutableArray *pinnedCertificates = [NSMutableArray array];
                for (NSData *certificateData in self.pinnedCertificates) {
                    [pinnedCertificates addObject:(__bridge_transfer id)SecCertificateCreateWithData(NULL, (__bridge CFDataRef)certificateData)];
                }
                SecTrustSetAnchorCertificates(serverTrust, (__bridge CFArrayRef)pinnedCertificates);
    
                if (!AFServerTrustIsValid(serverTrust)) {
                    return NO;
                }
    
                // obtain the chain after being validated, which *should* contain the pinned certificate in the last position (if it's the Root CA)
                NSArray *serverCertificates = AFCertificateTrustChainForServerTrust(serverTrust);
                
                for (NSData *trustChainCertificate in [serverCertificates reverseObjectEnumerator]) {
                    if ([self.pinnedCertificates containsObject:trustChainCertificate]) {
                        return YES;
                    }
                }
                
                return NO;
            }
            case AFSSLPinningModePublicKey: {
                // 判断本地的证书的publickey和验证的证书是一致的,则通过
                NSUInteger trustedPublicKeyCount = 0;
                NSArray *publicKeys = AFPublicKeyTrustChainForServerTrust(serverTrust);
    
                for (id trustChainPublicKey in publicKeys) {
                    for (id pinnedPublicKey in self.pinnedPublicKeys) {
                        if (AFSecKeyIsEqualToKey((__bridge SecKeyRef)trustChainPublicKey, (__bridge SecKeyRef)pinnedPublicKey)) {
                            trustedPublicKeyCount += 1;
                        }
                    }
                }
                return trustedPublicKeyCount > 0;
            }
        }
        
        return NO;
    }
    

    Serialization/AFURLRequestSerialization.h

    AFURLRequestSerialization并非是一个类,而是一个协议,这个协议规定了如果序列化一个request的统一的入口,即便是序列化不同种类的数据,都能找到相同的入口,这样对于整个框架的可用性和规范性都是很不错的体验

    - (nullable NSURLRequest *)requestBySerializingRequest:(NSURLRequest *)request
                                   withParameters:(nullable id)parameters
                                            error:(NSError * _Nullable __autoreleasing *)error NS_SWIFT_NOTHROW;
    

    AFHTTPRequestSerializer是这个文件里面最重要的类,所以我们从这个类来体验作者的思路,作为一个request要设置useragent还有timeout等,就不一一进行介绍了,那些在HTTP协议里面讲的还是很清楚的,

    首先看一下这个类到底能干啥,看到暴露出来的接口为:

    // 初始化的方法
    - (instancetype)init {
        self = [super init];
        if (!self) {
            return nil;
        }
    
        self.stringEncoding = NSUTF8StringEncoding;
    
        self.mutableHTTPRequestHeaders = [NSMutableDictionary dictionary];
    
        // Accept-Language HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
        NSMutableArray *acceptLanguagesComponents = [NSMutableArray array];
        [[NSLocale preferredLanguages] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            float q = 1.0f - (idx * 0.1f);
            [acceptLanguagesComponents addObject:[NSString stringWithFormat:@"%@;q=%0.1g", obj, q]];
            *stop = q <= 0.5f;
        }];
        [self setValue:[acceptLanguagesComponents componentsJoinedByString:@", "] forHTTPHeaderField:@"Accept-Language"];
    
        NSString *userAgent = nil;
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wgnu"
    #if TARGET_OS_IOS
        // User-Agent Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43
        userAgent = [NSString stringWithFormat:@"%@/%@ (%@; iOS %@; Scale/%0.2f)", [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleExecutableKey] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleIdentifierKey], [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleVersionKey], [[UIDevice currentDevice] model], [[UIDevice currentDevice] systemVersion], [[UIScreen mainScreen] scale]];
    #elif TARGET_OS_WATCH
        // User-Agent Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43
        userAgent = [NSString stringWithFormat:@"%@/%@ (%@; watchOS %@; Scale/%0.2f)", [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleExecutableKey] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleIdentifierKey], [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleVersionKey], [[WKInterfaceDevice currentDevice] model], [[WKInterfaceDevice currentDevice] systemVersion], [[WKInterfaceDevice currentDevice] screenScale]];
    #elif defined(__MAC_OS_X_VERSION_MIN_REQUIRED)
        userAgent = [NSString stringWithFormat:@"%@/%@ (Mac OS X %@)", [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleExecutableKey] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleIdentifierKey], [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleVersionKey], [[NSProcessInfo processInfo] operatingSystemVersionString]];
    #endif
    #pragma clang diagnostic pop
        if (userAgent) {
            if (![userAgent canBeConvertedToEncoding:NSASCIIStringEncoding]) {
                NSMutableString *mutableUserAgent = [userAgent mutableCopy];
                if (CFStringTransform((__bridge CFMutableStringRef)(mutableUserAgent), NULL, (__bridge CFStringRef)@"Any-Latin; Latin-ASCII; [:^ASCII:] Remove", false)) {
                    userAgent = mutableUserAgent;
                }
            }
            [self setValue:userAgent forHTTPHeaderField:@"User-Agent"];
        }
    
        // HTTP Method Definitions; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
        self.HTTPMethodsEncodingParametersInURI = [NSSet setWithObjects:@"GET", @"HEAD", @"DELETE", nil];
        
        // 这些属性依赖于AFHTTPRequestSerializerObservedKeyPaths()方法,如果一旦某个路径
        // 的值设置为null那么就会自动的移除掉这个字段,不会拼接到request中去
        self.mutableObservedChangedKeyPaths = [NSMutableSet set];
        for (NSString *keyPath in AFHTTPRequestSerializerObservedKeyPaths()) {
            if ([self respondsToSelector:NSSelectorFromString(keyPath)]) {
                [self addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew context:AFHTTPRequestSerializerObserverContext];
            }
        }
    
        return self;
    }
    
    // 这种最普通的调用,也是我们最常用的调用就是这个方法
    - (NSMutableURLRequest *)requestWithMethod:(NSString *)method
                                     URLString:(NSString *)URLString
                                    parameters:(id)parameters
                                         error:(NSError *__autoreleasing *)error
    {
        NSParameterAssert(method);
        NSParameterAssert(URLString);
    
        NSURL *url = [NSURL URLWithString:URLString];
    
        NSParameterAssert(url);
    
        NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:url];
        mutableRequest.HTTPMethod = method;
        // 循环的添加到mutableRequest中,分别设置不同的字段
        for (NSString *keyPath in AFHTTPRequestSerializerObservedKeyPaths()) {
            if ([self.mutableObservedChangedKeyPaths containsObject:keyPath]) {
                [mutableRequest setValue:[self valueForKeyPath:keyPath] forKey:keyPath];
            }
        }
        // 调用真正做封装的方法
        mutableRequest = [[self requestBySerializingRequest:mutableRequest withParameters:parameters error:error] mutableCopy];
    
    	return mutableRequest;
    }
    
    - (NSURLRequest *)requestBySerializingRequest:(NSURLRequest *)request
                                   withParameters:(id)parameters
                                            error:(NSError *__autoreleasing *)error
    {
        NSParameterAssert(request);
    
        NSMutableURLRequest *mutableRequest = [request mutableCopy];
        
        // 通过 HTTPRequestHeaders 字典设置头部字段
        [self.HTTPRequestHeaders enumerateKeysAndObjectsUsingBlock:^(id field, id value, BOOL * __unused stop) {
            if (![request valueForHTTPHeaderField:field]) {
                [mutableRequest setValue:value forHTTPHeaderField:field];
            }
        }];
        
        // 调用 AFQueryStringFromParameters 将参数转换为查询参数
        NSString *query = nil;
        if (parameters) {
            // 如果需要定制querry字段的信息,请传进来一个block
            if (self.queryStringSerialization) {
                NSError *serializationError;
                query = self.queryStringSerialization(request, parameters, &serializationError);
    
                if (serializationError) {
                    if (error) {
                        *error = serializationError;
                    }
    
                    return nil;
                }
            } else {
                switch (self.queryStringSerializationStyle) {
                    case AFHTTPRequestQueryStringDefaultStyle:
                        query = AFQueryStringFromParameters(parameters);
                        break;
                }
            }
        }
        // 将 parameters 添加到 URL 或者 HTTP body 中
        if ([self.HTTPMethodsEncodingParametersInURI containsObject:[[request HTTPMethod] uppercaseString]]) {
            if (query) {
                mutableRequest.URL = [NSURL URLWithString:[[mutableRequest.URL absoluteString] stringByAppendingFormat:mutableRequest.URL.query ? @"&%@" : @"?%@", query]];
            }
        } else {
            // #2864: an empty string is a valid x-www-form-urlencoded payload
            if (!query) {
                query = @"";
            }
            if (![mutableRequest valueForHTTPHeaderField:@"Content-Type"]) {
                [mutableRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
            }
            [mutableRequest setHTTPBody:[query dataUsingEncoding:self.stringEncoding]];
        }
    
        return mutableRequest;
    }
    

    看看稍微要复杂一点的,比如要上传一个图片之类的,我们的请求就需要用另外的方法:

    - (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method
                                                  URLString:(NSString *)URLString
                                                 parameters:(NSDictionary *)parameters
                                  constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
                                                      error:(NSError *__autoreleasing *)error
    {
        NSParameterAssert(method);
        NSParameterAssert(![method isEqualToString:@"GET"] && ![method isEqualToString:@"HEAD"]);
    
        NSMutableURLRequest *mutableRequest = [self requestWithMethod:method URLString:URLString parameters:nil error:error];
    
        __block AFStreamingMultipartFormData *formData = [[AFStreamingMultipartFormData alloc] initWithURLRequest:mutableRequest stringEncoding:NSUTF8StringEncoding];
    
        if (parameters) {
            // AFQueryStringPairsFromDictionary这个方法是通过递归的方式把所有的参数都转换成
            // field和value的固定AFQueryStringPair格式
            for (AFQueryStringPair *pair in AFQueryStringPairsFromDictionary(parameters)) {
                NSData *data = nil;
                if ([pair.value isKindOfClass:[NSData class]]) {
                    data = pair.value;
                } else if ([pair.value isEqual:[NSNull null]]) {
                    data = [NSData data];
                } else {
                    data = [[pair.value description] dataUsingEncoding:self.stringEncoding];
                }
    
                if (data) {
                    // AFMultipartFormData这个data必须要遵守的协议,具体的原因和上面用到协议的方式是一样的
                    [formData appendPartWithFormData:data name:[pair.field description]];
                }
            }
        }
    
        if (block) {
            block(formData);
        }
    
        return [formData requestByFinalizingMultipartFormData];
    }
    
    

    还有一个很重要的AFStreamingMultipartFormData类,这个类是这样的:

    @interface AFStreamingMultipartFormData ()
    @property (readwrite, nonatomic, copy) NSMutableURLRequest *request;
    @property (readwrite, nonatomic, assign) NSStringEncoding stringEncoding;
    @property (readwrite, nonatomic, copy) NSString *boundary;
    @property (readwrite, nonatomic, strong) AFMultipartBodyStream *bodyStream;
    @end
    

    然后再看AFMultipartBodyStream:

    @interface AFMultipartBodyStream : NSInputStream <NSStreamDelegate>
    @property (nonatomic, assign) NSUInteger numberOfBytesInPacket;
    @property (nonatomic, assign) NSTimeInterval delay;
    @property (nonatomic, strong) NSInputStream *inputStream;
    @property (readonly, nonatomic, assign) unsigned long long contentLength;
    @property (readonly, nonatomic, assign, getter = isEmpty) BOOL empty;
    
    - (instancetype)initWithStringEncoding:(NSStringEncoding)encoding;
    - (void)setInitialAndFinalBoundaries;
    - (void)appendHTTPBodyPart:(AFHTTPBodyPart *)bodyPart;
    @end
    

    再看AFHTTPBodyPart:

    interface AFHTTPBodyPart : NSObject
    @property (nonatomic, assign) NSStringEncoding stringEncoding;
    @property (nonatomic, strong) NSDictionary *headers;
    @property (nonatomic, copy) NSString *boundary;
    @property (nonatomic, strong) id body;
    @property (nonatomic, assign) unsigned long long bodyContentLength;
    @property (nonatomic, strong) NSInputStream *inputStream;
    
    @property (nonatomic, assign) BOOL hasInitialBoundary;
    @property (nonatomic, assign) BOOL hasFinalBoundary;
    
    @property (readonly, nonatomic, assign, getter = hasBytesAvailable) BOOL bytesAvailable;
    @property (readonly, nonatomic, assign) unsigned long long contentLength;
    
    - (NSInteger)read:(uint8_t *)buffer
            maxLength:(NSUInteger)length;
    @end
    

    AFStreamingMultipartFormData调用append(名字太长,就是协议里面方法)方法,就是他持有的AFMultipartBodyStream会把数据变换成AFHTTPBodyPart方法存放到一个数组里面去,当调用requestByFinalizingMultipartFormData会走[self.request setHTTPBodyStream:self.bodyStream]方法,因为AFMultipartBodyStream是NSStream的子类,
    重写了这个方法,就会调用

    - (NSInteger)read:(uint8_t *)buffer
            maxLength:(NSUInteger)length
    {
        if ([self streamStatus] == NSStreamStatusClosed) {
            return 0;
        }
    
        NSInteger totalNumberOfBytesRead = 0;
        // 会循环的去读取HTTPBodyPart里面的数据
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wgnu"
        while ((NSUInteger)totalNumberOfBytesRead < MIN(length, self.numberOfBytesInPacket)) {
            if (!self.currentHTTPBodyPart || ![self.currentHTTPBodyPart hasBytesAvailable]) {
                if (!(self.currentHTTPBodyPart = [self.HTTPBodyPartEnumerator nextObject])) {
                    break;
                }
            } else {
                NSUInteger maxLength = MIN(length, self.numberOfBytesInPacket) - (NSUInteger)totalNumberOfBytesRead;
                NSInteger numberOfBytesRead = [self.currentHTTPBodyPart read:&buffer[totalNumberOfBytesRead] maxLength:maxLength];
                if (numberOfBytesRead == -1) {
                    self.streamError = self.currentHTTPBodyPart.inputStream.streamError;
                    break;
                } else {
                    totalNumberOfBytesRead += numberOfBytesRead;
    
                    if (self.delay > 0.0f) {
                        [NSThread sleepForTimeInterval:self.delay];
                    }
                }
            }
        }
    #pragma clang diagnostic pop
    
        return totalNumberOfBytesRead;
    }
    

    另外在拼接HTTPBodydata的时候加了标记,在读入的时候需要重新的去标记:

    - (void)setInitialAndFinalBoundaries {
        if ([self.HTTPBodyParts count] > 0) {
            for (AFHTTPBodyPart *bodyPart in self.HTTPBodyParts) {
                bodyPart.hasInitialBoundary = NO;
                bodyPart.hasFinalBoundary = NO;
            }
    
            [[self.HTTPBodyParts firstObject] setHasInitialBoundary:YES];
            [[self.HTTPBodyParts lastObject] setHasFinalBoundary:YES];
        }
    }
    

    总的来说,设计的思路还是很清晰的,但是牵扯到一些HTTP的概念,还是需要好好的去学习,毕竟这个协议是当前应用最广泛的协议之一


    Serialization/AFURLResponseSerialization.h

    所有这个文件里面的类,都遵守了这个协议,作为基类AFHTTPResponseSerializer,还有一个验证返回结果是否正常的类

    - (nullable id)responseObjectForResponse:(nullable NSURLResponse *)response
                               data:(nullable NSData *)data
                              error:(NSError * _Nullable __autoreleasing *)error NS_SWIFT_NOTHROW;
    
    @end
    
    - (BOOL)validateResponse:(nullable NSHTTPURLResponse *)response
                        data:(nullable NSData *)data
                       error:(NSError * _Nullable __autoreleasing *)error;
    
    

    这个类所有的技巧都在这两个方法里面了:

    - (BOOL)validateResponse:(NSHTTPURLResponse *)response
                        data:(NSData *)data
                       error:(NSError * __autoreleasing *)error
    {
        BOOL responseIsValid = YES;
        NSError *validationError = nil;
        //返回内容类型无效  返回状态码无效
        if (response && [response isKindOfClass:[NSHTTPURLResponse class]]) {
            if (self.acceptableContentTypes && ![self.acceptableContentTypes containsObject:[response MIMEType]]) {
                if ([data length] > 0 && [response URL]) {
                    NSMutableDictionary *mutableUserInfo = [@{
                                                              NSLocalizedDescriptionKey: [NSString stringWithFormat:NSLocalizedStringFromTable(@"Request failed: unacceptable content-type: %@", @"AFNetworking", nil), [response MIMEType]],
                                                              NSURLErrorFailingURLErrorKey:[response URL],
                                                              AFNetworkingOperationFailingURLResponseErrorKey: response,
                                                            } mutableCopy];
                    if (data) {
                        mutableUserInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] = data;
                    }
    
                    validationError = AFErrorWithUnderlyingError([NSError errorWithDomain:AFURLResponseSerializationErrorDomain code:NSURLErrorCannotDecodeContentData userInfo:mutableUserInfo], validationError);
                }
    
                responseIsValid = NO;
            }
            // 根据在初始化方法中初始化的属性 acceptableContentTypes 和 acceptableStatusCodes 来判断当前响应是否有效
            if (self.acceptableStatusCodes && ![self.acceptableStatusCodes containsIndex:(NSUInteger)response.statusCode] && [response URL]) {
                NSMutableDictionary *mutableUserInfo = [@{
                                                   NSLocalizedDescriptionKey: [NSString stringWithFormat:NSLocalizedStringFromTable(@"Request failed: %@ (%ld)", @"AFNetworking", nil), [NSHTTPURLResponse localizedStringForStatusCode:response.statusCode], (long)response.statusCode],
                                                   NSURLErrorFailingURLErrorKey:[response URL],
                                                   AFNetworkingOperationFailingURLResponseErrorKey: response,
                                           } mutableCopy];
    
                if (data) {
                    mutableUserInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] = data;
                }
                
                validationError = AFErrorWithUnderlyingError([NSError errorWithDomain:AFURLResponseSerializationErrorDomain code:NSURLErrorBadServerResponse userInfo:mutableUserInfo], validationError);
    
                responseIsValid = NO;
            }
        }
    
        if (error && !responseIsValid) {
            *error = validationError;
        }
    
        return responseIsValid;
    }
    
    #pragma mark - AFURLResponseSerialization
    
    - (id)responseObjectForResponse:(NSURLResponse *)response
                               data:(NSData *)data
                              error:(NSError *__autoreleasing *)error
    {
        if (![self validateResponse:(NSHTTPURLResponse *)response data:data error:error]) {
            if (!error || AFErrorOrUnderlyingErrorHasCodeInDomain(*error, NSURLErrorCannotDecodeContentData, AFURLResponseSerializationErrorDomain)) {
                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);
            }
        }
    
        id responseObject = nil;
        NSError *serializationError = nil;
        @autoreleasepool {
            NSString *responseString = [[NSString alloc] initWithData:data encoding:stringEncoding];
            if (responseString && ![responseString isEqualToString:@" "]) {
                // Workaround for a bug in NSJSONSerialization when Unicode character escape codes are used instead of the actual character
                // See http://stackoverflow.com/a/12843465/157142
                data = [responseString dataUsingEncoding:NSUTF8StringEncoding];
    
                if (data) {
                    if ([data length] > 0) {
                        responseObject = [NSJSONSerialization JSONObjectWithData:data options:self.readingOptions error:&serializationError];
                    } else {
                        return nil;
                    }
                } else {
                    NSDictionary *userInfo = @{
                                               NSLocalizedDescriptionKey: NSLocalizedStringFromTable(@"Data failed decoding as a UTF-8 string", @"AFNetworking", nil),
                                               NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedStringFromTable(@"Could not decode string: %@", @"AFNetworking", nil), responseString]
                                               };
    
                    serializationError = [NSError errorWithDomain:AFURLResponseSerializationErrorDomain code:NSURLErrorCannotDecodeContentData userInfo:userInfo];
                }
            }
        }
    
        if (self.removesKeysWithNullValues && responseObject) {
            responseObject = AFJSONObjectByRemovingKeysWithNullValues(responseObject, self.readingOptions);
        }
    
        if (error) {
            *error = AFErrorWithUnderlyingError(serializationError, *error);
        }
    
        return responseObject;
    }
    

    其他的也是如此,只不过就是在序列化的时候会使用其他格式。


    到这里我们基本上就把AFNetworking的代码读了一遍,在这个库的代码中看到了开源库代码的易用、简洁、高效等等特性,这些设计的思路很值得我们去品味琢磨。(原创文章,转载请注明出处)

  • 相关阅读:
    第六周学习进度总结
    构建之法阅读笔记03
    文件操作
    数组相关
    compareTo
    我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? 比如n=3时,2*3的矩形块有3种覆盖方法:
    从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行
    整数中1出现的次数
    Java泛型(看着好有用)
    输入一个整数,输出该数32位二进制表示中1的个数。其中负数用补码表示。
  • 原文地址:https://www.cnblogs.com/sanjianghuiliu/p/6789800.html
Copyright © 2011-2022 走看看