Getting response of http request without content-length?

前端 未结 2 1369
情书的邮戳
情书的邮戳 2021-02-02 10:14

I have a little program sends http request and gets response with TCP protocol.

My request format;

GET / HTTP/1.0
Host: somewebsite.com
{two new line}


        
相关标签:
2条回答
  • 2021-02-02 10:47

    In HTTP/1.0 - server response without content-length is when the stream closes

    In HTTP/1.1 - server response without content-length is when the response is chunked encoded

    0 讨论(0)
  • 2021-02-02 11:00

    See relevant part of HTTP spec. In your specific case, if the server does not give content length back, then it MUST be closing the stream upon finishing the response. There is no other reliable way for you (as a client) to know. Regardless of HTTP version. @Julian chunked encoding is indeed a clever upgrade in HTTP/1.1 but is rather specific to streaming and there is no reason why a "plain" webserver would implement it. That is a server which knows the content length before initiating response. And i guess that the OP doesn't have the server under control, otherwise he won't be objecting missing HTTP headers.

    But even if you DO get the content length header, you must not unreservedly trust it. The server implementors are only fallible humans too. Take it as a "most probable" response, initial value to a resizable buffer. You still must be ready to handle less as well as more (the worse case).

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