twitter-archive / CocoaSPDY

SPDY for iOS and OS X
Apache License 2.0
2.39k stars 233 forks source link

Add support for NSURLCache. #141

Open kgoodier opened 8 years ago

kgoodier commented 8 years ago

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

NSURLRequestReturnCacheDataElseLoad

NSURLRequestReturnCacheDataDontLoad

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.

NSProgrammer commented 8 years ago

Looks great! I have a number of comments that should be addressed though. Thanks, Kevin!

NSProgrammer commented 8 years ago

+1

CLAassistant commented 5 years ago

CLA assistant check
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.