Skip to content

[SR-15987] URLProtocol.canonicalRequest(for: URLRequest) is never called in SCF #3173

Open
@karwa

Description

@karwa
Previous ID SR-15987
Radar None
Original Reporter @karwa
Type Bug
Environment

macOS:
Xcode 13.2.1, macOS 11.6

Linux:
Ubuntu 21.10 aarch64
Swift version 5.5.2 (swift-5.5.2-RELEASE)
Target: aarch64-unknown-linux-gnu

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee None
Priority Medium

md5: aff39a91b02fbac165e13759d65b536a

Issue Description:

This is supposed to be an override point so that URLProtocol conformances can adjust a URLRequest. It is invoked on Darwin but not in swift-corelibs-foundation.

import Foundation
#if canImport(FoundationNetworking)
  import FoundationNetworking
#endif

class TestProto: URLProtocol {
    override class func canInit(with request: URLRequest) -> Bool {
        request.url?.scheme == "foo"
    }
    override class func canInit(with task: URLSessionTask) -> Bool {
        task.originalRequest?.url?.scheme == "foo"
    }
    
    override class func canonicalRequest(for request: URLRequest) -> URLRequest {
        print("Called URLProtocol.canonicalRequest(for: URLRequest)")
        var copy = request
        copy.url = URL(string: "foo://canonical/")
        return copy
    }
    
    override func startLoading() {
        let response = URLResponse(url: self.request.url!, mimeType: "text/plain", expectedContentLength: 0, textEncodingName: nil)
        self.client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
        self.client?.urlProtocolDidFinishLoading(self)
    }

    override func stopLoading() {
    }
}

var config = URLSessionConfiguration.default
config.protocolClasses = [TestProto.self]
let task = URLSession(configuration: config).dataTask(with: URL(string: "foo://hello/")!) { data, response, error in
    print("RESPONSE URL: \(response?.url)")
}
task.resume()
sleep(2)

Output on macOS:

Called URLProtocol.canonicalRequest(for: URLRequest)
RESPONSE URL: Optional(foo://canonical/)

Output on Linux:

RESPONSE URL: Optional(foo://hello/)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions