TrevTheDev
TrevTheDev

Reputation: 2737

With a node HTTP2 push stream, what is the purpose of the request header vs. response header?

When a node HTTP2 server creates a new push stream, what is the purpose of the request header vs. response header?

Server code:

http2Server.on('stream', (stream) => {
  stream.pushStream({ ':path': '/' }, (err, pushStream, headers) => { // send request headers
    pushStream.respond({ ':status': 200 });                           // send response headers
    pushStream.end('some pushed data');
  });
  stream.end('some data');
});

Client code:

clientHttp2Session.on('stream', (pushedStream, requestHeaders) => { // receive request headers
  pushedStream.on('push', (responseHeaders) => {                    // receive response headers
    /* Process response headers */
  });
  pushedStream.on('data', (chunk) => { /* handle pushed data */ });
});

Both of these must be sent before any data is sent, so it seems one of them is redundant?

MDN states:

Request headers contain more information about the resource to be fetched, or about the client requesting the resource.

Response headers hold additional information about the response, like its location or about the server providing it.

However, that seems to be slanted towards a more client request, server response model - which doesn't apply to push.

Upvotes: 0

Views: 586

Answers (1)

Barry Pollard
Barry Pollard

Reputation: 45970

The "request header" as you call it, maps to the PUSH_PROMISE frame in HTTP/2 (you can see this in the NodeJS source code).

A PUSH_PROMISE frame is defined in the HTTP/2 spec, and is used to tell the client "hey I'm going to pretend you sent this request, and send you a response to that 'fake request' next."

It is used to inform the client this response is on it's way, and if it needs it, then not to bother making another request for it.

It also allows the client to cancel this push request with a RST_STREAM frame to say "No thanks, I don't want that." This may be because the server is pushing a resource that the client already has in it's cache or for some other reason.

Upvotes: 1

Related Questions