Is “Content-Length” or “chunked transfer-encoding” a must for Http request?

前端 未结 1 651
再見小時候
再見小時候 2021-01-15 10:44

Could someone tell me about that Content-Length or Transfer-Encoding: \"Chunked\" is a must for Http request ? I\'m using c++ to write a http sever.

The http respond

相关标签:
1条回答
  • 2021-01-15 11:23

    If you don't specify a Transfer-Encoding or Content-Length then the request (or response) is implicitly a variable length request/response and the only way to signal the end of the body is to shutdown the connection (and conversely detect the shutdown/eof in the receiver).

    This means that this kind of request/response is also implicitly Connection: close

    If you're implementing HTTP1.1 then you must support all three transfer methods.

    When I wrote my HTTP server I abstracted the concept of a "request stream" from the concept of a "connection stream". The "request stream" is polymorphic and supports the concept of reading to "EOF". There's no reason you couldn't also have a method on there to "read_chunk". In the case of a non-chunked request, this could simply read until EOF.

    This allowed me to implement simultaneous execution of multiple requests on the same connection (but there is some jiggery-pokery to ensure that the responses go back in the correct order!)

    0 讨论(0)
提交回复
热议问题