Intercept websocket HTTP upgrade request in URLProtocol

509 views Asked by At

I'm building a library for redirecting all requests in a whitelist to a different host. There are several examples out there about how to do this so most of the the work on that front is finished. The library makes some modifications to the original request if it is in the whitelist, such as encryption, modifications to the body of the request and more. I'm now trying to integrate websocket redirection into the library as well, as the request for the websocket (if in the whitelist) needs to be modified and redirected. the idea is for all underlying tasks to be handled transparently, so the developer using the library doesnt have to handle any of the redirection.

I am using URLprotocol which for normal HTTP requests works perfectly, where each request is passed to my URL protocols 'canInit' function where it is determined if it is in the whitelist or not, then either handled or not. however a websocket upgrade request is never seen by 'canInit', and so i cant modify the request.

Is it possible to intercept a URLSessionWebSocketTask HTTP upgrade request? either using URLProtocol or another method?

The code is proprietary so adding some pseudocode to show implementation.

class MyURLProtocol: NSURLProtocol {
  override class func canInit(request: NSURLRequest) -> Bool {
    println("URL = \(request.URL.absoluteString)")
    if (url is in whitelist) {
      return true
    }
    return false
  } 
}

As shown, I print all requests which are passed to the handler, but no ws:// or wss:// requests are ever passed to the handler.

0

There are 0 answers