源代码剖析

AFSecurityPolicy

AFSecurityPolicy 类用于服务器 SSL 证书安全连接,不讲解,自行观看其实现。

AFURLRequestSerialization

这里包含了三个类和两个函数:

  • AFHTTPRequestSerializer

  • AFJSONRequestSerializer

  • AFPropertyListRequestSerializer

  • NSString AFPercentEscapedStringFromString(NSString string)

  • NSString AFQueryStringFromParameters(NSString string)

后面两个函数一个用于请求字符串转义,另一个是生成编码 URL 请求参数工具方法

AFHTTPRequestSerializer

AFHTTPRequestSerializer 本身继承自 NSObject,实现了 AFURLRequestSerialization 协议,此协议是框架定义的,代码如下

@protocol AFURLRequestSerialization <NSObject, NSSecureCoding, NSCopying>

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

@end

就是根据 request 和请求参数构造 NSURLRequest 对象。
然后其中就是一系列成员变量用于存储 HTTP 请求所需的各项参数,看到这里大家应该也看出来了,这些类实际上不是真正存储 HTTPRequest 的对象,而是构造出一个模型类,模型类实现了特定生成方法,然后生成方法根据类成员变量构造实际的 HTTPRequest。明白了这一点,代码就很容易看懂了,因为完全是根据 HTTP 协议所需构造的类模型。有 HTTPHeader,有 RequestBody。先来看初始化方法

{
    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];

    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;
}

首先设置编码类型为 NSUTF8StringEncoding,然后填充 Accept-Language 请求头,设置 UserAgent,设置需要在 URI 中编码的 HTTP 方法名称。

AFJSONRequestSerializer

AFJSONRequestSerializer 继承自 AFHTTPRequestSerializer,相对于 AFHTTPRequestSerializer 只是将参数全部设置到 HTTPBody 了而已。其中 JSON 转化使用的就是苹果自家的 NSJSONSerialization。

- (NSURLRequest *)requestBySerializingRequest:(NSURLRequest *)request
                               withParameters:(id)parameters
                                        error:(NSError *__autoreleasing *)error
{
    NSParameterAssert(request);

    if ([self.HTTPMethodsEncodingParametersInURI containsObject:[[request HTTPMethod] uppercaseString]]) {
        return [super requestBySerializingRequest:request withParameters:parameters error:error];
    }

    NSMutableURLRequest *mutableRequest = [request mutableCopy];

    [self.HTTPRequestHeaders enumerateKeysAndObjectsUsingBlock:^(id field, id value, BOOL * __unused stop) {
        if (![request valueForHTTPHeaderField:field]) {
            [mutableRequest setValue:value forHTTPHeaderField:field];
        }
    }];

    if (parameters) {
        if (![mutableRequest valueForHTTPHeaderField:@"Content-Type"]) {
            [mutableRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        }

        [mutableRequest setHTTPBody:[NSJSONSerialization dataWithJSONObject:parameters options:self.writingOptions error:error]];
    }

    return mutableRequest;
}

AFPropertyListRequestSerializer

AFPropertyListRequestSerializer 基本和 AFJSONRequestSerializer 差不多,只不过使用 NSPropertyListSerialization 将其转化为了 PropertyList 的 XML 格式罢了。

AFURLResponseSerialization

AFURLResponseSerialization 包含了

  • AFHTTPResponseSerializer

  • AFXMLParserResponseSerializer

  • AFPropertyListResponseSerializer

  • AFJSONResponseSerializer

  • AFImageResponseSerializer

  • AFCompoundResponseSerializer

其实和 AFURLRequestSerialization 的流程是一样的,都是通过存储属性,使用协议方法编码解码相应的 Request 或者 Response。
下面是 AFHTTPRequestSerialization 的初始化方法

- (instancetype)init {
    self = [super init];
    if (!self) {
        return nil;
    }

    self.stringEncoding = NSUTF8StringEncoding;

    self.acceptableStatusCodes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 100)];
    self.acceptableContentTypes = nil;

    return self;
}

确实比请求对象的初始化方法简单太多了,只有编码类型、有效响应状态码(200 - 299)、可接受内容类型等成员变量初始化。
然后再看实现的协议方法

- (id)responseObjectForResponse:(NSURLResponse *)response
                           data:(NSData *)data
                          error:(NSError *__autoreleasing *)error
{
    [self validateResponse:(NSHTTPURLResponse *)response data:data error:error];

    return data;
}

实际上就是调用了另一个实例方法 validateResponse:data:

