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