Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Responses are not cached in CustomHTTPProtocol #135

Open
Jyothi-Narayan opened this issue Apr 4, 2023 · 0 comments
Open

Responses are not cached in CustomHTTPProtocol #135

Jyothi-Narayan opened this issue Apr 4, 2023 · 0 comments

Comments

@Jyothi-Narayan
Copy link

Cache storage policy is not set as per the caching policy in URLRequest received.
When the url request cache policy is NSURLRequestReturnCacheDataElseLoad , as per the below code storage policy is set to URLCache.StoragePolicy.notAllowed

public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
        let policy = URLCache.StoragePolicy(rawValue: request.cachePolicy.rawValue) ?? .notAllowed
        client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: policy)
        currentRequest?.initResponse(response: response)
        completionHandler(.allow)
    }

And it doesn't skip the api call when the url request policy is set to load from cache

override public class func canInit(with request: URLRequest) -> Bool {
        guard CustomHTTPProtocol.shouldHandleRequest(request) else { return false }

        if CustomHTTPProtocol.property(forKey: Constants.RequestHandledKey, in: request) != nil {
            return false
        }
        return true
    }

the above canInit func only ignores the request which are from ignored hosts, doesn't ignore the requests with cache policy set to NSURLRequestReturnCacheDataElseLoad/ NSURLRequestReturnCacheDataDontLoad

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant