Benjamin BALET
Benjamin BALET

Reputation: 969

Can I return "HTTP/1.1" protocol version when using HTTP/2?

On my project, I've some hardcoded HTTP status codes and descriptions, such as:

$this->output->set_header("HTTP/1.1 422 Unprocessable entity");

And I'm not clear about Section 8.1.3 of HTTP/2 specs because it looks like a HTTP/2 server doesn't return anymore the full header line (protocol, status code, and status description). For example:

HTTP/1.1 200 OK
Content-Type: image/jpeg
Content-Length: 123

Would become:

HEADERS
  + END_STREAM
  + END_HEADERS
:status = 200
content-type = image/jpeg
content-length = 123

When using my demo, browsers don't look to be bothered by responses starting by "HTTP/1.1".

So I am wondering if it is urgent to rely on the framework for returning a proper status code (the framework builds a string starting by $_SERVER['SERVER_PROTOCOL']). See this SO question for implementation. Or if it doesn't matter.

Upvotes: 0

Views: 1121

Answers (1)

Matthias247
Matthias247

Reputation: 10396

As you said there is no textual status line in the HTTP/2 protocol, which could be used to transfer a string that includes "HTTP/1.1". HTTP/2 only transmits the status code in numerical form in the pseudoheader value :status, which would be picked up by the client.

So the main question is what the framework of your choice is doing when you call

$this->output->set_header("HTTP/1.1 422 Unprocessable entity");

If the framework would in the HTTP/2 case parse this line, extract only the 422 and put it in the :status pseudoheader then everything is fine. If it would not parse it, then I don't know how it would be able to set a status code. Your framework/webserver also can't use this string as a normal header - since what you set here is only a single string value. Setting a normal header would require the name and the value of the header. So I guess your framework just drops the irrelevant data and everything is fine.

What you could do if you are interested is using the nghttp2 commandline tool nghttp to debug the connection. Make a call like nghttp -v https://your-host and it will debug-print you all received header values including the pseudo-headers.

Upvotes: 2

Related Questions