NetSurf
challenge.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
21#include "utils/http.h"
22
24#include "utils/http/generics.h"
27
28/**
29 * Representation of an HTTP challenge
30 */
33
34 lwc_string *scheme; /**< Challenge scheme */
35 http_parameter *params; /**< Challenge parameters */
36};
37
38/**
39 * Destroy an HTTP challenge
40 *
41 * \param self Challenge to destroy
42 */
44{
45 lwc_string_unref(self->scheme);
47 free(self);
48}
49
50/**
51 * Parse an HTTP challenge
52 *
53 * \param input Pointer to current input byte. Updated on exit.
54 * \param challenge Pointer to location to receive challenge
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 challenge is owned by the caller.
60 */
61nserror http__parse_challenge(const char **input, http_challenge **challenge)
62{
63 const char *pos = *input;
65 lwc_string *scheme;
66 http_parameter *first = NULL;
67 http_parameter *params = NULL;
68 nserror error;
69
70 /* challenge = auth-scheme 1*SP 1#auth-param
71 * auth-scheme = token
72 * auth-param = parameter
73 */
74
75 error = http__parse_token(&pos, &scheme);
76 if (error != NSERROR_OK)
77 return error;
78
79 if (*pos != ' ' && *pos != '\t') {
80 lwc_string_unref(scheme);
81 return NSERROR_NOT_FOUND;
82 }
83
84 http__skip_LWS(&pos);
85
86 error = http__parse_parameter(&pos, &first);
87 if (error != NSERROR_OK) {
88 lwc_string_unref(scheme);
89 return error;
90 }
91
92 http__skip_LWS(&pos);
93
94 if (*pos == ',') {
95 error = http__item_list_parse(&pos,
96 http__parse_parameter, first, &params);
97 if (error != NSERROR_OK && error != NSERROR_NOT_FOUND) {
98 lwc_string_unref(scheme);
99 return error;
100 }
101 } else {
102 params = first;
103 }
104
105 result = malloc(sizeof(*result));
106 if (result == NULL) {
108 lwc_string_unref(scheme);
109 return NSERROR_NOMEM;
110 }
111
113 result->scheme = scheme;
114 result->params = params;
115
116 *challenge = result;
117 *input = pos;
118
119 return NSERROR_OK;
120}
121
122/* See challenge.h for documentation */
124 lwc_string **scheme, http_parameter **parameters)
125{
126 if (cur == NULL)
127 return NULL;
128
129 *scheme = lwc_string_ref(cur->scheme);
130 *parameters = cur->params;
131
132 return (http_challenge *) cur->base.next;
133}
134
135/* See challenge.h for documentation */
137{
139}
140
STATIC char result[100]
Definition: arexx.c:77
static void http_destroy_challenge(http_challenge *self)
Destroy an HTTP challenge.
Definition: challenge.c:43
nserror http__parse_challenge(const char **input, http_challenge **challenge)
Parse an HTTP challenge.
Definition: challenge.c:61
const http_challenge * http_challenge_list_iterate(const http_challenge *cur, lwc_string **scheme, http_parameter **parameters)
Iterate over a challenge list.
Definition: challenge.c:123
void http_challenge_list_destroy(http_challenge *list)
Destroy a list of HTTP challenges.
Definition: challenge.c:136
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
#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
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 challenge.
Definition: challenge.c:31
http__item base
Definition: challenge.c:32
http_parameter * params
Challenge parameters.
Definition: challenge.c:35
lwc_string * scheme
Challenge scheme.
Definition: challenge.c:34
Representation of an HTTP parameter.
Definition: parameter.c:31