iTranslated by AI
Using HTTP/2 Cleartext (h2c) with nghttpd
By specifying --no-tls with nghttpd, you can use h2c (HTTP/2 cleartext) without TLS.
/usr/sbin/nghttpd 8000 --no-tls -v -d ./web
In this state, nghttpd does not support HTTP/1, so an HTTP client that can send a connection preface (PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n) is required. For curl, specify the --http2-prior-knowledge option
curl -v --http2-prior-knowledge http://localhost:8000
* Trying 127.0.0.1:8000...
* Connected to localhost (127.0.0.1) port 8000 (#0)
* h2h3 [:method: GET]
* h2h3 [:path: /]
* h2h3 [:scheme: http]
* h2h3 [:authority: localhost:8000]
* h2h3 [user-agent: curl/7.88.1]
* h2h3 [accept: */*]
* Using Stream ID: 1 (easy handle 0x55a9bf4ddce0)
> GET / HTTP/2
> Host: localhost:8000
> user-agent: curl/7.88.1
> accept: */*
>
< HTTP/2 200
< server: nghttpd nghttp2/1.52.0
< cache-control: max-age=3600
< date: Wed, 10 Jul 2024 04:09:08 GMT
< content-length: 6
< last-modified: Wed, 10 Jul 2024 03:26:27 GMT
< content-type: text/html
<
hello
* Connection #0 to host localhost left intact
Sending an HTTP/1.1 GET request returns the following response:
curl -v --http1.1 http://localhost:8000
* Trying 127.0.0.1:8000...
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET / HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.88.1
> Accept: */*
>
* Received HTTP/0.9 when not allowed
* Closing connection 0
curl: (1) Received HTTP/0.9 when not allowed
The response returned is an HTTP/2 frame of binary data, and since the beginning line does not include an HTTP version, it is misinterpreted as HTTP/0.9.
Looking at the nghttpd logs, a Settings frame like the following is sent first. A Settings frame is an agreement for how the client and server exchange data.
[id=9] [256.282] send SETTINGS frame <length=6, flags=0x00, stream_id=0>
(niv=1)
[SETTINGS_MAX_CONCURRENT_STREAMS(0x03):100]
The structure of a frame consists of a header and a payload. The header has a fixed length of 9 bytes, and the payload has an arbitrary length. The header is composed of the payload length (3 bytes), frame type (1 byte), flags (1 byte), and stream ID (4 bytes).
The value representing a SETTINGS frame is "0x04". In the log record, the length is 0x06, flags are 0x00, and the stream ID is 0x00. Therefore, the header is represented as follows:
0x00 0x00 0x06 # Payload length
0x04 # Frame type (Settings)
0x00 # Flags
0x00 0x00 0x00 0x00 # Stream ID
Although I have used line breaks for clarity, the actual data is a 9-byte sequence.
Next, let's examine the payload of the Settings frame. It consists of an identifier (2 bytes) and a value (4 bytes) for each configuration item. Since the payload length is 6 bytes, it means there is only one configuration item.
From the logs above, it can be seen that the identifier for SETTINGS_MAX_CONCURRENT_STREAMS is 0x03 and the value is 100 (0x64 in hexadecimal). The byte sequence for the entire Settings frame (15 = 9 + 6) is as follows:
0x00 0x00 0x06 # Payload length
0x04 # Frame type (Settings)
0x00 # Flags
0x00 0x00 0x00 0x00 # Stream ID
0x00 0x03 # Identifier SETTINGS_MAX_CONCURRENT_STREAMS
0x00 0x00 0x00 0x64 # Value
Discussion