1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * soup-multipart.c: multipart HTTP message bodies
5 * Copyright (C) 2008 Red Hat, Inc.
14 #include "soup-multipart.h"
18 * SECTION:soup-multipart
19 * @short_description: multipart HTTP message bodies
20 * @see_also: #SoupMessageBody, #SoupMessageHeaders
27 * Represents a multipart HTTP message body, parsed according to the
28 * syntax of RFC 2046. Of particular interest to HTTP are
29 * <literal>multipart/byte-ranges</literal> and
30 * <literal>multipart/form-data</literal>.
32 * Although the headers of a #SoupMultipart body part will contain the
33 * full headers from that body part, libsoup does not interpret them
34 * according to MIME rules. For example, each body part is assumed to
35 * have "binary" Content-Transfer-Encoding, even if its headers
36 * explicitly state otherwise. In other words, don't try to use
37 * #SoupMultipart for handling real MIME multiparts.
42 struct SoupMultipart {
43 char *mime_type, *boundary;
44 GPtrArray *headers, *bodies;
47 static SoupMultipart *
48 soup_multipart_new_internal (char *mime_type, char *boundary)
50 SoupMultipart *multipart;
52 multipart = g_slice_new (SoupMultipart);
53 multipart->mime_type = mime_type;
54 multipart->boundary = boundary;
55 multipart->headers = g_ptr_array_new_with_free_func ((GDestroyNotify)soup_message_headers_free);
56 multipart->bodies = g_ptr_array_new_with_free_func ((GDestroyNotify)soup_buffer_free);
62 generate_boundary (void)
66 data[0] = g_random_int ();
67 data[1] = g_random_int ();
69 /* The maximum boundary string length is 69 characters, and a
70 * stringified SHA256 checksum is 64 bytes long.
72 return g_compute_checksum_for_data (G_CHECKSUM_SHA256,
73 (const guchar *)&data,
79 * @mime_type: the MIME type of the multipart to create.
81 * Creates a new empty #SoupMultipart with a randomly-generated
82 * boundary string. Note that @mime_type must be the full MIME type,
83 * including "multipart/".
85 * Return value: a new empty #SoupMultipart of the given @mime_type
90 soup_multipart_new (const char *mime_type)
92 return soup_multipart_new_internal (g_strdup (mime_type),
93 generate_boundary ());
97 find_boundary (const char *start, const char *end,
98 const char *boundary, int boundary_len)
102 for (b = memchr (start, '-', end - start);
103 b && b + boundary_len + 4 < end;
104 b = memchr (b + 2, '-', end - (b + 2))) {
105 /* Check for "--boundary" */
107 memcmp (b + 2, boundary, boundary_len) != 0)
110 /* Check that it's at start of line */
111 if (!(b == start || (b[-1] == '\n' && b[-2] == '\r')))
114 /* Check for "--" or "\r\n" after boundary */
115 if ((b[boundary_len + 2] == '-' && b[boundary_len + 3] == '-') ||
116 (b[boundary_len + 2] == '\r' && b[boundary_len + 3] == '\n'))
123 * soup_multipart_new_from_message:
124 * @headers: the headers of the HTTP message to parse
125 * @body: the body of the HTTP message to parse
127 * Parses @headers and @body to form a new #SoupMultipart
129 * Return value: (nullable): a new #SoupMultipart (or %NULL if the
130 * message couldn't be parsed or wasn't multipart).
135 soup_multipart_new_from_message (SoupMessageHeaders *headers,
136 SoupMessageBody *body)
138 SoupMultipart *multipart;
139 const char *content_type, *boundary;
142 SoupBuffer *flattened;
143 const char *start, *split, *end, *body_end;
144 SoupMessageHeaders *part_headers;
145 SoupBuffer *part_body;
147 content_type = soup_message_headers_get_content_type (headers, ¶ms);
151 boundary = g_hash_table_lookup (params, "boundary");
152 if (strncmp (content_type, "multipart/", 10) != 0 || !boundary) {
153 g_hash_table_destroy (params);
157 multipart = soup_multipart_new_internal (
158 g_strdup (content_type), g_strdup (boundary));
159 g_hash_table_destroy (params);
161 flattened = soup_message_body_flatten (body);
162 body_end = flattened->data + flattened->length;
163 boundary = multipart->boundary;
164 boundary_len = strlen (boundary);
167 start = find_boundary (flattened->data, body_end,
168 boundary, boundary_len);
170 soup_multipart_free (multipart);
171 soup_buffer_free (flattened);
175 while (start[2 + boundary_len] != '-') {
176 end = find_boundary (start + 2 + boundary_len, body_end,
177 boundary, boundary_len);
179 soup_multipart_free (multipart);
180 soup_buffer_free (flattened);
184 split = strstr (start, "\r\n\r\n");
185 if (!split || split > end) {
186 soup_multipart_free (multipart);
187 soup_buffer_free (flattened);
192 /* @start points to the start of the boundary line
193 * preceding this part, and @split points to the end
194 * of the headers / start of the body.
196 * We tell soup_headers_parse() to start parsing at
197 * @start, because it skips the first line of the
198 * input anyway (expecting it to be either a
199 * Request-Line or Status-Line).
201 part_headers = soup_message_headers_new (SOUP_MESSAGE_HEADERS_MULTIPART);
202 g_ptr_array_add (multipart->headers, part_headers);
203 if (!soup_headers_parse (start, split - 2 - start,
205 soup_multipart_free (multipart);
206 soup_buffer_free (flattened);
210 /* @split, as previously mentioned, points to the
211 * start of the body, and @end points to the start of
212 * the following boundary line, which is to say 2 bytes
213 * after the end of the body.
215 part_body = soup_buffer_new_subbuffer (flattened,
216 split - flattened->data,
218 g_ptr_array_add (multipart->bodies, part_body);
223 soup_buffer_free (flattened);
228 * soup_multipart_get_length:
229 * @multipart: a #SoupMultipart
231 * Gets the number of body parts in @multipart
233 * Return value: the number of body parts in @multipart
238 soup_multipart_get_length (SoupMultipart *multipart)
240 return multipart->bodies->len;
244 * soup_multipart_get_part:
245 * @multipart: a #SoupMultipart
246 * @part: the part number to get (counting from 0)
247 * @headers: (out) (transfer none): return location for the MIME part
249 * @body: (out) (transfer none): return location for the MIME part
252 * Gets the indicated body part from @multipart.
254 * Return value: %TRUE on success, %FALSE if @part is out of range (in
255 * which case @headers and @body won't be set)
260 soup_multipart_get_part (SoupMultipart *multipart, int part,
261 SoupMessageHeaders **headers, SoupBuffer **body)
263 if (part < 0 || part >= multipart->bodies->len)
265 *headers = multipart->headers->pdata[part];
266 *body = multipart->bodies->pdata[part];
271 * soup_multipart_append_part:
272 * @multipart: a #SoupMultipart
273 * @headers: the MIME part headers
274 * @body: the MIME part body
276 * Adds a new MIME part to @multipart with the given headers and body.
277 * (The multipart will make its own copies of @headers and @body, so
278 * you should free your copies if you are not using them for anything
284 soup_multipart_append_part (SoupMultipart *multipart,
285 SoupMessageHeaders *headers,
288 SoupMessageHeaders *headers_copy;
289 SoupMessageHeadersIter iter;
290 const char *name, *value;
292 /* Copying @headers is annoying, but the alternatives seem
295 * 1) We don't want to use g_boxed_copy, because
296 * SoupMessageHeaders actually implements that as just a
297 * ref, which would be confusing since SoupMessageHeaders
298 * is mutable and the caller might modify @headers after
301 * 2) We can't change SoupMessageHeaders to not just do a ref
302 * from g_boxed_copy, because that would break language
303 * bindings (which need to be able to hold a ref on
304 * msg->request_headers, but don't want to duplicate it).
306 * 3) We don't want to steal the reference to @headers,
307 * because then we'd have to either also steal the
308 * reference to @body (which would be inconsistent with
309 * other SoupBuffer methods), or NOT steal the reference to
310 * @body, in which case there'd be inconsistency just
311 * between the two arguments of this method!
313 headers_copy = soup_message_headers_new (SOUP_MESSAGE_HEADERS_MULTIPART);
314 soup_message_headers_iter_init (&iter, headers);
315 while (soup_message_headers_iter_next (&iter, &name, &value))
316 soup_message_headers_append (headers_copy, name, value);
318 g_ptr_array_add (multipart->headers, headers_copy);
319 g_ptr_array_add (multipart->bodies, soup_buffer_copy (body));
323 * soup_multipart_append_form_string:
324 * @multipart: a multipart (presumably of type "multipart/form-data")
325 * @control_name: the name of the control associated with @data
326 * @data: the body data
328 * Adds a new MIME part containing @data to @multipart, using
329 * "Content-Disposition: form-data", as per the HTML forms
330 * specification. See soup_form_request_new_from_multipart() for more
336 soup_multipart_append_form_string (SoupMultipart *multipart,
337 const char *control_name, const char *data)
341 body = soup_buffer_new (SOUP_MEMORY_COPY, data, strlen (data));
342 soup_multipart_append_form_file (multipart, control_name,
344 soup_buffer_free (body);
348 * soup_multipart_append_form_file:
349 * @multipart: a multipart (presumably of type "multipart/form-data")
350 * @control_name: the name of the control associated with this file
351 * @filename: the name of the file, or %NULL if not known
352 * @content_type: the MIME type of the file, or %NULL if not known
353 * @body: the file data
355 * Adds a new MIME part containing @body to @multipart, using
356 * "Content-Disposition: form-data", as per the HTML forms
357 * specification. See soup_form_request_new_from_multipart() for more
363 soup_multipart_append_form_file (SoupMultipart *multipart,
364 const char *control_name, const char *filename,
365 const char *content_type, SoupBuffer *body)
367 SoupMessageHeaders *headers;
368 GString *disposition;
370 headers = soup_message_headers_new (SOUP_MESSAGE_HEADERS_MULTIPART);
371 disposition = g_string_new ("form-data; ");
372 soup_header_g_string_append_param_quoted (disposition, "name", control_name);
374 g_string_append (disposition, "; ");
375 soup_header_g_string_append_param_quoted (disposition, "filename", filename);
377 soup_message_headers_append (headers, "Content-Disposition",
379 g_string_free (disposition, TRUE);
382 soup_message_headers_append (headers, "Content-Type",
386 g_ptr_array_add (multipart->headers, headers);
387 g_ptr_array_add (multipart->bodies, soup_buffer_copy (body));
391 * soup_multipart_to_message:
392 * @multipart: a #SoupMultipart
393 * @dest_headers: the headers of the HTTP message to serialize @multipart to
394 * @dest_body: the body of the HTTP message to serialize @multipart to
396 * Serializes @multipart to @dest_headers and @dest_body.
401 soup_multipart_to_message (SoupMultipart *multipart,
402 SoupMessageHeaders *dest_headers,
403 SoupMessageBody *dest_body)
405 SoupMessageHeaders *part_headers;
406 SoupBuffer *part_body;
407 SoupMessageHeadersIter iter;
408 const char *name, *value;
413 params = g_hash_table_new (g_str_hash, g_str_equal);
414 g_hash_table_insert (params, "boundary", multipart->boundary);
415 soup_message_headers_set_content_type (dest_headers,
416 multipart->mime_type,
418 g_hash_table_destroy (params);
420 for (i = 0; i < multipart->bodies->len; i++) {
421 part_headers = multipart->headers->pdata[i];
422 part_body = multipart->bodies->pdata[i];
424 str = g_string_new (i == 0 ? NULL : "\r\n");
425 g_string_append (str, "--");
426 g_string_append (str, multipart->boundary);
427 g_string_append (str, "\r\n");
428 soup_message_headers_iter_init (&iter, part_headers);
429 while (soup_message_headers_iter_next (&iter, &name, &value))
430 g_string_append_printf (str, "%s: %s\r\n", name, value);
431 g_string_append (str, "\r\n");
432 soup_message_body_append (dest_body, SOUP_MEMORY_TAKE,
434 g_string_free (str, FALSE);
436 soup_message_body_append_buffer (dest_body, part_body);
439 str = g_string_new ("\r\n--");
440 g_string_append (str, multipart->boundary);
441 g_string_append (str, "--\r\n");
442 soup_message_body_append (dest_body, SOUP_MEMORY_TAKE,
444 g_string_free (str, FALSE);
446 /* (The "\r\n" after the close-delimiter seems wrong according
447 * to my reading of RFCs 2046 and 2616, but that's what
448 * everyone else does.)
453 * soup_multipart_free:
454 * @multipart: a #SoupMultipart
461 soup_multipart_free (SoupMultipart *multipart)
463 g_free (multipart->mime_type);
464 g_free (multipart->boundary);
465 g_ptr_array_free (multipart->headers, TRUE);
466 g_ptr_array_free (multipart->bodies, TRUE);
468 g_slice_free (SoupMultipart, multipart);
471 static SoupMultipart *
472 soup_multipart_copy (SoupMultipart *multipart)
477 copy = soup_multipart_new_internal (g_strdup (multipart->mime_type),
478 g_strdup (multipart->boundary));
479 for (i = 0; i < multipart->bodies->len; i++) {
480 soup_multipart_append_part (copy,
481 multipart->headers->pdata[i],
482 multipart->bodies->pdata[i]);
487 G_DEFINE_BOXED_TYPE (SoupMultipart, soup_multipart, soup_multipart_copy, soup_multipart_free)