Debian Codegraph @

doc/src/guide/multipart.asciidoc

raw
1 [[multipart]]
2 == Multipart requests
3
4 Multipart originates from MIME, an Internet standard that
5 extends the format of emails.
6
7 A multipart message is a list of parts. A part contains
8 headers and a body. The body of the parts may be
9 of any media type, and contain text or binary data.
10 It is possible for parts to contain a multipart media
11 type.
12
13 In the context of HTTP, multipart is most often used
14 with the `multipart/form-data` media type. It is what
15 browsers use to upload files through HTML forms.
16
17 The `multipart/byteranges` is also common. It is the
18 media type used to send arbitrary bytes from a resource,
19 enabling clients to resume downloads.
20
21 === Form-data
22
23 In the normal case, when a form is submitted, the
24 browser will use the `application/x-www-form-urlencoded`
25 content-type. This type is just a list of keys and
26 values and is therefore not fit for uploading files.
27
28 That's where the `multipart/form-data` content-type
29 comes in. When the form is configured to use this
30 content-type, the browser will create a multipart
31 message where each part corresponds to a field on
32 the form. For files, it also adds some metadata in
33 the part headers, like the file name.
34
35 A form with a text input, a file input and a select
36 choice box will result in a multipart message with
37 three parts, one for each field.
38
39 The browser does its best to determine the media type
40 of the files it sends this way, but you should not
41 rely on it for determining the contents of the file.
42 Proper investigation of the contents is recommended.
43
44 === Checking for multipart messages
45
46 The content-type header indicates the presence of
47 a multipart message:
48
49 [source,erlang]
50 ----
51 {<<"multipart">>, <<"form-data">>, _}
52 = cowboy_req:parse_header(<<"content-type">>, Req).
53 ----
54
55 === Reading a multipart message
56
57 Cowboy provides two sets of functions for reading
58 request bodies as multipart messages.
59
60 The `cowboy_req:read_part/1,2` functions return the
61 next part's headers, if any.
62
63 The `cowboy_req:read_part_body/1,2` functions return
64 the current part's body. For large bodies you may
65 need to call the function multiple times.
66
67 To read a multipart message you need to iterate over
68 all its parts:
69
70 [source,erlang]
71 ----
72 multipart(Req0) ->
73 case cowboy_req:read_part(Req0) of
74 {ok, _Headers, Req1} ->
75 {ok, _Body, Req} = cowboy_req:read_part_body(Req1),
76 multipart(Req);
77 {done, Req} ->
78 Req
79 end.
80 ----
81
82 When part bodies are too large, Cowboy will return
83 a `more` tuple, and allow you to loop until the part
84 body has been fully read.
85
86 The function `cow_multipart:form_data/1` can be used
87 to quickly obtain information about a part from a
88 `multipart/form-data` message. The function returns
89 a `data` or a `file` tuple depending on whether this
90 is a normal field or a file being uploaded.
91
92 The following snippet will use this function and
93 use different strategies depending on whether the
94 part is a file:
95
96 [source,erlang]
97 ----
98 multipart(Req0) ->
99 case cowboy_req:read_part(Req0) of
100 {ok, Headers, Req1} ->
101 try cow_multipart:form_data(Headers) of
102 {data, _FieldName} ->
103 {ok, _Body, Req} = cowboy_req:read_part_body(Req1),
104 multipart(Req);
105 {file, _FieldName, _Filename, _CType} ->
106 Req = stream_file(Req1),
107 multipart(Req)
108 catch _:_ ->
109 cowboy_req:reply(400, Req1)
110 end;
111 {done, Req} ->
112 Req
113 end.
114
115 stream_file(Req0) ->
116 case cowboy_req:read_part_body(Req0) of
117 {ok, _LastBodyChunk, Req} ->
118 Req;
119 {more, _BodyChunk, Req} ->
120 stream_file(Req)
121 end.
122 ----
123
124 Both the part header and body reading functions can take
125 options that will be given to the request body reading
126 functions. By default, `cowboy_req:read_part/1` reads
127 up to 64KB for up to 5 seconds. `cowboy_req:read_part_body/1`
128 has the same defaults as `cowboy_req:read_body/1`.
129
130 To change the defaults for part headers:
131
132 [source,erlang]
133 cowboy_req:read_part(Req, #{length => 128000}).
134
135 And for part bodies:
136
137 [source,erlang]
138 cowboy_req:read_part_body(Req, #{length => 1000000, period => 7000}).
139
140 === Skipping unwanted parts
141
142 Part bodies do not have to be read. Cowboy will automatically
143 skip it when you request the next part's body.
144
145 The following snippet reads all part headers and skips
146 all bodies:
147
148 [source,erlang]
149 ----
150 multipart(Req0) ->
151 case cowboy_req:read_part(Req0) of
152 {ok, _Headers, Req} ->
153 multipart(Req);
154 {done, Req} ->
155 Req
156 end.
157 ----
158
159 Similarly, if you start reading the body and it ends up
160 being too big, you can simply continue with the next part.
161 Cowboy will automatically skip what remains.
162
163 While Cowboy can skip part bodies automatically, the read
164 rate is not configurable. Depending on your application
165 you may want to skip manually, in particular if you observe
166 poor performance while skipping.
167
168 You do not have to read all parts either. You can stop
169 reading as soon as you find the data you need.
170
171 // @todo Cover the building of multipart messages.