NetSurf
parameter.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 <stdlib.h>
20#include <string.h>
21
22#include "utils/http.h"
23
24#include "utils/http/generics.h"
27
28/**
29 * Representation of an HTTP parameter
30 */
33
34 lwc_string *name; /**< Parameter name */
35 lwc_string *value; /**< Parameter value */
36};
37
38/**
39 * Destructor for an HTTP parameter
40 *
41 * \param self Parameter to destroy
42 */
44{
45 lwc_string_unref(self->name);
46 lwc_string_unref(self->value);
47 free(self);
48}
49
50/**
51 * Parse an HTTP parameter
52 *
53 * \param input Pointer to current input byte. Updated on exit.
54 * \param parameter Pointer to location to receive on-heap parameter.
55 * \return NSERROR_OK on success,
56 * NSERROR_NOMEM on memory exhaustion,
57 * NSERROR_NOT_FOUND if no parameter could be parsed
58 *
59 * The returned parameter is owned by the caller.
60 */
61nserror http__parse_parameter(const char **input, http_parameter **parameter)
62{
63 const char *pos = *input;
64 lwc_string *name;
65 lwc_string *value;
66 http_parameter *param;
67 nserror error;
68
69 /* token "=" ( token | quoted-string ) */
70
71 error = http__parse_token(&pos, &name);
72 if (error != NSERROR_OK)
73 return error;
74
75 http__skip_LWS(&pos);
76
77 if (*pos != '=') {
78 lwc_string_unref(name);
79 return NSERROR_NOT_FOUND;
80 }
81
82 pos++;
83
84 http__skip_LWS(&pos);
85
86 if (*pos == '"')
87 error = http__parse_quoted_string(&pos, &value);
88 else
89 error = http__parse_token(&pos, &value);
90
91 if (error != NSERROR_OK) {
92 lwc_string_unref(name);
93 return error;
94 }
95
96 param = malloc(sizeof(*param));
97 if (param == NULL) {
98 lwc_string_unref(value);
99 lwc_string_unref(name);
100 return NSERROR_NOMEM;
101 }
102
104 param->name = name;
105 param->value = value;
106
107 *parameter = param;
108 *input = pos;
109
110 return NSERROR_OK;
111}
112
113/* See parameter.h for documentation */
115 lwc_string *name, lwc_string **value)
116{
117 bool match;
118
119 while (list != NULL) {
120 if (lwc_string_caseless_isequal(name, list->name,
121 &match) == lwc_error_ok && match)
122 break;
123
124 list = (http_parameter *) list->base.next;
125 }
126
127 if (list == NULL)
128 return NSERROR_NOT_FOUND;
129
130 *value = lwc_string_ref(list->value);
131
132 return NSERROR_OK;
133}
134
135/* See parameter.h for documentation */
137 lwc_string **name, lwc_string **value)
138{
139 if (cur == NULL)
140 return NULL;
141
142 *name = lwc_string_ref(cur->name);
143 *value = lwc_string_ref(cur->value);
144
145 return (http_parameter *) cur->base.next;
146}
147
148/* See parameter.h for documentation */
150{
152}
153
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
#define http__item_list_destroy(l)
Definition: generics.h:46
#define HTTP__ITEM_INIT(item, n, f)
Definition: generics.h:35
HTTP header parsing functions.
void http_parameter_list_destroy(http_parameter *list)
Destroy a list of HTTP parameters.
Definition: parameter.c:149
const http_parameter * http_parameter_list_iterate(const http_parameter *cur, lwc_string **name, lwc_string **value)
Iterate over a parameter list.
Definition: parameter.c:136
nserror http_parameter_list_find_item(const http_parameter *list, lwc_string *name, lwc_string **value)
Find a named item in an HTTP parameter list.
Definition: parameter.c:114
static void http_destroy_parameter(http_parameter *self)
Destructor for an HTTP parameter.
Definition: parameter.c:43
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
nserror http__parse_quoted_string(const char **input, lwc_string **value)
Parse an HTTP quoted-string.
Definition: primitives.c:102
Interface to utility string handling.
Representation of an item.
Definition: generics.h:29
struct http__item * next
Next item in list, or NULL.
Definition: generics.h:30
Representation of an HTTP parameter.
Definition: parameter.c:31
lwc_string * name
Parameter name.
Definition: parameter.c:34
http__item base
Definition: parameter.c:32
lwc_string * value
Parameter value.
Definition: parameter.c:35