在 HTTP 502 中,无效响应是什么意思?

In HTTP 502, what is meant by an invalid response?

我正在尝试测试服务器以查看其行为是否符合以下定义,为此,我需要构建无效响应并将其从上游服务器发送到网关服务器。 HTTP 规范说明了以下关于状态代码 502 - Bad Gateway 的内容。

The 502 (Bad Gateway) status code indicates that the server, while acting as a gateway or proxy, received an invalid response from an inbound server it accessed while attempting to fulfill the request.

但究竟什么构成无效响应? non-standard 响应代码(例如,某个随机数,如 6789)是否算作无效响应?或包含无效 HTTP 版本的响应?

遇到这个:,它表示不完整 headers 和响应正文会导致 502 错误。服务器如何确定 headers 或 body 是否不完整?更重要的是,是否可以通过编程方式创建这样的响应(我正在使用 Java)?

我设法在 HTTP 规范(RFC7230 第 3.3.3 节)中找到以下内容。

If a message is received without Transfer-Encoding and with either multiple Content-Length header fields having differing field-values or a single Content-Length header field having an invalid value, then the message framing is invalid and the recipient MUST treat it as an unrecoverable error. If this is a request message, the server MUST respond with a 400 (Bad Request) status code and then close the connection. If this is a response message received by a proxy, the proxy MUST close the connection to the server, discard the received response, and send a 502 (Bad Gateway) response to the client. If this is a response message received by a user agent, the user agent MUST close the connection to the server and discard the received response.

除此之外,我还注意到当上游服务器关闭时,Nginx returns 充当反向 proxy/load 平衡器时为 502。