libUPnP 1.14.31
httpparser.h
Go to the documentation of this file.
1/*******************************************************************************
2 *
3 * Copyright (c) 2000-2003 Intel Corporation
4 * All rights reserved.
5 * Copyright (c) 2012 France Telecom All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * - Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 * - Neither name of Intel Corporation nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 ******************************************************************************/
32
33#ifndef GENLIB_NET_HTTP_HTTPPARSER_H
34#define GENLIB_NET_HTTP_HTTPPARSER_H
35
39
40#include "LinkedList.h"
41#include "membuffer.h"
42#include "uri.h"
43
44/* private types */
45
46/* scanner */
47
48/* Used to represent different types of tokens in input. */
49typedef enum
50{
51 TT_IDENTIFIER,
52 TT_WHITESPACE,
53 TT_CRLF,
54 TT_CTRL,
55 TT_SEPARATOR,
56 TT_QUOTEDSTRING
57} token_type_t;
58
59typedef struct
60{
64 size_t cursor;
68} scanner_t;
69
70typedef enum
71{
72 POS_REQUEST_LINE,
73 POS_RESPONSE_LINE,
74 POS_HEADERS,
75 POS_ENTITY,
76 POS_COMPLETE
77} parser_pos_t;
78
79#define ENTREAD_DETERMINE_READ_METHOD 1
80#define ENTREAD_USING_CLEN 2
81#define ENTREAD_USING_CHUNKED 3
82#define ENTREAD_UNTIL_CLOSE 4
83#define ENTREAD_CHUNKY_BODY 5
84#define ENTREAD_CHUNKY_HEADERS 6
85
86/* end of private section. */
87
88/* method in a HTTP request.
89 * IMPORTANT: The enum values of the standard HTTP method should match
90 * those of Upnp_HttpMethod enum defined in upnp.h */
91typedef enum
92{
93 HTTPMETHOD_PUT = UPNP_HTTPMETHOD_PUT,
94 HTTPMETHOD_DELETE = UPNP_HTTPMETHOD_DELETE,
95 HTTPMETHOD_GET = UPNP_HTTPMETHOD_GET,
96 HTTPMETHOD_HEAD = UPNP_HTTPMETHOD_HEAD,
97 HTTPMETHOD_POST = UPNP_HTTPMETHOD_POST,
98 HTTPMETHOD_MPOST,
99 HTTPMETHOD_SUBSCRIBE,
100 HTTPMETHOD_UNSUBSCRIBE,
101 HTTPMETHOD_NOTIFY,
102 HTTPMETHOD_MSEARCH,
103 HTTPMETHOD_UNKNOWN,
104 SOAPMETHOD_POST,
105 HTTPMETHOD_SIMPLEGET
106} http_method_t;
107
108/* different types of HTTP headers */
109#define HDR_UNKNOWN -1
110#define HDR_CACHE_CONTROL 1
111#define HDR_CALLBACK 2
112#define HDR_CONTENT_LENGTH 3
113#define HDR_CONTENT_TYPE 4
114#define HDR_DATE 5
115#define HDR_EXT 6
116#define HDR_HOST 7
117/*define HDR_IF_MODIFIED_SINCE 8 */
118/*define HDR_IF_UNMODIFIED_SINCE 9 */
119/*define HDR_LAST_MODIFIED 10 */
120#define HDR_LOCATION 11
121#define HDR_MAN 12
122#define HDR_MX 13
123#define HDR_NT 14
124#define HDR_NTS 15
125#define HDR_SERVER 16
126#define HDR_SEQ 17
127#define HDR_SID 18
128#define HDR_SOAPACTION 19
129#define HDR_ST 20
130#define HDR_TIMEOUT 21
131#define HDR_TRANSFER_ENCODING 22
132#define HDR_USN 23
133#define HDR_USER_AGENT 24
134
135/* Adding new header definition */
136#define HDR_ACCEPT 25
137#define HDR_ACCEPT_ENCODING 26
138#define HDR_ACCEPT_CHARSET 27
139#define HDR_ACCEPT_LANGUAGE 28
140#define HDR_ACCEPT_RANGE 29
141#define HDR_CONTENT_ENCODING 30
142#define HDR_CONTENT_LANGUAGE 31
143#define HDR_CONTENT_LOCATION 32
144#define HDR_CONTENT_RANGE 33
145#define HDR_IF_RANGE 34
146#define HDR_RANGE 35
147#define HDR_TE 36
148
167
168typedef struct
169{
176 /* private. */
177 membuffer name_buf;
179
180typedef struct
181{
182 int initialized;
184 http_method_t method;
188 http_method_t request_method;
197 /* fields used in both request or response messages. */
200 /* http major version. */
201 int major_version;
202 /* http minor version. */
203 int minor_version;
208 /* private fields. */
212 char *url_buf;
214
215typedef struct
216{
217 http_message_t msg;
224 /* private data -- don't touch. */
225 parser_pos_t position;
226 int ent_position;
227 unsigned int content_length;
228 size_t chunk_size;
232 scanner_t scanner;
234
235#ifdef __cplusplus
236extern "C" {
237#endif /* __cplusplus */
238
239/************************************************************************
240 * Function : httpmsg_init
241 *
242 * Parameters :
243 * INOUT http_message_t* msg ; HTTP Message Object
244 *
245 * Description : Initialize and allocate memory for http message
246 *
247 * Return : void ;
248 *
249 * Note :
250 ************************************************************************/
251void httpmsg_init(http_message_t *msg);
252
253/************************************************************************
254 * Function : httpmsg_destroy
255 *
256 * Parameters :
257 * INOUT http_message_t* msg ; HTTP Message Object
258 *
259 * Description : Free memory allocated for the http message
260 *
261 * Return : void ;
262 *
263 * Note :
264 ************************************************************************/
265void httpmsg_destroy(http_message_t *msg);
266
267/************************************************************************
268 * Function : httpmsg_find_hdr_str
269 *
270 * Parameters :
271 * IN http_message_t* msg ; HTTP Message Object
272 * IN const char* header_name ; Header name to be compared with
273 *
274 * Description : Compares the header name with the header names stored
275 * in the linked list of messages
276 *
277 * Return : http_header_t* - Pointer to a header on success;
278 * NULL on failure
279 * Note :
280 ************************************************************************/
281http_header_t *httpmsg_find_hdr_str(
282 http_message_t *msg, const char *header_name);
283
284/************************************************************************
285 * Function : httpmsg_find_hdr
286 *
287 * Parameters :
288 * IN http_message_t* msg ; HTTP Message Object
289 * IN int header_name_id ; Header Name ID to be compared with
290 * OUT memptr* value ; Buffer to get the ouput to.
291 *
292 * Description : Finds header from a list, with the given 'name_id'.
293 *
294 * Return : http_header_t* - Pointer to a header on success;
295 * NULL on failure
296 *
297 * Note :
298 ************************************************************************/
299http_header_t *httpmsg_find_hdr(
300 http_message_t *msg, int header_name_id, memptr *value);
301
302/************************************************************************
303 * Function: parser_request_init
304 *
305 * Parameters:
306 * OUT http_parser_t* parser ; HTTP Parser object
307 *
308 * Description: Initializes parser object for a request
309 *
310 * Returns:
311 * void
312 ************************************************************************/
313void parser_request_init(http_parser_t *parser);
314
315/************************************************************************
316 * Function: parser_response_init
317 *
318 * Parameters:
319 * OUT http_parser_t* parser ; HTTP Parser object
320 * IN http_method_t request_method ; Request method
321 *
322 * Description: Initializes parser object for a response
323 *
324 * Returns:
325 * void
326 ************************************************************************/
327void parser_response_init(http_parser_t *parser, http_method_t request_method);
328
329/************************************************************************
330 * Function: parser_parse
331 *
332 * Parameters:
333 * INOUT http_parser_t* parser ; HTTP Parser object
334 *
335 * Description: The parser function. Depending on the position of the
336 * parser object the actual parsing function is invoked
337 *
338 * Returns:
339 * void
340 ************************************************************************/
341parse_status_t parser_parse(http_parser_t *parser);
342
343/************************************************************************
344 * Function: parser_parse_responseline
345 *
346 * Parameters:
347 * INOUT http_parser_t* parser ; HTTP Parser object
348 *
349 * Description: Get HTTP Method, URL location and version information.
350 *
351 * Returns:
352 * PARSE_OK
353 * PARSE_SUCCESS
354 * PARSE_FAILURE
355 ************************************************************************/
356parse_status_t parser_parse_responseline(http_parser_t *parser);
357
358/************************************************************************
359 * Function: parser_parse_headers
360 *
361 * Parameters:
362 * INOUT http_parser_t* parser ; HTTP Parser object
363 *
364 * Description: Get HTTP Method, URL location and version information.
365 *
366 * Returns:
367 * PARSE_OK
368 * PARSE_SUCCESS
369 * PARSE_FAILURE
370 ************************************************************************/
371parse_status_t parser_parse_headers(http_parser_t *parser);
372
373/************************************************************************
374 * Function: parser_parse_entity
375 *
376 * Parameters:
377 * INOUT http_parser_t* parser ; HTTP Parser object
378 *
379 * Description: Determines method to read entity
380 *
381 * Returns:
382 * PARSE_OK
383 * PARSE_FAILURE
384 * PARSE_COMPLETE -- no more reading to do
385 ************************************************************************/
386parse_status_t parser_parse_entity(http_parser_t *parser);
387
388/************************************************************************
389 * Function: parser_get_entity_read_method
390 *
391 * Parameters:
392 * INOUT http_parser_t* parser ; HTTP Parser object
393 *
394 * Description: Determines method to read entity
395 *
396 * Returns:
397 * PARSE_OK
398 * PARSE_FAILURE
399 * PARSE_COMPLETE -- no more reading to do
400 ************************************************************************/
401parse_status_t parser_get_entity_read_method(http_parser_t *parser);
402
403/************************************************************************
404 * Function: parser_append
405 *
406 * Parameters:
407 * INOUT http_parser_t* parser ; HTTP Parser Object
408 * IN const char* buf ; buffer to be appended to the parser
409 * buffer
410 * IN size_t buf_length ; Size of the buffer
411 *
412 * Description: The parser function. Depending on the position of the
413 * parser object the actual parsing function is invoked
414 *
415 * Returns:
416 * void
417 ************************************************************************/
418parse_status_t parser_append(
419 http_parser_t *parser, const char *buf, size_t buf_length);
420
421/************************************************************************
422 * Function: matchstr
423 *
424 * Parameters:
425 * IN char *str ; String to be matched
426 * IN size_t slen ; Length of the string
427 * IN const char* fmt ; Pattern format
428 * ...
429 *
430 * Description: Matches a variable parameter list with a string
431 * and takes actions based on the data type specified.
432 *
433 * Returns:
434 * PARSE_OK
435 * PARSE_NO_MATCH -- failure to match pattern 'fmt'
436 * PARSE_FAILURE -- 'str' is bad input
437 ************************************************************************/
438parse_status_t matchstr(char *str, size_t slen, const char *fmt, ...);
439
440/************************************************************************
441 * Function: raw_to_int
442 *
443 * Parameters:
444 * IN memptr* raw_value ; Buffer to be converted
445 * IN int base ; Base to use for conversion
446 *
447 * Description: Converts raw character data to long-integer value
448 *
449 * Returns:
450 * int
451 ************************************************************************/
452int raw_to_int(memptr *raw_value, int base);
453
454/************************************************************************
455 * Function: raw_find_str
456 *
457 * Parameters:
458 * IN memptr* raw_value ; Buffer containing the string
459 * IN const char* str ; Substring to be found
460 *
461 * Description: Find a substring from raw character string buffer
462 *
463 * Side effects: raw_value is transformed to lowercase.
464 *
465 * Returns:
466 * int - index at which the substring is found.
467 ************************************************************************/
468int raw_find_str(memptr *raw_value, const char *str);
469
470/************************************************************************
471 * Function: method_to_str
472 *
473 * Parameters:
474 * IN http_method_t method ; HTTP method
475 *
476 * Description: A wrapper function that maps a method id to a method
477 * nameConverts a http_method id stored in the HTTP Method
478 *
479 * Returns:
480 * const char* ptr - Ptr to the HTTP Method
481 ************************************************************************/
482const char *method_to_str(http_method_t method);
483
487
488#ifdef DEBUG
491 http_message_t *h_msg);
492#else
493 #define print_http_headers(h_msg) \
494 do { \
495 } while (0)
496#endif
497
498#ifdef __cplusplus
499} /* extern "C" */
500#endif /* __cplusplus */
501
502#endif /* GENLIB_NET_HTTP_HTTPPARSER_H */
struct LINKEDLIST LinkedList
void print_http_headers(http_message_t *h_msg)
Print the HTTP headers.
Definition httpparser.c:2223
parse_status_t
Definition httpparser.h:151
@ PARSE_INCOMPLETE
Definition httpparser.h:155
@ PARSE_NO_MATCH
Definition httpparser.h:163
@ PARSE_FAILURE
Definition httpparser.h:159
@ PARSE_INCOMPLETE_ENTITY
Definition httpparser.h:157
@ PARSE_SUCCESS
Definition httpparser.h:153
@ PARSE_OK
Definition httpparser.h:161
@ PARSE_CONTINUE_1
Definition httpparser.h:165
Definition httpparser.h:169
memptr name
Definition httpparser.h:171
membuffer value
Definition httpparser.h:175
int name_id
Definition httpparser.h:173
Definition httpparser.h:181
uri_type uri
Definition httpparser.h:186
http_method_t method
Definition httpparser.h:184
int is_request
Definition httpparser.h:199
membuffer status_msg
Definition httpparser.h:192
memptr entity
Definition httpparser.h:207
size_t amount_discarded
Definition httpparser.h:196
int status_code
Definition httpparser.h:190
membuffer msg
Definition httpparser.h:210
http_method_t request_method
Definition httpparser.h:188
LinkedList headers
Definition httpparser.h:205
char * url_buf
Definition httpparser.h:212
Definition httpparser.h:216
int http_error_code
Definition httpparser.h:220
int valid_ssdp_notify_hack
Definition httpparser.h:223
size_t entity_start_position
Definition httpparser.h:231
Definition membuffer.h:58
Definition membuffer.h:48
Definition httpparser.h:60
int entire_msg_loaded
Definition httpparser.h:67
membuffer * msg
Definition httpparser.h:62
size_t cursor
Definition httpparser.h:64
struct URI uri_type
Represents a URI used in parse_uri and elsewhere.