nhttp_form_data (nhttp_lib v2.2.0)

View Source

The multipart/form-data rules of RFC 7578 for one part of the body.

parse_content_disposition/1 reads disposition-type *( OWS ";" OWS [ parameter ] ) (RFC 2183 Section 2). The type and the parameter names are case-insensitive, so the result holds them in lowercase. Parameter values keep their case. A repeated parameter keeps every value, in the order of the field.

part/1 reads the content-disposition and the content-type fields of one part. The disposition type is form-data, and it has exactly one name parameter (RFC 7578 Section 4.2). A repeated name or filename is duplicate_parameter, because two recipients can select different values. A part without a Content-Type is text/plain (RFC 7578 Section 4.4). The function ignores every other field (RFC 7578 Section 4.8).

RFC 7578 Section 4.2 forbids the filename* parameter. A part with filename* and no filename is extended_filename, so a caller that identifies an upload by its filename does not read the part as a plain field. A part with both keeps filename, and the function ignores filename*.

The receive rule

A parameter value is a token or a quoted-string. The parser removes the backslash of each quoted-pair (RFC 822 Section 3.3), and it does no percent-decoding.

Browsers follow the WHATWG form submission algorithm. It writes ", CR and LF in a name or a filename as %22, %0D and %0A, and it writes \ unescaped. This parser keeps %22, %0D and %0A as they are, and it reads \ as the start of a quoted-pair. A name or a filename that contains \ thus does not read back as the sender wrote it. A value that ends in \ loses its closing DQUOTE to the quoted-pair, so part/1 refuses the field with invalid_content_disposition. No encoding on the sender side can change this, because the RFC rule and the WHATWG rule disagree on that octet.

The quoted-pair also makes a risk for a check of the file name extension. filename="a.ph\p" reads as a.php here. Go mime/multipart keeps the backslash and reads a.ph\p. If one check runs in front of this parser and one runs behind it, the two can disagree. Check the filename that part/1 returns, not the filename that another parser read.

Difference from the MIME grammar

RFC 2183 takes its parameter grammar from RFC 2045 Section 5.1 and RFC 822 Section 3.3. This module reads the RFC 9110 Section 5.6.6 grammar of nhttp_media_type instead. The two differ in three points:

  • A MIME token can hold { and }. An RFC 9110 token cannot, so a name or a value with them must be a quoted-string.
  • RFC 2045 allows comments, (...), and white space around =. This module refuses both.
  • RFC 822 limits a quoted-string to US-ASCII. This module accepts octets 0x80 to 0xFF in it, as RFC 7578 Section 5.1 allows for raw UTF-8.

The send rule

headers/2 writes the fields of one part as the WHATWG form submission algorithm does (WHATWG HTML Section 4.10.22.8). In the name, a CR without an LF after it and an LF without a CR before it become CRLF. Then, in the name and the filename, CR becomes %0D, LF becomes %0A and " becomes %22. The function does no other escape, and it never writes filename*. A file part, one with a filename, gets a content-type, application/octet-stream when the options give none. Another part gets one only when the options give it (RFC 7578 Section 4.4). The field names are lowercase.

A name or a filename that holds \ does not read back as the sender wrote it. The WHATWG rule sends \ unescaped, and part/1 reads it as the start of a quoted-pair, as RFC 822 Section 3.3 requires. Chrome, Firefox and curl send \ the same way, so the two ends of this library agree with the browsers and not with each other on that octet.

The filename

A filename can hold a path. Use only the last path segment of it (RFC 7578 Section 4.2), and never use it as a path on the local file system.

Summary

Functions

The content-disposition field of a part with this name, and its content-type field when one applies. Octets 0x80 to 0xFF stay as they are (RFC 7578 Section 5.1). A control octet that the escapes do not remove is {invalid_field, Reason, FieldName}.

Parse a Content-Disposition field value into its normal form.

Read the name, the filename and the content type of one part.

Types

disposition()

-type disposition() :: #{type := binary(), params := [{binary(), binary()}]}.

form_opts()

-type form_opts() :: #{filename => binary(), content_type => binary()}.

part_error()

-type part_error() ::
          missing_content_disposition | invalid_content_disposition | not_form_data | missing_name |
          duplicate_parameter | extended_filename | invalid_content_type.

part_info()

-type part_info() :: #{name := binary(), filename => binary(), content_type := binary()}.

Functions

headers(Name, Opts)

-spec headers(Name :: binary(), form_opts()) ->
                 {ok, nhttp_lib:headers()} | {error, nhttp_multipart:field_error()}.

The content-disposition field of a part with this name, and its content-type field when one applies. Octets 0x80 to 0xFF stay as they are (RFC 7578 Section 5.1). A control octet that the escapes do not remove is {invalid_field, Reason, FieldName}.

parse_content_disposition(Bin)

-spec parse_content_disposition(binary()) -> {ok, disposition()} | {error, invalid_content_disposition}.

Parse a Content-Disposition field value into its normal form.

part(Headers)

-spec part(nhttp_lib:headers()) -> {ok, part_info()} | {error, part_error()}.

Read the name, the filename and the content type of one part.

The lookup is exact on the lowercase field names, the form that every codec of this library gives. Two or more content-disposition lines are invalid_content_disposition, and two or more content-type lines are invalid_content_type. The function returns the content-type value as the field carries it. Give it to nhttp_media_type:parse/1 to read it.