1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * soup-message-headers.c: HTTP message header arrays
5 * Copyright (C) 2007, 2008 Red Hat, Inc.
10 #include "soup-message-headers.h"
12 #include "soup-misc-private.h"
15 * SECTION:soup-message-headers
16 * @short_description: HTTP message headers
17 * @see_also: #SoupMessage
19 * #SoupMessageHeaders represents the HTTP message headers associated
20 * with a request or response.
26 * The HTTP message headers associated with a request or response.
30 * SoupMessageHeadersType:
31 * @SOUP_MESSAGE_HEADERS_REQUEST: request headers
32 * @SOUP_MESSAGE_HEADERS_RESPONSE: response headers
33 * @SOUP_MESSAGE_HEADERS_MULTIPART: multipart body part headers
35 * Value passed to soup_message_headers_new() to set certain default
39 typedef void (*SoupHeaderSetter) (SoupMessageHeaders *, const char *);
40 static const char *intern_header_name (const char *name, SoupHeaderSetter *setter);
41 static void clear_special_headers (SoupMessageHeaders *hdrs);
48 struct SoupMessageHeaders {
51 SoupMessageHeadersType type;
53 SoupEncoding encoding;
54 goffset content_length;
55 SoupExpectation expectations;
62 * soup_message_headers_new:
63 * @type: the type of headers
65 * Creates a #SoupMessageHeaders. (#SoupMessage does this
66 * automatically for its own headers. You would only need to use this
67 * method if you are manually parsing or generating message headers.)
69 * Return value: a new #SoupMessageHeaders
72 soup_message_headers_new (SoupMessageHeadersType type)
74 SoupMessageHeaders *hdrs;
76 hdrs = g_slice_new0 (SoupMessageHeaders);
77 /* FIXME: is "5" a good default? */
78 hdrs->array = g_array_sized_new (TRUE, FALSE, sizeof (SoupHeader), 5);
86 static SoupMessageHeaders *
87 soup_message_headers_copy (SoupMessageHeaders *hdrs)
94 * soup_message_headers_free:
95 * @hdrs: a #SoupMessageHeaders
100 soup_message_headers_free (SoupMessageHeaders *hdrs)
102 if (--hdrs->ref_count == 0) {
103 soup_message_headers_clear (hdrs);
104 g_array_free (hdrs->array, TRUE);
105 g_clear_pointer (&hdrs->concat, g_hash_table_destroy);
106 g_slice_free (SoupMessageHeaders, hdrs);
110 G_DEFINE_BOXED_TYPE (SoupMessageHeaders, soup_message_headers, soup_message_headers_copy, soup_message_headers_free)
113 * soup_message_headers_clear:
114 * @hdrs: a #SoupMessageHeaders
119 soup_message_headers_clear (SoupMessageHeaders *hdrs)
121 SoupHeader *hdr_array = (SoupHeader *)hdrs->array->data;
124 for (i = 0; i < hdrs->array->len; i++)
125 g_free (hdr_array[i].value);
126 g_array_set_size (hdrs->array, 0);
129 g_hash_table_remove_all (hdrs->concat);
131 clear_special_headers (hdrs);
135 * soup_message_headers_clean_connection_headers:
136 * @hdrs: a #SoupMessageHeaders
138 * Removes all the headers listed in the Connection header.
143 soup_message_headers_clean_connection_headers (SoupMessageHeaders *hdrs)
146 const char *connection;
149 connection = soup_message_headers_get_list (hdrs, "Connection");
153 tokens = soup_header_parse_list (connection);
154 for (t = tokens; t; t = t->next)
155 soup_message_headers_remove (hdrs, t->data);
156 soup_header_free_list (tokens);
160 * soup_message_headers_append:
161 * @hdrs: a #SoupMessageHeaders
162 * @name: the header name to add
163 * @value: the new value of @name
165 * Appends a new header with name @name and value @value to @hdrs. (If
166 * there is an existing header with name @name, then this creates a
167 * second one, which is only allowed for list-valued headers; see also
168 * soup_message_headers_replace().)
170 * The caller is expected to make sure that @name and @value are
171 * syntactically correct.
174 soup_message_headers_append (SoupMessageHeaders *hdrs,
175 const char *name, const char *value)
178 SoupHeaderSetter setter;
180 g_return_if_fail (name != NULL);
181 g_return_if_fail (value != NULL);
183 /* Setting a syntactically invalid header name or value is
184 * considered to be a programming error. However, it can also
185 * be a security hole, so we want to fail here even if
186 * compiled with G_DISABLE_CHECKS.
188 #ifndef G_DISABLE_CHECKS
189 g_return_if_fail (*name && strpbrk (name, " \t\r\n:") == NULL);
190 g_return_if_fail (strpbrk (value, "\r\n") == NULL);
192 if (*name && strpbrk (name, " \t\r\n:")) {
193 g_warning ("soup_message_headers_append: Ignoring bad name '%s'", name);
196 if (strpbrk (value, "\r\n")) {
197 g_warning ("soup_message_headers_append: Ignoring bad value '%s'", value);
202 header.name = intern_header_name (name, &setter);
203 header.value = g_strdup (value);
204 g_array_append_val (hdrs->array, header);
206 g_hash_table_remove (hdrs->concat, header.name);
208 setter (hdrs, header.value);
212 * soup_message_headers_replace:
213 * @hdrs: a #SoupMessageHeaders
214 * @name: the header name to replace
215 * @value: the new value of @name
217 * Replaces the value of the header @name in @hdrs with @value. (See
218 * also soup_message_headers_append().)
220 * The caller is expected to make sure that @name and @value are
221 * syntactically correct.
224 soup_message_headers_replace (SoupMessageHeaders *hdrs,
225 const char *name, const char *value)
227 soup_message_headers_remove (hdrs, name);
228 soup_message_headers_append (hdrs, name, value);
232 find_header (SoupHeader *hdr_array, const char *interned_name, int nth)
236 for (i = 0; hdr_array[i].name; i++) {
237 if (hdr_array[i].name == interned_name) {
246 find_last_header (SoupHeader *hdr_array, guint length, const char *interned_name, int nth)
250 for (i = length; i >= 0; i--) {
251 if (hdr_array[i].name == interned_name) {
260 * soup_message_headers_remove:
261 * @hdrs: a #SoupMessageHeaders
262 * @name: the header name to remove
264 * Removes @name from @hdrs. If there are multiple values for @name,
265 * they are all removed.
268 soup_message_headers_remove (SoupMessageHeaders *hdrs, const char *name)
270 SoupHeader *hdr_array = (SoupHeader *)(hdrs->array->data);
271 SoupHeaderSetter setter;
274 g_return_if_fail (name != NULL);
276 name = intern_header_name (name, &setter);
277 while ((index = find_header (hdr_array, name, 0)) != -1) {
278 g_free (hdr_array[index].value);
279 g_array_remove_index (hdrs->array, index);
282 g_hash_table_remove (hdrs->concat, name);
288 * soup_message_headers_get_one:
289 * @hdrs: a #SoupMessageHeaders
292 * Gets the value of header @name in @hdrs. Use this for headers whose
293 * values are <emphasis>not</emphasis> comma-delimited lists, and
294 * which therefore can only appear at most once in the headers. For
295 * list-valued headers, use soup_message_headers_get_list().
297 * If @hdrs does erroneously contain multiple copies of the header, it
298 * is not defined which one will be returned. (Ideally, it will return
299 * whichever one makes libsoup most compatible with other HTTP
302 * Return value: the header's value or %NULL if not found.
307 soup_message_headers_get_one (SoupMessageHeaders *hdrs, const char *name)
309 SoupHeader *hdr_array = (SoupHeader *)(hdrs->array->data);
310 guint hdr_length = hdrs->array->len;
313 g_return_val_if_fail (name != NULL, NULL);
315 name = intern_header_name (name, NULL);
317 index = find_last_header (hdr_array, hdr_length, name, 0);
319 return (index == -1) ? NULL : hdr_array[index].value;
323 * soup_message_headers_get_list:
324 * @hdrs: a #SoupMessageHeaders
327 * Gets the value of header @name in @hdrs. Use this for headers whose
328 * values are comma-delimited lists, and which are therefore allowed
329 * to appear multiple times in the headers. For non-list-valued
330 * headers, use soup_message_headers_get_one().
332 * If @name appears multiple times in @hdrs,
333 * soup_message_headers_get_list() will concatenate all of the values
334 * together, separated by commas. This is sometimes awkward to parse
335 * (eg, WWW-Authenticate, Set-Cookie), but you have to be able to deal
336 * with it anyway, because the HTTP spec explicitly states that this
337 * transformation is allowed, and so an upstream proxy could do the
340 * Return value: the header's value or %NULL if not found.
345 soup_message_headers_get_list (SoupMessageHeaders *hdrs, const char *name)
347 SoupHeader *hdr_array = (SoupHeader *)(hdrs->array->data);
352 g_return_val_if_fail (name != NULL, NULL);
354 name = intern_header_name (name, NULL);
356 value = g_hash_table_lookup (hdrs->concat, name);
361 index = find_header (hdr_array, name, 0);
364 else if (find_header (hdr_array, name, 1) == -1)
365 return hdr_array[index].value;
367 concat = g_string_new (NULL);
368 for (i = 0; (index = find_header (hdr_array, name, i)) != -1; i++) {
370 g_string_append (concat, ", ");
371 g_string_append (concat, hdr_array[index].value);
373 value = g_string_free (concat, FALSE);
376 hdrs->concat = g_hash_table_new_full (NULL, NULL, NULL, g_free);
377 g_hash_table_insert (hdrs->concat, (gpointer)name, value);
382 * soup_message_headers_get:
383 * @hdrs: a #SoupMessageHeaders
386 * Gets the value of header @name in @hdrs.
388 * This method was supposed to work correctly for both single-valued
389 * and list-valued headers, but because some HTTP clients/servers
390 * mistakenly send multiple copies of headers that are supposed to be
391 * single-valued, it sometimes returns incorrect results. To fix this,
392 * the methods soup_message_headers_get_one() and
393 * soup_message_headers_get_list() were introduced, so callers can
394 * explicitly state which behavior they are expecting.
396 * Return value: as with soup_message_headers_get_list().
398 * Deprecated: Use soup_message_headers_get_one() or
399 * soup_message_headers_get_list() instead.
402 soup_message_headers_get (SoupMessageHeaders *hdrs, const char *name)
404 return soup_message_headers_get_list (hdrs, name);
408 * SoupMessageHeadersIter:
410 * An opaque type used to iterate over a %SoupMessageHeaders
413 * After intializing the iterator with
414 * soup_message_headers_iter_init(), call
415 * soup_message_headers_iter_next() to fetch data from it.
417 * You may not modify the headers while iterating over them.
421 SoupMessageHeaders *hdrs;
423 } SoupMessageHeadersIterReal;
426 * soup_message_headers_iter_init:
427 * @iter: (out) (transfer none): a pointer to a %SoupMessageHeadersIter
429 * @hdrs: a %SoupMessageHeaders
431 * Initializes @iter for iterating @hdrs.
434 soup_message_headers_iter_init (SoupMessageHeadersIter *iter,
435 SoupMessageHeaders *hdrs)
437 SoupMessageHeadersIterReal *real = (SoupMessageHeadersIterReal *)iter;
444 * soup_message_headers_iter_next:
445 * @iter: (inout) (transfer none): a %SoupMessageHeadersIter
446 * @name: (out) (transfer none): pointer to a variable to return
448 * @value: (out) (transfer none): pointer to a variable to return
449 * the header value in
451 * Yields the next name/value pair in the %SoupMessageHeaders being
452 * iterated by @iter. If @iter has already yielded the last header,
453 * then soup_message_headers_iter_next() will return %FALSE and @name
454 * and @value will be unchanged.
456 * Return value: %TRUE if another name and value were returned, %FALSE
457 * if the end of the headers has been reached.
460 soup_message_headers_iter_next (SoupMessageHeadersIter *iter,
461 const char **name, const char **value)
463 SoupMessageHeadersIterReal *real = (SoupMessageHeadersIterReal *)iter;
464 SoupHeader *hdr_array = (SoupHeader *)real->hdrs->array->data;
466 if (real->index >= real->hdrs->array->len)
469 *name = hdr_array[real->index].name;
470 *value = hdr_array[real->index].value;
476 * SoupMessageHeadersForeachFunc:
477 * @name: the header name
478 * @value: the header value
479 * @user_data: the data passed to soup_message_headers_foreach()
481 * The callback passed to soup_message_headers_foreach().
485 * soup_message_headers_foreach:
486 * @hdrs: a #SoupMessageHeaders
487 * @func: (scope call): callback function to run for each header
488 * @user_data: data to pass to @func
490 * Calls @func once for each header value in @hdrs.
492 * Beware that unlike soup_message_headers_get(), this processes the
493 * headers in exactly the way they were added, rather than
494 * concatenating multiple same-named headers into a single value.
495 * (This is intentional; it ensures that if you call
496 * soup_message_headers_append() multiple times with the same name,
497 * then the I/O code will output multiple copies of the header when
498 * sending the message to the remote implementation, which may be
499 * required for interoperability in some cases.)
501 * You may not modify the headers from @func.
504 soup_message_headers_foreach (SoupMessageHeaders *hdrs,
505 SoupMessageHeadersForeachFunc func,
508 SoupHeader *hdr_array = (SoupHeader *)hdrs->array->data;
511 for (i = 0; i < hdrs->array->len; i++)
512 func (hdr_array[i].name, hdr_array[i].value, user_data);
516 G_LOCK_DEFINE_STATIC (header_pool);
517 static GHashTable *header_pool, *header_setters;
519 static void transfer_encoding_setter (SoupMessageHeaders *, const char *);
520 static void content_length_setter (SoupMessageHeaders *, const char *);
521 static void expectation_setter (SoupMessageHeaders *, const char *);
522 static void content_type_setter (SoupMessageHeaders *, const char *);
525 intern_header_locked (const char *name)
529 interned = g_hash_table_lookup (header_pool, name);
531 char *dup = g_strdup (name);
532 g_hash_table_insert (header_pool, dup, dup);
539 intern_header_name (const char *name, SoupHeaderSetter *setter)
541 const char *interned;
543 G_LOCK (header_pool);
546 header_pool = g_hash_table_new (soup_str_case_hash, soup_str_case_equal);
547 header_setters = g_hash_table_new (NULL, NULL);
548 g_hash_table_insert (header_setters,
549 intern_header_locked ("Transfer-Encoding"),
550 transfer_encoding_setter);
551 g_hash_table_insert (header_setters,
552 intern_header_locked ("Content-Length"),
553 content_length_setter);
554 g_hash_table_insert (header_setters,
555 intern_header_locked ("Expect"),
557 g_hash_table_insert (header_setters,
558 intern_header_locked ("Content-Type"),
559 content_type_setter);
562 interned = intern_header_locked (name);
564 *setter = g_hash_table_lookup (header_setters, interned);
566 G_UNLOCK (header_pool);
571 clear_special_headers (SoupMessageHeaders *hdrs)
573 SoupHeaderSetter setter;
577 /* Make sure header_setters has been initialized */
578 intern_header_name ("", NULL);
580 g_hash_table_iter_init (&iter, header_setters);
581 while (g_hash_table_iter_next (&iter, &key, &value)) {
587 /* Specific headers */
590 transfer_encoding_setter (SoupMessageHeaders *hdrs, const char *value)
593 if (g_ascii_strcasecmp (value, "chunked") == 0)
594 hdrs->encoding = SOUP_ENCODING_CHUNKED;
596 hdrs->encoding = SOUP_ENCODING_UNRECOGNIZED;
602 content_length_setter (SoupMessageHeaders *hdrs, const char *value)
604 /* Transfer-Encoding trumps Content-Length */
605 if (hdrs->encoding == SOUP_ENCODING_CHUNKED)
611 hdrs->content_length = g_ascii_strtoull (value, &end, 10);
613 hdrs->encoding = SOUP_ENCODING_UNRECOGNIZED;
615 hdrs->encoding = SOUP_ENCODING_CONTENT_LENGTH;
622 * @SOUP_ENCODING_UNRECOGNIZED: unknown / error
623 * @SOUP_ENCODING_NONE: no body is present (which is not the same as a
624 * 0-length body, and only occurs in certain places)
625 * @SOUP_ENCODING_CONTENT_LENGTH: Content-Length encoding
626 * @SOUP_ENCODING_EOF: Response body ends when the connection is closed
627 * @SOUP_ENCODING_CHUNKED: chunked encoding (currently only supported
629 * @SOUP_ENCODING_BYTERANGES: multipart/byteranges (Reserved for future
630 * use: NOT CURRENTLY IMPLEMENTED)
632 * How a message body is encoded for transport
636 * soup_message_headers_get_encoding:
637 * @hdrs: a #SoupMessageHeaders
639 * Gets the message body encoding that @hdrs declare. This may not
640 * always correspond to the encoding used on the wire; eg, a HEAD
641 * response may declare a Content-Length or Transfer-Encoding, but
642 * it will never actually include a body.
644 * Return value: the encoding declared by @hdrs.
647 soup_message_headers_get_encoding (SoupMessageHeaders *hdrs)
651 if (hdrs->encoding != -1)
652 return hdrs->encoding;
654 /* If Transfer-Encoding was set, hdrs->encoding would already
655 * be set. So we don't need to check that possibility.
657 header = soup_message_headers_get_one (hdrs, "Content-Length");
659 content_length_setter (hdrs, header);
660 if (hdrs->encoding != -1)
661 return hdrs->encoding;
664 /* Per RFC 2616 4.4, a response body that doesn't indicate its
665 * encoding otherwise is terminated by connection close, and a
666 * request that doesn't indicate otherwise has no body. Note
667 * that SoupMessage calls soup_message_headers_set_encoding()
668 * to override the response body default for our own
669 * server-side messages.
671 hdrs->encoding = (hdrs->type == SOUP_MESSAGE_HEADERS_RESPONSE) ?
672 SOUP_ENCODING_EOF : SOUP_ENCODING_NONE;
673 return hdrs->encoding;
677 * soup_message_headers_set_encoding:
678 * @hdrs: a #SoupMessageHeaders
679 * @encoding: a #SoupEncoding
681 * Sets the message body encoding that @hdrs will declare. In particular,
682 * you should use this if you are going to send a request or response in
686 soup_message_headers_set_encoding (SoupMessageHeaders *hdrs,
687 SoupEncoding encoding)
689 if (encoding == hdrs->encoding)
693 case SOUP_ENCODING_NONE:
694 case SOUP_ENCODING_EOF:
695 soup_message_headers_remove (hdrs, "Transfer-Encoding");
696 soup_message_headers_remove (hdrs, "Content-Length");
699 case SOUP_ENCODING_CONTENT_LENGTH:
700 soup_message_headers_remove (hdrs, "Transfer-Encoding");
703 case SOUP_ENCODING_CHUNKED:
704 soup_message_headers_remove (hdrs, "Content-Length");
705 soup_message_headers_replace (hdrs, "Transfer-Encoding", "chunked");
709 g_return_if_reached ();
712 hdrs->encoding = encoding;
716 * soup_message_headers_get_content_length:
717 * @hdrs: a #SoupMessageHeaders
719 * Gets the message body length that @hdrs declare. This will only
720 * be non-0 if soup_message_headers_get_encoding() returns
721 * %SOUP_ENCODING_CONTENT_LENGTH.
723 * Return value: the message body length declared by @hdrs.
726 soup_message_headers_get_content_length (SoupMessageHeaders *hdrs)
728 SoupEncoding encoding;
730 encoding = soup_message_headers_get_encoding (hdrs);
731 if (encoding == SOUP_ENCODING_CONTENT_LENGTH)
732 return hdrs->content_length;
738 * soup_message_headers_set_content_length:
739 * @hdrs: a #SoupMessageHeaders
740 * @content_length: the message body length
742 * Sets the message body length that @hdrs will declare, and sets
743 * @hdrs's encoding to %SOUP_ENCODING_CONTENT_LENGTH.
745 * You do not normally need to call this; if @hdrs is set to use
746 * Content-Length encoding, libsoup will automatically set its
747 * Content-Length header for you immediately before sending the
748 * headers. One situation in which this method is useful is when
749 * generating the response to a HEAD request; Calling
750 * soup_message_headers_set_content_length() allows you to put the
751 * correct content length into the response without needing to waste
752 * memory by filling in a response body which won't actually be sent.
755 soup_message_headers_set_content_length (SoupMessageHeaders *hdrs,
756 goffset content_length)
760 g_snprintf (length, sizeof (length), "%" G_GUINT64_FORMAT,
762 soup_message_headers_remove (hdrs, "Transfer-Encoding");
763 soup_message_headers_replace (hdrs, "Content-Length", length);
767 expectation_setter (SoupMessageHeaders *hdrs, const char *value)
770 if (!g_ascii_strcasecmp (value, "100-continue"))
771 hdrs->expectations = SOUP_EXPECTATION_CONTINUE;
773 hdrs->expectations = SOUP_EXPECTATION_UNRECOGNIZED;
775 hdrs->expectations = 0;
780 * @SOUP_EXPECTATION_CONTINUE: "100-continue"
781 * @SOUP_EXPECTATION_UNRECOGNIZED: any unrecognized expectation
783 * Represents the parsed value of the "Expect" header.
787 * soup_message_headers_get_expectations:
788 * @hdrs: a #SoupMessageHeaders
790 * Gets the expectations declared by @hdrs's "Expect" header.
791 * Currently this will either be %SOUP_EXPECTATION_CONTINUE or
792 * %SOUP_EXPECTATION_UNRECOGNIZED.
794 * Return value: the contents of @hdrs's "Expect" header
797 soup_message_headers_get_expectations (SoupMessageHeaders *hdrs)
799 return hdrs->expectations;
803 * soup_message_headers_set_expectations:
804 * @hdrs: a #SoupMessageHeaders
805 * @expectations: the expectations to set
807 * Sets @hdrs's "Expect" header according to @expectations.
809 * Currently %SOUP_EXPECTATION_CONTINUE is the only known expectation
810 * value. You should set this value on a request if you are sending a
811 * large message body (eg, via POST or PUT), and want to give the
812 * server a chance to reject the request after seeing just the headers
813 * (eg, because it will require authentication before allowing you to
814 * post, or because you're POSTing to a URL that doesn't exist). This
815 * saves you from having to transmit the large request body when the
816 * server is just going to ignore it anyway.
819 soup_message_headers_set_expectations (SoupMessageHeaders *hdrs,
820 SoupExpectation expectations)
822 g_return_if_fail ((expectations & ~SOUP_EXPECTATION_CONTINUE) == 0);
824 if (expectations & SOUP_EXPECTATION_CONTINUE)
825 soup_message_headers_replace (hdrs, "Expect", "100-continue");
827 soup_message_headers_remove (hdrs, "Expect");
832 * @start: the start of the range
833 * @end: the end of the range
835 * Represents a byte range as used in the Range header.
837 * If @end is non-negative, then @start and @end represent the bounds
838 * of of the range, counting from 0. (Eg, the first 500 bytes would be
839 * represented as @start = 0 and @end = 499.)
841 * If @end is -1 and @start is non-negative, then this represents a
842 * range starting at @start and ending with the last byte of the
843 * requested resource body. (Eg, all but the first 500 bytes would be
844 * @start = 500, and @end = -1.)
846 * If @end is -1 and @start is negative, then it represents a "suffix
847 * range", referring to the last -@start bytes of the resource body.
848 * (Eg, the last 500 bytes would be @start = -500 and @end = -1.)
854 sort_ranges (gconstpointer a, gconstpointer b)
856 SoupRange *ra = (SoupRange *)a;
857 SoupRange *rb = (SoupRange *)b;
859 return ra->start - rb->start;
862 /* like soup_message_headers_get_ranges(), except it returns:
863 * SOUP_STATUS_OK if there is no Range or it should be ignored.
864 * SOUP_STATUS_PARTIAL_CONTENT if there is at least one satisfiable range.
865 * SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE if @check_satisfiable
866 * is %TRUE and the request is not satisfiable given @total_length.
869 soup_message_headers_get_ranges_internal (SoupMessageHeaders *hdrs,
870 goffset total_length,
871 gboolean check_satisfiable,
875 const char *range = soup_message_headers_get_one (hdrs, "Range");
876 GSList *range_list, *r;
880 guint status = SOUP_STATUS_OK;
882 if (!range || strncmp (range, "bytes", 5) != 0)
886 while (g_ascii_isspace (*range))
890 while (g_ascii_isspace (*range))
893 range_list = soup_header_parse_list (range);
897 array = g_array_new (FALSE, FALSE, sizeof (SoupRange));
898 for (r = range_list; r; r = r->next) {
903 cur.start = g_ascii_strtoll (spec, &end, 10) + total_length;
904 cur.end = total_length - 1;
906 cur.start = g_ascii_strtoull (spec, &end, 10);
910 cur.end = g_ascii_strtoull (end, &end, 10);
911 if (cur.end < cur.start) {
912 status = SOUP_STATUS_OK;
916 cur.end = total_length - 1;
919 status = SOUP_STATUS_OK;
921 } else if (check_satisfiable && cur.start >= total_length) {
922 if (status == SOUP_STATUS_OK)
923 status = SOUP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE;
927 g_array_append_val (array, cur);
928 status = SOUP_STATUS_PARTIAL_CONTENT;
930 soup_header_free_list (range_list);
932 if (status != SOUP_STATUS_PARTIAL_CONTENT) {
933 g_array_free (array, TRUE);
938 g_array_sort (array, sort_ranges);
939 for (i = 1; i < array->len; i++) {
940 SoupRange *cur = &((SoupRange *)array->data)[i];
941 SoupRange *prev = &((SoupRange *)array->data)[i - 1];
943 if (cur->start <= prev->end) {
944 prev->end = MAX (prev->end, cur->end);
945 g_array_remove_index (array, i);
950 *ranges = (SoupRange *)array->data;
951 *length = array->len;
953 g_array_free (array, FALSE);
954 return SOUP_STATUS_PARTIAL_CONTENT;
958 * soup_message_headers_get_ranges:
959 * @hdrs: a #SoupMessageHeaders
960 * @total_length: the total_length of the response body
961 * @ranges: (out): return location for an array of #SoupRange
962 * @length: the length of the returned array
964 * Parses @hdrs's Range header and returns an array of the requested
965 * byte ranges. The returned array must be freed with
966 * soup_message_headers_free_ranges().
968 * If @total_length is non-0, its value will be used to adjust the
969 * returned ranges to have explicit start and end values, and the
970 * returned ranges will be sorted and non-overlapping. If
971 * @total_length is 0, then some ranges may have an end value of -1,
972 * as described under #SoupRange, and some of the ranges may be
975 * Beware that even if given a @total_length, this function does not
976 * check that the ranges are satisfiable.
979 * #SoupServer has built-in handling for range requests. If your
980 * server handler returns a %SOUP_STATUS_OK response containing the
981 * complete response body (rather than pausing the message and
982 * returning some of the response body later), and there is a Range
983 * header in the request, then libsoup will automatically convert the
984 * response to a %SOUP_STATUS_PARTIAL_CONTENT response containing only
985 * the range(s) requested by the client.
987 * The only time you need to process the Range header yourself is if
988 * either you need to stream the response body rather than returning
989 * it all at once, or you do not already have the complete response
990 * body available, and only want to generate the parts that were
991 * actually requested by the client.
994 * Return value: %TRUE if @hdrs contained a syntactically-valid
995 * "Range" header, %FALSE otherwise (in which case @range and @length
1001 soup_message_headers_get_ranges (SoupMessageHeaders *hdrs,
1002 goffset total_length,
1008 status = soup_message_headers_get_ranges_internal (hdrs, total_length, FALSE, ranges, length);
1009 return status == SOUP_STATUS_PARTIAL_CONTENT;
1013 * soup_message_headers_free_ranges:
1014 * @hdrs: a #SoupMessageHeaders
1015 * @ranges: an array of #SoupRange
1017 * Frees the array of ranges returned from soup_message_headers_get_ranges().
1022 soup_message_headers_free_ranges (SoupMessageHeaders *hdrs,
1029 * soup_message_headers_set_ranges:
1030 * @hdrs: a #SoupMessageHeaders
1031 * @ranges: an array of #SoupRange
1032 * @length: the length of @range
1034 * Sets @hdrs's Range header to request the indicated ranges. (If you
1035 * only want to request a single range, you can use
1036 * soup_message_headers_set_range().)
1041 soup_message_headers_set_ranges (SoupMessageHeaders *hdrs,
1048 header = g_string_new ("bytes=");
1049 for (i = 0; i < length; i++) {
1051 g_string_append_c (header, ',');
1052 if (ranges[i].end >= 0) {
1053 g_string_append_printf (header, "%" G_GINT64_FORMAT "-%" G_GINT64_FORMAT,
1054 ranges[i].start, ranges[i].end);
1055 } else if (ranges[i].start >= 0) {
1056 g_string_append_printf (header,"%" G_GINT64_FORMAT "-",
1059 g_string_append_printf (header, "%" G_GINT64_FORMAT,
1064 soup_message_headers_replace (hdrs, "Range", header->str);
1065 g_string_free (header, TRUE);
1069 * soup_message_headers_set_range:
1070 * @hdrs: a #SoupMessageHeaders
1071 * @start: the start of the range to request
1072 * @end: the end of the range to request
1074 * Sets @hdrs's Range header to request the indicated range.
1075 * @start and @end are interpreted as in a #SoupRange.
1077 * If you need to request multiple ranges, use
1078 * soup_message_headers_set_ranges().
1083 soup_message_headers_set_range (SoupMessageHeaders *hdrs,
1089 range.start = start;
1091 soup_message_headers_set_ranges (hdrs, &range, 1);
1095 * soup_message_headers_get_content_range:
1096 * @hdrs: a #SoupMessageHeaders
1097 * @start: return value for the start of the range
1098 * @end: return value for the end of the range
1099 * @total_length: return value for the total length of the resource,
1100 * or %NULL if you don't care.
1102 * Parses @hdrs's Content-Range header and returns it in @start,
1103 * @end, and @total_length. If the total length field in the header
1104 * was specified as "*", then @total_length will be set to -1.
1106 * Return value: %TRUE if @hdrs contained a "Content-Range" header
1107 * containing a byte range which could be parsed, %FALSE otherwise.
1112 soup_message_headers_get_content_range (SoupMessageHeaders *hdrs,
1115 goffset *total_length)
1117 const char *header = soup_message_headers_get_one (hdrs, "Content-Range");
1121 if (!header || strncmp (header, "bytes ", 6) != 0)
1125 while (g_ascii_isspace (*header))
1127 if (!g_ascii_isdigit (*header))
1130 *start = g_ascii_strtoull (header, &p, 10);
1133 *end = g_ascii_strtoull (p + 1, &p, 10);
1141 length = g_ascii_strtoull (p, &p, 10);
1144 *total_length = length;
1149 * soup_message_headers_set_content_range:
1150 * @hdrs: a #SoupMessageHeaders
1151 * @start: the start of the range
1152 * @end: the end of the range
1153 * @total_length: the total length of the resource, or -1 if unknown
1155 * Sets @hdrs's Content-Range header according to the given values.
1156 * (Note that @total_length is the total length of the entire resource
1157 * that this is a range of, not simply @end - @start + 1.)
1160 * #SoupServer has built-in handling for range requests, and you do
1161 * not normally need to call this function youself. See
1162 * soup_message_headers_get_ranges() for more details.
1168 soup_message_headers_set_content_range (SoupMessageHeaders *hdrs,
1171 goffset total_length)
1175 if (total_length >= 0) {
1176 header = g_strdup_printf ("bytes %" G_GINT64_FORMAT "-%"
1177 G_GINT64_FORMAT "/%" G_GINT64_FORMAT,
1178 start, end, total_length);
1180 header = g_strdup_printf ("bytes %" G_GINT64_FORMAT "-%"
1181 G_GINT64_FORMAT "/*", start, end);
1183 soup_message_headers_replace (hdrs, "Content-Range", header);
1188 parse_content_foo (SoupMessageHeaders *hdrs, const char *header_name,
1189 char **foo, GHashTable **params)
1194 header = soup_message_headers_get_one (hdrs, header_name);
1199 *foo = g_strdup (header);
1200 semi = strchr (*foo, ';');
1205 while (p - 1 > *foo && g_ascii_isspace(p[-1]))
1209 semi = strchr (header, ';');
1218 *params = soup_header_parse_semi_param_list ("");
1222 *params = soup_header_parse_semi_param_list (semi);
1227 set_content_foo (SoupMessageHeaders *hdrs, const char *header_name,
1228 const char *foo, GHashTable *params)
1231 GHashTableIter iter;
1232 gpointer key, value;
1234 str = g_string_new (foo);
1236 g_hash_table_iter_init (&iter, params);
1237 while (g_hash_table_iter_next (&iter, &key, &value)) {
1238 g_string_append (str, "; ");
1239 soup_header_g_string_append_param (str, key, value);
1243 soup_message_headers_replace (hdrs, header_name, str->str);
1244 g_string_free (str, TRUE);
1248 content_type_setter (SoupMessageHeaders *hdrs, const char *value)
1250 g_free (hdrs->content_type);
1252 char *content_type, *p;
1254 parse_content_foo (hdrs, "Content-Type", &content_type, NULL);
1255 p = strpbrk (content_type, " /");
1256 if (!p || *p != '/' || strpbrk (p + 1, " /")) {
1257 g_free (content_type);
1258 hdrs->content_type = NULL;
1260 hdrs->content_type = content_type;
1262 hdrs->content_type = NULL;
1266 * soup_message_headers_get_content_type:
1267 * @hdrs: a #SoupMessageHeaders
1268 * @params: (out) (element-type utf8 utf8) (allow-none) (transfer full):
1269 * return location for the Content-Type parameters (eg, "charset"), or
1272 * Looks up the "Content-Type" header in @hdrs, parses it, and returns
1273 * its value in *@content_type and *@params. @params can be %NULL if you
1274 * are only interested in the content type itself.
1276 * Return value: a string with the value of the "Content-Type" header
1277 * or NULL if @hdrs does not contain that header or it cannot be
1278 * parsed (in which case *@params will be unchanged).
1283 soup_message_headers_get_content_type (SoupMessageHeaders *hdrs,
1284 GHashTable **params)
1286 if (!hdrs->content_type)
1290 parse_content_foo (hdrs, "Content-Type", NULL, params);
1291 return hdrs->content_type;
1295 * soup_message_headers_set_content_type:
1296 * @hdrs: a #SoupMessageHeaders
1297 * @content_type: the MIME type
1298 * @params: (allow-none) (element-type utf8 utf8): additional
1299 * parameters, or %NULL
1301 * Sets the "Content-Type" header in @hdrs to @content_type,
1302 * optionally with additional parameters specified in @params.
1307 soup_message_headers_set_content_type (SoupMessageHeaders *hdrs,
1308 const char *content_type,
1311 set_content_foo (hdrs, "Content-Type", content_type, params);
1315 * soup_message_headers_get_content_disposition:
1316 * @hdrs: a #SoupMessageHeaders
1317 * @disposition: (out) (transfer full): return location for the
1318 * disposition-type, or %NULL
1319 * @params: (out) (transfer full) (element-type utf8 utf8): return
1320 * location for the Content-Disposition parameters, or %NULL
1322 * Looks up the "Content-Disposition" header in @hdrs, parses it, and
1323 * returns its value in *@disposition and *@params. @params can be
1324 * %NULL if you are only interested in the disposition-type.
1326 * In HTTP, the most common use of this header is to set a
1327 * disposition-type of "attachment", to suggest to the browser that a
1328 * response should be saved to disk rather than displayed in the
1329 * browser. If @params contains a "filename" parameter, this is a
1330 * suggestion of a filename to use. (If the parameter value in the
1331 * header contains an absolute or relative path, libsoup will truncate
1332 * it down to just the final path component, so you do not need to
1333 * test this yourself.)
1335 * Content-Disposition is also used in "multipart/form-data", however
1336 * this is handled automatically by #SoupMultipart and the associated
1339 * Return value: %TRUE if @hdrs contains a "Content-Disposition"
1340 * header, %FALSE if not (in which case *@disposition and *@params
1341 * will be unchanged).
1346 soup_message_headers_get_content_disposition (SoupMessageHeaders *hdrs,
1348 GHashTable **params)
1350 gpointer orig_key, orig_value;
1352 if (!parse_content_foo (hdrs, "Content-Disposition",
1353 disposition, params))
1356 /* If there is a filename parameter, make sure it contains
1357 * only a single path component
1359 if (params && g_hash_table_lookup_extended (*params, "filename",
1360 &orig_key, &orig_value)) {
1361 char *filename = strrchr (orig_value, '/');
1364 g_hash_table_insert (*params, g_strdup (orig_key), filename + 1);
1370 * soup_message_headers_set_content_disposition:
1371 * @hdrs: a #SoupMessageHeaders
1372 * @disposition: the disposition-type
1373 * @params: (allow-none) (element-type utf8 utf8): additional
1374 * parameters, or %NULL
1376 * Sets the "Content-Disposition" header in @hdrs to @disposition,
1377 * optionally with additional parameters specified in @params.
1379 * See soup_message_headers_get_content_disposition() for a discussion
1380 * of how Content-Disposition is used in HTTP.
1385 soup_message_headers_set_content_disposition (SoupMessageHeaders *hdrs,
1386 const char *disposition,
1389 set_content_foo (hdrs, "Content-Disposition", disposition, params);