简体   繁体   English

如何知道何时在HTTP代理连接中停止数据隧道传输

[英]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. 我正在尝试编写自己的HTTP代理服务器,但对协议有疑问。

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. 我认为这是准确的,但也是从1998年开始的。如果有人对我有更好的参考,我将不胜感激。

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. 我收到一个CONNECT请求,代理授权等。然后,我连接到该请求的资源URI中指定的主机和端口。 Then I send a status line, ideally HTTP/1.1 200 Connection established , followed by some headers and a CRLF like normal. 然后,我发送一条状态行,理想情况下是HTTP/1.1 200 Connection established ,然后是一些标头和一个CRLF(如正常)。

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. 我应该双向传输数据,这很有意义,因为我可以通过基于HTTP的代理连接支持任何类型的基于TCP的协议,包括HTTPS甚至WebSocket。

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. 如果此代理确实可以支持任何基于TCP的协议,那么我不知道如何知道交互何时结束。 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. HTTP消息将是简单的两步读写操作,HTTPS交互将涉及多个此类交换,而WebSocket交互将无限地涉及许多交换。

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. 这会将EOS传播到对等端。
  2. If the socket you read EOS from was already shutdown for writing, which you will have to remember, close both sockets. 如果您从中读取EOS的套接字已经被关闭以进行写入(必须记住),请关闭两个套接字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM