libUPnP  1.6.17
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 
40 #include "LinkedList.h"
41 #include "membuffer.h"
42 #include "uri.h"
43 #include "upnputil.h"
44 
45 /* private types */
46 
47 /* scanner */
48 
49 /* Used to represent different types of tokens in input. */
50 typedef enum
51 {
52  TT_IDENTIFIER,
53  TT_WHITESPACE,
54  TT_CRLF,
55  TT_CTRL,
56  TT_SEPARATOR,
57  TT_QUOTEDSTRING,
58 } token_type_t;
59 
60 typedef struct
61 {
65  size_t cursor;
69 } scanner_t;
70 
71 typedef enum
72 {
73  POS_REQUEST_LINE,
74  POS_RESPONSE_LINE,
75  POS_HEADERS,
76  POS_ENTITY,
77  POS_COMPLETE,
78 } parser_pos_t;
79 
80 #define ENTREAD_DETERMINE_READ_METHOD 1
81 #define ENTREAD_USING_CLEN 2
82 #define ENTREAD_USING_CHUNKED 3
83 #define ENTREAD_UNTIL_CLOSE 4
84 #define ENTREAD_CHUNKY_BODY 5
85 #define ENTREAD_CHUNKY_HEADERS 6
86 
87 /* end of private section. */
88 
89 /* method in a HTTP request. */
90 typedef enum
91 {
92  HTTPMETHOD_POST,
93  HTTPMETHOD_MPOST,
94  HTTPMETHOD_SUBSCRIBE,
95  HTTPMETHOD_UNSUBSCRIBE,
96  HTTPMETHOD_NOTIFY,
97  HTTPMETHOD_GET,
98  HTTPMETHOD_HEAD,
99  HTTPMETHOD_MSEARCH,
100  HTTPMETHOD_UNKNOWN,
101  SOAPMETHOD_POST,
102  HTTPMETHOD_SIMPLEGET
103 } http_method_t;
104 
105 /* different types of HTTP headers */
106 #define HDR_UNKNOWN -1
107 #define HDR_CACHE_CONTROL 1
108 #define HDR_CALLBACK 2
109 #define HDR_CONTENT_LENGTH 3
110 #define HDR_CONTENT_TYPE 4
111 #define HDR_DATE 5
112 #define HDR_EXT 6
113 #define HDR_HOST 7
114 /*define HDR_IF_MODIFIED_SINCE 8 */
115 /*define HDR_IF_UNMODIFIED_SINCE 9 */
116 /*define HDR_LAST_MODIFIED 10 */
117 #define HDR_LOCATION 11
118 #define HDR_MAN 12
119 #define HDR_MX 13
120 #define HDR_NT 14
121 #define HDR_NTS 15
122 #define HDR_SERVER 16
123 #define HDR_SEQ 17
124 #define HDR_SID 18
125 #define HDR_SOAPACTION 19
126 #define HDR_ST 20
127 #define HDR_TIMEOUT 21
128 #define HDR_TRANSFER_ENCODING 22
129 #define HDR_USN 23
130 #define HDR_USER_AGENT 24
131 
132 /* Adding new header difinition */
133 #define HDR_ACCEPT 25
134 #define HDR_ACCEPT_ENCODING 26
135 #define HDR_ACCEPT_CHARSET 27
136 #define HDR_ACCEPT_LANGUAGE 28
137 #define HDR_ACCEPT_RANGE 29
138 #define HDR_CONTENT_ENCODING 30
139 #define HDR_CONTENT_LANGUAGE 31
140 #define HDR_CONTENT_LOCATION 32
141 #define HDR_CONTENT_RANGE 33
142 #define HDR_IF_RANGE 34
143 #define HDR_RANGE 35
144 #define HDR_TE 36
145 
147 typedef enum {
163 
164 typedef struct {
168  int name_id;
171  /* private. */
172  membuffer name_buf;
173 } http_header_t;
174 
175 typedef struct {
176  int initialized;
178  http_method_t method;
182  http_method_t request_method;
191  /* fields used in both request or response messages. */
194  /* http major version. */
195  int major_version;
196  /* http minor version. */
197  int minor_version;
202  /* private fields. */
206  char *urlbuf;
208 
209 typedef struct {
210  http_message_t msg;
217  /* private data -- don't touch. */
218  parser_pos_t position;
219  int ent_position;
220  unsigned int content_length;
221  size_t chunk_size;
225  scanner_t scanner;
226 } http_parser_t;
227 
228 #ifdef __cplusplus
229 extern "C" {
230 #endif /* __cplusplus */
231 
232 /************************************************************************
233 * Function : httpmsg_init
234 *
235 * Parameters :
236 * INOUT http_message_t* msg ; HTTP Message Object
237 *
238 * Description : Initialize and allocate memory for http message
239 *
240 * Return : void ;
241 *
242 * Note :
243 ************************************************************************/
244 void httpmsg_init( INOUT http_message_t* msg );
245 
246 /************************************************************************
247 * Function : httpmsg_destroy
248 *
249 * Parameters :
250 * INOUT http_message_t* msg ; HTTP Message Object
251 *
252 * Description : Free memory allocated for the http message
253 *
254 * Return : void ;
255 *
256 * Note :
257 ************************************************************************/
258 void httpmsg_destroy( INOUT http_message_t* msg );
259 
260 /************************************************************************
261 * Function : httpmsg_find_hdr_str
262 *
263 * Parameters :
264 * IN http_message_t* msg ; HTTP Message Object
265 * IN const char* header_name ; Header name to be compared with
266 *
267 * Description : Compares the header name with the header names stored
268 * in the linked list of messages
269 *
270 * Return : http_header_t* - Pointer to a header on success;
271 * NULL on failure
272 * Note :
273 ************************************************************************/
274 http_header_t* httpmsg_find_hdr_str( IN http_message_t* msg,
275  IN const char* header_name );
276 
277 /************************************************************************
278 * Function : httpmsg_find_hdr
279 *
280 * Parameters :
281 * IN http_message_t* msg ; HTTP Message Object
282 * IN int header_name_id ; Header Name ID to be compared with
283 * OUT memptr* value ; Buffer to get the ouput to.
284 *
285 * Description : Finds header from a list, with the given 'name_id'.
286 *
287 * Return : http_header_t* - Pointer to a header on success;
288 * NULL on failure
289 *
290 * Note :
291 ************************************************************************/
292 http_header_t* httpmsg_find_hdr( IN http_message_t* msg,
293  IN int header_name_id, OUT memptr* value );
294 
295 /************************************************************************
296 * Function: parser_request_init
297 *
298 * Parameters:
299 * OUT http_parser_t* parser ; HTTP Parser object
300 *
301 * Description: Initializes parser object for a request
302 *
303 * Returns:
304 * void
305 ************************************************************************/
306 void parser_request_init( OUT http_parser_t* parser );
307 
308 /************************************************************************
309 * Function: parser_response_init
310 *
311 * Parameters:
312 * OUT http_parser_t* parser ; HTTP Parser object
313 * IN http_method_t request_method ; Request method
314 *
315 * Description: Initializes parser object for a response
316 *
317 * Returns:
318 * void
319 ************************************************************************/
320 void parser_response_init( OUT http_parser_t* parser,
321  IN http_method_t request_method );
322 
323 /************************************************************************
324 * Function: parser_parse
325 *
326 * Parameters:
327 * INOUT http_parser_t* parser ; HTTP Parser object
328 *
329 * Description: The parser function. Depending on the position of the
330 * parser object the actual parsing function is invoked
331 *
332 * Returns:
333 * void
334 ************************************************************************/
335 parse_status_t parser_parse(INOUT http_parser_t * parser);
336 
337 /************************************************************************
338 * Function: parser_parse_responseline
339 *
340 * Parameters:
341 * INOUT http_parser_t* parser ; HTTP Parser object
342 *
343 * Description: Get HTTP Method, URL location and version information.
344 *
345 * Returns:
346 * PARSE_OK
347 * PARSE_SUCCESS
348 * PARSE_FAILURE
349 ************************************************************************/
350 parse_status_t parser_parse_responseline(INOUT http_parser_t *parser);
351 
352 /************************************************************************
353 * Function: parser_parse_headers
354 *
355 * Parameters:
356 * INOUT http_parser_t* parser ; HTTP Parser object
357 *
358 * Description: Get HTTP Method, URL location and version information.
359 *
360 * Returns:
361 * PARSE_OK
362 * PARSE_SUCCESS
363 * PARSE_FAILURE
364 ************************************************************************/
365 parse_status_t parser_parse_headers(INOUT http_parser_t *parser);
366 
367 /************************************************************************
368 * Function: parser_parse_entity
369 *
370 * Parameters:
371 * INOUT http_parser_t* parser ; HTTP Parser object
372 *
373 * Description: Determines method to read entity
374 *
375 * Returns:
376 * PARSE_OK
377 * PARSE_FAILURE
378 * PARSE_COMPLETE -- no more reading to do
379 ************************************************************************/
380 parse_status_t parser_parse_entity(INOUT http_parser_t *parser);
381 
382 /************************************************************************
383 * Function: parser_get_entity_read_method
384 *
385 * Parameters:
386 * INOUT http_parser_t* parser ; HTTP Parser object
387 *
388 * Description: Determines method to read entity
389 *
390 * Returns:
391 * PARSE_OK
392 * PARSE_FAILURE
393 * PARSE_COMPLETE -- no more reading to do
394 ************************************************************************/
395 parse_status_t parser_get_entity_read_method( INOUT http_parser_t* parser );
396 
397 /************************************************************************
398 * Function: parser_append
399 *
400 * Parameters:
401 * INOUT http_parser_t* parser ; HTTP Parser Object
402 * IN const char* buf ; buffer to be appended to the parser
403 * buffer
404 * IN size_t buf_length ; Size of the buffer
405 *
406 * Description: The parser function. Depending on the position of the
407 * parser object the actual parsing function is invoked
408 *
409 * Returns:
410 * void
411 ************************************************************************/
412 parse_status_t parser_append( INOUT http_parser_t* parser,
413  IN const char* buf,
414  IN size_t buf_length );
415 
416 /************************************************************************
417 * Function: matchstr
418 *
419 * Parameters:
420 * IN char *str ; String to be matched
421 * IN size_t slen ; Length of the string
422 * IN const char* fmt ; Pattern format
423 * ...
424 *
425 * Description: Matches a variable parameter list with a string
426 * and takes actions based on the data type specified.
427 *
428 * Returns:
429 * PARSE_OK
430 * PARSE_NO_MATCH -- failure to match pattern 'fmt'
431 * PARSE_FAILURE -- 'str' is bad input
432 ************************************************************************/
433 parse_status_t matchstr( IN char *str, IN size_t slen, IN const char* fmt, ... );
434 
435 /************************************************************************
436 * Function: raw_to_int
437 *
438 * Parameters:
439 * IN memptr* raw_value ; Buffer to be converted
440 * IN int base ; Base to use for conversion
441 *
442 * Description: Converts raw character data to long-integer value
443 *
444 * Returns:
445 * int
446 ************************************************************************/
447 int raw_to_int( IN memptr* raw_value, int base );
448 
449 /************************************************************************
450 * Function: raw_find_str
451 *
452 * Parameters:
453 * IN memptr* raw_value ; Buffer containg the string
454 * IN const char* str ; Substring to be found
455 *
456 * Description: Find a substring from raw character string buffer
457 *
458 * Side effects: raw_value is transformed to lowercase.
459 *
460 * Returns:
461 * int - index at which the substring is found.
462 ************************************************************************/
463 int raw_find_str( IN memptr* raw_value, IN const char* str );
464 
465 /************************************************************************
466 * Function: method_to_str
467 *
468 * Parameters:
469 * IN http_method_t method ; HTTP method
470 *
471 * Description: A wrapper function that maps a method id to a method
472 * nameConverts a http_method id stored in the HTTP Method
473 *
474 * Returns:
475 * const char* ptr - Ptr to the HTTP Method
476 ************************************************************************/
477 const char* method_to_str( IN http_method_t method );
478 
479 #ifdef __cplusplus
480 } /* extern "C" */
481 #endif /* __cplusplus */
482 
483 #endif /* GENLIB_NET_HTTP_HTTPPARSER_H */
484 
parse_status_t
Definition: httpparser.h:147
int entire_msg_loaded
Definition: httpparser.h:68
int http_error_code
Definition: httpparser.h:213
Definition: httpparser.h:175
Definition: httpparser.h:153
Definition: httpparser.h:164
uri_type uri
Definition: httpparser.h:180
membuffer * msg
Definition: httpparser.h:63
memptr entity
Definition: httpparser.h:201
Represents a URI used in parse_uri and elsewhere.
Definition: uri.h:136
size_t amount_discarded
Definition: httpparser.h:190
membuffer value
Definition: httpparser.h:170
Definition: httpparser.h:161
Definition: httpparser.h:149
Definition: httpparser.h:159
Definition: httpparser.h:151
Definition: httpparser.h:60
membuffer status_msg
Definition: httpparser.h:186
size_t entity_start_position
Definition: httpparser.h:224
http_method_t method
Definition: httpparser.h:178
int is_request
Definition: httpparser.h:193
size_t cursor
Definition: httpparser.h:65
Definition: httpparser.h:209
char * urlbuf
Definition: httpparser.h:206
membuffer msg
Definition: httpparser.h:204
http_method_t request_method
Definition: httpparser.h:182
Definition: httpparser.h:155
Definition: membuffer.h:56
Definition: membuffer.h:47
LinkedList headers
Definition: httpparser.h:199
Definition: LinkedList.h:83
int valid_ssdp_notify_hack
Definition: httpparser.h:216
Definition: httpparser.h:157
int name_id
Definition: httpparser.h:168
int status_code
Definition: httpparser.h:184
memptr name
Definition: httpparser.h:166