HTTP Header field is case-insensitive
goで http.Response からヘッダの値を取得する場合は以下のようになる
code:header.go
func handler(w *http.ResponseWriter, r *http.Request) {
hval := r.Header.Get("Cache-Control")
}
チームでペアプロしていてgolang http.Request.Header.Get の仕様に踏み込んだがRFCでは case-insensitive なんだよねという話になり初耳だったので調べてみた
https://cs.opensource.google/go/go/+/refs/tags/go1.22.0:src/net/http/header.go;l=43-51
https://cs.opensource.google/go/go/+/refs/tags/go1.22.0:src/net/textproto/header.go;l=25-39
基本的にHeaderへのアクセサは大文字小文字を区別しない
// Get gets the first value associated with the given key.
// It is case insensitive; CanonicalMIMEHeaderKey is used
// to canonicalize the provided key.
とあるように case insensitive で[CanonicalMIMEHeaderKey]として正規化して取得する
HTTP においても Header (field) で規定されている
RFC 9112: HTTP/1.1 Field Syntax セクション
Each field line consists of a case-insensitive field name followed by a colon (":"), optional leading whitespace, the field line value, and optional trailing whitespace.
Appendix:
HTTP 関連 RFC が大量に出た話と 3 行まとめ | blog.jxck.io
HTTP のリソースと仕様書 - HTTP | MDN