🙄

【Go】h2c で TLS なしの HTTP/2 サーバーをつくる

2024/07/05に公開

h2c は net/http2 に含まれるパッケージである。次のリポジトリを利用させてもらう。

git clone https://github.com/thrawn01/h2c-golang-example.git
cd cmd/server
go get

ポート番号を自分の環境に合わせて修正する。今回は 8000 に変更した。サーバーを起動させるときにデバッグを有効にする

GODEBUG=http2debug=2 go run main.go

curl で HTTP/2 リクエストを送信する

curl --http2-prior-knowledge -v 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 0x561bf2f5dce0)
> GET / HTTP/2
> Host: localhost:8000
> user-agent: curl/7.88.1
> accept: */*
> 
< HTTP/2 200 
< content-type: text/plain; charset=utf-8
< content-length: 22
< date: Fri, 05 Jul 2024 02:45:54 GMT
< 
Hello, /, http: true
* Connection #0 to host localhost left intact

サーバーは次のようなログを出力する

Listening [0.0.0.0:8000]...
2024/07/05 11:52:09 h2c: attempting h2c with prior knowledge.
2024/07/05 11:52:09 http2: server connection from 127.0.0.1:42050 on 0xc0000fc000
2024/07/05 11:52:09 http2: Framer 0xc00018a0e0: wrote SETTINGS len=30, settings: MAX_FRAME_SIZE=1048576, MAX_CONCURRENT_STREAMS=250, MAX_HEADER_LIST_SIZE=1048896, HEADER_TABLE_SIZE=4096, INITIAL_WINDOW_SIZE=1048576
2024/07/05 11:52:09 http2: Framer 0xc00018a0e0: read SETTINGS len=18, settings: MAX_CONCURRENT_STREAMS=100, INITIAL_WINDOW_SIZE=33554432, ENABLE_PUSH=0
2024/07/05 11:52:09 http2: server read frame SETTINGS len=18, settings: MAX_CONCURRENT_STREAMS=100, INITIAL_WINDOW_SIZE=33554432, ENABLE_PUSH=0
2024/07/05 11:52:09 http2: server processing setting [MAX_CONCURRENT_STREAMS = 100]
2024/07/05 11:52:09 http2: server processing setting [INITIAL_WINDOW_SIZE = 33554432]
2024/07/05 11:52:09 http2: server processing setting [ENABLE_PUSH = 0]
2024/07/05 11:52:09 http2: Framer 0xc00018a0e0: read WINDOW_UPDATE len=4 (conn) incr=33488897
2024/07/05 11:52:09 http2: server read frame WINDOW_UPDATE len=4 (conn) incr=33488897
2024/07/05 11:52:09 http2: Framer 0xc00018a0e0: read HEADERS flags=END_STREAM|END_HEADERS stream=1 len=30
2024/07/05 11:52:09 http2: decoded hpack field header field ":method" = "GET"
2024/07/05 11:52:09 http2: decoded hpack field header field ":path" = "/"
2024/07/05 11:52:09 http2: decoded hpack field header field ":scheme" = "http"
2024/07/05 11:52:09 http2: Framer 0xc00018a0e0: wrote SETTINGS flags=ACK len=0
2024/07/05 11:52:09 http2: Framer 0xc00018a0e0: wrote WINDOW_UPDATE len=4 (conn) incr=983041
2024/07/05 11:52:09 http2: decoded hpack field header field ":authority" = "localhost:8000"
2024/07/05 11:52:09 http2: decoded hpack field header field "user-agent" = "curl/7.88.1"
2024/07/05 11:52:09 http2: decoded hpack field header field "accept" = "*/*"
2024/07/05 11:52:09 http2: server read frame HEADERS flags=END_STREAM|END_HEADERS stream=1 len=30
2024/07/05 11:52:09 http2: server encoding header ":status" = "200"
2024/07/05 11:52:09 http2: server encoding header "content-type" = "text/plain; charset=utf-8"
2024/07/05 11:52:09 http2: server encoding header "content-length" = "22"
2024/07/05 11:52:09 http2: server encoding header "date" = "Fri, 05 Jul 2024 02:52:09 GMT"
2024/07/05 11:52:09 http2: Framer 0xc00018a0e0: wrote HEADERS flags=END_HEADERS stream=1 len=49
2024/07/05 11:52:09 http2: Framer 0xc00018a0e0: wrote DATA flags=END_STREAM stream=1 len=22 data="Hello, /, http: true\r\n"
2024/07/05 11:52:09 http2: Framer 0xc00018a0e0: read SETTINGS flags=ACK len=0
2024/07/05 11:52:09 http2: server read frame SETTINGS flags=ACK len=0

Discussion