Get content from HTTP request even if there is no contentlength header

前端 未结 3 1879
暗喜
暗喜 2021-01-21 05:29

Am testing with a client who send me a HTTP request with no content length header but has a content.

How do I extract this content without the help of contentlength head

3条回答
  •  心在旅途
    2021-01-21 06:20

    If this were a response then the message could be terminated by closing the connection. But that's not an option here because the client still needs to read the response.

    Apart from Content-Length:, the other methods of determining content length are:

    • Transfer-Encoding: chunked
    • guesswork

    Hopefully it's the former, in which case the request should look something like this:

    POST /some/path HTTP/1.1
    Host: www.example.com
    Content-Type: text/plain
    Transfer-Encoding: chunked
    
    25
    This is the data in the first chunk
    
    1C
    and this is the second one
    
    3
    con
    8
    sequence
    0
    
    

    (shamelessly stolen from the Wikipedia article and modified for a request)

    • each chunk is of the form: hex-encoded length, CRLF, data, CRLF
    • after the final data-carrying chunk comes a zero-length chunk with no data
    • after the zero-length chunk comes optional extra HTTP headers
    • after the optional HTTP headers comes another CRLF

提交回复
热议问题