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