同じ名前のHTTPヘッダを複数回出力してもよいのか

結論: 値がcomma-separated listと定められているヘッダはOK

同じ名前のHTTPヘッダを複数回出力する、とは

こういう出力:

...
Vary: Accept-Encoding
Vary: User-Agent
...

同じ名前のHTTPヘッダを複数回出力すると、受信者はどう振る舞うのか

RFCより引用:

A sender MUST NOT generate multiple header fields with the same field name in a message unless either the entire field value for that header field is defined as a comma-separated list [i.e., #(values)] or the header field is a well-known exception (as noted below).

RFC 7230 - Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing

A recipient MAY combine multiple header fields with the same field name into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field value to the combined field value in order, separated by a comma.

RFC 7230 - Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing
  • 送信者は値がカンマ区切りリストと定義されていないヘッダを複数回出力してはならない
  • 受信者は複数の同名ヘッダを出現した順にカンマ区切りリストへ結合してもよい

ということが書いてあります。

つまり以下のヘッダ出力があったとき:

...
Vary: Accept-Encoding
Vary: User-Agent
...

……受信者は以下のように結合するかもしれません:

...
Vary: Accept-Encoding, User-Agent
...