BBABaseAPIRequest+Private.h 7.4 KB
//
//  BBABaseAPIRequest+Private.h
//  BBAPods-APIRequest
//
//  Created by liumuye on 2017/9/22.
//  Copyright © 2017年 Baidu. All rights reserved.
//

#import "BBABaseAPIRequest.h"
#import "BBAAPIRequestOperation.h"
#import "BBAAPIRequestManagerProtocol.h"

NS_ASSUME_NONNULL_BEGIN
@class BBAMultipartRequestPart;

/// Session含CompletionHandler的代理方法直接回调外部业务设置的Block, 与AF接口方式不同, 供NSURLProtocol接管使用
typedef void(^APIRequestWillRedirectWithHandler)(__kindof BBABaseAPIRequest *_Nonnull apiRequest, NSURLRequest * _Nonnull newURLRequest, void(^CompletionHandler)(NSURLRequest *));    // Redirect
typedef void(^APIRequestDidReceiveChallengeWithHandler)(__kindof BBABaseAPIRequest * _Nonnull apiRequest, NSURLAuthenticationChallenge *challenge, void (^CompletionHandler)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential));  //  ReceiveChallenge
typedef void(^APIRequestDidReceiveResponseWithHandler)(__kindof BBABaseAPIRequest * _Nonnull apiRequest, void (^CompletionHandler)(NSURLSessionResponseDisposition disposition));  //  ReceiveResponse

@interface BBAAPIRequestMetricsInfo()
//  NetworkInfo
@property (atomic, assign, readwrite) NSInteger ipStack;     //  0.未知 1.v4-only 2.v6-only 3.双栈
@property (atomic, assign, readwrite) NSInteger netQuality;  //  1.好网 2.弱网 3.无网 4.未知
@property (atomic, assign, readwrite) BOOL isWeakNet;        //  是否弱网
@property (atomic, copy,   readwrite) NSString *netTypeBegin;//  请求发送前的网络制式类型
@property (atomic, copy,   readwrite) NSString *netTypeEnd;  //  请求结束时的网络制式类型
@property (atomic, assign, readwrite) BOOL netTypeChanged;   //  请求过程中网络制式是否变化
@property (atomic, copy,   readwrite) NSString *clientIP;    //  出口IP
@property (atomic, assign, readwrite) BOOL viaVPN;           //  是否有VPN

/// NetEngine
@property (atomic, assign, readwrite) NSUInteger netEngine;  //  网络引擎类型, 1:AFN; 2.NSURLSession; 3:CroNet(TurboNet in baidu)

/// Success/Fail
@property (atomic, assign, readwrite) BOOL isSuccess;        //  成功与否
@property (atomic, assign, readwrite) BOOL canceled;         //  主动取消
@property (atomic, assign, readwrite) NSInteger errCode;     //  错误码
@property (atomic, assign, readwrite) NSInteger underlyErrCode;     //  详细错误码
@property (atomic, copy,   readwrite) NSString *errMsg;      //  错误简述

//  TimingInfo
@property (atomic, strong, readwrite) NSDate *startTime;     //  请求开始时间
@property (atomic, strong, readwrite) NSDate *resumeTime;    //  请求发送时间
@property (atomic, strong, readwrite) NSDate *finishTime;    //  请求结束时间
@property (atomic, assign, readwrite) NSTimeInterval queueDuration;  //  排队耗时
@property (atomic, assign, readwrite) NSTimeInterval dnsDuration;    //  dns 耗时
@property (atomic, assign, readwrite) NSTimeInterval tcpDuration;    //  tcp 耗时
@property (atomic, assign, readwrite) NSTimeInterval sslDuration;    //  ssl 耗时
@property (atomic, assign, readwrite) NSTimeInterval ttfbDuration;   //  ttfb 耗时
@property (atomic, assign, readwrite) NSTimeInterval completeWaitDuration;//  回调等待耗时

/// ConnectionInfo
@property (atomic, assign, readwrite) BOOL socketReused;     //  是否复用连接
@property (atomic, assign, readwrite) BOOL isIPv6Socket;     //  是否是IPv6的连接
@property (atomic, assign, readwrite) BOOL viaProxy;         //  是否通过代理
@property (atomic, copy,   readwrite) NSString *localIPPort; //  local IP和端口号 (NSURLSession iOS13+; Cronet iOS9+)
@property (atomic, copy,   readwrite) NSString *remoteIPPort;//  remote IP和端口号 (NSURLSession iOS13+; Cronet iOS9+)

