NetSurf
content-type.c
Go to the documentation of this file.
1/*
2 * Copyright 2010 John-Mark Bell <jmb@netsurf-browser.org>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include "utils/http.h"
24
25#include "utils/http/generics.h"
28
29/* See content-type.h for documentation */
30nserror http_parse_content_type(const char *header_value,
32{
33 const char *pos = header_value;
34 lwc_string *type;
35 lwc_string *subtype = NULL;
36 http_parameter *params = NULL;
37 char *mime;
38 size_t mime_len;
39 lwc_string *imime;
41 nserror error;
42
43 /* type "/" subtype *( ";" parameter ) */
44
45 http__skip_LWS(&pos);
46
47 error = http__parse_token(&pos, &type);
48 if (error != NSERROR_OK)
49 return error;
50
51 http__skip_LWS(&pos);
52
53 if (*pos != '/') {
54 lwc_string_unref(type);
55 return NSERROR_NOT_FOUND;
56 }
57
58 pos++;
59
60 http__skip_LWS(&pos);
61
62 error = http__parse_token(&pos, &subtype);
63 if (error != NSERROR_OK) {
64 lwc_string_unref(type);
65 return error;
66 }
67
68 http__skip_LWS(&pos);
69
70 if (*pos == ';') {
71 error = http__item_list_parse(&pos,
72 http__parse_parameter, NULL, &params);
73 if (error != NSERROR_OK && error != NSERROR_NOT_FOUND) {
74 lwc_string_unref(subtype);
75 lwc_string_unref(type);
76 return error;
77 }
78 }
79
80 /* <type> + <subtype> + '/' */
81 mime_len = lwc_string_length(type) + lwc_string_length(subtype) + 1;
82
83 mime = malloc(mime_len + 1);
84 if (mime == NULL) {
86 lwc_string_unref(subtype);
87 lwc_string_unref(type);
88 return NSERROR_NOMEM;
89 }
90
91 sprintf(mime, "%.*s/%.*s",
92 (int) lwc_string_length(type), lwc_string_data(type),
93 (int) lwc_string_length(subtype), lwc_string_data(subtype));
94
95 lwc_string_unref(subtype);
96 lwc_string_unref(type);
97
98 if (lwc_intern_string(mime, mime_len, &imime) != lwc_error_ok) {
100 free(mime);
101 return NSERROR_NOMEM;
102 }
103
104 free(mime);
105
106 ct = malloc(sizeof(*ct));
107 if (ct == NULL) {
108 lwc_string_unref(imime);
110 return NSERROR_NOMEM;
111 }
112
113 ct->media_type = imime;
114 ct->parameters = params;
115
116 *result = ct;
117
118 return NSERROR_OK;
119}
120
121/* See content-type.h for documentation */
123{
124 lwc_string_unref(victim->media_type);
126 free(victim);
127}
128
STATIC char result[100]
Definition: arexx.c:77
nserror http_parse_content_type(const char *header_value, http_content_type **result)
Parse an HTTP Content-Type header value.
Definition: content-type.c:30
void http_content_type_destroy(http_content_type *victim)
Destroy a content type object.
Definition: content-type.c:122
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_NOT_FOUND
Requested item not found.
Definition: errors.h:34
@ NSERROR_NOMEM
Memory exhaustion.
Definition: errors.h:32
@ NSERROR_OK
No error.
Definition: errors.h:30
const char * type
Definition: filetype.cpp:44
#define http__item_list_parse(i, p, f, r)
Definition: generics.h:52
HTTP header parsing functions.
void http_parameter_list_destroy(http_parameter *list)
Destroy a list of HTTP parameters.
Definition: parameter.c:149
nserror http__parse_parameter(const char **input, http_parameter **parameter)
Parse an HTTP parameter.
Definition: parameter.c:61
nserror http__parse_token(const char **input, lwc_string **value)
Parse an HTTP token.
Definition: primitives.c:68
void http__skip_LWS(const char **input)
Skip past linear whitespace in input.
Definition: primitives.c:31
Interface to utility string handling.
http_parameter * parameters
Definition: content-type.h:28
lwc_string * media_type
Definition: content-type.h:27
Representation of an HTTP parameter.
Definition: parameter.c:31