NWTCPConnection and URLSession

NEPacketTunnelProvider lets you open a TCP connection with or without TLS through the extension's tunnel using the createTCPConnectionThroughTunnel function. However, this function returns a NWTCPConnection, which only has affordances for reading and writing raw data over the connection. Is there any way for me to construct a URLSession or URLRequest that can use this existing connection, so that I can send and receive HTTP data over it without having to roll my own HTTP request/response parsing?

Answered by Systems Engineer in 711043022

Is there any way for me to construct a URLSession or URLRequest that can use this existing connection, so that I can send and receive HTTP data over it without having to roll my own HTTP request/response parsing?

No, there is not. The APIs for NWTCPConnection and NEPacketTunnelProvider were created so that you can take existing app traffic and encapsulate it inside your tunneling protocol. It was not really meant for you to be sending one-off requests using this API and that is why there is not higher level application protocol support for things like HTTP here.

Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
Accepted Answer

Is there any way for me to construct a URLSession or URLRequest that can use this existing connection, so that I can send and receive HTTP data over it without having to roll my own HTTP request/response parsing?

No, there is not. The APIs for NWTCPConnection and NEPacketTunnelProvider were created so that you can take existing app traffic and encapsulate it inside your tunneling protocol. It was not really meant for you to be sending one-off requests using this API and that is why there is not higher level application protocol support for things like HTTP here.

Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
NWTCPConnection and URLSession
 
 
Q