1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * soup-message-headers.c: HTTP message header arrays
4 *
5 * Copyright (C) 2007, 2008 Red Hat, Inc.
6 */
7
8 #ifdef HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include <string.h>
13
14 #include "soup-message-headers.h"
15 #include "soup.h"
16 #include "soup-misc-private.h"
17
18 /**
19 * SECTION:soup-message-headers
20 * @short_description: HTTP message headers
21 * @see_also: #SoupMessage
22 *
23 * #SoupMessageHeaders represents the HTTP message headers associated
24 * with a request or response.
25 **/
26
27 /**
28 * SoupMessageHeaders:
29 *
30 * The HTTP message headers associated with a request or response.
31 */
32
33 /**
34 * SoupMessageHeadersType:
35 * @SOUP_MESSAGE_HEADERS_REQUEST: request headers
36 * @SOUP_MESSAGE_HEADERS_RESPONSE: response headers
37 * @SOUP_MESSAGE_HEADERS_MULTIPART: multipart body part headers
38 *
39 * Value passed to soup_message_headers_new() to set certain default
40 * behaviors.
41 **/
42
43 typedef void (*SoupHeaderSetter) (SoupMessageHeaders *, const char *);
44 static const char *intern_header_name (const char *name, SoupHeaderSetter *setter);
45 static void clear_special_headers (SoupMessageHeaders *hdrs);
46
47 typedef struct {
48 const char *name;
49 char *value;
50 } SoupHeader;
51
52 struct SoupMessageHeaders {
53 GArray *array;
54 GHashTable *concat;
55 SoupMessageHeadersType type;
56
57 SoupEncoding encoding;
58 goffset content_length;
59 SoupExpectation expectations;
60 char *content_type;
61
62 int ref_count;
63 };
64
65 /**
66 * soup_message_headers_new:
67 * @type: the type of headers
68 *
69 * Creates a #SoupMessageHeaders. (#SoupMessage does this
70 * automatically for its own headers. You would only need to use this
71 * method if you are manually parsing or generating message headers.)
72 *
73 * Return value: a new #SoupMessageHeaders
74 **/
75 SoupMessageHeaders *
soup_message_headers_new(SoupMessageHeadersType type)76 soup_message_headers_new (SoupMessageHeadersType type)
77 {
78 SoupMessageHeaders *hdrs;
79
80 hdrs = g_slice_new0 (SoupMessageHeaders);
81 /* FIXME: is "5" a good default? */
82 hdrs->array = g_array_sized_new (TRUE, FALSE, sizeof (SoupHeader), 5);
83 hdrs->type = type;
84 hdrs->encoding = -1;
85 hdrs->ref_count = 1;
86
87 return hdrs;
88 }
89
90 static SoupMessageHeaders *
soup_message_headers_copy(SoupMessageHeaders * hdrs)91 soup_message_headers_copy (SoupMessageHeaders *hdrs)
92 {
93 g_atomic_int_inc (&hdrs->ref_count);
94 return hdrs;
95 }
96
97 /**
98 * soup_message_headers_free:
99 * @hdrs: a #SoupMessageHeaders
100 *
101 * Frees @hdrs.
102 **/
103 void
soup_message_headers_free(SoupMessageHeaders * hdrs)104 soup_message_headers_free (SoupMessageHeaders *hdrs)
105 {
106 if (!g_atomic_int_dec_and_test (&hdrs->ref_count))
107 return;
108
109 soup_message_headers_clear (hdrs);
110 g_array_free (hdrs->array, TRUE);
111 g_clear_pointer (&hdrs->concat, g_hash_table_destroy);
112 g_slice_free (SoupMessageHeaders, hdrs);
113 }
114
G_DEFINE_BOXED_TYPE(SoupMessageHeaders,soup_message_headers,soup_message_headers_copy,soup_message_headers_free)115 G_DEFINE_BOXED_TYPE (SoupMessageHeaders, soup_message_headers, soup_message_headers_copy, soup_message_headers_free)
116
117 /**
118 * soup_message_headers_get_headers_type:
119 * @hdrs: a #SoupMessageHeaders
120 *
121 * Gets the type of headers.
122 *
123 * Return value: the header's type.
124 *
125 * Since: 2.50
126 **/
127 SoupMessageHeadersType
128 soup_message_headers_get_headers_type (SoupMessageHeaders *hdrs)
129 {
130 return hdrs->type;
131 }
132
133 /**
134 * soup_message_headers_clear:
135 * @hdrs: a #SoupMessageHeaders
136 *
137 * Clears @hdrs.
138 **/
139 void
soup_message_headers_clear(SoupMessageHeaders * hdrs)140 soup_message_headers_clear (SoupMessageHeaders *hdrs)
141 {
142 SoupHeader *hdr_array = (SoupHeader *)hdrs->array->data;
143 guint i;
144
145 for (i = 0; i < hdrs->array->len; i++)
146 g_free (hdr_array[i].value);
147 g_array_set_size (hdrs->array, 0);
148
149 if (hdrs->concat)
150 g_hash_table_remove_all (hdrs->concat);
151
152 clear_special_headers (hdrs);
153 }
154
155 /**
156 * soup_message_headers_clean_connection_headers:
157 * @hdrs: a #SoupMessageHeaders
158 *
159 * Removes all the headers listed in the Connection header.
160 *
161 * Since: 2.36
162 */
163 void
soup_message_headers_clean_connection_headers(SoupMessageHeaders * hdrs)164 soup_message_headers_clean_connection_headers (SoupMessageHeaders *hdrs)
165 {
166 /* RFC 2616 14.10 */
167 const char *connection;
168 GSList *tokens, *t;
169
170 connection = soup_message_headers_get_list (hdrs, "Connection");
171 if (!connection)
172 return;
173
174 tokens = soup_header_parse_list (connection);
175 for (t = tokens; t; t = t->next)
176 soup_message_headers_remove (hdrs, t->data);
177 soup_header_free_list (tokens);
178 }
179
180 /**
181 * soup_message_headers_append:
182 * @hdrs: a #SoupMessageHeaders
183 * @name: the header name to add
184 * @value: the new value of @name
185 *
186 * Appends a new header with name @name and value @value to @hdrs. (If
187 * there is an existing header with name @name, then this creates a
188 * second one, which is only allowed for list-valued headers; see also
189 * soup_message_headers_replace().)
190 *
191 * The caller is expected to make sure that @name and @value are
192 * syntactically correct.
193 **/
194 void
soup_message_headers_append(SoupMessageHeaders * hdrs,const char * name,const char * value)195 soup_message_headers_append (SoupMessageHeaders *hdrs,
196 const char *name, const char *value)
197 {
198 SoupHeader header;
199 SoupHeaderSetter setter;
200
201 g_return_if_fail (name != NULL);
202 g_return_if_fail (value != NULL);
203
204 /* Setting a syntactically invalid header name or value is
205 * considered to be a programming error. However, it can also
206 * be a security hole, so we want to fail here even if
207 * compiled with G_DISABLE_CHECKS.
208 */
209 #ifndef G_DISABLE_CHECKS
210 g_return_if_fail (*name && strpbrk (name, " \t\r\n:") == NULL);
211 g_return_if_fail (strpbrk (value, "\r\n") == NULL);
212 #else
213 if (*name && strpbrk (name, " \t\r\n:")) {
214 g_warning ("soup_message_headers_append: Ignoring bad name '%s'", name);
215 return;
216 }
217 if (strpbrk (value, "\r\n")) {
218 g_warning ("soup_message_headers_append: Ignoring bad value '%s'", value);
219 return;
220 }
221 #endif
222
223 header.name = intern_header_name (name, &setter);
224 header.value = g_strdup (value);
225 g_array_append_val (hdrs->array, header);
226 if (hdrs->concat)
227 g_hash_table_remove (hdrs->concat, header.name);
228 if (setter)
229 setter (hdrs, header.value);
230 }
231
232 /**
233 * soup_message_headers_replace:
234 * @hdrs: a #SoupMessageHeaders
235 * @name: the header name to replace
236 * @value: the new value of @name
237 *
238 * Replaces the value of the header @name in @hdrs with @value. (See
239 * also soup_message_headers_append().)
240 *
241 * The caller is expected to make sure that @name and @value are
242 * syntactically correct.
243 **/
244 void
soup_message_headers_replace(SoupMessageHeaders * hdrs,const char * name,const char * value)245 soup_message_headers_replace (SoupMessageHeaders *hdrs,
246 const char *name, const char *value)
247 {
248 soup_message_headers_remove (hdrs, name);
249 soup_message_headers_append (hdrs, name, value);
250 }
251
252 static int
find_header(SoupHeader * hdr_array,const char * interned_name,int nth)253 find_header (SoupHeader *hdr_array, const char *interned_name, int nth)
254 {
255 int i;
256
257 for (i = 0; hdr_array[i].name; i++) {
258 if (hdr_array[i].name == interned_name) {
259 if (nth-- == 0)
260 return i;
261 }
262 }
263 return -1;
264 }
265
266 static int
find_last_header(SoupHeader * hdr_array,guint length,const char * interned_name,int nth)267 find_last_header (SoupHeader *hdr_array, guint length, const char *interned_name, int nth)
268 {
269 int i;
270
271 for (i = length; i >= 0; i--) {
272 if (hdr_array[i].name == interned_name) {
273 if (nth-- == 0)
274 return i;
275 }
276 }
277 return -1;
278 }
279
280 /**
281 * soup_message_headers_remove:
282 * @hdrs: a #SoupMessageHeaders
283 * @name: the header name to remove
284 *
285 * Removes @name from @hdrs. If there are multiple values for @name,
286 * they are all removed.
287 **/
288 void
soup_message_headers_remove(SoupMessageHeaders * hdrs,const char * name)289 soup_message_headers_remove (SoupMessageHeaders *hdrs, const char *name)
290 {
291 SoupHeader *hdr_array = (SoupHeader *)(hdrs->array->data);
292 SoupHeaderSetter setter;
293 int index;
294
295 g_return_if_fail (name != NULL);
296
297 name = intern_header_name (name, &setter);
298 while ((index = find_header (hdr_array, name, 0)) != -1) {
299 g_free (hdr_array[index].value);
300 g_array_remove_index (hdrs->array, index);
301 }
302 if (hdrs->concat)
303 g_hash_table_remove (hdrs->concat, name);
304 if (setter)
305 setter (hdrs, NULL);
306 }
307
308 /**
309 * soup_message_headers_get_one:
310 * @hdrs: a #SoupMessageHeaders
311 * @name: (in): header name
312 *
313 * Gets the value of header @name in @hdrs. Use this for headers whose
314 * values are <emphasis>not</emphasis> comma-delimited lists, and
315 * which therefore can only appear at most once in the headers. For
316 * list-valued headers, use soup_message_headers_get_list().
317 *
318 * If @hdrs does erroneously contain multiple copies of the header, it
319 * is not defined which one will be returned. (Ideally, it will return
320 * whichever one makes libsoup most compatible with other HTTP
321 * implementations.)
322 *
323 * Return value: (nullable) (transfer none): the header's value or %NULL if not found.
324 *
325 * Since: 2.28
326 **/
327 const char *
soup_message_headers_get_one(SoupMessageHeaders * hdrs,const char * name)328 soup_message_headers_get_one (SoupMessageHeaders *hdrs, const char *name)
329 {
330 SoupHeader *hdr_array = (SoupHeader *)(hdrs->array->data);
331 guint hdr_length = hdrs->array->len;
332 int index;
333
334 g_return_val_if_fail (name != NULL, NULL);
335
336 name = intern_header_name (name, NULL);
337
338 index = find_last_header (hdr_array, hdr_length, name, 0);
339
340 return (index == -1) ? NULL : hdr_array[index].value;
341 }
342
343 /**
344 * soup_message_headers_header_contains:
345 * @hdrs: a #SoupMessageHeaders
346 * @name: header name
347 * @token: token to look for
348 *
349 * Checks whether the list-valued header @name is present in @hdrs,
350 * and contains a case-insensitive match for @token.
351 *
352 * (If @name is present in @hdrs, then this is equivalent to calling
353 * soup_header_contains() on its value.)
354 *
355 * Return value: %TRUE if the header is present and contains @token,
356 * %FALSE otherwise.
357 *
358 * Since: 2.50
359 **/
360 gboolean
soup_message_headers_header_contains(SoupMessageHeaders * hdrs,const char * name,const char * token)361 soup_message_headers_header_contains (SoupMessageHeaders *hdrs, const char *name, const char *token)
362 {
363 const char *value;
364
365 value = soup_message_headers_get_list (hdrs, name);
366 if (!value)
367 return FALSE;
368 return soup_header_contains (value, token);
369 }
370
371 /**
372 * soup_message_headers_header_equals:
373 * @hdrs: a #SoupMessageHeaders
374 * @name: header name
375 * @value: expected value
376 *
377 * Checks whether the header @name is present in @hdrs and is
378 * (case-insensitively) equal to @value.
379 *
380 * Return value: %TRUE if the header is present and its value is
381 * @value, %FALSE otherwise.
382 *
383 * Since: 2.50
384 **/
385 gboolean
soup_message_headers_header_equals(SoupMessageHeaders * hdrs,const char * name,const char * value)386 soup_message_headers_header_equals (SoupMessageHeaders *hdrs, const char *name, const char *value)
387 {
388 const char *internal_value;
389
390 internal_value = soup_message_headers_get_list (hdrs, name);
391 if (!internal_value)
392 return FALSE;
393 return !g_ascii_strcasecmp (internal_value, value);
394 }
395
396 /**
397 * soup_message_headers_get_list:
398 * @hdrs: a #SoupMessageHeaders
399 * @name: header name
400 *
401 * Gets the value of header @name in @hdrs. Use this for headers whose
402 * values are comma-delimited lists, and which are therefore allowed
403 * to appear multiple times in the headers. For non-list-valued
404 * headers, use soup_message_headers_get_one().
405 *
406 * If @name appears multiple times in @hdrs,
407 * soup_message_headers_get_list() will concatenate all of the values
408 * together, separated by commas. This is sometimes awkward to parse
409 * (eg, WWW-Authenticate, Set-Cookie), but you have to be able to deal
410 * with it anyway, because the HTTP spec explicitly states that this
411 * transformation is allowed, and so an upstream proxy could do the
412 * same thing.
413 *
414 * Return value: (nullable) (transfer none): the header's value or %NULL if not found.
415 *
416 * Since: 2.28
417 **/
418 const char *
soup_message_headers_get_list(SoupMessageHeaders * hdrs,const char * name)419 soup_message_headers_get_list (SoupMessageHeaders *hdrs, const char *name)
420 {
421 SoupHeader *hdr_array = (SoupHeader *)(hdrs->array->data);
422 GString *concat;
423 char *value;
424 int index, i;
425
426 g_return_val_if_fail (name != NULL, NULL);
427
428 name = intern_header_name (name, NULL);
429 if (hdrs->concat) {
430 value = g_hash_table_lookup (hdrs->concat, name);
431 if (value)
432 return value;
433 }
434
435 index = find_header (hdr_array, name, 0);
436 if (index == -1)
437 return NULL;
438 else if (find_header (hdr_array, name, 1) == -1)
439 return hdr_array[index].value;
440
441 concat = g_string_new (NULL);
442 for (i = 0; (index = find_header (hdr_array, name, i)) != -1; i++) {
443 if (i != 0)
444 g_string_append (concat, ", ");
445 g_string_append (concat, hdr_array[index].value);
446 }
447 value = g_string_free (concat, FALSE);
448
449 if (!hdrs->concat)
450 hdrs->concat = g_hash_table_new_full (NULL, NULL, NULL, g_free);
451 g_hash_table_insert (hdrs->concat, (gpointer)name, value);
452 return value;
453 }
454
455 /**
456 * soup_message_headers_get:
457 * @hdrs: a #SoupMessageHeaders
458 * @name: header name
459 *
460 * Gets the value of header @name in @hdrs.
461 *
462 * This method was supposed to work correctly for both single-valued
463 * and list-valued headers, but because some HTTP clients/servers
464 * mistakenly send multiple copies of headers that are supposed to be
465 * single-valued, it sometimes returns incorrect results. To fix this,
466 * the methods soup_message_headers_get_one() and
467 * soup_message_headers_get_list() were introduced, so callers can
468 * explicitly state which behavior they are expecting.
469 *
470 * Return value: (nullable): as with soup_message_headers_get_list().
471 *
472 * Deprecated: Use soup_message_headers_get_one() or
473 * soup_message_headers_get_list() instead.
474 **/
475 const char *
soup_message_headers_get(SoupMessageHeaders * hdrs,const char * name)476 soup_message_headers_get (SoupMessageHeaders *hdrs, const char *name)
477 {
478 return soup_message_headers_get_list (hdrs, name);
479 }
480
481 /**
482 * SoupMessageHeadersIter:
483 *
484 * An opaque type used to iterate over a %SoupMessageHeaders
485 * structure.
486 *
487 * After intializing the iterator with
488 * soup_message_headers_iter_init(), call
489 * soup_message_headers_iter_next() to fetch data from it.
490 *
491 * You may not modify the headers while iterating over them.
492 **/
493
494 typedef struct {
495 SoupMessageHeaders *hdrs;
496 int index;
497 } SoupMessageHeadersIterReal;
498
499 /**
500 * soup_message_headers_iter_init:
501 * @iter: (out) (transfer none): a pointer to a %SoupMessageHeadersIter
502 * structure
503 * @hdrs: a %SoupMessageHeaders
504 *
505 * Initializes @iter for iterating @hdrs.
506 **/
507 void
soup_message_headers_iter_init(SoupMessageHeadersIter * iter,SoupMessageHeaders * hdrs)508 soup_message_headers_iter_init (SoupMessageHeadersIter *iter,
509 SoupMessageHeaders *hdrs)
510 {
511 SoupMessageHeadersIterReal *real = (SoupMessageHeadersIterReal *)iter;
512
513 real->hdrs = hdrs;
514 real->index = 0;
515 }
516
517 /**
518 * soup_message_headers_iter_next:
519 * @iter: (inout) (transfer none): a %SoupMessageHeadersIter
520 * @name: (out) (transfer none): pointer to a variable to return
521 * the header name in
522 * @value: (out) (transfer none): pointer to a variable to return
523 * the header value in
524 *
525 * Yields the next name/value pair in the %SoupMessageHeaders being
526 * iterated by @iter. If @iter has already yielded the last header,
527 * then soup_message_headers_iter_next() will return %FALSE and @name
528 * and @value will be unchanged.
529 *
530 * Return value: %TRUE if another name and value were returned, %FALSE
531 * if the end of the headers has been reached.
532 **/
533 gboolean
soup_message_headers_iter_next(SoupMessageHeadersIter * iter,const char ** name,const char ** value)534 soup_message_headers_iter_next (SoupMessageHeadersIter *iter,
535 const char **name, const char **value)
536 {
537 SoupMessageHeadersIterReal *real = (SoupMessageHeadersIterReal *)iter;
538 SoupHeader *hdr_array = (SoupHeader *)real->hdrs->array->data;
539
540 if (real->index >= real->hdrs->array->len)
541 return FALSE;
542
543 *name = hdr_array[real->index].name;
544 *value = hdr_array[real->index].value;
545 real->index++;
546 return TRUE;
547 }
548
549 /**
550 * SoupMessageHeadersForeachFunc:
551 * @name: the header name
552 * @value: the header value
553 * @user_data: the data passed to soup_message_headers_foreach()
554 *
555 * The callback passed to soup_message_headers_foreach().
556 **/
557
558 /**
559 * soup_message_headers_foreach:
560 * @hdrs: a #SoupMessageHeaders
561 * @func: (scope call): callback function to run for each header
562 * @user_data: data to pass to @func
563 *
564 * Calls @func once for each header value in @hdrs.
565 *
566 * Beware that unlike soup_message_headers_get(), this processes the
567 * headers in exactly the way they were added, rather than
568 * concatenating multiple same-named headers into a single value.
569 * (This is intentional; it ensures that if you call
570 * soup_message_headers_append() multiple times with the same name,
571 * then the I/O code will output multiple copies of the header when
572 * sending the message to the remote implementation, which may be
573 * required for interoperability in some cases.)
574 *
575 * You may not modify the headers from @func.
576 **/
577 void
soup_message_headers_foreach(SoupMessageHeaders * hdrs,SoupMessageHeadersForeachFunc func,gpointer user_data)578 soup_message_headers_foreach (SoupMessageHeaders *hdrs,
579 SoupMessageHeadersForeachFunc func,
580 gpointer user_data)
581 {
582 SoupHeader *hdr_array = (SoupHeader *)hdrs->array->data;
583 guint i;
584
585 for (i = 0; i < hdrs->array->len; i++)
586 func (hdr_array[i].name, hdr_array[i].value, user_data);
587 }
588
589
590 G_LOCK_DEFINE_STATIC (header_pool);
591 static GHashTable *header_pool, *header_setters;
592
593 static void transfer_encoding_setter (SoupMessageHeaders *, const char *);
594 static void content_length_setter (SoupMessageHeaders *, const char *);
595 static void expectation_setter (SoupMessageHeaders *, const char *);
596 static void content_type_setter (SoupMessageHeaders *, const char *);
597
598 static char *
intern_header_locked(const char * name)599 intern_header_locked (const char *name)
600 {
601 char *interned;
602
603 interned = g_hash_table_lookup (header_pool, name);
604 if (!interned) {
605 char *dup = g_strdup (name);
606 g_hash_table_insert (header_pool, dup, dup);
607 interned = dup;
608 }
609 return interned;
610 }
611
612 static const char *
intern_header_name(const char * name,SoupHeaderSetter * setter)613 intern_header_name (const char *name, SoupHeaderSetter *setter)
614 {
615 const char *interned;
616
617 G_LOCK (header_pool);
618
619 if (!header_pool) {
620 header_pool = g_hash_table_new (soup_str_case_hash, soup_str_case_equal);
621 header_setters = g_hash_table_new (NULL, NULL);
622 g_hash_table_insert (header_setters,
623 intern_header_locked ("Transfer-Encoding"),
624 transfer_encoding_setter);
625 g_hash_table_insert (header_setters,
626 intern_header_locked ("Content-Length"),
627 content_length_setter);
628 g_hash_table_insert (header_setters,
629 intern_header_locked ("Expect"),
630 expectation_setter);
631 g_hash_table_insert (header_setters,
632 intern_header_locked ("Content-Type"),
633 content_type_setter);
634 }
635
636 interned = intern_header_locked (name);
637 if (setter)
638 *setter = g_hash_table_lookup (header_setters, interned);
639
640 G_UNLOCK (header_pool);
641 return interned;
642 }
643
644 static void
clear_special_headers(SoupMessageHeaders * hdrs)645 clear_special_headers (SoupMessageHeaders *hdrs)
646 {
647 SoupHeaderSetter setter;
648 GHashTableIter iter;
649 gpointer key, value;
650
651 /* Make sure header_setters has been initialized */
652 intern_header_name ("", NULL);
653
654 g_hash_table_iter_init (&iter, header_setters);
655 while (g_hash_table_iter_next (&iter, &key, &value)) {
656 setter = value;
657 setter (hdrs, NULL);
658 }
659 }
660
661 /* Specific headers */
662
663 static void
transfer_encoding_setter(SoupMessageHeaders * hdrs,const char * value)664 transfer_encoding_setter (SoupMessageHeaders *hdrs, const char *value)
665 {
666 if (value) {
667 /* "identity" is a wrong value according to RFC errata 408,
668 * and RFC 7230 does not list it as valid transfer-coding.
669 * Nevertheless, the obsolete RFC 2616 stated "identity"
670 * as valid, so we can't handle it as unrecognized here
671 * for compatibility reasons.
672 */
673 if (g_ascii_strcasecmp (value, "chunked") == 0)
674 hdrs->encoding = SOUP_ENCODING_CHUNKED;
675 else if (g_ascii_strcasecmp (value, "identity") != 0)
676 hdrs->encoding = SOUP_ENCODING_UNRECOGNIZED;
677 } else
678 hdrs->encoding = -1;
679 }
680
681 static void
content_length_setter(SoupMessageHeaders * hdrs,const char * value)682 content_length_setter (SoupMessageHeaders *hdrs, const char *value)
683 {
684 /* Transfer-Encoding trumps Content-Length */
685 if (hdrs->encoding == SOUP_ENCODING_CHUNKED)
686 return;
687
688 if (value) {
689 char *end;
690
691 hdrs->content_length = g_ascii_strtoull (value, &end, 10);
692 if (*end)
693 hdrs->encoding = SOUP_ENCODING_UNRECOGNIZED;
694 else
695 hdrs->encoding = SOUP_ENCODING_CONTENT_LENGTH;
696 } else
697 hdrs->encoding = -1;
698 }
699
700 /**
701 * SoupEncoding:
702 * @SOUP_ENCODING_UNRECOGNIZED: unknown / error
703 * @SOUP_ENCODING_NONE: no body is present (which is not the same as a
704 * 0-length body, and only occurs in certain places)
705 * @SOUP_ENCODING_CONTENT_LENGTH: Content-Length encoding
706 * @SOUP_ENCODING_EOF: Response body ends when the connection is closed
707 * @SOUP_ENCODING_CHUNKED: chunked encoding (currently only supported
708 * for response)
709 * @SOUP_ENCODING_BYTERANGES: multipart/byteranges (Reserved for future
710 * use: NOT CURRENTLY IMPLEMENTED)
711 *
712 * How a message body is encoded for transport
713 **/
714
715 /**
716 * soup_message_headers_get_encoding:
717 * @hdrs: a #SoupMessageHeaders
718 *
719 * Gets the message body encoding that @hdrs declare. This may not
720 * always correspond to the encoding used on the wire; eg, a HEAD
721 * response may declare a Content-Length or Transfer-Encoding, but
722 * it will never actually include a body.
723 *
724 * Return value: the encoding declared by @hdrs.
725 **/
726 SoupEncoding
soup_message_headers_get_encoding(SoupMessageHeaders * hdrs)727 soup_message_headers_get_encoding (SoupMessageHeaders *hdrs)
728 {
729 const char *header;
730
731 if (hdrs->encoding != -1)
732 return hdrs->encoding;
733
734 /* If Transfer-Encoding was set, hdrs->encoding would already
735 * be set. So we don't need to check that possibility.
736 */
737 header = soup_message_headers_get_one (hdrs, "Content-Length");
738 if (header) {
739 content_length_setter (hdrs, header);
740 if (hdrs->encoding != -1)
741 return hdrs->encoding;
742 }
743
744 /* Per RFC 2616 4.4, a response body that doesn't indicate its
745 * encoding otherwise is terminated by connection close, and a
746 * request that doesn't indicate otherwise has no body. Note
747 * that SoupMessage calls soup_message_headers_set_encoding()
748 * to override the response body default for our own
749 * server-side messages.
750 */
751 hdrs->encoding = (hdrs->type == SOUP_MESSAGE_HEADERS_RESPONSE) ?
752 SOUP_ENCODING_EOF : SOUP_ENCODING_NONE;
753 return hdrs->encoding;
754 }
755
756 /**
757 * soup_message_headers_set_encoding:
758 * @hdrs: a #SoupMessageHeaders
759 * @encoding: a #SoupEncoding
760 *
761 * Sets the message body encoding that @hdrs will declare. In particular,
762 * you should use this if you are going to send a request or response in
763 * chunked encoding.
764 **/
765 void
soup_message_headers_set_encoding(SoupMessageHeaders * hdrs,SoupEncoding encoding)766 soup_message_headers_set_encoding (SoupMessageHeaders *hdrs,
767 SoupEncoding encoding)
768 {
769 if (encoding == hdrs->encoding)
770 return;
771
772 switch (encoding) {
773 case SOUP_ENCODING_NONE:
774 case SOUP_ENCODING_EOF:
775 soup_message_headers_remove (hdrs, "Transfer-Encoding");
776 soup_message_headers_remove (hdrs, "Content-Length");
777 break;
778
779 case SOUP_ENCODING_CONTENT_LENGTH:
780 soup_message_headers_remove (hdrs, "Transfer-Encoding");
781 break;
782
783 case SOUP_ENCODING_CHUNKED:
784 soup_message_headers_remove (hdrs, "Content-Length");
785 soup_message_headers_replace (hdrs, "Transfer-Encoding", "chunked");
786 break;
787
788 default:
789 g_return_if_reached ();
790 }
791
792 hdrs->encoding = encoding;
793 }
794
795 /**
796 * soup_message_headers_get_content_length:
797 * @hdrs: a #SoupMessageHeaders
798 *
799 * Gets the message body length that @hdrs declare. This will only
800 * be non-0 if soup_message_headers_get_encoding() returns
801 * %SOUP_ENCODING_CONTENT_LENGTH.
802 *
803 * Return value: the message body length declared by @hdrs.
804 **/
805 goffset
soup_message_headers_get_content_length(SoupMessageHeaders * hdrs)806 soup_message_headers_get_content_length (SoupMessageHeaders *hdrs)
807 {
808 SoupEncoding encoding;
809
810 encoding = soup_message_headers_get_encoding (hdrs);
811 if (encoding == SOUP_ENCODING_CONTENT_LENGTH)
812 return hdrs->content_length;
813 else
814 return 0;
815 }
816
817 /**
818 * soup_message_headers_set_content_length:
819 * @hdrs: a #SoupMessageHeaders
820 * @content_length: the message body length
821 *
822 * Sets the message body length that @hdrs will declare, and sets
823 * @hdrs's encoding to %SOUP_ENCODING_CONTENT_LENGTH.
824 *
825 * You do not normally need to call this; if @hdrs is set to use
826 * Content-Length encoding, libsoup will automatically set its
827 * Content-Length header for you immediately before sending the
828 * headers. One situation in which this method is useful is when
829 * generating the response to a HEAD request; Calling
830 * soup_message_headers_set_content_length() allows you to put the
831 * correct content length into the response without needing to waste
832 * memory by filling in a response body which won't actually be sent.
833 **/
834 void
soup_message_headers_set_content_length(SoupMessageHeaders * hdrs,goffset content_length)835 soup_message_headers_set_content_length (SoupMessageHeaders *hdrs,
836 goffset content_length)
837 {
838 char length[128];
839
840 g_snprintf (length, sizeof (length), "%" G_GUINT64_FORMAT,
841 content_length);
842 soup_message_headers_remove (hdrs, "Transfer-Encoding");
843 soup_message_headers_replace (hdrs, "Content-Length", length);
844 }
845
846 static void
expectation_setter(SoupMessageHeaders * hdrs,const char * value)847 expectation_setter (SoupMessageHeaders *hdrs, const char *value)
848 {
849 if (value) {
850 if (!g_ascii_strcasecmp (value, "100-continue"))
851 hdrs->expectations = SOUP_EXPECTATION_CONTINUE;
852 else
853 hdrs->expectations = SOUP_EXPECTATION_UNRECOGNIZED;
854 } else
855 hdrs->expectations = 0;
856 }
857
858 /**
859 * SoupExpectation:
860 * @SOUP_EXPECTATION_CONTINUE: "100-continue"
861 * @SOUP_EXPECTATION_UNRECOGNIZED: any unrecognized expectation
862 *
863 * Represents the parsed value of the "Expect" header.
864 **/
865
866 /**
867 * soup_message_headers_get_expectations:
868 * @hdrs: a #SoupMessageHeaders
869 *
870 * Gets the expectations declared by @hdrs's "Expect" header.
871 * Currently this will either be %SOUP_EXPECTATION_CONTINUE or
872 * %SOUP_EXPECTATION_UNRECOGNIZED.
873 *
874 * Return value: the contents of @hdrs's "Expect" header
875 **/
876 SoupExpectation
soup_message_headers_get_expectations(SoupMessageHeaders * hdrs)877 soup_message_headers_get_expectations (SoupMessageHeaders *hdrs)
878 {
879 return hdrs->expectations;
880 }
881
882 /**
883 * soup_message_headers_set_expectations:
884 * @hdrs: a #SoupMessageHeaders
885 * @expectations: the expectations to set
886 *
887 * Sets @hdrs's "Expect" header according to @expectations.
888 *
889 * Currently %SOUP_EXPECTATION_CONTINUE is the only known expectation
890 * value. You should set this value on a request if you are sending a
891 * large message body (eg, via POST or PUT), and want to give the
892 * server a chance to reject the request after seeing just the headers
893 * (eg, because it will require authentication before allowing you to
894 * post, or because you're POSTing to a URL that doesn't exist). This
895 * saves you from having to transmit the large request body when the
896 * server is just going to ignore it anyway.
897 **/
898 void
soup_message_headers_set_expectations(SoupMessageHeaders * hdrs,SoupExpectation expectations)899 soup_message_headers_set_expectations (SoupMessageHeaders *hdrs,
900 SoupExpectation expectations)
901 {
902 g_return_if_fail ((expectations & ~SOUP_EXPECTATION_CONTINUE) == 0);
903
904 if (expectations & SOUP_EXPECTATION_CONTINUE)
905 soup_message_headers_replace (hdrs, "Expect", "100-continue");
906 else
907 soup_message_headers_remove (hdrs, "Expect");
908 }
909
910 /**
911 * SoupRange:
912 * @start: the start of the range
913 * @end: the end of the range
914 *
915 * Represents a byte range as used in the Range header.
916 *
917 * If @end is non-negative, then @start and @end represent the bounds
918 * of of the range, counting from 0. (Eg, the first 500 bytes would be
919 * represented as @start = 0 and @end = 499.)
920 *
921 * If @end is -1 and @start is non-negative, then this represents a
922 * range starting at @start and ending with the last byte of the
923 * requested resource body. (Eg, all but the first 500 bytes would be
924 * @start = 500, and @end = -1.)
925 *
926 * If @end is -1 and @start is negative, then it represents a "suffix
927 * range", referring to the last -@start bytes of the resource body.
928 * (Eg, the last 500 bytes would be @start = -500 and @end = -1.)
929 *
930 * Since: 2.26
931 **/
932
933 static int
sort_ranges(gconstpointer a,gconstpointer b)934 sort_ranges (gconstpointer a, gconstpointer b)
935 {
936 SoupRange *ra = (SoupRange *)a;
937 SoupRange *rb = (SoupRange *)b;
938
939 return ra->start - rb->start;
940 }
941
942 /* like soup_message_headers_get_ranges(), except it returns:
943 * SOUP_STATUS_OK if there is no Range or it should be ignored.
944 * SOUP_STATUS_PARTIAL_CONTENT if there is at least one satisfiable range.
945 * SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE if @check_satisfiable
946 * is %TRUE and the request is not satisfiable given @total_length.
947 */
948 guint
soup_message_headers_get_ranges_internal(SoupMessageHeaders * hdrs,goffset total_length,gboolean check_satisfiable,SoupRange ** ranges,int * length)949 soup_message_headers_get_ranges_internal (SoupMessageHeaders *hdrs,
950 goffset total_length,
951 gboolean check_satisfiable,
952 SoupRange **ranges,
953 int *length)
954 {
955 const char *range = soup_message_headers_get_one (hdrs, "Range");
956 GSList *range_list, *r;
957 GArray *array;
958 char *spec, *end;
959 guint status = SOUP_STATUS_OK;
960
961 if (!range || strncmp (range, "bytes", 5) != 0)
962 return status;
963
964 range += 5;
965 while (g_ascii_isspace (*range))
966 range++;
967 if (*range++ != '=')
968 return status;
969 while (g_ascii_isspace (*range))
970 range++;
971
972 range_list = soup_header_parse_list (range);
973 if (!range_list)
974 return status;
975
976 array = g_array_new (FALSE, FALSE, sizeof (SoupRange));
977 for (r = range_list; r; r = r->next) {
978 SoupRange cur;
979
980 spec = r->data;
981 if (*spec == '-') {
982 cur.start = g_ascii_strtoll (spec, &end, 10) + total_length;
983 cur.end = total_length - 1;
984 } else {
985 cur.start = g_ascii_strtoull (spec, &end, 10);
986 if (*end == '-')
987 end++;
988 if (*end) {
989 cur.end = g_ascii_strtoull (end, &end, 10);
990 if (cur.end < cur.start) {
991 status = SOUP_STATUS_OK;
992 break;
993 }
994 } else
995 cur.end = total_length - 1;
996 }
997 if (*end) {
998 status = SOUP_STATUS_OK;
999 break;
1000 } else if (check_satisfiable && cur.start >= total_length) {
1001 if (status == SOUP_STATUS_OK)
1002 status = SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE;
1003 continue;
1004 }
1005
1006 g_array_append_val (array, cur);
1007 status = SOUP_STATUS_PARTIAL_CONTENT;
1008 }
1009 soup_header_free_list (range_list);
1010
1011 if (status != SOUP_STATUS_PARTIAL_CONTENT) {
1012 g_array_free (array, TRUE);
1013 return status;
1014 }
1015
1016 if (total_length) {
1017 guint i;
1018
1019 g_array_sort (array, sort_ranges);
1020 for (i = 1; i < array->len; i++) {
1021 SoupRange *cur = &((SoupRange *)array->data)[i];
1022 SoupRange *prev = &((SoupRange *)array->data)[i - 1];
1023
1024 if (cur->start <= prev->end) {
1025 prev->end = MAX (prev->end, cur->end);
1026 g_array_remove_index (array, i);
1027 }
1028 }
1029 }
1030
1031 *ranges = (SoupRange *)array->data;
1032 *length = array->len;
1033
1034 g_array_free (array, FALSE);
1035 return SOUP_STATUS_PARTIAL_CONTENT;
1036 }
1037
1038 /**
1039 * soup_message_headers_get_ranges:
1040 * @hdrs: a #SoupMessageHeaders
1041 * @total_length: the total_length of the response body
1042 * @ranges: (out) (array length=length): return location for an array
1043 * of #SoupRange
1044 * @length: the length of the returned array
1045 *
1046 * Parses @hdrs's Range header and returns an array of the requested
1047 * byte ranges. The returned array must be freed with
1048 * soup_message_headers_free_ranges().
1049 *
1050 * If @total_length is non-0, its value will be used to adjust the
1051 * returned ranges to have explicit start and end values, and the
1052 * returned ranges will be sorted and non-overlapping. If
1053 * @total_length is 0, then some ranges may have an end value of -1,
1054 * as described under #SoupRange, and some of the ranges may be
1055 * redundant.
1056 *
1057 * Beware that even if given a @total_length, this function does not
1058 * check that the ranges are satisfiable.
1059 *
1060 * <note><para>
1061 * #SoupServer has built-in handling for range requests. If your
1062 * server handler returns a %SOUP_STATUS_OK response containing the
1063 * complete response body (rather than pausing the message and
1064 * returning some of the response body later), and there is a Range
1065 * header in the request, then libsoup will automatically convert the
1066 * response to a %SOUP_STATUS_PARTIAL_CONTENT response containing only
1067 * the range(s) requested by the client.
1068 *
1069 * The only time you need to process the Range header yourself is if
1070 * either you need to stream the response body rather than returning
1071 * it all at once, or you do not already have the complete response
1072 * body available, and only want to generate the parts that were
1073 * actually requested by the client.
1074 * </para></note>
1075 *
1076 * Return value: %TRUE if @hdrs contained a syntactically-valid
1077 * "Range" header, %FALSE otherwise (in which case @range and @length
1078 * will not be set).
1079 *
1080 * Since: 2.26
1081 **/
1082 gboolean
soup_message_headers_get_ranges(SoupMessageHeaders * hdrs,goffset total_length,SoupRange ** ranges,int * length)1083 soup_message_headers_get_ranges (SoupMessageHeaders *hdrs,
1084 goffset total_length,
1085 SoupRange **ranges,
1086 int *length)
1087 {
1088 guint status;
1089
1090 status = soup_message_headers_get_ranges_internal (hdrs, total_length, FALSE, ranges, length);
1091 return status == SOUP_STATUS_PARTIAL_CONTENT;
1092 }
1093
1094 /**
1095 * soup_message_headers_free_ranges:
1096 * @hdrs: a #SoupMessageHeaders
1097 * @ranges: an array of #SoupRange
1098 *
1099 * Frees the array of ranges returned from soup_message_headers_get_ranges().
1100 *
1101 * Since: 2.26
1102 **/
1103 void
soup_message_headers_free_ranges(SoupMessageHeaders * hdrs,SoupRange * ranges)1104 soup_message_headers_free_ranges (SoupMessageHeaders *hdrs,
1105 SoupRange *ranges)
1106 {
1107 g_free (ranges);
1108 }
1109
1110 /**
1111 * soup_message_headers_set_ranges:
1112 * @hdrs: a #SoupMessageHeaders
1113 * @ranges: an array of #SoupRange
1114 * @length: the length of @range
1115 *
1116 * Sets @hdrs's Range header to request the indicated ranges. (If you
1117 * only want to request a single range, you can use
1118 * soup_message_headers_set_range().)
1119 *
1120 * Since: 2.26
1121 **/
1122 void
soup_message_headers_set_ranges(SoupMessageHeaders * hdrs,SoupRange * ranges,int length)1123 soup_message_headers_set_ranges (SoupMessageHeaders *hdrs,
1124 SoupRange *ranges,
1125 int length)
1126 {
1127 GString *header;
1128 int i;
1129
1130 header = g_string_new ("bytes=");
1131 for (i = 0; i < length; i++) {
1132 if (i > 0)
1133 g_string_append_c (header, ',');
1134 if (ranges[i].end >= 0) {
1135 g_string_append_printf (header, "%" G_GINT64_FORMAT "-%" G_GINT64_FORMAT,
1136 ranges[i].start, ranges[i].end);
1137 } else if (ranges[i].start >= 0) {
1138 g_string_append_printf (header,"%" G_GINT64_FORMAT "-",
1139 ranges[i].start);
1140 } else {
1141 g_string_append_printf (header, "%" G_GINT64_FORMAT,
1142 ranges[i].start);
1143 }
1144 }
1145
1146 soup_message_headers_replace (hdrs, "Range", header->str);
1147 g_string_free (header, TRUE);
1148 }
1149
1150 /**
1151 * soup_message_headers_set_range:
1152 * @hdrs: a #SoupMessageHeaders
1153 * @start: the start of the range to request
1154 * @end: the end of the range to request
1155 *
1156 * Sets @hdrs's Range header to request the indicated range.
1157 * @start and @end are interpreted as in a #SoupRange.
1158 *
1159 * If you need to request multiple ranges, use
1160 * soup_message_headers_set_ranges().
1161 *
1162 * Since: 2.26
1163 **/
1164 void
soup_message_headers_set_range(SoupMessageHeaders * hdrs,goffset start,goffset end)1165 soup_message_headers_set_range (SoupMessageHeaders *hdrs,
1166 goffset start,
1167 goffset end)
1168 {
1169 SoupRange range;
1170
1171 range.start = start;
1172 range.end = end;
1173 soup_message_headers_set_ranges (hdrs, &range, 1);
1174 }
1175
1176 /**
1177 * soup_message_headers_get_content_range:
1178 * @hdrs: a #SoupMessageHeaders
1179 * @start: (out): return value for the start of the range
1180 * @end: (out): return value for the end of the range
1181 * @total_length: (out) (optional): return value for the total length of the
1182 * resource, or %NULL if you don't care.
1183 *
1184 * Parses @hdrs's Content-Range header and returns it in @start,
1185 * @end, and @total_length. If the total length field in the header
1186 * was specified as "*", then @total_length will be set to -1.
1187 *
1188 * Return value: %TRUE if @hdrs contained a "Content-Range" header
1189 * containing a byte range which could be parsed, %FALSE otherwise.
1190 *
1191 * Since: 2.26
1192 **/
1193 gboolean
soup_message_headers_get_content_range(SoupMessageHeaders * hdrs,goffset * start,goffset * end,goffset * total_length)1194 soup_message_headers_get_content_range (SoupMessageHeaders *hdrs,
1195 goffset *start,
1196 goffset *end,
1197 goffset *total_length)
1198 {
1199 const char *header = soup_message_headers_get_one (hdrs, "Content-Range");
1200 goffset length;
1201 char *p;
1202
1203 if (!header || strncmp (header, "bytes ", 6) != 0)
1204 return FALSE;
1205
1206 header += 6;
1207 while (g_ascii_isspace (*header))
1208 header++;
1209 if (!g_ascii_isdigit (*header))
1210 return FALSE;
1211
1212 *start = g_ascii_strtoull (header, &p, 10);
1213 if (*p != '-')
1214 return FALSE;
1215 *end = g_ascii_strtoull (p + 1, &p, 10);
1216 if (*p != '/')
1217 return FALSE;
1218 p++;
1219 if (*p == '*') {
1220 length = -1;
1221 p++;
1222 } else
1223 length = g_ascii_strtoull (p, &p, 10);
1224
1225 if (total_length)
1226 *total_length = length;
1227 return *p == '\0';
1228 }
1229
1230 /**
1231 * soup_message_headers_set_content_range:
1232 * @hdrs: a #SoupMessageHeaders
1233 * @start: the start of the range
1234 * @end: the end of the range
1235 * @total_length: the total length of the resource, or -1 if unknown
1236 *
1237 * Sets @hdrs's Content-Range header according to the given values.
1238 * (Note that @total_length is the total length of the entire resource
1239 * that this is a range of, not simply @end - @start + 1.)
1240 *
1241 * <note><para>
1242 * #SoupServer has built-in handling for range requests, and you do
1243 * not normally need to call this function youself. See
1244 * soup_message_headers_get_ranges() for more details.
1245 * </para></note>
1246 *
1247 * Since: 2.26
1248 **/
1249 void
soup_message_headers_set_content_range(SoupMessageHeaders * hdrs,goffset start,goffset end,goffset total_length)1250 soup_message_headers_set_content_range (SoupMessageHeaders *hdrs,
1251 goffset start,
1252 goffset end,
1253 goffset total_length)
1254 {
1255 char *header;
1256
1257 if (total_length >= 0) {
1258 header = g_strdup_printf ("bytes %" G_GINT64_FORMAT "-%"
1259 G_GINT64_FORMAT "/%" G_GINT64_FORMAT,
1260 start, end, total_length);
1261 } else {
1262 header = g_strdup_printf ("bytes %" G_GINT64_FORMAT "-%"
1263 G_GINT64_FORMAT "/*", start, end);
1264 }
1265 soup_message_headers_replace (hdrs, "Content-Range", header);
1266 g_free (header);
1267 }
1268
1269 static gboolean
parse_content_foo(SoupMessageHeaders * hdrs,const char * header_name,char ** foo,GHashTable ** params)1270 parse_content_foo (SoupMessageHeaders *hdrs, const char *header_name,
1271 char **foo, GHashTable **params)
1272 {
1273 const char *header;
1274 char *semi;
1275
1276 header = soup_message_headers_get_one (hdrs, header_name);
1277 if (!header)
1278 return FALSE;
1279
1280 if (foo) {
1281 *foo = g_strdup (header);
1282 semi = strchr (*foo, ';');
1283 if (semi) {
1284 char *p = semi;
1285
1286 *semi++ = '\0';
1287 while (p - 1 > *foo && g_ascii_isspace(p[-1]))
1288 *(--p) = '\0';
1289 }
1290 } else {
1291 semi = strchr (header, ';');
1292 if (semi)
1293 semi++;
1294 }
1295
1296 if (!params)
1297 return TRUE;
1298
1299 if (!semi) {
1300 *params = soup_header_parse_semi_param_list ("");
1301 return TRUE;
1302 }
1303
1304 *params = soup_header_parse_semi_param_list (semi);
1305 return TRUE;
1306 }
1307
1308 static void
set_content_foo(SoupMessageHeaders * hdrs,const char * header_name,const char * foo,GHashTable * params)1309 set_content_foo (SoupMessageHeaders *hdrs, const char *header_name,
1310 const char *foo, GHashTable *params)
1311 {
1312 GString *str;
1313 GHashTableIter iter;
1314 gpointer key, value;
1315
1316 str = g_string_new (foo);
1317 if (params) {
1318 g_hash_table_iter_init (&iter, params);
1319 while (g_hash_table_iter_next (&iter, &key, &value)) {
1320 g_string_append (str, "; ");
1321 soup_header_g_string_append_param (str, key, value);
1322 }
1323 }
1324
1325 soup_message_headers_replace (hdrs, header_name, str->str);
1326 g_string_free (str, TRUE);
1327 }
1328
1329 static void
content_type_setter(SoupMessageHeaders * hdrs,const char * value)1330 content_type_setter (SoupMessageHeaders *hdrs, const char *value)
1331 {
1332 g_free (hdrs->content_type);
1333 hdrs->content_type = NULL;
1334
1335 if (value) {
1336 char *content_type = NULL, *p;
1337
1338 parse_content_foo (hdrs, "Content-Type", &content_type, NULL);
1339 g_return_if_fail (content_type != NULL);
1340
1341 p = strpbrk (content_type, " /");
1342 if (!p || *p != '/' || strpbrk (p + 1, " /"))
1343 g_free (content_type);
1344 else
1345 hdrs->content_type = content_type;
1346 }
1347 }
1348
1349 /**
1350 * soup_message_headers_get_content_type:
1351 * @hdrs: a #SoupMessageHeaders
1352 * @params: (out) (element-type utf8 utf8) (allow-none) (transfer full):
1353 * return location for the Content-Type parameters (eg, "charset"), or
1354 * %NULL
1355 *
1356 * Looks up the "Content-Type" header in @hdrs, parses it, and returns
1357 * its value in *@content_type and *@params. @params can be %NULL if you
1358 * are only interested in the content type itself.
1359 *
1360 * Return value: (nullable): a string with the value of the
1361 * "Content-Type" header or %NULL if @hdrs does not contain that
1362 * header or it cannot be parsed (in which case *@params will be
1363 * unchanged).
1364 *
1365 * Since: 2.26
1366 **/
1367 const char *
soup_message_headers_get_content_type(SoupMessageHeaders * hdrs,GHashTable ** params)1368 soup_message_headers_get_content_type (SoupMessageHeaders *hdrs,
1369 GHashTable **params)
1370 {
1371 if (!hdrs->content_type)
1372 return NULL;
1373
1374 if (params)
1375 parse_content_foo (hdrs, "Content-Type", NULL, params);
1376 return hdrs->content_type;
1377 }
1378
1379 /**
1380 * soup_message_headers_set_content_type:
1381 * @hdrs: a #SoupMessageHeaders
1382 * @content_type: the MIME type
1383 * @params: (allow-none) (element-type utf8 utf8): additional
1384 * parameters, or %NULL
1385 *
1386 * Sets the "Content-Type" header in @hdrs to @content_type,
1387 * optionally with additional parameters specified in @params.
1388 *
1389 * Since: 2.26
1390 **/
1391 void
soup_message_headers_set_content_type(SoupMessageHeaders * hdrs,const char * content_type,GHashTable * params)1392 soup_message_headers_set_content_type (SoupMessageHeaders *hdrs,
1393 const char *content_type,
1394 GHashTable *params)
1395 {
1396 set_content_foo (hdrs, "Content-Type", content_type, params);
1397 }
1398
1399 /**
1400 * soup_message_headers_get_content_disposition:
1401 * @hdrs: a #SoupMessageHeaders
1402 * @disposition: (out) (transfer full): return location for the
1403 * disposition-type, or %NULL
1404 * @params: (out) (transfer full) (element-type utf8 utf8): return
1405 * location for the Content-Disposition parameters, or %NULL
1406 *
1407 * Looks up the "Content-Disposition" header in @hdrs, parses it, and
1408 * returns its value in *@disposition and *@params. @params can be
1409 * %NULL if you are only interested in the disposition-type.
1410 *
1411 * In HTTP, the most common use of this header is to set a
1412 * disposition-type of "attachment", to suggest to the browser that a
1413 * response should be saved to disk rather than displayed in the
1414 * browser. If @params contains a "filename" parameter, this is a
1415 * suggestion of a filename to use. (If the parameter value in the
1416 * header contains an absolute or relative path, libsoup will truncate
1417 * it down to just the final path component, so you do not need to
1418 * test this yourself.)
1419 *
1420 * Content-Disposition is also used in "multipart/form-data", however
1421 * this is handled automatically by #SoupMultipart and the associated
1422 * form methods.
1423 *
1424 * Return value: %TRUE if @hdrs contains a "Content-Disposition"
1425 * header, %FALSE if not (in which case *@disposition and *@params
1426 * will be unchanged).
1427 *
1428 * Since: 2.26
1429 **/
1430 gboolean
soup_message_headers_get_content_disposition(SoupMessageHeaders * hdrs,char ** disposition,GHashTable ** params)1431 soup_message_headers_get_content_disposition (SoupMessageHeaders *hdrs,
1432 char **disposition,
1433 GHashTable **params)
1434 {
1435 gpointer orig_key, orig_value;
1436
1437 if (!parse_content_foo (hdrs, "Content-Disposition",
1438 disposition, params))
1439 return FALSE;
1440
1441 /* If there is a filename parameter, make sure it contains
1442 * only a single path component
1443 */
1444 if (params && g_hash_table_lookup_extended (*params, "filename",
1445 &orig_key, &orig_value)) {
1446 char *filename = strrchr (orig_value, '/');
1447
1448 if (filename)
1449 g_hash_table_insert (*params, g_strdup (orig_key), filename + 1);
1450 }
1451 return TRUE;
1452 }
1453
1454 /**
1455 * soup_message_headers_set_content_disposition:
1456 * @hdrs: a #SoupMessageHeaders
1457 * @disposition: the disposition-type
1458 * @params: (allow-none) (element-type utf8 utf8): additional
1459 * parameters, or %NULL
1460 *
1461 * Sets the "Content-Disposition" header in @hdrs to @disposition,
1462 * optionally with additional parameters specified in @params.
1463 *
1464 * See soup_message_headers_get_content_disposition() for a discussion
1465 * of how Content-Disposition is used in HTTP.
1466 *
1467 * Since: 2.26
1468 **/
1469 void
soup_message_headers_set_content_disposition(SoupMessageHeaders * hdrs,const char * disposition,GHashTable * params)1470 soup_message_headers_set_content_disposition (SoupMessageHeaders *hdrs,
1471 const char *disposition,
1472 GHashTable *params)
1473 {
1474 set_content_foo (hdrs, "Content-Disposition", disposition, params);
1475 }
1476
1477