CustomHTTPProtocol
http://blog.csdn.net/jingcheng345413/article/details/54967739
一、概念
NSURLProtocol也是苹果众多黑魔法中的一种,使用它可以轻松地重定义整个URL Loading System。当你注册自定义NSURLProtocol后,就有机会对所有的请求进行统一的处理,基于这一点它可以让你:
1.自定义请求和响应
2.提供自定义的全局缓存支持
3.重定向网络请求
4.提供HTTP Mocking (方便前期测试)
5.其他一些全局的网络请求修改需求
二、使用方法
1.继承NSURLPorotocl,并注册你的NSURLProtocol
- [NSURLProtocol registerClass:[MyURLProtocol class]];
当NSURLConnection准备发起请求时,它会遍历所有已注册的NSURLProtocol,询问它们能否处理当前请求。所以你需要尽早注册这个Protocol。
2.对于NSURLSession的请求,注册NSURLProtocol的方式稍有不同,是通过NSURLSessionConfiguration注册的:
- NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
- NSArray *protocolArray = @[ [MyURLProtocol class] ];
- configuration.protocolClasses = protocolArray;
- NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:[NSOperationQueue mainQueue]];
- NSURLSessionTask *task = [session dataTaskWithRequest:request];
- [task resume];
3. 请求结束后注销NSURLProtocol
- [NSURLProtocol unregisterClass:[MyURLProtocol class]];
4.实现NSURLProtocol的相关方法
(1)当遍历到我们自定义的NSURLProtocol时,系统先会调用canInitWithRequest:这个方法。顾名思义,这是整个流程的入口,只有这个方法返回YES我们才能够继续后续的处理。我们可以在这个方法的实现里面进行请求的过滤,筛选出需要进行处理的请求。
- + (BOOL)canInitWithRequest:(NSURLRequest *)request
- {
- if ([NSURLProtocol propertyForKey:MyURLProtocolHandled inRequest:request])
- {
- return NO;
- }
- if (![scheme hasPrefix:@"http"])
- {
- return NO;
- }
- return YES;
- }
(2)当筛选出需要处理的请求后,就可以进行后续的处理,需要至少实现如下4个方法
- + (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
- {
- return request;
- }
- + (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b
- {
- return [super requestIsCacheEquivalent:a toRequest:b];
- }
- - (void)startLoading
- {
- NSMutableURLRequest *mutableReqeust = [[self request] mutableCopy];
- [NSURLProtocol setProperty:@(YES) forKey:MyURLProtocolHandled inRequest:mutableReqeust];
- self.connection = [NSURLConnection connectionWithRequest:mutableReqeust delegate:self];
- }
- - (void)stopLoading
- {
- [self.connection cancel];
- self.connection = nil;
- }
说明:
(1)canonicalRequestForRequest: 返回规范化后的request,一般就只是返回当前request即可。
(2)requestIsCacheEquivalent:toRequest: 用于判断你的自定义reqeust是否相同,这里返回默认实现即可。它的主要应用场景是某些直接使用缓存而非再次请求网络的地方。
(3)startLoading和stopLoading 实现请求和取消流程。
5.实现NSURLConnectionDelegate和NSURLConnectionDataDelegate
因为在第二步中我们接管了整个请求过程,所以需要实现相应的协议并使用NSURLProtocolClient将消息回传给URL Loading System。在我们的场景中推荐实现所有协议。
- - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
- {
- [self.client URLProtocol:self didFailWithError:error];
- }
- - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response
- {
- if (response != nil)
- {
- [[self client] URLProtocol:self wasRedirectedToRequest:request redirectResponse:response];
- }
- return request;
- }
- - (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection
- {
- return YES;
- }
- - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
- {
- [self.client URLProtocol:self didReceiveAuthenticationChallenge:challenge];
- }
- - (void)connection:(NSURLConnection *)connection didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
- {
- [self.client URLProtocol:self didCancelAuthenticationChallenge:challenge];
- }
- - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
- {
- [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:[[self request] cachePolicy]];
- }
- - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- {
- [self.client URLProtocol:self didLoadData:data];
- }
- - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
- {
- return cachedResponse;
- }
- - (void)connectionDidFinishLoading:(NSURLConnection *)connection
- {
- [self.client URLProtocolDidFinishLoading:self];
- }
三、NSURLProtocol那些坑
坑1:企图在canonicalRequestForRequest:进行request的自定义操作,导致各种递归调用导致连接超时。这个API的表述其实很暧昧:
It is up to each concrete protocol implementation to define what “canonical” means. A protocol should guarantee that the same input request always yields the same canonical form.
所谓的canonical form到底是什么呢?而围观了包括NSEtcHosts和RNCachingURLProtocol在内的实现,它们都是直接返回当前request。在这个方法内进行request的修改非常容易导致递归调用(即使通过setProperty:forKey:inRequest:对请求打了标记)
坑2:没有实现足够的回调方法导致各种奇葩问题。如connection:willSendRequest:redirectResponse: 内如果没有通过[self client]回传消息,那么需要重定向的网页就会出现问题:host不对或者造成跨域调用导致资源无法加载。
坑3.崩溃报错:
- 0 libobjc.A.dylib objc_msgSend + 16
- 1 CFNetwork CFURLProtocol_NS::forgetProtocolClient() + 124
有一点苹果说明的不是很清楚,苹果自己实现CustomHTTPProtocol源码中很好的体现了这一点:
NSURLProtocolClient回调动作必须跟请求的托管发送保持在一个线程、相同的Runloop,具体实现逻辑如下:
(1)在start方法中记录当前线程和Runloop模式;
(2)所有对于NSURLProtocolClient的回调,都在记录的线程、以相同的Runloop模式触发,使用如下方法:
- [self performSelector:onThread:withObject:waitUntilDone:modes:];
坑4:httpBody
NSURLProtocol在拦截NSURLSession的POST请求时不能获取到Request中的HTTPBody。苹果官方的解释是Body是NSData类型,而且还没有大小限制。为了性能考虑,拦截时就没有拷贝。
5.如果还有什么其它问题,建议仔细看看苹果的CustomHTTPProtocol源码,应该会发现一些问题。
CustomHTTPProtocol的更多相关文章
- iOS 开发中使用 NSURLProtocol 拦截 HTTP 请求
这篇文章会提供一种在 Cocoa 层拦截所有 HTTP 请求的方法,其实标题已经说明了拦截 HTTP 请求需要的了解的就是 NSURLProtocol. 由于文章的内容较长,会分成两部分,这篇文章介绍 ...
- iOS苹果官方Demo合集
Mirror of Apple’s iOS samples This repository mirrors Apple’s iOS samples. Name Topic Framework Desc ...
- iOS进阶之使用 NSURLProtocol 拦截 HTTP 请求(转载)
这篇文章会提供一种在 Cocoa 层拦截所有 HTTP 请求的方法,其实标题已经说明了拦截 HTTP 请求需要的了解的就是 NSURLProtocol. 由于文章的内容较长,会分成两部分,这篇文章介绍 ...
- iOS WKWebView (NSURLProtocol)拦截js、css,图片资源
项目地址github:<a href="https://github.com/LiuShuoyu/HybirdWKWebVIew/">HybirdWKWebVIew&l ...
- Sanic框架
Sanic框架 1. 入门 Sanic 是一款类似Flask的Web服务器,它运行在Python 3.5+上. 除了与Flask功能类似之外,它还支持异步请求处理,这意味着你可以使用Python3.5 ...
- sanic官方文档解析之Custom Protocols(自定义协议)和Socket(网络套接字)
1,Custom Protocol:自定义协议 温馨提示:自定义协议是一个高级用法,大多数的读者不需要用到此功能 通过特殊的自定义协议,你可以改变sanic的协议,自定义协议需要继承子类asyncio ...
随机推荐
- 搭建 Docker Swarm 集群
准备三台主机 A:192.168.1.5 B:192.168.1.7 C:192.168.1.10 Docker Swarm集群中的节点主机开放以下三个端口 2377端口, 用于集群管理通信 ...
- 【LeetCode】缺失的第一个正数【原地HashMap】
给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输入: [7,8,9,11 ...
- MQTT v5.0------SUBSCRIBE 报文
SUBSCRIBE 报文 固定报头: 剩余长度字段 表示可变报头的长度加上有效载荷的长度,被编码为变长字节整数. 可变报头 SUBSCRIBE报文可变报头按顺序包含以下字段:报文标识符(Packet ...
- ES集群7.3.0设置快照,存储库进行索引备份和恢复等
说明:三台ES节点组成ES集群,一台kibana主机,版本均是7.3.0,白金试用版 官方地址:https://www.elastic.co/guide/en/elasticsearch/refere ...
- python第五章程序练习题
5.2 def isOdd(a): if a%2!=0: return True else: a=eval(input()) print(isOdd(a)) 5.3 def isNum(x): try ...
- C#条码生成及打印实例代码
";//条码 ; ; //打印按钮 private void button1_Click(object sender, EventArgs e) { //实例化打印对象 PrintDocum ...
- kubernetes(k8s) Prometheus+grafana监控告警安装部署
主机数据收集 主机数据的采集是集群监控的基础:外部模块收集各个主机采集到的数据分析就能对整个集群完成监控和告警等功能.一般主机数据采集和对外提供数据使用cAdvisor 和node-exporter等 ...
- 18 java I/O 系统
流的类继承结构 我们首先看看流的类继承结构,有助于理解下个标签的内容 InputStream OutputStream Reader Writer File类 File类技能表示一个特定文件的名称,又 ...
- scp 基本用法(提高scp传输速度)
Outline spc 可以帮你实现: Linux Server 之间互传数据: Linux Server 和 Windows Server 之间互传数据: 参考: https://www.cnblo ...
- Java自学-接口与继承 final
Java的修饰符final final修饰类,方法,基本类型变量,引用的时候分别有不同的意思. 示例 1 : final修饰类 当Hero被修饰成final的时候,表示Hero不能够被继承 其子类会出 ...