Get HTTP Status code from NWConnection (web-socket) handshake failure

In our Mac application, we are creating a web-socket connection using NWConnection and we are able to successfully establish the connection and read/write data from both sides. We have auth tokens which are sent in headers of NWProtocolWebSocket.Options to the server. If token is good, server accepts the web-socket connection. As per RFC 6455, if server does not want to accept the connection for any reason during web-socket handshake, it returns 403 status code. In our case, if cookies are not valid, server returns 403 during web-socket handshake.

However, we could not find a way to read this status code in Network.framework. We are only getting failed state with NWErrorwhich is .posix(53) but there is no indication of the status code 403. We tried looking into protocol metadata on NWConnection object and they are nil.

We tested the same using URLSessionWebSocketTask where in failure callback method, we could see 403 status code on task.response which means client is getting the code correctly from server.

So, is there a way to read the HTTP status code returned by server during web-socket handshake using Network.framework?

Answered by DTS Engineer in 831267022

The bug mentioned above (FB9878278) remains unresolved.

You could probably work around this by doing the initial WebSocket handshake yourself then pushing the WebSocket protocol on to the protocol stack (setting skipHandshake to avoid it trying to handshake again).. However, it’s not clear whether that’d be a win versus using a third-party WebSocket library.

Regarding third-party libraries, there’s on obvious candidate here, namely NIOWebSocket. I believe you’ll be able to combine that with SwiftNIO Transport Services, so you can continue using NWConnection under the covers.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

We opened a TSI where Matt informed us that NWProtocolWebSocket does not have HTTP stack (unlike URLSessionWebSocketTask) and can not parse HTTP responses from server. Therefore, any error returned as part of HTTP response from server during WebSocket handshake will not be available to clients.

We have opened a feedback to have ability to parse and return HTTP response in NWProtocolWebSocket: FB9878278

Just hit this limitation myself, anyone find any resolution or is there an update on the feedback that was submitted?

The bug mentioned above (FB9878278) remains unresolved.

You could probably work around this by doing the initial WebSocket handshake yourself then pushing the WebSocket protocol on to the protocol stack (setting skipHandshake to avoid it trying to handshake again).. However, it’s not clear whether that’d be a win versus using a third-party WebSocket library.

Regarding third-party libraries, there’s on obvious candidate here, namely NIOWebSocket. I believe you’ll be able to combine that with SwiftNIO Transport Services, so you can continue using NWConnection under the covers.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Get HTTP Status code from NWConnection (web-socket) handshake failure
 
 
Q