ひだまりソケットは壊れない

ソフトウェア開発に関する話を書きます。 最近は主に Android アプリ、Windows アプリ (UWP アプリ)、Java 関係です。

まじめなことを書くつもりでやっています。 適当なことは 「一角獣は夜に啼く」 に書いています。

HTTP の media type の記述中に空白文字を入れても良いのかどうか

上の記事を読んで、私も Media Types の記述中の空白文字の扱いについて気になってたことを思いだしたので調べてみました。 RFC 2045 における BNFRFC 822 の定義のものが使用されている (?) ようですが、空白文字の扱いについてはよくわかりませんでした。

HTTP 1.1 (RFC 2616) の場合

今回の moznion さんの記事だと web ブラウザ相手っぽいので、HTTP 1.1 について調べてみることにしましょう。

RFC 2616 の拡張 BNF

RFC 2616 では、RFC 822 に似た拡張 BNF が使われています。

その定義の中に、暗黙的な linear white space について記述がありました。

2.1 Augmented BNF

(中略)

implied *LWS
The grammar described by this specification is word-based. Except where noted otherwise, linear white space (LWS) can be included between any two adjacent words (token or quoted-string), and between adjacent words and separators, without changing the interpretation of a field. At least one delimiter (LWS and/or separators) MUST exist between any two tokens (for the definition of "token" below), since they would otherwise be interpreted as a single token.
RFC 2616, section 2.1

つまり、単語ベースなので、(特に空白文字について言及がない限りは) BNF に明示的に記述されてなくても空白文字を入れてもいいよ、ってことだと思います。 多分。

RFC 2616 における Media type

で、Media type について次のように記述されていました。

3.7 Media Types

HTTP uses Internet Media Types [17] in the Content-Type (section 14.17) and Accept (section 14.1) header fields in order to provide open and extensible data typing and type negotiation.

  • media-type = type "/" subtype *( ";" parameter )
  • type = token
  • subtype = token

Parameters MAY follow the type/subtype in the form of attribute/value pairs (as defined in section 3.6).

The type, subtype, and parameter attribute names are case-insensitive. Parameter values might or might not be case-sensitive, depending on the semantics of the parameter name. Linear white space (LWS) MUST NOT be used between the type and subtype, nor between an attribute and its value. The presence or absence of a parameter might be significant to the processing of a media-type, depending on its definition within the media type registry.

RFC 2616, section 3.7

強調した部分に空白文字について書かれています: 連続した空白文字 (でいいんだろうか? Linear white space?) を、typesubtype の間に入れてはいけないし、attributevalue の間に入れてもいけない。

というわけで、特に言及されてない type "/" subtypeparameter の間には空白文字を入れてもいいみたいですね。

そんなわけで

moznion さんの記事中の

1. parameterのセミコロン区切りの部分に空白を入れても動く (Firefox, Chrome, Safariで動く)

text/html ; charset=UTF-8 ; foo=bar のようにしても動きます.
これはよく見かけるからなんとなく良さそうですが,BNF的に正しいのかどうか.まあ,検証に用いたブラウザ全てで動いたのでまあ良いのかしら……

というのは、HTTP 1.1 的に正しい media type だといえると思います。

あとの 3 つは HTTP 1.1 的にはだめそうですね。