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