pupnp (libupnp) snapshot from SourceForge: git clone git://pupnp.git.sourceforge...
[igd2-for-linux:pandonghui1211s-igd2-for-linux.git] / pupnp_branch-1.6.x / upnp / src / inc / httpparser.h
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 #ifndef GENLIB_NET_HTTP_HTTPPARSER_H
33 #define GENLIB_NET_HTTP_HTTPPARSER_H
34
35 #include "util.h"
36 #include "membuffer.h"
37 #include "uri.h"
38
39 #include "LinkedList.h"
40
41 ////// private types ////////////
42
43
44 //////////////////////
45 // scanner
46 ///////////////////////
47 // Used to represent different types of tokens in input
48 typedef enum // token_type_t
49 {
50         TT_IDENTIFIER, 
51         TT_WHITESPACE, 
52         TT_CRLF, 
53         TT_CTRL,                                // needed ??
54         TT_SEPARATOR,                   // ??
55         TT_QUOTEDSTRING,                // ??
56 } token_type_t;
57
58 typedef struct // scanner_t
59 {
60         membuffer* msg;                         // raw http msg
61         size_t cursor;                          // current position in buffer
62         xboolean entire_msg_loaded;     // set this to TRUE if the entire msg is loaded in
63                                                                 //   in 'msg'; else FALSE if only partial msg in 'msg'
64                                                                 //   (default is FALSE)
65 } scanner_t;
66
67 typedef enum // parser_pos_t
68 {
69         POS_REQUEST_LINE,
70         POS_RESPONSE_LINE,
71         POS_HEADERS,
72         POS_ENTITY,
73         POS_COMPLETE,
74 } parser_pos_t;
75
76
77 #define ENTREAD_DETERMINE_READ_METHOD   1
78 #define ENTREAD_USING_CLEN                              2
79 #define ENTREAD_USING_CHUNKED                   3
80 #define ENTREAD_UNTIL_CLOSE                             4
81 #define ENTREAD_CHUNKY_BODY                             5
82 #define ENTREAD_CHUNKY_HEADERS                  6
83
84
85 // end of private section
86 //////////////////
87 // ##################################################################################
88
89 // method in a HTTP request
90 typedef enum // http_method_t
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,     //murari
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//Beg_Murari
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 //End_Murari
146
147 // status of parsing
148 typedef enum // parse_status_t
149 {
150         PARSE_SUCCESS = 0,      // msg was parsed successfully
151         PARSE_INCOMPLETE,       // need more data to continue
152         PARSE_INCOMPLETE_ENTITY,        // for responses that don't have length specified
153         PARSE_FAILURE,          // parse failed; check status code for details
154         PARSE_OK,                       // done partial
155         PARSE_NO_MATCH,         // token not matched
156
157         // private
158         PARSE_CONTINUE_1
159 } parse_status_t;
160
161 typedef struct // http_header_t
162 {
163         memptr name;            // header name as a string
164         int name_id;            // header name id (for a selective group of headers only)
165         membuffer value;        // raw-value; could be multi-lined; min-length = 0
166
167     // private
168     membuffer name_buf;
169 } http_header_t;
170
171 typedef struct // http_message_t
172 {
173     int initialized;
174         // request only
175         http_method_t method;
176         uri_type uri;
177
178         // response only
179         http_method_t request_method;
180         int status_code;
181         membuffer status_msg;
182
183         // fields used in both request or response messages
184         xboolean is_request;    // if TRUE, msg is a request, else response
185
186         int major_version;              // http major.minor version
187         int minor_version;
188
189
190         LinkedList headers;
191 //NNS:  dlist headers;                  // dlist<http_header_t *>
192         memptr entity;                  // message body(entity)
193
194         // private fields
195         membuffer msg;          // entire raw message
196         char *urlbuf;   // storage for url string
197 } http_message_t;
198
199 typedef struct // http_parser_t
200 {
201         http_message_t msg;
202         int http_error_code;    // read-only; in case of parse error, this
203                                                         //  contains the HTTP error code (4XX or 5XX)
204
205     // read-only; this is set to true if a NOTIFY request has no content-length.
206     //  used to read valid sspd notify msg.
207     xboolean valid_ssdp_notify_hack;
208
209         // private data -- don't touch
210         parser_pos_t position;
211         int ent_position;
212         unsigned int content_length;
213         int chunk_size;
214         size_t entity_start_position;
215         scanner_t scanner;
216 } http_parser_t;
217
218
219 //--------------------------------------------------
220 //////////////// functions /////////////////////////
221 //--------------------------------------------------
222
223 #ifdef __cplusplus
224 extern "C" {
225 #endif // __cplusplus
226
227
228 /************************************************************************
229 *       Function :      httpmsg_init
230 *
231 *       Parameters :
232 *               INOUT http_message_t* msg ;     HTTP Message Object
233 *
234 *       Description :   Initialize and allocate memory for http message
235 *
236 *       Return : void ;
237 *
238 *       Note :
239 ************************************************************************/
240 void httpmsg_init( INOUT http_message_t* msg );
241
242 /************************************************************************
243 *       Function :      httpmsg_destroy
244 *
245 *       Parameters :
246 *               INOUT http_message_t* msg ;     HTTP Message Object
247 *
248 *       Description :   Free memory allocated for the http message
249 *
250 *       Return : void ;
251 *
252 *       Note :
253 ************************************************************************/
254 void httpmsg_destroy( INOUT http_message_t* msg );
255
256 /************************************************************************
257 *       Function :      httpmsg_find_hdr_str
258 *
259 *       Parameters :
260 *               IN http_message_t* msg ;        HTTP Message Object
261 *               IN const char* header_name ; Header name to be compared with    
262 *
263 *       Description :   Compares the header name with the header names stored 
264 *               in      the linked list of messages
265 *
266 *       Return : http_header_t* - Pointer to a header on success;
267 *                        NULL on failure                                                                                                                
268 *
269 *       Note :
270 ************************************************************************/
271 http_header_t* httpmsg_find_hdr_str( IN http_message_t* msg,
272                         IN const char* header_name );
273
274 /************************************************************************
275 *       Function :      httpmsg_find_hdr
276 *
277 *       Parameters :
278 *               IN http_message_t* msg ; HTTP Message Object
279 *               IN int header_name_id ;  Header Name ID to be compared with
280 *               OUT memptr* value ;              Buffer to get the ouput to.
281 *
282 *       Description :   Finds header from a list, with the given 'name_id'.
283 *
284 *       Return : http_header_t*  - Pointer to a header on success;                                                                              *
285 *                        NULL on failure                                                                                                                
286 *
287 *       Note :
288 ************************************************************************/
289 http_header_t* httpmsg_find_hdr( IN http_message_t* msg, 
290                         IN int header_name_id, OUT memptr* value );
291
292 /************************************************************************
293 * Function: parser_request_init                                                                                 
294 *                                                                                                                                               
295 * Parameters:                                                                                                                   
296 *       OUT http_parser_t* parser ; HTTP Parser object                                                                  
297 *                                                                                                                               
298 * Description: Initializes parser object for a request                                  
299 *                                                                                                                                               
300 * Returns:                                                                                                                              
301 *        void                                                                                                                           
302 ************************************************************************/
303 void parser_request_init( OUT http_parser_t* parser );
304
305 /************************************************************************
306 * Function: parser_response_init                                                                                
307 *                                                                                                                                               
308 * Parameters:                                                                                                                   
309 *       OUT http_parser_t* parser       ;         HTTP Parser object
310 *       IN http_method_t request_method ; Request method                                        
311 *                                                                                                                                               
312 * Description: Initializes parser object for a response                                 
313 *                                                                                                                                               
314 * Returns:                                                                                                                              
315 *        void                                                                                                                           
316 ************************************************************************/
317 void parser_response_init( OUT http_parser_t* parser, 
318                            IN http_method_t request_method );
319
320 /************************************************************************
321 * Function: parser_parse                                                                                                
322 *                                                                                                                                               
323 * Parameters:                                                                                                                   
324 *       INOUT http_parser_t* parser ; HTTP Parser object                                        
325 *                                                                                                                                               
326 * Description: The parser function. Depending on the position of the    
327 *       parser object the actual parsing function is invoked                            
328 *                                                                                                                                               
329 * Returns:                                                                                                                              
330 *        void                                                                                                                           
331 ************************************************************************/
332 parse_status_t parser_parse(INOUT http_parser_t * parser);
333
334 /************************************************************************
335 * Function: parser_parse_responseline                                                                   
336 *                                                                                                                                               
337 * Parameters:                                                                                                                   
338 *       INOUT http_parser_t* parser     ; HTTP Parser object                                    
339 *                                                                                                                                               
340 * Description: Get HTTP Method, URL location and version information.   
341 *                                                                                                                                               
342 * Returns:                                                                                                                              
343 *       PARSE_OK                                                                                                                        
344 *       PARSE_SUCCESS                                                                                                           
345 *       PARSE_FAILURE                                                                                                           
346 ************************************************************************/
347 parse_status_t parser_parse_responseline(INOUT http_parser_t *parser);
348
349 /************************************************************************
350 * Function: parser_parse_headers                                                                        
351 *                                                                                                                                               
352 * Parameters:                                                                                                                   
353 *       INOUT http_parser_t* parser     ; HTTP Parser object                                                                            
354 *                                                                                                       
355 * Description: Get HTTP Method, URL location and version information.   
356 *                                                                                                                                               
357 * Returns:                                                                                                                              
358 *       PARSE_OK                                                                                                                        
359 *       PARSE_SUCCESS                                                                                                           
360 *       PARSE_FAILURE                                                                                                           
361 ************************************************************************/
362 parse_status_t parser_parse_headers(INOUT http_parser_t *parser);
363
364 /************************************************************************
365 * Function: parser_parse_entity                                                                                 
366 *                                                                                                                                               
367 * Parameters:                                                                                                                   
368 *       INOUT http_parser_t* parser     ; HTTP Parser object                                    
369 *                                                                                                                                               
370 * Description: Determines method to read entity                                                 
371 *                                                                                                                                               
372 * Returns:                                                                                                                              
373 *        PARSE_OK                                                                                                                       
374 *        PARSE_FAILURE                                                                                                          
375 *        PARSE_COMPLETE -- no more reading to do                                                        
376 ************************************************************************/
377 parse_status_t parser_parse_entity(INOUT http_parser_t *parser);
378
379 /************************************************************************
380 * Function: parser_get_entity_read_method                                                               
381 *                                                                                                                                               
382 * Parameters:                                                                                                                   
383 *       INOUT http_parser_t* parser     ; HTTP Parser object                                    
384 *                                                                                                                                               
385 * Description: Determines method to read entity                                                 
386 *                                                                                                                                               
387 * Returns:                                                                                                                              
388 *        PARSE_OK                                                                                                                       
389 *        PARSE_FAILURE                                                                                                          
390 *        PARSE_COMPLETE -- no more reading to do                                                        
391 ************************************************************************/
392 parse_status_t parser_get_entity_read_method( INOUT http_parser_t* parser );
393
394 /************************************************************************
395 * Function: parser_append                                                                                               
396 *                                                                                                                                               
397 * Parameters:                                                                                                                   
398 *       INOUT http_parser_t* parser ;   HTTP Parser Object                                      
399 *       IN const char* buf      ;                       buffer to be appended to the parser 
400 *                                                                       buffer                                                  
401 *       IN size_t buf_length ;                  Size of the buffer                                                                                              
402 *                                                                                                                                               
403 * Description: The parser function. Depending on the position of the    
404 *       parser object the actual parsing function is invoked                            
405 *                                                                                                                                               
406 * Returns:                                                                                                                              
407 *        void                                                                                                                           
408 ************************************************************************/
409 parse_status_t parser_append( INOUT http_parser_t* parser, 
410                                  IN const char* buf,
411                                  IN size_t buf_length );
412
413 /************************************************************************
414 * Function: matchstr                                                                                                    
415 *                                                                                                                                               
416 * Parameters:                                                                                                                   
417 *       IN char *str ;           String to be matched                                                                                                           
418 *       IN size_t slen ;     Length of the string                                                                                                               
419 *       IN const char* fmt ; Pattern format                                                                                             
420 *       ...                                                                                                                                     
421 *                                                                                                                                               
422 * Description: Matches a variable parameter list with a string                  
423 *       and takes actions based on the data type specified.                                     
424 *                                                                                                                                               
425 * Returns:                                                                                                                              
426 *   PARSE_OK                                                                                                                    
427 *   PARSE_NO_MATCH -- failure to match pattern 'fmt'                                    
428 *   PARSE_FAILURE       -- 'str' is bad input                                                   
429 ************************************************************************/
430 int matchstr( IN char *str, IN size_t slen, IN const char* fmt, ... );
431
432 // ====================================================
433 // misc functions
434
435
436 /************************************************************************
437 * Function: raw_to_int                                                                                                  
438 *                                                                                                                                               
439 * Parameters:                                                                                                                   
440 *       IN memptr* raw_value ;  Buffer to be converted                                  
441 *       IN int base ;                   Base  to use for conversion
442 *                                                                                                                                               
443 * Description: Converts raw character data to long-integer value                                        
444 *                                                                                                                                               
445 * Returns:                                                                                                                              
446 *        int                                                                                                                            
447 ************************************************************************/
448 int raw_to_int( IN memptr* raw_value, int base );
449
450 /************************************************************************
451 * Function: raw_find_str
452 *
453 * Parameters:
454 *       IN memptr* raw_value ; Buffer containg the string
455 *       IN const char* str ;    Substring to be found
456 *
457 * Description: Find a substring from raw character string buffer
458 *
459 * Side effects: raw_value is transformed to lowercase.
460 *
461 * Returns:
462 *        int - index at which the substring is found.                                           
463 ************************************************************************/
464 int raw_find_str( IN memptr* raw_value, IN const char* str );
465
466 /************************************************************************
467 * Function: method_to_str                                                                                               
468 *                                                                                                                                               
469 * Parameters:                                                                                                                   
470 * IN http_method_t method ; HTTP method                                         
471 *                                                                                                                                               
472 * Description: A wrapper function that maps a method id to a method             
473 *       nameConverts a http_method id stored in the HTTP Method                         
474 *                                                                                                                                               
475 * Returns:                                                                                                                              
476 *        const char* ptr - Ptr to the HTTP Method                                                                                                                                                                                       *
477 ************************************************************************/
478 const char* method_to_str( IN http_method_t method );
479
480 /************************************************************************
481 * Function: print_http_headers
482 *
483 * Parameters:
484 *       http_message_t* hmsg ; HTTP Message object
485 *
486 * Description:
487 *
488 * Returns:
489 *        void
490 ************************************************************************/
491 #ifdef DEBUG
492 void print_http_headers( IN http_message_t *hmsg );
493 #else
494 static UPNP_INLINE void print_http_headers( IN http_message_t *hmsg ) {}
495 #endif
496
497 #ifdef __cplusplus
498 }               // extern "C"
499 #endif  // __cplusplus
500
501
502 #endif // GENLIB_NET_HTTP_HTTPPARSER_H
503