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