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  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * - Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  * - Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * - Neither name of Intel Corporation nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  ******************************************************************************/
31 
32 
33 #ifndef GENLIB_NET_HTTP_HTTPPARSER_H
34 #define GENLIB_NET_HTTP_HTTPPARSER_H
35 
36 
42 #include "LinkedList.h"
43 #include "membuffer.h"
44 #include "uri.h"
45 #include "util.h"
46 
47 
49 
50 
52 // scanner
54 // Used to represent different types of tokens in input
55 typedef enum // token_type_t
56 {
57  TT_IDENTIFIER,
58  TT_WHITESPACE,
59  TT_CRLF,
60  TT_CTRL, // needed ??
61  TT_SEPARATOR, // ??
62  TT_QUOTEDSTRING, // ??
63 } token_type_t;
64 
65 typedef struct // scanner_t
66 {
67  membuffer* msg; // raw http msg
68  size_t cursor; // current position in buffer
69  xboolean entire_msg_loaded; // set this to TRUE if the entire msg is loaded in
70  // in 'msg'; else FALSE if only partial msg in 'msg'
71  // (default is FALSE)
72 } scanner_t;
73 
74 typedef enum // parser_pos_t
75 {
76  POS_REQUEST_LINE,
77  POS_RESPONSE_LINE,
78  POS_HEADERS,
79  POS_ENTITY,
80  POS_COMPLETE,
81 } parser_pos_t;
82 
83 
84 #define ENTREAD_DETERMINE_READ_METHOD 1
85 #define ENTREAD_USING_CLEN 2
86 #define ENTREAD_USING_CHUNKED 3
87 #define ENTREAD_UNTIL_CLOSE 4
88 #define ENTREAD_CHUNKY_BODY 5
89 #define ENTREAD_CHUNKY_HEADERS 6
90 
91 
92 // end of private section
94 // ##################################################################################
95 
96 // method in a HTTP request
97 typedef enum // http_method_t
98 {
99  HTTPMETHOD_POST,
100  HTTPMETHOD_MPOST,
101  HTTPMETHOD_SUBSCRIBE,
102  HTTPMETHOD_UNSUBSCRIBE,
103  HTTPMETHOD_NOTIFY,
104  HTTPMETHOD_GET,
105  HTTPMETHOD_HEAD,
106  HTTPMETHOD_MSEARCH,
107  HTTPMETHOD_UNKNOWN,
108  SOAPMETHOD_POST, //murari
109  HTTPMETHOD_SIMPLEGET
110 } http_method_t;
111 
112 // different types of HTTP headers
113 #define HDR_UNKNOWN -1
114 #define HDR_CACHE_CONTROL 1
115 #define HDR_CALLBACK 2
116 #define HDR_CONTENT_LENGTH 3
117 #define HDR_CONTENT_TYPE 4
118 #define HDR_DATE 5
119 #define HDR_EXT 6
120 #define HDR_HOST 7
121 //#define HDR_IF_MODIFIED_SINCE 8
122 //#define HDR_IF_UNMODIFIED_SINCE 9
123 //#define HDR_LAST_MODIFIED 10
124 #define HDR_LOCATION 11
125 #define HDR_MAN 12
126 #define HDR_MX 13
127 #define HDR_NT 14
128 #define HDR_NTS 15
129 #define HDR_SERVER 16
130 #define HDR_SEQ 17
131 #define HDR_SID 18
132 #define HDR_SOAPACTION 19
133 #define HDR_ST 20
134 #define HDR_TIMEOUT 21
135 #define HDR_TRANSFER_ENCODING 22
136 #define HDR_USN 23
137 #define HDR_USER_AGENT 24
138 
139 //Adding new header difinition//Beg_Murari
140 #define HDR_ACCEPT 25
141 #define HDR_ACCEPT_ENCODING 26
142 #define HDR_ACCEPT_CHARSET 27
143 #define HDR_ACCEPT_LANGUAGE 28
144 #define HDR_ACCEPT_RANGE 29
145 #define HDR_CONTENT_ENCODING 30
146 #define HDR_CONTENT_LANGUAGE 31
147 #define HDR_CONTENT_LOCATION 32
148 #define HDR_CONTENT_RANGE 33
149 #define HDR_IF_RANGE 34
150 #define HDR_RANGE 35
151 #define HDR_TE 36
152 //End_Murari
153 
154 // status of parsing
155 typedef enum // parse_status_t
156 {
157  PARSE_SUCCESS = 0, // msg was parsed successfully
158  PARSE_INCOMPLETE, // need more data to continue
159  PARSE_INCOMPLETE_ENTITY, // for responses that don't have length specified
160  PARSE_FAILURE, // parse failed; check status code for details
161  PARSE_OK, // done partial
162  PARSE_NO_MATCH, // token not matched
163 
164  // private
165  PARSE_CONTINUE_1
166 } parse_status_t;
167 
168 typedef struct // http_header_t
169 {
170  memptr name; // header name as a string
171  int name_id; // header name id (for a selective group of headers only)
172  membuffer value; // raw-value; could be multi-lined; min-length = 0
173 
174  // private
175  membuffer name_buf;
176 } http_header_t;
177 
178 typedef struct // http_message_t
179 {
180  int initialized;
181  // request only
182  http_method_t method;
183  uri_type uri;
184 
185  // response only
186  http_method_t request_method;
187  int status_code;
188  membuffer status_msg;
189 
190  // fields used in both request or response messages
191  xboolean is_request; // if TRUE, msg is a request, else response
192 
193  int major_version; // http major.minor version
194  int minor_version;
195 
196 
197  LinkedList headers;
198 //NNS: dlist headers; // dlist<http_header_t *>
199  memptr entity; // message body(entity)
200 
201  // private fields
202  membuffer msg; // entire raw message
203  char *urlbuf; // storage for url string
205 
206 typedef struct // http_parser_t
207 {
208  http_message_t msg;
209  int http_error_code; // read-only; in case of parse error, this
210  // contains the HTTP error code (4XX or 5XX)
211 
212  // read-only; this is set to true if a NOTIFY request has no content-length.
213  // used to read valid sspd notify msg.
214  xboolean valid_ssdp_notify_hack;
215 
216  // private data -- don't touch
217  parser_pos_t position;
218  int ent_position;
219  unsigned int content_length;
220  int chunk_size;
221  size_t entity_start_position;
222  scanner_t scanner;
223 } http_parser_t;
224 
225 
226 //--------------------------------------------------
228 //--------------------------------------------------
229 
230 #ifdef __cplusplus
231 extern "C" {
232 #endif // __cplusplus
233 
234 
235 /************************************************************************
236 * Function : httpmsg_init
237 *
238 * Parameters :
239 * INOUT http_message_t* msg ; HTTP Message Object
240 *
241 * Description : Initialize and allocate memory for http message
242 *
243 * Return : void ;
244 *
245 * Note :
246 ************************************************************************/
247 void httpmsg_init( INOUT http_message_t* msg );
248 
249 /************************************************************************
250 * Function : httpmsg_destroy
251 *
252 * Parameters :
253 * INOUT http_message_t* msg ; HTTP Message Object
254 *
255 * Description : Free memory allocated for the http message
256 *
257 * Return : void ;
258 *
259 * Note :
260 ************************************************************************/
261 void httpmsg_destroy( INOUT http_message_t* msg );
262 
263 /************************************************************************
264 * Function : httpmsg_find_hdr_str
265 *
266 * Parameters :
267 * IN http_message_t* msg ; HTTP Message Object
268 * IN const char* header_name ; Header name to be compared with
269 *
270 * Description : Compares the header name with the header names stored
271 * in the linked list of messages
272 *
273 * Return : http_header_t* - Pointer to a header on success;
274 * NULL on failure
275 *
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 int matchstr( IN char *str, IN size_t slen, IN const char* fmt, ... );
438 
439 // ====================================================
440 // misc functions
441 
442 
443 /************************************************************************
444 * Function: raw_to_int
445 *
446 * Parameters:
447 * IN memptr* raw_value ; Buffer to be converted
448 * IN int base ; Base to use for conversion
449 *
450 * Description: Converts raw character data to long-integer value
451 *
452 * Returns:
453 * int
454 ************************************************************************/
455 int raw_to_int( IN memptr* raw_value, int base );
456 
457 /************************************************************************
458 * Function: raw_find_str
459 *
460 * Parameters:
461 * IN memptr* raw_value ; Buffer containg the string
462 * IN const char* str ; Substring to be found
463 *
464 * Description: Find a substring from raw character string buffer
465 *
466 * Side effects: raw_value is transformed to lowercase.
467 *
468 * Returns:
469 * int - index at which the substring is found.
470 ************************************************************************/
471 int raw_find_str( IN memptr* raw_value, IN const char* str );
472 
473 /************************************************************************
474 * Function: method_to_str
475 *
476 * Parameters:
477 * IN http_method_t method ; HTTP method
478 *
479 * Description: A wrapper function that maps a method id to a method
480 * nameConverts a http_method id stored in the HTTP Method
481 *
482 * Returns:
483 * const char* ptr - Ptr to the HTTP Method *
484 ************************************************************************/
485 const char* method_to_str( IN http_method_t method );
486 
487 
488 #ifdef __cplusplus
489 } /* extern "C" */
490 #endif /* __cplusplus */
491 
492 
493 #endif /* GENLIB_NET_HTTP_HTTPPARSER_H */
494 
Definition: httpparser.h:178
Definition: httpparser.h:168
Represents a URI used in parse_uri and elsewhere.
Definition: uri.h:148
Definition: httpparser.h:65
Definition: httpparser.h:206
Definition: membuffer.h:52
Definition: membuffer.h:43
Definition: LinkedList.h:111