Calculate content length POST

2020-07-02 10:08发布

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--

标签: http post
1条回答
叛逆
2楼-- · 2020-07-02 10:32

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

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.

According to the HTTP spec you aren't technically required to specify the Content-Length header. From RFC 2616 14.13:

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.

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.

查看更多
登录 后发表回答