美文网首页iOS Developer程序员
AFNetworking源码分析 (3)--AFURLReque

AFNetworking源码分析 (3)--AFURLReque

作者: Arnold134777 | 来源:发表于2017-06-17 22:15 被阅读104次

    系列文章:

    上文中我们分析了AF中关于网络下载,NSURLSessionDownloadTask的分析。
    本章将开始分析Serialization中的一个核心类请求数据封装类--AFURLRequestSerialization

    1.初始化requestSerializer

    参考例子沿用(1)中的例子:

    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", @"text/json" ,@"text/javascript", nil];
    session.responseSerializer = [AFHTTPResponseSerializer serializer];
    [session GET:@"https://www.baidu.com"
      parameters:nil
        progress:nil
         success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
             NSLog(@"请求成功");
         } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
              NSLog(@"请求失败");
         }];
    

    查看manager的调用栈我们在AFHTTPSessionManager的如下接口完成对全局
    requestSerializer的初始化。

    - (instancetype)initWithBaseURL:(NSURL *)url
               sessionConfiguration:(NSURLSessionConfiguration *)configuration
    {
        ...
        self.requestSerializer = [AFHTTPRequestSerializer serializer];
        ...
    
        return self;
    }
    

    1.1 [AFHTTPRequestSerializer serializer]

    // 1.指定数据的编码格式
    self.stringEncoding = NSUTF8StringEncoding;
    
    // 2.请求的头部信息字典
    self.mutableHTTPRequestHeaders = [NSMutableDictionary dictionary];
    
    // 3.设置头部中请求的自然语言列表
    // 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"];
    
    // 4.设置请求的UA
    NSString *userAgent = nil;
    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]];
    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];
    
    // 5.kvo的方式监听allowsCellularAccess,cachePolicy,HTTPShouldHandleCookies,HTTPShouldUsePipelining,networkServiceType,timeoutInterval的属性的变化
    self.mutableObservedChangedKeyPaths = [NSMutableSet set];
    for (NSString *keyPath in AFHTTPRequestSerializerObservedKeyPaths()) {
        if ([self respondsToSelector:NSSelectorFromString(keyPath)]) {
            [self addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionNew context:AFHTTPRequestSerializerObserverContext];
        }
    }
    

    举例当外部更改cachePolicy的值时,依次执行如下方法:

     // 1.必须调用willChangeValueForKey与didChangeValueForKey自己触发observeValueForKeyPath的调用,因为代码段2
    - (void)setCachePolicy:(NSURLRequestCachePolicy)cachePolicy {
        [self willChangeValueForKey:NSStringFromSelector(@selector(cachePolicy))];
        _cachePolicy = cachePolicy;
        [self didChangeValueForKey:NSStringFromSelector(@selector(cachePolicy))];
    }
    
    // 2.可以指定属性变化时observeValueForKeyPath是否自动执行,很显然这里监听的几个属性都是需要手动触发,这也就解释了1中的问题
    + (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key {
        if ([AFHTTPRequestSerializerObservedKeyPaths() containsObject:key]) {
            return NO;
        }
    
        return [super automaticallyNotifiesObserversForKey:key];
    }
    
    // 3.监听上述kvo监听的几个属性值的变化,操作全局的变化的属性集合
    - (void)observeValueForKeyPath:(NSString *)keyPath
                          ofObject:(__unused id)object
                            change:(NSDictionary *)change
                           context:(void *)context
    {
        if (context == AFHTTPRequestSerializerObserverContext) {
            if ([change[NSKeyValueChangeNewKey] isEqual:[NSNull null]]) {
                [self.mutableObservedChangedKeyPaths removeObject:keyPath];
            } else {
                [self.mutableObservedChangedKeyPaths addObject:keyPath];
            }
        }
    }
    

    2.根据requestSerializer初始化request

    初始化request的代码如下:

    NSError *serializationError = nil;
    NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:method URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters error:&serializationError];
    

    下面我们来具体分析具体的处理逻辑。

    2.1 requestWithMethod:URLString:parameters:error:

    // 1.初始化request的实例
    NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:url];
    mutableRequest.HTTPMethod = method;
    
    // 2.将requestSerializer 中的存储的请求策略的属性值赋值给re  quest
    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];
    

    2.2 requestBySerializingRequest:withParameters:error:

    该接口是AFURLRequestSerialization 协议定义的接口,该协议同时实现NSObject,NSSecureCoding, NSCopying 三个协议用于实现基本的Object行为,安全编码,拷贝功能。下面我们会依次分析实现这个协议的三个类:AFHTTPRequestSerializer,AFJSONRequestSerializer,AFPropertyListRequestSerializer

    2.2.1 AFHTTPRequestSerializer

    - (NSURLRequest *)requestBySerializingRequest:(NSURLRequest *)request
                                   withParameters:(id)parameters
                                            error:(NSError *__autoreleasing *)error
    {
        NSParameterAssert(request);
        
        // 1.遍历外部设置的httpRequestHeaders然后依次设置到mutableRequest中
        NSMutableURLRequest *mutableRequest = [request mutableCopy];
    
        [self.HTTPRequestHeaders enumerateKeysAndObjectsUsingBlock:^(id field, id value, BOOL * __unused stop) {
            if (![request valueForHTTPHeaderField:field]) {
                [mutableRequest setValue:value forHTTPHeaderField:field];
            }
        }];
    
       // 2.处理请求的参数
        NSString *query = nil;
        if (parameters) {
            // 2.1 如果外部自己实现参数的处理则直接获取外部处理数据
            if (self.queryStringSerialization) {
                NSError *serializationError;
                query = self.queryStringSerialization(request, parameters, &serializationError);
    
                if (serializationError) {
                    if (error) {
                        *error = serializationError;
                    }
    
                    return nil;
                }
            } else {
                // 2.2 否则走默认的实现方式,这里递归的方式实现(下面会具体分析)
                switch (self.queryStringSerializationStyle) {
                    case AFHTTPRequestQueryStringDefaultStyle:
                        query = AFQueryStringFromParameters(parameters);
                        break;
                }
            }
        }
    
        // 3.将parameters处理后的数据拼接到URL中还是设置到httpBody中,默认的`GET`, `HEAD`,`DELETE`是拼接到URL中,`POST`,`PUT`是设置到httpBody中
        if ([self.HTTPMethodsEncodingParametersInURI containsObject:[[request HTTPMethod] uppercaseString]]) {
            if (query && query.length > 0) {
                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;
    }
    
    2.2.1.1 AFQueryStringFromParameters(NSDictionary *parameters)

    源码注释很清楚,此处只写出核心思想:

    • 核心函数:AFQueryStringPairsFromKeyAndValue
    • 核心思想:递归的方式实现,针对NSDictionary,NSArray,NSSet 三种数据遍历处理,然后针对每个内部元素递归处理。
    • Model:AFQueryStringPair是临时存储key与value的实例类,其中有一个核心的函数AFPercentEscapedStringFromString
    2.2.1.2 AFPercentEscapedStringFromString(NSString *string)
    NSString * AFPercentEscapedStringFromString(NSString *string) {
    
        // 1.将部分分隔符与保留字排除在允许不编码的字符集内
        static NSString * const kAFCharactersGeneralDelimitersToEncode = @":#[]@"; // does not include "?" or "/" due to RFC 3986 - Section 3.4
        static NSString * const kAFCharactersSubDelimitersToEncode = @"!$&'()*+,;=";
        
        NSMutableCharacterSet * allowedCharacterSet = [[NSCharacterSet URLQueryAllowedCharacterSet] mutableCopy];
        [allowedCharacterSet removeCharactersInString:[kAFCharactersGeneralDelimitersToEncode stringByAppendingString:kAFCharactersSubDelimitersToEncode]];
    
        // FIXME: https://github.com/AFNetworking/AFNetworking/pull/3028
        // return [string stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet];
    
        // 2.处理多字节字符的问题,我们平常书写的字符, 并不全部都是用唯一的一个16位字符来表示, 而是有一部分用两个16位字符来表示(表情字符)
        static NSUInteger const batchSize = 50;
    
        NSUInteger index = 0;
        NSMutableString *escaped = @"".mutableCopy;
       
        while (index < string.length) {
    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Wgnu"
            NSUInteger length = MIN(string.length - index, batchSize);
    #pragma GCC diagnostic pop
            NSRange range = NSMakeRange(index, length);
            // 2.1 分段处理一个个完整的字符,然后再次拼接
            // To avoid breaking up character sequences such as 👴🏻👮🏽
            range = [string rangeOfComposedCharacterSequencesForRange:range];
    
            NSString *substring = [string substringWithRange:range];
            NSString *encoded = [substring stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet];
            [escaped appendString:encoded];
    
            index += range.length;
        }
    
        return escaped;
    }
    

    2.2.2 AFJSONRequestSerializer,AFPropertyListRequestSerializer

    都是AFHTTPRequestSerializer的派生类,一般针对POST,PUT的网络请求,对请求的字典参数分别使用NSJSONSerializationNSPropertyListSerialization 序列化。

    /**
     `AFJSONRequestSerializer` is a subclass of `AFHTTPRequestSerializer` that encodes parameters as JSON using `NSJSONSerialization`, setting the `Content-Type` of the encoded request to `application/json`.
     */
    @interface AFJSONRequestSerializer : AFHTTPRequestSerializer
    
    /**
     `AFPropertyListRequestSerializer` is a subclass of `AFHTTPRequestSerializer` that encodes parameters as JSON using `NSPropertyListSerializer`, setting the `Content-Type` of the encoded request to `application/x-plist`.
     */
    @interface AFPropertyListRequestSerializer : AFHTTPRequestSerializer
    

    2.3 multipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock:error:

    我们先看看接口注释:

    Creates an `NSMutableURLRequest` object with the specified HTTP method and URLString, and constructs a `multipart/form-data` 
    HTTP body, using the specified parameters and multipart form data block。
    Multipart form requests are automatically streamed, reading files directly from disk along with in-memory data in a single HTTP body. The resulting `NSMutableURLRequest` object has an `HTTPBodyStream` property, so refrain from setting `HTTPBodyStream` or `HTTPBody` on this request object, as it will clear out the multipart form body stream.
    

    显而易见这里是上传数据的接口。

    2.3.1 图片上传的例子

    首先利用ImageServer,搭建一个本地的图片服务器供iOS上传接口测试使用。下面是iOS具体上传的例子:

    - (void)uploadImage{
        self.session = [AFHTTPSessionManager manager];
        _session.responseSerializer = [AFJSONResponseSerializer serializer];
        _session.responseSerializer.acceptableContentTypes = nil;
        NSString *path = [[NSBundle mainBundle] pathForResource:@"github_1"
                                               ofType:@"jpeg"];
        NSData *data = [NSData dataWithContentsOfFile:path];
        NSString *url = @"http://192.168.2.12:8888/";
        NSMutableURLRequest *request = [_session.requestSerializer
                                        multipartFormRequestWithMethod:@"POST"
                                        URLString:url
                                        parameters:nil
                                        constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                                            [formData appendPartWithFileData:data
                                                                        name:@"file"
                                                                    fileName:@"github_1.jpeg"
                                                                    mimeType:@"application/octet-stream"];
                                        }
                                        
                                        error:nil];
        request.timeoutInterval = 10;
        
        NSURLSessionUploadTask *uploadTask = [_session uploadTaskWithRequest:request fromData:nil progress:nil completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
            if (error == nil) {
                NSLog(@"文件上传成功");
            } else {
                NSLog(@"文件上传失败 -> %@",error);
            }
        }];
        [uploadTask resume];
    }
    

    2.3.2 具体的实现分析

    我们先使用Charles抓包看一下请求的格式:

    POST / HTTP/1.1
    Host    127.0.0.1:8989
    Content-Type    multipart/form-data; boundary=Boundary+0544FD8CDFD7A4C1
    Connection  keep-alive
    Accept  */*
    User-Agent  InterView/1.0 (iPhone; iOS 10.0; Scale/2.00)
    Accept-Language en;q=1, zh-Hans-US;q=0.9
    Content-Length  11027
    Accept-Encoding gzip, deflate
    
    
    --Boundary+0544FD8CDFD7A4C1
    Content-Disposition: form-data; name="file"; filename="github_1.jpeg"
    Content-Type: application/octet-stream
    
    // 二进制数据
    
    --Boundary+0544FD8CDFD7A4C1--
    
    
    • Content-Type必须是这样的Content-Type: multipart/form-data; boundary=${bound},类似0544FD8CDFD7A4C1是随机数生成的。
    • 每一部分以$--{bound}开始,以--{bound}--结束。

    下面我们针对代码具体分析。

    2.3.2.1 AFHTTPRequestSerializer multipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock:error:
    // 1.初始化一个待提交的multipart的实例
    __block AFStreamingMultipartFormData *formData = [[AFStreamingMultipartFormData alloc] initWithURLRequest:mutableRequest stringEncoding:NSUTF8StringEncoding];
    
    // 2.针对请求的参数依次拼接到请求的formData中
    if (parameters) {
        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) {
                [formData appendPartWithFormData:data name:[pair.field description]];
            }
        }
    }
    
    // 3.外部如果需要对提交的表单做操作可以实现该block,完成自定义的操作
    if (block) {
        block(formData);
    }
    
    // 4.将整理的表单数据配置到请求的实例中
    return [formData requestByFinalizingMultipartFormData];
    
    2.3.2.2 AFStreamingMultipartFormData initWithURLRequest:stringEncoding:
    self.request = urlRequest;
    self.stringEncoding = encoding;
    
    // 1.这里说明了是随机数字的16进制字符拼接(见下接口实现)
    self.boundary = AFCreateMultipartFormBoundary();
    
    // 2.初始化数据流的实例,后续会设置到请求request中
    self.bodyStream = [[AFMultipartBodyStream alloc] initWithStringEncoding:encoding];
    
    
    static NSString * AFCreateMultipartFormBoundary() {
        return [NSString stringWithFormat:@"Boundary+%08X%08X", arc4random(), arc4random()];
    }
    
    2.3.2.3 AFStreamingMultipartFormData requestByFinalizingMultipartFormData
    - (NSMutableURLRequest *)requestByFinalizingMultipartFormData {
        if ([self.bodyStream isEmpty]) {
            return self.request;
        }
    
        // 1.对bodyStream中的HTTPBodyParts集合中的每一个AFHTTPBodyPart做hasInitialBoundary,hasFinalBoundary的标识,以便区分设置2.2.2开始部分请求数据。
        
        [self.bodyStream setInitialAndFinalBoundaries];
        
        // 2.将数据流绑定到请求的request实例中
        // 官方描述:The input stream should be unopened and the receiver will take over as the stream’s delegate
        [self.request setHTTPBodyStream:self.bodyStream];
    
        // 3.设置请求的头信息
        [self.request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", self.boundary] forHTTPHeaderField:@"Content-Type"];
        [self.request setValue:[NSString stringWithFormat:@"%llu", [self.bodyStream contentLength]] forHTTPHeaderField:@"Content-Length"];
    
        return self.request;
    }
    
    2.3.2.4 read:maxLength:

    [self.request setHTTPBodyStream:self.bodyStream];设置后,请求任务开始执行,下面的方法就会被回调:

    - (NSInteger)read:(uint8_t *)buffer
            maxLength:(NSUInteger)length
    {
        if ([self streamStatus] == NSStreamStatusClosed) {
            return 0;
        }
    
        NSInteger totalNumberOfBytesRead = 0;
        
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wgnu"
    
        // 1.遍历的方式读取么一个AFHTTPBodyPart中的数据
        while ((NSUInteger)totalNumberOfBytesRead < MIN(length, self.numberOfBytesInPacket)) {
            if (!self.currentHTTPBodyPart || ![self.currentHTTPBodyPart hasBytesAvailable]) {
                // 2.读取到最后一个为nil时跳出循环
                if (!(self.currentHTTPBodyPart = [self.HTTPBodyPartEnumerator nextObject])) {
                    break;
                }
            } else {
               // 3.读取当前的AFHTTPBodyPart中的收
                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;
    }
    
    2.3.2.5 AFHTTPBodyPart read:maxLength:
    // 四个状态的改变,表示依次解析顶部分割标识,headers数据,二进制数据bodyData,底部分割标识
    - (NSInteger)read:(uint8_t *)buffer
            maxLength:(NSUInteger)length
    {
        NSInteger totalNumberOfBytesRead = 0;
    
        if (_phase == AFEncapsulationBoundaryPhase) {
           
        }
    
        if (_phase == AFHeaderPhase) {
        
        }
    
        if (_phase == AFBodyPhase) {
            
        }
    
        if (_phase == AFFinalBoundaryPhase) {
           
        }
    
        return totalNumberOfBytesRead;
    }
    
    - (NSInteger)readData:(NSData *)data
               intoBuffer:(uint8_t *)buffer
                maxLength:(NSUInteger)length
    {
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wgnu"
        NSRange range = NSMakeRange((NSUInteger)_phaseReadOffset, MIN([data length] - ((NSUInteger)_phaseReadOffset), length));
        [data getBytes:buffer range:range];
    #pragma clang diagnostic pop
    
        _phaseReadOffset += range.length;
        
        // 1.当_phaseReadOffset < [data length] 标识数据已经读取完毕
        if (((NSUInteger)_phaseReadOffset) >= [data length]) {
            // 2.存在有数据未读取完成,继续改变状态读取相应的信息
            [self transitionToNextPhase];
        }
    
        return (NSInteger)range.length;
    }
    
    

    相关文章

      网友评论

        本文标题:AFNetworking源码分析 (3)--AFURLReque

        本文链接:https://www.haomeiwen.com/subject/acbbqxtx.html