libUPnP  1.6.17
httpparser.h
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2000-2003 Intel Corporation 
00004  * All rights reserved. 
00005  * Copyright (c) 2012 France Telecom All rights reserved. 
00006  *
00007  * Redistribution and use in source and binary forms, with or without 
00008  * modification, are permitted provided that the following conditions are met: 
00009  *
00010  * - Redistributions of source code must retain the above copyright notice, 
00011  * this list of conditions and the following disclaimer. 
00012  * - Redistributions in binary form must reproduce the above copyright notice, 
00013  * this list of conditions and the following disclaimer in the documentation 
00014  * and/or other materials provided with the distribution. 
00015  * - Neither name of Intel Corporation nor the names of its contributors 
00016  * may be used to endorse or promote products derived from this software 
00017  * without specific prior written permission.
00018  * 
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00020  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
00021  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
00022  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR 
00023  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00024  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00025  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
00026  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
00027  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00028  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
00029  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030  *
00031  ******************************************************************************/
00032 
00033 #ifndef GENLIB_NET_HTTP_HTTPPARSER_H
00034 #define GENLIB_NET_HTTP_HTTPPARSER_H
00035 
00040 #include "LinkedList.h"
00041 #include "membuffer.h"
00042 #include "uri.h"
00043 #include "upnputil.h"
00044 
00045 /* private types */
00046 
00047 /* scanner */
00048 
00049 /* Used to represent different types of tokens in input. */
00050 typedef enum
00051 {
00052         TT_IDENTIFIER, 
00053         TT_WHITESPACE, 
00054         TT_CRLF, 
00055         TT_CTRL,
00056         TT_SEPARATOR,
00057         TT_QUOTEDSTRING,
00058 } token_type_t;
00059 
00060 typedef struct
00061 {
00063         membuffer* msg;
00065         size_t cursor;
00068         int entire_msg_loaded;
00069 } scanner_t;
00070 
00071 typedef enum
00072 {
00073         POS_REQUEST_LINE,
00074         POS_RESPONSE_LINE,
00075         POS_HEADERS,
00076         POS_ENTITY,
00077         POS_COMPLETE,
00078 } parser_pos_t;
00079 
00080 #define ENTREAD_DETERMINE_READ_METHOD   1
00081 #define ENTREAD_USING_CLEN              2
00082 #define ENTREAD_USING_CHUNKED           3
00083 #define ENTREAD_UNTIL_CLOSE             4
00084 #define ENTREAD_CHUNKY_BODY             5
00085 #define ENTREAD_CHUNKY_HEADERS          6
00086 
00087 /* end of private section. */
00088 
00089 /* method in a HTTP request. */
00090 typedef enum
00091 {
00092         HTTPMETHOD_POST, 
00093         HTTPMETHOD_MPOST, 
00094         HTTPMETHOD_SUBSCRIBE, 
00095         HTTPMETHOD_UNSUBSCRIBE, 
00096         HTTPMETHOD_NOTIFY, 
00097         HTTPMETHOD_GET,
00098         HTTPMETHOD_HEAD, 
00099         HTTPMETHOD_MSEARCH, 
00100         HTTPMETHOD_UNKNOWN,
00101         SOAPMETHOD_POST,
00102         HTTPMETHOD_SIMPLEGET
00103 } http_method_t;
00104 
00105 /* different types of HTTP headers */
00106 #define HDR_UNKNOWN                     -1
00107 #define HDR_CACHE_CONTROL               1
00108 #define HDR_CALLBACK                    2
00109 #define HDR_CONTENT_LENGTH              3
00110 #define HDR_CONTENT_TYPE                4
00111 #define HDR_DATE                        5
00112 #define HDR_EXT                         6
00113 #define HDR_HOST                        7
00114 /*define HDR_IF_MODIFIED_SINCE          8 */
00115 /*define HDR_IF_UNMODIFIED_SINCE        9 */
00116 /*define HDR_LAST_MODIFIED              10 */
00117 #define HDR_LOCATION                    11
00118 #define HDR_MAN                         12
00119 #define HDR_MX                          13
00120 #define HDR_NT                          14
00121 #define HDR_NTS                         15
00122 #define HDR_SERVER                      16
00123 #define HDR_SEQ                         17
00124 #define HDR_SID                         18
00125 #define HDR_SOAPACTION                  19
00126 #define HDR_ST                          20
00127 #define HDR_TIMEOUT                     21
00128 #define HDR_TRANSFER_ENCODING           22
00129 #define HDR_USN                         23
00130 #define HDR_USER_AGENT                  24
00131 
00132 /* Adding new header difinition */
00133 #define HDR_ACCEPT                      25
00134 #define HDR_ACCEPT_ENCODING             26
00135 #define HDR_ACCEPT_CHARSET              27
00136 #define HDR_ACCEPT_LANGUAGE             28
00137 #define HDR_ACCEPT_RANGE                29
00138 #define HDR_CONTENT_ENCODING            30
00139 #define HDR_CONTENT_LANGUAGE            31
00140 #define HDR_CONTENT_LOCATION            32
00141 #define HDR_CONTENT_RANGE               33
00142 #define HDR_IF_RANGE                    34
00143 #define HDR_RANGE                       35
00144 #define HDR_TE                          36
00145 
00147 typedef enum {
00149         PARSE_SUCCESS = 0,
00151         PARSE_INCOMPLETE,
00153         PARSE_INCOMPLETE_ENTITY,
00155         PARSE_FAILURE,
00157         PARSE_OK,
00159         PARSE_NO_MATCH,
00161         PARSE_CONTINUE_1
00162 } parse_status_t;
00163 
00164 typedef struct {
00166         memptr name;
00168         int name_id;
00170         membuffer value;
00171         /* private. */
00172         membuffer name_buf;
00173 } http_header_t;
00174 
00175 typedef struct {
00176         int initialized;
00178         http_method_t method;
00180         uri_type uri;
00182         http_method_t request_method;
00184         int status_code;
00186         membuffer status_msg;
00190        size_t amount_discarded;
00191         /* fields used in both request or response messages. */
00193         int is_request;
00194         /* http major version. */
00195         int major_version;
00196         /* http minor version. */
00197         int minor_version;
00199         LinkedList headers;
00201         memptr entity;
00202         /* private fields. */
00204         membuffer msg;
00206         char *urlbuf;
00207 } http_message_t;
00208 
00209 typedef struct {
00210         http_message_t msg;
00213         int http_error_code;
00216         int valid_ssdp_notify_hack;
00217         /* private data -- don't touch. */
00218         parser_pos_t position;
00219         int ent_position;
00220         unsigned int content_length;
00221         size_t chunk_size;
00224         size_t entity_start_position;
00225         scanner_t scanner;
00226 } http_parser_t;
00227 
00228 #ifdef __cplusplus
00229 extern "C" {
00230 #endif /* __cplusplus */
00231 
00232 /************************************************************************
00233 *       Function :      httpmsg_init
00234 *
00235 *       Parameters :
00236 *               INOUT http_message_t* msg ;     HTTP Message Object
00237 *
00238 *       Description :   Initialize and allocate memory for http message
00239 *
00240 *       Return : void ;
00241 *
00242 *       Note :
00243 ************************************************************************/
00244 void httpmsg_init( INOUT http_message_t* msg );
00245 
00246 /************************************************************************
00247 *       Function :      httpmsg_destroy
00248 *
00249 *       Parameters :
00250 *               INOUT http_message_t* msg ;     HTTP Message Object
00251 *
00252 *       Description :   Free memory allocated for the http message
00253 *
00254 *       Return : void ;
00255 *
00256 *       Note :
00257 ************************************************************************/
00258 void httpmsg_destroy( INOUT http_message_t* msg );
00259 
00260 /************************************************************************
00261 *       Function :      httpmsg_find_hdr_str
00262 *
00263 *       Parameters :
00264 *               IN http_message_t* msg ;        HTTP Message Object
00265 *               IN const char* header_name ; Header name to be compared with    
00266 *
00267 *       Description :   Compares the header name with the header names stored 
00268 *               in      the linked list of messages
00269 *
00270 *       Return : http_header_t* - Pointer to a header on success;
00271 *                        NULL on failure
00272 *       Note :
00273 ************************************************************************/
00274 http_header_t* httpmsg_find_hdr_str( IN http_message_t* msg,
00275                         IN const char* header_name );
00276 
00277 /************************************************************************
00278 *       Function :      httpmsg_find_hdr
00279 *
00280 *       Parameters :
00281 *               IN http_message_t* msg ; HTTP Message Object
00282 *               IN int header_name_id ;  Header Name ID to be compared with
00283 *               OUT memptr* value ;              Buffer to get the ouput to.
00284 *
00285 *       Description :   Finds header from a list, with the given 'name_id'.
00286 *
00287 *       Return : http_header_t*  - Pointer to a header on success;
00288 *                NULL on failure
00289 *
00290 *       Note :
00291 ************************************************************************/
00292 http_header_t* httpmsg_find_hdr( IN http_message_t* msg, 
00293                         IN int header_name_id, OUT memptr* value );
00294 
00295 /************************************************************************
00296 * Function: parser_request_init                                                                                 
00297 *                                                                                                                                               
00298 * Parameters:                                                                                                                   
00299 *       OUT http_parser_t* parser ; HTTP Parser object                                                                  
00300 *                                                                                                                               
00301 * Description: Initializes parser object for a request                                  
00302 *                                                                                                                                               
00303 * Returns:                                                                                                                              
00304 *        void                                                                                                                           
00305 ************************************************************************/
00306 void parser_request_init( OUT http_parser_t* parser );
00307 
00308 /************************************************************************
00309 * Function: parser_response_init                                                                                
00310 *                                                                                                                                               
00311 * Parameters:                                                                                                                   
00312 *       OUT http_parser_t* parser       ;         HTTP Parser object
00313 *       IN http_method_t request_method ; Request method                                        
00314 *                                                                                                                                               
00315 * Description: Initializes parser object for a response                                 
00316 *                                                                                                                                               
00317 * Returns:                                                                                                                              
00318 *        void                                                                                                                           
00319 ************************************************************************/
00320 void parser_response_init( OUT http_parser_t* parser, 
00321                            IN http_method_t request_method );
00322 
00323 /************************************************************************
00324 * Function: parser_parse                                                                                                
00325 *                                                                                                                                               
00326 * Parameters:                                                                                                                   
00327 *       INOUT http_parser_t* parser ; HTTP Parser object                                        
00328 *                                                                                                                                               
00329 * Description: The parser function. Depending on the position of the    
00330 *       parser object the actual parsing function is invoked                            
00331 *                                                                                                                                               
00332 * Returns:                                                                                                                              
00333 *        void                                                                                                                           
00334 ************************************************************************/
00335 parse_status_t parser_parse(INOUT http_parser_t * parser);
00336 
00337 /************************************************************************
00338 * Function: parser_parse_responseline                                                                   
00339 *                                                                                                                                               
00340 * Parameters:                                                                                                                   
00341 *       INOUT http_parser_t* parser     ; HTTP Parser object                                    
00342 *                                                                                                                                               
00343 * Description: Get HTTP Method, URL location and version information.   
00344 *                                                                                                                                               
00345 * Returns:                                                                                                                              
00346 *       PARSE_OK                                                                                                                        
00347 *       PARSE_SUCCESS                                                                                                           
00348 *       PARSE_FAILURE                                                                                                           
00349 ************************************************************************/
00350 parse_status_t parser_parse_responseline(INOUT http_parser_t *parser);
00351 
00352 /************************************************************************
00353 * Function: parser_parse_headers                                                                        
00354 *                                                                                                                                               
00355 * Parameters:                                                                                                                   
00356 *       INOUT http_parser_t* parser     ; HTTP Parser object                                                                            
00357 *                                                                                                       
00358 * Description: Get HTTP Method, URL location and version information.   
00359 *                                                                                                                                               
00360 * Returns:                                                                                                                              
00361 *       PARSE_OK                                                                                                                        
00362 *       PARSE_SUCCESS                                                                                                           
00363 *       PARSE_FAILURE                                                                                                           
00364 ************************************************************************/
00365 parse_status_t parser_parse_headers(INOUT http_parser_t *parser);
00366 
00367 /************************************************************************
00368 * Function: parser_parse_entity                                                                                 
00369 *                                                                                                                                               
00370 * Parameters:                                                                                                                   
00371 *       INOUT http_parser_t* parser     ; HTTP Parser object                                    
00372 *                                                                                                                                               
00373 * Description: Determines method to read entity                                                 
00374 *                                                                                                                                               
00375 * Returns:                                                                                                                              
00376 *        PARSE_OK                                                                                                                       
00377 *        PARSE_FAILURE                                                                                                          
00378 *        PARSE_COMPLETE -- no more reading to do                                                        
00379 ************************************************************************/
00380 parse_status_t parser_parse_entity(INOUT http_parser_t *parser);
00381 
00382 /************************************************************************
00383 * Function: parser_get_entity_read_method                                                               
00384 *                                                                                                                                               
00385 * Parameters:                                                                                                                   
00386 *       INOUT http_parser_t* parser     ; HTTP Parser object                                    
00387 *                                                                                                                                               
00388 * Description: Determines method to read entity                                                 
00389 *                                                                                                                                               
00390 * Returns:                                                                                                                              
00391 *        PARSE_OK                                                                                                                       
00392 *        PARSE_FAILURE                                                                                                          
00393 *        PARSE_COMPLETE -- no more reading to do                                                        
00394 ************************************************************************/
00395 parse_status_t parser_get_entity_read_method( INOUT http_parser_t* parser );
00396 
00397 /************************************************************************
00398 * Function: parser_append                                                                                               
00399 *                                                                                                                                               
00400 * Parameters:                                                                                                                   
00401 *       INOUT http_parser_t* parser ;   HTTP Parser Object                                      
00402 *       IN const char* buf      ;       buffer to be appended to the parser
00403 *                                       buffer
00404 *       IN size_t buf_length ;          Size of the buffer
00405 *                                                                                                                                               
00406 * Description: The parser function. Depending on the position of the    
00407 *       parser object the actual parsing function is invoked                            
00408 *                                                                                                                                               
00409 * Returns:                                                                                                                              
00410 *        void                                                                                                                           
00411 ************************************************************************/
00412 parse_status_t parser_append( INOUT http_parser_t* parser, 
00413                                  IN const char* buf,
00414                                  IN size_t buf_length );
00415 
00416 /************************************************************************
00417 * Function: matchstr                                                                                                    
00418 *                                                                                                                                               
00419 * Parameters:                                                                                                                   
00420 *       IN char *str ;           String to be matched
00421 *       IN size_t slen ;     Length of the string
00422 *       IN const char* fmt ; Pattern format                                                                                             
00423 *       ...                                                                                                                                     
00424 *                                                                                                                                               
00425 * Description: Matches a variable parameter list with a string                  
00426 *       and takes actions based on the data type specified.                                     
00427 *                                                                                                                                               
00428 * Returns:                                                                                                                              
00429 *   PARSE_OK                                                                                                                    
00430 *   PARSE_NO_MATCH -- failure to match pattern 'fmt'                                    
00431 *   PARSE_FAILURE       -- 'str' is bad input                                                   
00432 ************************************************************************/
00433 parse_status_t matchstr( IN char *str, IN size_t slen, IN const char* fmt, ... );
00434 
00435 /************************************************************************
00436 * Function: raw_to_int                                                                                                  
00437 *                                                                                                                                               
00438 * Parameters:                                                                                                                   
00439 *       IN memptr* raw_value ;  Buffer to be converted                                  
00440 *       IN int base ;                   Base  to use for conversion
00441 *                                                                                                                                               
00442 * Description: Converts raw character data to long-integer value                                        
00443 *                                                                                                                                               
00444 * Returns:                                                                                                                              
00445 *        int                                                                                                                            
00446 ************************************************************************/
00447 int raw_to_int( IN memptr* raw_value, int base );
00448 
00449 /************************************************************************
00450 * Function: raw_find_str
00451 *
00452 * Parameters:
00453 *       IN memptr* raw_value ; Buffer containg the string
00454 *       IN const char* str ;    Substring to be found
00455 *
00456 * Description: Find a substring from raw character string buffer
00457 *
00458 * Side effects: raw_value is transformed to lowercase.
00459 *
00460 * Returns:
00461 *        int - index at which the substring is found.                                           
00462 ************************************************************************/
00463 int raw_find_str( IN memptr* raw_value, IN const char* str );
00464 
00465 /************************************************************************
00466 * Function: method_to_str                                                                                               
00467 *                                                                                                                                               
00468 * Parameters:                                                                                                                   
00469 * IN http_method_t method ; HTTP method                                         
00470 *                                                                                                                                               
00471 * Description: A wrapper function that maps a method id to a method             
00472 *       nameConverts a http_method id stored in the HTTP Method                         
00473 *                                                                                                                                               
00474 * Returns:                                                                                                                              
00475 *        const char* ptr - Ptr to the HTTP Method
00476 ************************************************************************/
00477 const char* method_to_str( IN http_method_t method );
00478 
00479 #ifdef __cplusplus
00480 }               /* extern "C" */
00481 #endif  /* __cplusplus */
00482 
00483 #endif /* GENLIB_NET_HTTP_HTTPPARSER_H */
00484