Christopher Shroba
Christopher Shroba

Reputation: 7574

When does an HTTP server start returning data?

After a connection has been established between client and server, the client starts sending an HTTP request. This consists of a line that looks something like GET / HTTP/1.1 followed by several lines of headers. My question is, how does a web server know when to start returning data? Does the client somehow close its side of the connection to indicate it is done with the request and is ready to start receiving the response? Does the server just know after the "\r\n\r\n" string at the end of the headers? Is it something else entirely?

Thanks!

Upvotes: 0

Views: 93

Answers (2)

Ali Kemal Saglam
Ali Kemal Saglam

Reputation: 66

You should read HTTP 1.1 core specifications.

19.4.6 Introduction of Transfer-Encoding

HTTP/1.1 introduces the Transfer-Encoding header field (section 14.41). Proxies/gateways MUST remove any transfer-coding prior to forwarding a message via a MIME-compliant protocol.

A process for decoding the "chunked" transfer-coding (section 3.6) can be represented in pseudo-code as:

   length := 0
   read chunk-size, chunk-extension (if any) and CRLF
   while (chunk-size > 0) {
      read chunk-data and CRLF
      append chunk-data to entity-body
      length := length + chunk-size
      read chunk-size and CRLF
   }
   read entity-header
   while (entity-header not empty) {
      append entity-header to existing header fields
      read entity-header
   }
   Content-Length := length
   Remove "chunked" from Transfer-Encoding

Upvotes: 0

user207421
user207421

Reputation: 310985

You need to read the HTTP 1.1 specification. The serve has to read the entire request before it can formulate and send the response. There are at least two ways it can know where the request ends:

  1. Content-length header
  2. Chunked transfer encoding.

Upvotes: 1

Related Questions