CocoaSPDY
CocoaSPDY copied to clipboard
Add support for NSURLCache.
The NSURL loading system has subtle and poorly documented behavior regarding caching. CocoaSPDY was not doing the right thing, thus there was no support for NSURLCache in the protocol. This patch adds basic support for both NSURLConnection and NSURLSession based requests. This is not yet a fully-featured client caching implementation.
If the request specifies a NSURLRequest cachePolicy of:
NSURLRequestUseProtocolCachePolicy
- NSURL system does not provide a cached response to the protocol constructor. It is up to the protocol to load and validate the cached response.
NSURLRequestReturnCacheDataElseLoad
- NSURL system will provide the cached response, if available. The protocol is expected to validate the response, and load if not available or not valid.
NSURLRequestReturnCacheDataDontLoad
- NSURL system will provide the cached response, if available. The protocol is expected to validate the response. The protocol will not be loaded if no cached response is available, or if one is but is invalid, the protocol should not load the request.
In the cases where the protocol has a cached response and it is valid, it is supposed to call URLProtocol:cachedResponseIsValid. CocoaSPDY was not doing this. Determining validity of the cached response is the job of the protocol, and a basic implementation has been provided here.
CocoaSPDY also has to jump through some hoops whenever NSURLSession is being used, as Apple has not provided a way to get the NSURLSessionConfiguration and thus we cannot get the right NSURLCache. Fortunately we have already provided a workaround for this.
SPDYMetadata has been extended to provide the source of the response.
Observationally, we've seen the NSURL loading system fail to insert items into the NSURLCache when using NSURLSession and iOS 7/8. NSURLConnection works fine on all these, and NSURLSession works fine on iOS 9. Best guess is there's a bug in the NSURLProtocol implementation that fails to insert into the cache.
So, here we are creating a workaround for that specific case. CocoaSPDY will buffer the data internally, up to a certain size limit based on the cache size, and will manually insert into the cache when the response is done. This could potentially be expanded in the future for unclaimed, finished push responses, but for now those are excluded.
Looks great! I have a number of comments that should be addressed though. Thanks, Kevin!
+1
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.