Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP/2 HEADERS and DATA Frames

Tags:

http

http2

I am trying to understand HTTP/2 in detail. I read this article about streams, messages and frames: https://hpbn.co/http2/#streams-messages-and-frames. I don't know if I got the concept right.

I came to the following conclusion:

  • A message is a combination of a HEADER frame and one or more DATA frames.
  • A DATA frame can ONLY be sent with an HEADERS frame, because I don't see any indicators which shows the DATA frames Stream ID (RFC 7540, 6.1)
    • If this is true, A DATA Frame can only be sent within a message
  • A Stream can be chunked into many Frames, while it can be related to a Stream by its Stream ID

Furthermore: How is a message represented in the specification?

like image 261
csnewb Avatar asked Jun 09 '17 14:06

csnewb


People also ask

What are frames in http 2?

HTTP Frames Once the HTTP/2 connection is established, endpoints can begin exchanging frames. 4.1. Frame Format All frames begin with a fixed 9-octet header followed by a variable-length payload.

How many header frames are in a message?

You got a few things incorrectly. A message is a combination of one or two HEADER frames (carrying the HTTP headers), zero or more DATA frames, and one optional terminal HEADER frame (carrying the HTTP trailers). You can look at examples in this section of RFC 7540 .

What are the fields of the frame header?

The fields of the frame header are defined as: The length of the frame payload expressed as an unsigned 24-bit integer. Values greater than 2 14 (16,384) MUST NOT be sent unless the receiver has set a larger value for SETTINGS_MAX_FRAME_SIZE. The 9 octets of the frame header are not included in this value. The 8-bit type of the frame.

What are header fields in http 2?

Just as in HTTP/1, a header field in HTTP/2 is a name with one or more associated values. Header fields are used within HTTP request and response messages as well as in server push operations (see Section 8.2 ). Header lists are collections of zero or more header fields.


2 Answers

You got a few things incorrectly.

A message is a combination of one or two HEADER frames (carrying the HTTP headers), zero or more DATA frames, and one optional terminal HEADER frame (carrying the HTTP trailers). You can look at examples in this section of RFC 7540. There is a special case for 100 Continue responses, that can begin with two HEADERS rather than one. In what follows we can ignore this case.

A DATA frame does have a stream ID, because all frames share the frame header defined in this section of RFC 7540. What is described in section 6.1 is only the body of the DATA frame.

A message is a half of a HTTP/2 stream. A message represents either a HTTP request or a HTTP response.

A HTTP/2 stream is the combination of a request message and a response message. Note that this is not to be confused with the flag_end_stream that signals the last frame sent for that particular stream by either peer.

A typical GET request performed by a browser will then have (assuming the stream ID is 13):

  • one HEADERS frame with ID=13, flag_end_headers=true and flag_end_stream=true (a GET request typically has no body)

A typical response to that GET request will then have:

  • one HEADERS frame with ID=13 and flag_end_headers=true
  • one or more DATA frames, all with ID=13; the last DATA frame will have flag_end_stream=true.

Note that due to multiplexing, frames can interleave; this means that if you have two concurrent responses (say for stream 13 and stream 15), for example, you can have this sequence:

HEADERS(13) HEADERS(15) DATA(15) DATA(13) DATA(13] DATA(15) DATA(15]

where the bracket ] means that it's the last frame in the stream.

like image 164
sbordet Avatar answered Sep 25 '22 02:09

sbordet


A supplement:

A DATA frame can ONLY be sent with an HEADERS frame

Not true. DATA frames can also be sent with PUSH_PROMISE.

I don't see any indicators which shows the DATA frames Stream ID (RFC 7540, 6.1)

That's because section 6.1 shows the Payload of a frame, which does not include the header part. 4.1 tells you what the 9 bytes header looks like:

enter image description here

like image 24
laike9m Avatar answered Sep 25 '22 02:09

laike9m