/// HttpInfo
@property (atomic, assign, readwrite) BOOL useCache;         //  命中HTTP缓存
@property (atomic, copy,   readwrite) NSString *protocol;    //  HTTP协议类型
@property (atomic, assign, readwrite) NSInteger statusCode;  //  HTTP状态码
@property (atomic, assign, readwrite) NSInteger requestBodyLength;   //  请求上行包大小
@property (atomic, assign, readwrite) NSInteger responseLength;      //  请求响应包大小

@end

@interface BBABaseAPIRequest ()

@property (nonatomic, weak, nullable) NSURLSession *session;

@property (nonatomic, assign) BOOL useSyncOperation;

@property (nonatomic, weak) BBAAPIRequestOperation * apiRequestOperation;

@property (nonatomic, assign, readwrite) BOOL isCancelled;

@property (nonatomic, strong, nullable) NSOutputStream *downloadResumeOutputStream;
@property (nonatomic, assign) uint64_t downloadTotalSize;
@property (nonatomic, assign) uint64_t downloadWrittenSize;
@property (atomic, assign) BOOL downloadResponseHeadersBlockInvoked;

//  Session直接创建Task时缓存数据
@property (nonatomic, strong) NSMutableData *mutableData;
@property (nonatomic, nullable) APIRequestWillRedirectWithHandler willRedirectHandler;
@property (nonatomic, nullable) APIRequestDidReceiveChallengeWithHandler didReceiveChallengeHandler;
@property (nonatomic, nullable) APIRequestDidReceiveResponseWithHandler didReceiveResponseHandler;

@property (atomic, assign, readwrite) NSUInteger netEngine;
@property (atomic, strong, readwrite) BBAAPIRequestMetricsInfo *metrics;

//  TurboNet下强制使用SeesionConfiguration.urlCache内容
//  若命中则直接返回,TODO(liumuye): 11.13仅供搜索落地页预取使用,未达到通用目的
@property (nonatomic, assign) BOOL forceUseURLCache;

@property (nonatomic, copy) NSString *bdTraceId;

@property (nonatomic, copy, readwrite) NSString *tag;

@property (nonatomic, strong, nullable, readwrite) NSURLSessionTask *task;
@property (nonatomic, strong, readwrite) NSNumber *taskIdentfier;

//响应内容
@property (atomic, copy, nullable, readwrite) NSDictionary * responseHeaders;
@property (nonatomic, readwrite) NSInteger responseStatusCode; //状态码
@property (nonatomic, strong, nullable, readwrite) NSString *responseString;
@property (nonatomic, strong, nullable, readwrite) NSData *responseData;
@property (nonatomic, strong, nullable, readwrite) id responseJSON;

@property (nonatomic, strong, nullable, readwrite) NSURLResponse *urlResponse;

//错误信息
@property (nonatomic, strong, nullable, readwrite) NSError *error;
@property (nonatomic, assign, readwrite) NSInteger errCode;
@property (nonatomic, assign, readwrite) NSInteger underlyErrCode;

@property (nonatomic, strong, readwrite) NSDictionary *resumeDataDict;

@property (nonatomic, strong, readwrite) NSDate *startDate;             //  业务调用start
@property (nonatomic, strong, readwrite) NSDate *taskResumeDate;        //  urlsessionTask启动
@property (nonatomic, strong, readwrite) NSDate *taskCompleteDate;      //  urlsessionTask完成
@property (nonatomic, strong, readwrite) NSDate *endDate;               //  执行业务回调

@property (nonatomic, assign, readwrite) APIRequestQuicStatus quicStatus;

@property (nonatomic, assign) BOOL probeExecuted;

@end

@interface BBABaseAPIRequest (Private)

/// 标识网络引擎类型
- (void)markNetEngineType;

- (BOOL)isDownloadRequest;
- (uint64_t)downloadResumeTemporaryPartialSize;
- (NSError*)moveDownloadTemporaryToTarget;

- (BOOL)isUploadRequest;
- (NSMutableArray<BBAMultipartRequestPart *> *)multipartPartList;

- (void)resetAllCallbacks;

//  是否是搜索请求
- (BOOL)isSearchRequest;

//  11.6搜索使用, TODO: 11.7 门面改造后填充
- (void)resumeRequestDirectly;

@end
NS_ASSUME_NONNULL_END