13
13
#include "soup-headers.h"
14
14
#include "soup-misc.h"
19
* @str: the header string (including the Request-Line or Status-Line,
20
* and the trailing blank line)
21
* @len: length of @str up to (but not including) the terminating blank line.
22
* @dest: #SoupMessageHeaders to store the header values in
24
* Parses the headers of an HTTP request or response in @str and
25
* stores the results in @dest. Beware that @dest may be modified even
28
* This is a low-level method; normally you would use
29
* soup_headers_parse_request() or soup_headers_parse_response().
31
* Return value: success or failure
17
34
soup_headers_parse (const char *str, int len, SoupMessageHeaders *dest)
19
36
const char *headers_start;
623
decode_rfc2231 (char *encoded_string)
627
q = strchr (encoded_string, '\'');
630
if (g_ascii_strncasecmp (encoded_string, "UTF-8",
631
q - encoded_string) != 0)
634
q = strchr (q + 1, '\'');
638
decoded = soup_uri_decode (q + 1);
639
/* strlen(decoded) <= strlen(q + 1) < strlen(encoded_string) */
640
strcpy (encoded_string, decoded);
605
645
static GHashTable *
606
646
parse_param_list (const char *header, char delim)
632
672
*name_end = '\0';
634
674
value = (char *)skip_lws (eq + 1);
676
if (name_end[-1] == '*' && name_end > item + 1) {
678
if (!decode_rfc2231 (value)) {
682
} else if (*value == '"')
636
683
decode_quoted_string (value);
654
701
* Tokens that don't have an associated value will still be added to
655
702
* the resulting hash table, but with a %NULL value.
704
* This also handles RFC2231 encoding (which in HTTP is mostly used
705
* for giving UTF8-encoded filenames in the Content-Disposition
657
708
* Return value: a #GHashTable of list elements, which can be freed
658
709
* with soup_header_free_param_list().
673
724
* Tokens that don't have an associated value will still be added to
674
725
* the resulting hash table, but with a %NULL value.
727
* This also handles RFC2231 encoding (which in HTTP is mostly used
728
* for giving UTF8-encoded filenames in the Content-Disposition
676
731
* Return value: a #GHashTable of list elements, which can be freed
677
732
* with soup_header_free_param_list().
695
750
g_hash_table_destroy (param_list);
754
append_param_rfc2231 (GString *string, const char *value)
758
g_string_append (string, "*=UTF-8''");
759
encoded = soup_uri_encode (value, " *'%()<>@,;:\\\"/[]?=");
760
g_string_append (string, encoded);
765
append_param_quoted (GString *string, const char *value)
769
g_string_append (string, "=\"");
771
while (*value == '\\' || *value == '"') {
772
g_string_append_c (string, '\\');
773
g_string_append_c (string, *value++);
775
len = strcspn (value, "\\\"");
776
g_string_append_len (string, value, len);
779
g_string_append_c (string, '"');
783
* soup_header_g_string_append_param:
784
* @string: a #GString being used to construct an HTTP header value
785
* @name: a parameter name
786
* @value: a parameter value
788
* Appends something like <literal>@name="@value"</literal> to @string,
789
* taking care to appropriately escape any quotes or backslashes in @value.
791
* Alternatively, if @value is a non-ASCII UTF-8 string, it will be
792
* appended using RFC2231 syntax. Although in theory this is supposed
793
* to work anywhere in HTTP that uses this style of parameter, in
794
* reality, it can only be used portably with the Content-Disposition
795
* "filename" parameter.
798
soup_header_g_string_append_param (GString *string, const char *name,
803
g_string_append (string, name);
805
for (v = value; *v; v++) {
807
if (g_utf8_validate (value, -1, NULL)) {
808
append_param_rfc2231 (string, value);
814
append_param_quoted (string, value);