当前位置: 动力学知识库 > 问答 > 编程问答 >

tcp - How to know when to stop data tunneling in HTTP proxy connection

问题描述:

I am trying to write my own HTTP proxy server and I have a question about the protocol.

First, I would like to mention that I am using this page as a reference. I think it's accurate but it's also from 1998. If anyone has a better reference for me I would be grateful to them.

So basically I understand that the connection starts with a handshake. I receive a CONNECT request, proxy-authorization, etc. Then I connect to the host and port specified in the request's resource URI. Then I send a status line, ideally HTTP/1.1 200 Connection established, followed by some headers and a CRLF like normal.

Once this handshake is complete my client and the host my client asked for are connected through my proxy server. I am supposed to tunnel data in both directions, which makes sense since I could be supporting any type of TCP based protocol, including HTTPS or even WebSocket, over this HTTP based proxy connection.

What doesn't make sense to me is how I know when to stop. If this proxy can really support any TCP based protocol then I don't understand how to know when the interaction is over. An HTTP message would be a simple 2 step read-write, an HTTPS interaction would involve several such exchanges, and a WebSocket interaction would involve indefinitely many exchanges.

I'm not asking for a perfect solution. I would be happy with something pragmatic like a timeout, but I would like to know what standard best practices are in order to do this project as well as I can.

Thanks to everyone for any help.

网友答案:

Just copy data in both directions simultaneously until you read an end of stream. Then:

  1. Shutdown the opposite socket for writing and stop copying in that direction. That propagates the EOS to the peer.
  2. If the socket you read EOS from was already shutdown for writing, which you will have to remember, close both sockets.
分享给朋友:
您可能感兴趣的文章:
随机阅读: