Go: How to create a http proxy pass without loading body

huangapple go评论87阅读模式
英文:

Go: How to create a http proxy pass without loading body

问题

我有N个位于世界各地的代理。我需要通过这些代理调用一些端点,但不加载它们的主体。
例如,我想调用google.com,我将这个URL发送给所有N个代理,它们会发送一个GET方法的HTTP请求到google.com,并将响应发送给我。
问题是所有代理都在加载完整的响应主体,但我只需要响应状态和头部信息。在Golang中有没有解决方案可以在第一时间阻止下载响应主体?

我尝试使用一些curl标志来调用它,但它只是忽略显示响应主体,并在后台下载主体。

英文:

I have N agents around the world. I need to call some endpoints through these agents but without loading their bodies.
For example I want to call google.com and I send this url to all N agents and they send an http request with method GET to google.com send the response to me.
Problem is all agents are loading the full body response but I only need the response status and headers. Is there any solution in Golang to prevent downloading body response in the first place?

I tried calling it with some curl flags but it is just ignoring to show the response body and in the background body is downloaded

答案1

得分: 1

你试图实现的目标与HTTP协议不兼容。

不论是HEAD还是GET,让我们从语言/实现无关的角度来看待这个问题。

无论实现如何,你的HTTP客户端都会使用GET/POST/PUT方法向服务器发送HTTP/1.1请求。接下来,服务器会接收到这个请求,其中包含以下有效载荷(假设使用GET,但对于其他方法也是一样的):

GET /somepath HTTP/1.1
Host: somedomain.com
<更多请求头>

此时,服务器将回复HTTP响应头部和HTTP响应体。无法阻止这种情况发生,因为这实际上会破坏HTTP协议。你可以在客户端避免读取响应体,但响应体仍然会通过网络传输。

然而,对于需要发送请求体的POST/PUT方法,你可以使用Expect头部。

如果在请求中包含Expect: 100-continue头部,服务器将向你发送一个HTTP 100 Continue响应,表示客户端可以发送请求体,此时你可以断开连接。

然而,这意味着你将无法获得实际的HTTP响应代码,这个代码将在你发送请求体时被发送。例如,如果你的有效载荷无效,你将无法获得HTTP 400 Bad Request响应,因为你实际上没有发送有效载荷。只有当你发送有效载荷时,服务器才会发送完整的响应,包括响应体。

不幸的是,你想要实现的目标是不可能的。

英文:

What you are trying to achieve is incompatible with the HTTP protocol.

HEAD vs GET aside, let's look at this from a language/implementation independent angle.

Your HTTP client, regardless of implementation, is sending a HTTP/1.1 request to a server using the GET/POST/PUT method. What happens next is that the server receives this request, with the following payload (this assumes GET, but it's the same for all):

GET /somepath HTTP/1.1
Host: somedomain.com
&lt;more request headers here&gt;

At that point, the server will reply with the HTTP response headers and the HTTP response body. There is no way to prevent this from happening as it would literally break the HTTP protocol. You can always avoid reading the response body on the client side, but the response body will always travel "through the wire".

However, for POST/PUT (methods which require a request body to be sent), you could use the Expect header.

If you include the Expect: 100-continue header in your request, then the server will send back to you a HTTP 100 Continue response, which signals the client that it is clear to send the request body, at which point you can cut the connection.

This, however, means that you would not get the actual HTTP response code that would be sent if you had sent the request body. For example, you wouldn't get a HTTP 400 Bad Request if your payload was invalid because you actually didn't send a payload. If you sent the payload, then the server would send back a full response, including the body.

Unfortunately, what you are trying to achieve isn't possible.

huangapple
  • 本文由 发表于 2023年7月27日 03:34:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76774512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定