1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
/*
This file is part of libmicrohttpd
Copyright (C) 2007 Daniel Pittman and Christian Grothoff
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file microhttpd/internal.c
* @brief internal shared structures
* @author Daniel Pittman
* @author Christian Grothoff
*/
#include "internal.h"
#include "mhd_str.h"
#ifdef HAVE_MESSAGES
#if DEBUG_STATES
/**
* State to string dictionary.
*/
const char *
MHD_state_to_string (enum MHD_CONNECTION_STATE state)
{
switch (state)
{
case MHD_CONNECTION_INIT:
return "connection init";
case MHD_CONNECTION_URL_RECEIVED:
return "connection url received";
case MHD_CONNECTION_HEADER_PART_RECEIVED:
return "header partially received";
case MHD_CONNECTION_HEADERS_RECEIVED:
return "headers received";
case MHD_CONNECTION_HEADERS_PROCESSED:
return "headers processed";
case MHD_CONNECTION_CONTINUE_SENDING:
return "continue sending";
case MHD_CONNECTION_CONTINUE_SENT:
return "continue sent";
case MHD_CONNECTION_BODY_RECEIVED:
return "body received";
case MHD_CONNECTION_FOOTER_PART_RECEIVED:
return "footer partially received";
case MHD_CONNECTION_FOOTERS_RECEIVED:
return "footers received";
case MHD_CONNECTION_HEADERS_SENDING:
return "headers sending";
case MHD_CONNECTION_HEADERS_SENT:
return "headers sent";
case MHD_CONNECTION_NORMAL_BODY_READY:
return "normal body ready";
case MHD_CONNECTION_NORMAL_BODY_UNREADY:
return "normal body unready";
case MHD_CONNECTION_CHUNKED_BODY_READY:
return "chunked body ready";
case MHD_CONNECTION_CHUNKED_BODY_UNREADY:
return "chunked body unready";
case MHD_CONNECTION_BODY_SENT:
return "body sent";
case MHD_CONNECTION_FOOTERS_SENDING:
return "footers sending";
case MHD_CONNECTION_FOOTERS_SENT:
return "footers sent";
case MHD_CONNECTION_CLOSED:
return "closed";
default:
return "unrecognized connection state";
}
}
#endif
#endif
#ifdef HAVE_MESSAGES
/**
* fprintf-like helper function for logging debug
* messages.
*/
void
MHD_DLOG (const struct MHD_Daemon *daemon,
const char *format,
...)
{
va_list va;
if (0 == (daemon->options & MHD_USE_ERROR_LOG))
return;
va_start (va, format);
daemon->custom_error_log (daemon->custom_error_log_cls,
format,
va);
va_end (va);
}
#endif
/**
* Convert all occurrences of '+' to ' '.
*
* @param arg string that is modified (in place), must be 0-terminated
*/
void
MHD_unescape_plus (char *arg)
{
char *p;
for (p = strchr (arg, '+'); NULL != p; p = strchr (p + 1, '+'))
*p = ' ';
}
/**
* Process escape sequences ('%HH') Updates val in place; the
* result should be UTF-8 encoded and cannot be larger than the input.
* The result must also still be 0-terminated.
*
* @param val value to unescape (modified in the process)
* @return length of the resulting val (strlen(val) maybe
* shorter afterwards due to elimination of escape sequences)
*/
size_t
MHD_http_unescape (char *val)
{
char *rpos = val;
char *wpos = val;
while ('\0' != *rpos)
{
uint32_t num;
switch (*rpos)
{
case '%':
if (2 == MHD_strx_to_uint32_n_ (rpos + 1,
2,
&num))
{
*wpos = (char) ((unsigned char) num);
wpos++;
rpos += 3;
break;
}
/* TODO: add bad sequence handling */
/* intentional fall through! */
default:
*wpos = *rpos;
wpos++;
rpos++;
}
}
*wpos = '\0'; /* add 0-terminator */
return wpos - val;
}
/**
* Parse and unescape the arguments given by the client
* as part of the HTTP request URI.
*
* @param kind header kind to pass to @a cb
* @param connection connection to add headers to
* @param[in,out] args argument URI string (after "?" in URI),
* clobbered in the process!
* @param cb function to call on each key-value pair found
* @param[out] num_headers set to the number of headers found
* @return #MHD_NO on failure (@a cb returned #MHD_NO),
* #MHD_YES for success (parsing succeeded, @a cb always
* returned #MHD_YES)
*/
enum MHD_Result
MHD_parse_arguments_ (struct MHD_Connection *connection,
enum MHD_ValueKind kind,
char *args,
MHD_ArgumentIterator_ cb,
unsigned int *num_headers)
{
struct MHD_Daemon *daemon = connection->daemon;
char *equals;
char *amper;
*num_headers = 0;
while ( (NULL != args) &&
('\0' != args[0]) )
{
size_t key_len;
size_t value_len;
equals = strchr (args, '=');
amper = strchr (args, '&');
if (NULL == amper)
{
/* last argument */
if (NULL == equals)
{
/* last argument, without '=' */
MHD_unescape_plus (args);
key_len = daemon->unescape_callback (daemon->unescape_callback_cls,
connection,
args);
if (MHD_NO == cb (connection,
args,
key_len,
NULL,
0,
kind))
return MHD_NO;
(*num_headers)++;
break;
}
/* got 'foo=bar' */
equals[0] = '\0';
equals++;
MHD_unescape_plus (args);
key_len = daemon->unescape_callback (daemon->unescape_callback_cls,
connection,
args);
MHD_unescape_plus (equals);
value_len = daemon->unescape_callback (daemon->unescape_callback_cls,
connection,
equals);
if (MHD_NO == cb (connection,
args,
key_len,
equals,
value_len,
kind))
return MHD_NO;
(*num_headers)++;
break;
}
/* amper is non-NULL here */
amper[0] = '\0';
amper++;
if ( (NULL == equals) ||
(equals >= amper) )
{
/* got 'foo&bar' or 'foo&bar=val', add key 'foo' with NULL for value */
MHD_unescape_plus (args);
key_len = daemon->unescape_callback (daemon->unescape_callback_cls,
connection,
args);
if (MHD_NO == cb (connection,
args,
key_len,
NULL,
0,
kind))
return MHD_NO;
/* continue with 'bar' */
(*num_headers)++;
args = amper;
continue;
}
/* equals and amper are non-NULL here, and equals < amper,
so we got regular 'foo=value&bar...'-kind of argument */
equals[0] = '\0';
equals++;
MHD_unescape_plus (args);
key_len = daemon->unescape_callback (daemon->unescape_callback_cls,
connection,
args);
MHD_unescape_plus (equals);
value_len = daemon->unescape_callback (daemon->unescape_callback_cls,
connection,
equals);
if (MHD_NO == cb (connection,
args,
key_len,
equals,
value_len,
kind))
return MHD_NO;
(*num_headers)++;
args = amper;
}
return MHD_YES;
}
/* end of internal.c */
|