Arre
Arre

Reputation: 135

Why is the browser satisfied with a response without content-length

Usually when I send a response to the browser I have to enter content-length in the http headers, otherwise the browser never stops loading (wait for more data)

But recently, I tested rust code:

let response = format!("HTTP/1.1 200 OK\r\n\r\n{}", contents);
stream.write(response.as_bytes()).unwrap();

The browser receives this without any problems, stops loading after receiving the response.(even though content-length is not specified in the response)

Can someone pls explain this?... What makes the browser satisfied with the response in this scenario (even though it does not contain: Content-length)

Upvotes: 1

Views: 674

Answers (1)

Steffen Ullrich
Steffen Ullrich

Reputation: 123461

Content-length is optional as long as the connection is closed after the response is done. From RFC 7230 section 3.3.3 Message Body Length:

  1. Otherwise, this is a response message without a declared message body length, so the message body length is determined by the number of octets received prior to the server closing the connection.

Upvotes: 1

Related Questions