HTTP HEAD方法为动态生成的文件获取什么响应头? [英] What response header should HTTP HEAD method get for a dynamically generated file?

查看:226
本文介绍了HTTP HEAD方法为动态生成的文件获取什么响应头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http规范说明了HEAD请求:

The http spec says about the HEAD request:


HEAD方法与GET相同,但服务器必须不是
在响应中返回消息体。元信息在HTTP头中包含
以响应HEAD请求应该与响应GET请求时发送的信息相同

The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request.


Content-Length实体标题字段指示
实体主体的大小,以十进制数的OCTET发送对于收件人,或者在
HEAD方法的情况下,如果请求是GET,那么
已发送的实体主体的大小。

The Content-Length entity-header field indicates the size of the entity-body, in decimal number of OCTETs, sent to the recipient or, in the case of the HEAD method, the size of the entity-body that would have been sent had the request been a GET.

因此,如果请求的资源是动态生成的,对于 HEAD 请求,资源 ALSO 生成?是否应该 Content-Length 标头或 Transfer-Encoding:chunked 标头?

So if the requested resource is dynamically generated, for the HEAD request, will the resource ALSO be generated? Should there a Content-Length header or Transfer-Encoding: chunked header?

(我觉得这是关于如何实现HTTP协议。)

(I feel this is about how to implement the HTTP protocol.)

我没有看到规范要求是否要发送Content-Length标头或如何将其发送给动态生成的资源。这种动态资源将使用分块传输编码发送,如果使用 GET Content-Length 标头>方法。因此,如果HEAD响应应该模仿GET响应,则不应发送Contnet-Length。

I don't see the spec mandates whether to send Content-Length header or how to send it for a dynamically generated resource. Such dynamic resource will be sent with chunked transfer encoding and there'll be no Content-Length header if accessed with a GET method. So, if HEAD response should mimic GET response, the Contnet-Length should not be sent either.

推荐答案

a)那不是HTTP规范。它是1999年过时的RFC 2616的W3C副本。

a) That's not the HTTP spec. It is the W3C copy of the now obsolete RFC 2616, dated 1999.

b)关于当前规范,请参阅 https://greenbytes.de/tech/webdav/rfc7231.html#HEAD

b) For the current spec, see https://greenbytes.de/tech/webdav/rfc7231.html#HEAD

回答你的问题:如果你不知道有效载荷大小而没有实际生成它,那么在HEAD响应中不发送Content-Length就可以了。

To answer your question: if you don't know the payload size without actually generating it, not sending Content-Length in the HEAD response is ok.

这篇关于HTTP HEAD方法为动态生成的文件获取什么响应头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