libUPnP  1.14.24
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 "upnputil.h"
43 #include "uri.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 = UPNP_HTTPMETHOD_PUT,
95  HTTPMETHOD_DELETE = UPNP_HTTPMETHOD_DELETE,
96  HTTPMETHOD_GET = UPNP_HTTPMETHOD_GET,
97  HTTPMETHOD_HEAD = UPNP_HTTPMETHOD_HEAD,
98  HTTPMETHOD_POST = UPNP_HTTPMETHOD_POST,
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
152 {
168 
169 typedef struct
170 {
174  int name_id;
177  /* private. */
178  membuffer name_buf;
179 } http_header_t;
180 
181 typedef struct
182 {
183  int initialized;
185  http_method_t method;
189  http_method_t request_method;
198  /* fields used in both request or response messages. */
201  /* http major version. */
202  int major_version;
203  /* http minor version. */
204  int minor_version;
209  /* private fields. */
213  char *urlbuf;
215 
216 typedef struct
217 {
218  http_message_t msg;
225  /* private data -- don't touch. */
226  parser_pos_t position;
227  int ent_position;
228  unsigned int content_length;
229  size_t chunk_size;
233  scanner_t scanner;
234 } http_parser_t;
235 
236 #ifdef __cplusplus
237 extern "C" {
238 #endif /* __cplusplus */
239 
240 /************************************************************************
241  * Function : httpmsg_init
242  *
243  * Parameters :
244  * INOUT http_message_t* msg ; HTTP Message Object
245  *
246  * Description : Initialize and allocate memory for http message
247  *
248  * Return : void ;
249  *
250  * Note :
251  ************************************************************************/
252 void httpmsg_init(http_message_t *msg);
253 
254 /************************************************************************
255  * Function : httpmsg_destroy
256  *
257  * Parameters :
258  * INOUT http_message_t* msg ; HTTP Message Object
259  *
260  * Description : Free memory allocated for the http message
261  *
262  * Return : void ;
263  *
264  * Note :
265  ************************************************************************/
266 void httpmsg_destroy(http_message_t *msg);
267 
268 /************************************************************************
269  * Function : httpmsg_find_hdr_str
270  *
271  * Parameters :
272  * IN http_message_t* msg ; HTTP Message Object
273  * IN const char* header_name ; Header name to be compared with
274  *
275  * Description : Compares the header name with the header names stored
276  * in the linked list of messages
277  *
278  * Return : http_header_t* - Pointer to a header on success;
279  * NULL on failure
280  * Note :
281  ************************************************************************/
282 http_header_t *httpmsg_find_hdr_str(
283  http_message_t *msg, const char *header_name);
284 
285 /************************************************************************
286  * Function : httpmsg_find_hdr
287  *
288  * Parameters :
289  * IN http_message_t* msg ; HTTP Message Object
290  * IN int header_name_id ; Header Name ID to be compared with
291  * OUT memptr* value ; Buffer to get the ouput to.
292  *
293  * Description : Finds header from a list, with the given 'name_id'.
294  *
295  * Return : http_header_t* - Pointer to a header on success;
296  * NULL on failure
297  *
298  * Note :
299  ************************************************************************/
300 http_header_t *httpmsg_find_hdr(
301  http_message_t *msg, int header_name_id, memptr *value);
302 
303 /************************************************************************
304  * Function: parser_request_init
305  *
306  * Parameters:
307  * OUT http_parser_t* parser ; HTTP Parser object
308  *
309  * Description: Initializes parser object for a request
310  *
311  * Returns:
312  * void
313  ************************************************************************/
314 void parser_request_init(http_parser_t *parser);
315 
316 /************************************************************************
317  * Function: parser_response_init
318  *
319  * Parameters:
320  * OUT http_parser_t* parser ; HTTP Parser object
321  * IN http_method_t request_method ; Request method
322  *
323  * Description: Initializes parser object for a response
324  *
325  * Returns:
326  * void
327  ************************************************************************/
328 void parser_response_init(http_parser_t *parser, http_method_t request_method);
329 
330 /************************************************************************
331  * Function: parser_parse
332  *
333  * Parameters:
334  * INOUT http_parser_t* parser ; HTTP Parser object
335  *
336  * Description: The parser function. Depending on the position of the
337  * parser object the actual parsing function is invoked
338  *
339  * Returns:
340  * void
341  ************************************************************************/
342 parse_status_t parser_parse(http_parser_t *parser);
343 
344 /************************************************************************
345  * Function: parser_parse_responseline
346  *
347  * Parameters:
348  * INOUT http_parser_t* parser ; HTTP Parser object
349  *
350  * Description: Get HTTP Method, URL location and version information.
351  *
352  * Returns:
353  * PARSE_OK
354  * PARSE_SUCCESS
355  * PARSE_FAILURE
356  ************************************************************************/
357 parse_status_t parser_parse_responseline(http_parser_t *parser);
358 
359 /************************************************************************
360  * Function: parser_parse_headers
361  *
362  * Parameters:
363  * INOUT http_parser_t* parser ; HTTP Parser object
364  *
365  * Description: Get HTTP Method, URL location and version information.
366  *
367  * Returns:
368  * PARSE_OK
369  * PARSE_SUCCESS
370  * PARSE_FAILURE
371  ************************************************************************/
372 parse_status_t parser_parse_headers(http_parser_t *parser);
373 
374 /************************************************************************
375  * Function: parser_parse_entity
376  *
377  * Parameters:
378  * INOUT http_parser_t* parser ; HTTP Parser object
379  *
380  * Description: Determines method to read entity
381  *
382  * Returns:
383  * PARSE_OK
384  * PARSE_FAILURE
385  * PARSE_COMPLETE -- no more reading to do
386  ************************************************************************/
387 parse_status_t parser_parse_entity(http_parser_t *parser);
388 
389 /************************************************************************
390  * Function: parser_get_entity_read_method
391  *
392  * Parameters:
393  * INOUT http_parser_t* parser ; HTTP Parser object
394  *
395  * Description: Determines method to read entity
396  *
397  * Returns:
398  * PARSE_OK
399  * PARSE_FAILURE
400  * PARSE_COMPLETE -- no more reading to do
401  ************************************************************************/
402 parse_status_t parser_get_entity_read_method(http_parser_t *parser);
403 
404 /************************************************************************
405  * Function: parser_append
406  *
407  * Parameters:
408  * INOUT http_parser_t* parser ; HTTP Parser Object
409  * IN const char* buf ; buffer to be appended to the parser
410  * buffer
411  * IN size_t buf_length ; Size of the buffer
412  *
413  * Description: The parser function. Depending on the position of the
414  * parser object the actual parsing function is invoked
415  *
416  * Returns:
417  * void
418  ************************************************************************/
419 parse_status_t parser_append(
420  http_parser_t *parser, const char *buf, size_t buf_length);
421 
422 /************************************************************************
423  * Function: matchstr
424  *
425  * Parameters:
426  * IN char *str ; String to be matched
427  * IN size_t slen ; Length of the string
428  * IN const char* fmt ; Pattern format
429  * ...
430  *
431  * Description: Matches a variable parameter list with a string
432  * and takes actions based on the data type specified.
433  *
434  * Returns:
435  * PARSE_OK
436  * PARSE_NO_MATCH -- failure to match pattern 'fmt'
437  * PARSE_FAILURE -- 'str' is bad input
438  ************************************************************************/
439 parse_status_t matchstr(char *str, size_t slen, const char *fmt, ...);
440 
441 /************************************************************************
442  * Function: raw_to_int
443  *
444  * Parameters:
445  * IN memptr* raw_value ; Buffer to be converted
446  * IN int base ; Base to use for conversion
447  *
448  * Description: Converts raw character data to long-integer value
449  *
450  * Returns:
451  * int
452  ************************************************************************/
453 int raw_to_int(memptr *raw_value, int base);
454 
455 /************************************************************************
456  * Function: raw_find_str
457  *
458  * Parameters:
459  * IN memptr* raw_value ; Buffer containg the string
460  * IN const char* str ; Substring to be found
461  *
462  * Description: Find a substring from raw character string buffer
463  *
464  * Side effects: raw_value is transformed to lowercase.
465  *
466  * Returns:
467  * int - index at which the substring is found.
468  ************************************************************************/
469 int raw_find_str(memptr *raw_value, const char *str);
470 
471 /************************************************************************
472  * Function: method_to_str
473  *
474  * Parameters:
475  * IN http_method_t method ; HTTP method
476  *
477  * Description: A wrapper function that maps a method id to a method
478  * nameConverts a http_method id stored in the HTTP Method
479  *
480  * Returns:
481  * const char* ptr - Ptr to the HTTP Method
482  ************************************************************************/
483 const char *method_to_str(http_method_t method);
484 
489 #ifdef DEBUG
490 void print_http_headers(
492  http_message_t *hmsg);
493 #else
494  #define print_http_headers(hmsg) \
495  do { \
496  } while (0)
497 #endif
498 
499 #ifdef __cplusplus
500 } /* extern "C" */
501 #endif /* __cplusplus */
502 
503 #endif /* GENLIB_NET_HTTP_HTTPPARSER_H */
parse_status_t
Definition: httpparser.h:151
int entire_msg_loaded
Definition: httpparser.h:68
int http_error_code
Definition: httpparser.h:221
Definition: httpparser.h:181
Definition: httpparser.h:158
Definition: httpparser.h:169
uri_type uri
Definition: httpparser.h:187
membuffer * msg
Definition: httpparser.h:63
memptr entity
Definition: httpparser.h:208
Represents a URI used in parse_uri and elsewhere.
Definition: uri.h:133
size_t amount_discarded
Definition: httpparser.h:197
membuffer value
Definition: httpparser.h:176
Definition: httpparser.h:166
Definition: httpparser.h:154
Definition: httpparser.h:164
void print_http_headers(http_message_t *hmsg)
Print the HTTP headers.
Definition: httpparser.c:2223
Definition: httpparser.h:156
Definition: httpparser.h:60
membuffer status_msg
Definition: httpparser.h:193
size_t entity_start_position
Definition: httpparser.h:232
http_method_t method
Definition: httpparser.h:185
int is_request
Definition: httpparser.h:200
size_t cursor
Definition: httpparser.h:65
Definition: httpparser.h:216
char * urlbuf
Definition: httpparser.h:213
membuffer msg
Definition: httpparser.h:211
http_method_t request_method
Definition: httpparser.h:189
Definition: httpparser.h:160
Definition: membuffer.h:57
Definition: membuffer.h:47
LinkedList headers
Definition: httpparser.h:206
Definition: LinkedList.h:83
int valid_ssdp_notify_hack
Definition: httpparser.h:224
Definition: httpparser.h:162
int name_id
Definition: httpparser.h:174
int status_code
Definition: httpparser.h:191
memptr name
Definition: httpparser.h:172