辞書: multipart/form-data

投稿日: 更新日:

multipart/form-dataとは

HTMLでファイルを送るときの手法です。

RFC

元々はRFC 2388で定義されたものでした。

しかしどうやらこれがobsoletesになって、最新はRFC 7578になっているようです。

boundary

RFC 2046の5.1に記載があります。

用語

  • bounary parameter: Content-Typeに付けるboundaryパラメータのこと
    • 70文字以下である必要がある。
  • bounary delimiter: 区切りに使われる文字列で、"–" + boundary parameterになる。

仕様

バウンダリ文字列に使われる文字は以下のように記載があります。

  • デリミタ(bounary delimiter)がカプセルするデータに含まれてはいけない。
  • しかし確率的にぶつかる可能性が低いものを選ぶのでもよい。
  • 代わりのアルゴリズムとして、デリミタをより「読みやすくする」方法がある。そのために、"—“のような文字列を使うのもよい。

よく使われるのが「”-“をいくつか足したもの + ランダム文字列」なのは、これが理由です。

NOTE: Because boundary delimiters must not appear in the body parts being encapsulated, a user agent must exercise care to choose a unique boundary parameter value. The boundary parameter value in the example above could have been the result of an algorithm designed to produce boundary delimiters with a very low probability of already existing in the data to be encapsulated without having to prescan the data. Alternate algorithms might result in more “readable” boundary delimiters for a recipient with an old user agent, but would require more attention to the possibility that the boundary delimiter might appear at the beginning of some line in the encapsulated part. The simplest boundary delimiter line possible is something like “—”, with a closing boundary delimiter line of “—–”.

区切りが”–“から始まるというのは以下の箇所からです。

The Content-Type field for multipart entities requires one parameter, “boundary”. The boundary delimiter line is then defined as a line consisting entirely of two hyphen characters (”-", decimal value 45) followed by the boundary parameter value from the Content-Type header field, optional linear whitespace, and a terminating CRLF.

最後の区切りが"–“で終わるというのは、以下の箇所からです。

The boundary delimiter line following the last body part is a distinguished delimiter that indicates that no further body parts will follow. Such a delimiter line is identical to the previous delimiter lines, with the addition of two more hyphens after the boundary parameter value.

まとめると以下のようになります。

  • Content-Typeのパラメータにboundaryを付ける
    • boundaryは1〜70文字なら何でもいいが、以下の点を満たすとよい
      • ぶつからない、あるいはぶつかる可能性が低いこと
      • 読みやすくするために”—“のように始まることも考慮
  • 区切りは”–" + boundary parameter
  • 最後の区切りは"–" + boundary parameter + “–”

外部サイト