计算内容长度POST [英] Calculate content length POST
问题描述
如何计算内容长度,例如:
How can I calculate content length for example of:
POST /Upload/ HTTP/1.1
Host: test.lan
User-Agent: Shockwave Flash
Connection: Keep-Alive
Cache-Control: no-cache
Accept: text/*
Content-Length: ?????
Content-Type: multipart/form-data; boundary=----------------------------4d2179e6b3c0
------------------------------4d2179e6b3c0
Content-Disposition: form-data; name="Filename"
phpinfo.php
------------------------------4d2179e6b3c0
Content-Disposition: form-data; name="ASPSESSID"
6e223eb1c7561e9c599f03cc04e9444b
------------------------------4d2179e6b3c0
Content-Disposition: form-data; name="Filedata"; filename="phpinfo.php"
Content-Type: application/octet-stream
<? phpinfo(); ?>
------------------------------4d2179e6b3c0
Content-Disposition: form-data; name="Upload"
Submit Query
------------------------------4d2179e6b3c0--
推荐答案
内容 -
长度值应通过在消息头终止后总计所有数据来计算。在您的示例中,这是此后的所有内容(为了便于阅读,包含 CRLF
字符):
The Content-Length
value should be calculated by totaling all data after the termination of the message headers. In the case of your example, this is everything after this point (with CRLF
characters included for readability):
...
Content-Length: ?????\r\n
Content-Type: multipart/form-data; boundary=--------------------4d2179e6b3c0\r\n
\r\n
第一个空行后的所有内容( \\\
) - 包括你的边界分隔符 - 应计入总长度。实际上,这通常意味着您需要在生成完整的消息实体主体后将 Content-Length
标头值制成表格。获得完整的消息后,您可以在其前面加上标题以创建完整的HTTP消息。
Everything coming after the first empty line (\r\n
) -- including your boundary delimiters -- should be counted in the total length. In practice, this usually means that you'll need to tabulate the Content-Length
header value after generating the full message entity body. Once you have the full body of the message you can prepend it with your headers to create the full HTTP message.
根据HTTP规范,您在技术上不是必需指定 Content-Length
标头。来自 RFC 2616 14.13 :
According to the HTTP spec you aren't technically required to specify the Content-Length
header. From RFC 2616 14.13:
应用程序应该使用此字段来指示消息正文的传输长度,除非4.4节中的规则禁止这样做。
Applications SHOULD use this field to indicate the transfer-length of the message-body, unless this is prohibited by the rules in section 4.4.
但是,对于大多数服务器来说,这是一个非常标准的要求,如果 Content-Length $,它通常会发回错误响应c $ c>缺失或指定不正确。
However, this is a pretty standard requirement for most servers which will generally send back an error response if the Content-Length
is missing or incorrectly specified.
这篇关于计算内容长度POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!