- (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]] &&
            !([response MIMEType] == nil && [data length] == 0)) {

            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;
        }

        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;
}

看起来代码虽然很多,但是不要慌,仔细看下去,实际上就只有两项检查:检查 Content-Type、检查 HTTPStatusCode
AFJSONResponseSerializer 继承于 AFHTTPResponseSerializer。相比于父类,这个类才是真正开始使用返回的数据,首先在初始化函数中添加如下代码

self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil];

也就是说接受 JSON 数据并且转义。然后就是协议方法

- (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;
        }
    }

    id responseObject = nil;
    NSError *serializationError = 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
    BOOL isSpace = [data isEqualToData:[NSData dataWithBytes:" " length:1]];
    if (data.length > 0 && !isSpace) {
        responseObject = [NSJSONSerialization JSONObjectWithData:data options:self.readingOptions error:&serializationError];
    } else {
        return nil;
    }

    if (self.removesKeysWithNullValues && responseObject) {
        responseObject = AFJSONObjectByRemovingKeysWithNullValues(responseObject, self.readingOptions);
    }

    if (error) {
        *error = AFErrorWithUnderlyingError(serializationError, *error);
    }

    return responseObject;
}

除了验证返回的数据外,还使用 NSJSONSerialization 将数据转义成字典或者数组。除此以外,还根据 removesKeysWithNullValues 的值决定是否移除 [NSNull null] 类型,AFJSONObjectByRemovingKeysWithNullValues 函数使用递归的方法,最终构造出无 null 的对象。这点确实可以借鉴一二。

AFURLSessionManager

这个类是最大的类,AFURLSessionManager 继承自 NSObject,实现 <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate, NSURLSessionDownloadDelegate, NSSecureCoding, NSCopying> 协议,用过苹果网络通信的朋友应该知道,NSURLSession 使用 block 或者 delegate 传递各类状态,这里实际上是使用了 delegate。
每个 AFURLSessionManager 都持有 NSURLSession、NSOperationQueue、id<AFURLResponseSerialization>、AFSecurityPolicy、AFNetworkReachabilityManager、NSArray<NSURLSessionTask >、NSArray<NSURLSessionDataTask >、NSArray<NSURLSessionUploadTask >、NSArray<NSURLSessionDownloadTask > 等成员变量,这里基本上也能看出 AFURLSessionManager 到底做了什么,先来看初始化函数

- (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 = [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

    self.mutableTaskDelegatesKeyedByTaskIdentifier = [[NSMutableDictionary alloc] init];

    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;
}

这里传入了一个参数 NSURLSessionConfiguration,熟悉网络编程的朋友应该知道,NSURLSession 有三种配置类型 defaultSessionConfigurationephemeralSessionConfigurationbackgroundSessionConfiguration,三者区别如下:

  1. Default session 使用持久磁盘 cache,用户凭证存储在钥匙串中。

  2. Ephemeral session 不存储任何数据到磁盘中。

  3. Background session 和 Default session 类似,但是在单独进程中运行

一般情况下是 Default session configuration 类型。operationQueue 用于异步回调使用 self.session = [NSURLSession sessionWithConfiguration:self.sessionConfiguration delegate:self delegateQueue:self.operationQueue]; 则很显而易见的看出来 session 创建的过程。同时创建 AFJSONResponseSerializer 作为响应序列化器,创建 securityPolicy 和 reachabilityManager。
最后使用了很巧妙的方法,getTasksWithCompletionHandler 函数返回所有的 task,然后存储到各自的数组中。
对于子类调用的 dataTaskWithRequest:uploadProgress:downloadProgress:completionHandler: 实际上是调用了 url_session_manager_create_task_safely block,实际上调用的是 [NSURLSession dataTaskWithRequest],然后调用 - [AFURLSessionManager addDelegateForDataTask:uploadProgress:downloadProgress:completionHandler:] 将 task 的回调设置为自身,大家可以注意到,代码中有很多地方使用了锁机制,因为回调本身是放到 GCD 多线程中的,所以需要注意竞争问题导致的资源抢占。

总结

AFNetworking 实际上只是对苹果本身的 NSURLSession 做了封装,帮助开发者更容易获得各类信息,比如进度条、信息编码解码,而真正想要使用好网络通信,苹果自身提供的 NSURLSession 才是需要仔细研究的。笔者认为,AFNetworking 实际上是开源项目中难得的容易阅读,代码也不多的项目,非常值得作为第一个入手的源码阅读项目。


山河永寂
2.4k 声望159 粉丝