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 // 00031 /*TU* 00032 00033 TombUPnP - a library for developing UPnP applications. 00034 00035 Copyright (C) 2006-2010 Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc> 00036 00037 This library is free software; you can redistribute it and/or 00038 modify it under the terms of the GNU Lesser General Public 00039 License version 2.1 as published by the Free Software Foundation. 00040 00041 This library is distributed in the hope that it will be useful, 00042 but WITHOUT ANY WARRANTY; without even the implied warranty of 00043 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00044 Lesser General Public License for more details. 00045 00046 You should have received a copy of the GNU Lesser General Public 00047 License along with this library; if not, write to the Free Software 00048 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. 00049 00050 $Id: httpparser.h 2081 2010-03-23 20:18:00Z lww $ 00051 */ 00052 00053 #ifndef GENLIB_NET_HTTP_HTTPPARSER_H 00054 #define GENLIB_NET_HTTP_HTTPPARSER_H 00055 00056 #include "util.h" 00057 #include "membuffer.h" 00058 #include "uri.h" 00059 00060 #include "LinkedList.h" 00061 00063 00064 00066 // scanner 00068 // Used to represent different types of tokens in input 00069 typedef enum // token_type_t 00070 { 00071 TT_IDENTIFIER, 00072 TT_WHITESPACE, 00073 TT_CRLF, 00074 TT_CTRL, // needed ?? 00075 TT_SEPARATOR, // ?? 00076 TT_QUOTEDSTRING, // ?? 00077 } token_type_t; 00078 00079 typedef struct // scanner_t 00080 { 00081 membuffer* msg; // raw http msg 00082 size_t cursor; // current position in buffer 00083 xboolean entire_msg_loaded; // set this to TRUE if the entire msg is loaded in 00084 // in 'msg'; else FALSE if only partial msg in 'msg' 00085 // (default is FALSE) 00086 } scanner_t; 00087 00088 typedef enum // parser_pos_t 00089 { 00090 POS_REQUEST_LINE, 00091 POS_RESPONSE_LINE, 00092 POS_HEADERS, 00093 POS_ENTITY, 00094 POS_COMPLETE, 00095 } parser_pos_t; 00096 00097 00098 #define ENTREAD_DETERMINE_READ_METHOD 1 00099 #define ENTREAD_USING_CLEN 2 00100 #define ENTREAD_USING_CHUNKED 3 00101 #define ENTREAD_UNTIL_CLOSE 4 00102 #define ENTREAD_CHUNKY_BODY 5 00103 #define ENTREAD_CHUNKY_HEADERS 6 00104 00105 00106 // end of private section 00108 // ################################################################################## 00109 00110 // method in a HTTP request 00111 typedef enum // http_method_t 00112 { 00113 HTTPMETHOD_POST, 00114 HTTPMETHOD_MPOST, 00115 HTTPMETHOD_SUBSCRIBE, 00116 HTTPMETHOD_UNSUBSCRIBE, 00117 HTTPMETHOD_NOTIFY, 00118 HTTPMETHOD_GET, 00119 HTTPMETHOD_HEAD, 00120 HTTPMETHOD_MSEARCH, 00121 HTTPMETHOD_UNKNOWN, 00122 SOAPMETHOD_POST, //murari 00123 HTTPMETHOD_SIMPLEGET 00124 } http_method_t; 00125 00126 // different types of HTTP headers 00127 #define HDR_UNKNOWN -1 00128 #define HDR_CACHE_CONTROL 1 00129 #define HDR_CALLBACK 2 00130 #define HDR_CONTENT_LENGTH 3 00131 #define HDR_CONTENT_TYPE 4 00132 #define HDR_DATE 5 00133 #define HDR_EXT 6 00134 #define HDR_HOST 7 00135 //#define HDR_IF_MODIFIED_SINCE 8 00136 //#define HDR_IF_UNMODIFIED_SINCE 9 00137 //#define HDR_LAST_MODIFIED 10 00138 #define HDR_LOCATION 11 00139 #define HDR_MAN 12 00140 #define HDR_MX 13 00141 #define HDR_NT 14 00142 #define HDR_NTS 15 00143 #define HDR_SERVER 16 00144 #define HDR_SEQ 17 00145 #define HDR_SID 18 00146 #define HDR_SOAPACTION 19 00147 #define HDR_ST 20 00148 #define HDR_TIMEOUT 21 00149 #define HDR_TRANSFER_ENCODING 22 00150 #define HDR_USN 23 00151 #define HDR_USER_AGENT 24 00152 00153 //Adding new header difinition//Beg_Murari 00154 #define HDR_ACCEPT 25 00155 #define HDR_ACCEPT_ENCODING 26 00156 #define HDR_ACCEPT_CHARSET 27 00157 #define HDR_ACCEPT_LANGUAGE 28 00158 #define HDR_ACCEPT_RANGE 29 00159 #define HDR_CONTENT_ENCODING 30 00160 #define HDR_CONTENT_LANGUAGE 31 00161 #define HDR_CONTENT_LOCATION 32 00162 #define HDR_CONTENT_RANGE 33 00163 #define HDR_IF_RANGE 34 00164 #define HDR_RANGE 35 00165 #define HDR_TE 36 00166 //End_Murari 00167 00168 // status of parsing 00169 typedef enum // parse_status_t 00170 { 00171 PARSE_SUCCESS = 0, // msg was parsed successfully 00172 PARSE_INCOMPLETE, // need more data to continue 00173 PARSE_INCOMPLETE_ENTITY, // for responses that don't have length specified 00174 PARSE_FAILURE, // parse failed; check status code for details 00175 PARSE_OK, // done partial 00176 PARSE_NO_MATCH, // token not matched 00177 00178 // private 00179 PARSE_CONTINUE_1 00180 } parse_status_t; 00181 00182 typedef struct // http_header_t 00183 { 00184 memptr name; // header name as a string 00185 int name_id; // header name id (for a selective group of headers only) 00186 membuffer value; // raw-value; could be multi-lined; min-length = 0 00187 00188 // private 00189 membuffer name_buf; 00190 } http_header_t; 00191 00192 typedef struct // http_message_t 00193 { 00194 int initialized; 00195 // request only 00196 http_method_t method; 00197 uri_type uri; 00198 00199 // response only 00200 http_method_t request_method; 00201 int status_code; 00202 membuffer status_msg; 00203 00204 // fields used in both request or response messages 00205 xboolean is_request; // if TRUE, msg is a request, else response 00206 00207 int major_version; // http major.minor version 00208 int minor_version; 00209 00210 00211 LinkedList headers; 00212 //NNS: dlist headers; // dlist<http_header_t *> 00213 memptr entity; // message body(entity) 00214 00215 // private fields 00216 membuffer msg; // entire raw message 00217 char *urlbuf; // storage for url string 00218 } http_message_t; 00219 00220 typedef struct // http_parser_t 00221 { 00222 http_message_t msg; 00223 int http_error_code; // read-only; in case of parse error, this 00224 // contains the HTTP error code (4XX or 5XX) 00225 00226 // read-only; this is set to true if a NOTIFY request has no content-length. 00227 // used to read valid sspd notify msg. 00228 xboolean valid_ssdp_notify_hack; 00229 00230 // private data -- don't touch 00231 parser_pos_t position; 00232 int ent_position; 00233 off_t content_length; 00234 int chunk_size; 00235 size_t entity_start_position; 00236 scanner_t scanner; 00237 } http_parser_t; 00238 00239 00240 //-------------------------------------------------- 00242 //-------------------------------------------------- 00243 00244 #ifdef __cplusplus 00245 extern "C" { 00246 #endif // __cplusplus 00247 00248 00249 /************************************************************************ 00250 * Function : httpmsg_init 00251 * 00252 * Parameters : 00253 * INOUT http_message_t* msg ; HTTP Message Object 00254 * 00255 * Description : Initialize and allocate memory for http message 00256 * 00257 * Return : void ; 00258 * 00259 * Note : 00260 ************************************************************************/ 00261 void httpmsg_init( INOUT http_message_t* msg ); 00262 00263 /************************************************************************ 00264 * Function : httpmsg_destroy 00265 * 00266 * Parameters : 00267 * INOUT http_message_t* msg ; HTTP Message Object 00268 * 00269 * Description : Free memory allocated for the http message 00270 * 00271 * Return : void ; 00272 * 00273 * Note : 00274 ************************************************************************/ 00275 void httpmsg_destroy( INOUT http_message_t* msg ); 00276 00277 /************************************************************************ 00278 * Function : httpmsg_find_hdr_str 00279 * 00280 * Parameters : 00281 * IN http_message_t* msg ; HTTP Message Object 00282 * IN const char* header_name ; Header name to be compared with 00283 * 00284 * Description : Compares the header name with the header names stored 00285 * in the linked list of messages 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_str( IN http_message_t* msg, 00293 IN const char* header_name ); 00294 00295 /************************************************************************ 00296 * Function : httpmsg_find_hdr 00297 * 00298 * Parameters : 00299 * IN http_message_t* msg ; HTTP Message Object 00300 * IN int header_name_id ; Header Name ID to be compared with 00301 * OUT memptr* value ; Buffer to get the ouput to. 00302 * 00303 * Description : Finds header from a list, with the given 'name_id'. 00304 * 00305 * Return : http_header_t* - Pointer to a header on success; * 00306 * NULL on failure 00307 * 00308 * Note : 00309 ************************************************************************/ 00310 http_header_t* httpmsg_find_hdr( IN http_message_t* msg, 00311 IN int header_name_id, OUT memptr* value ); 00312 00313 /************************************************************************ 00314 * Function: parser_request_init 00315 * 00316 * Parameters: 00317 * OUT http_parser_t* parser ; HTTP Parser object 00318 * 00319 * Description: Initializes parser object for a request 00320 * 00321 * Returns: 00322 * void 00323 ************************************************************************/ 00324 void parser_request_init( OUT http_parser_t* parser ); 00325 00326 /************************************************************************ 00327 * Function: parser_response_init 00328 * 00329 * Parameters: 00330 * OUT http_parser_t* parser ; HTTP Parser object 00331 * IN http_method_t request_method ; Request method 00332 * 00333 * Description: Initializes parser object for a response 00334 * 00335 * Returns: 00336 * void 00337 ************************************************************************/ 00338 void parser_response_init( OUT http_parser_t* parser, 00339 IN http_method_t request_method ); 00340 00341 /************************************************************************ 00342 * Function: parser_parse 00343 * 00344 * Parameters: 00345 * INOUT http_parser_t* parser ; HTTP Parser object 00346 * 00347 * Description: The parser function. Depending on the position of the 00348 * parser object the actual parsing function is invoked 00349 * 00350 * Returns: 00351 * void 00352 ************************************************************************/ 00353 parse_status_t parser_parse(INOUT http_parser_t * parser); 00354 00355 /************************************************************************ 00356 * Function: parser_parse_responseline 00357 * 00358 * Parameters: 00359 * INOUT http_parser_t* parser ; HTTP Parser object 00360 * 00361 * Description: Get HTTP Method, URL location and version information. 00362 * 00363 * Returns: 00364 * PARSE_OK 00365 * PARSE_SUCCESS 00366 * PARSE_FAILURE 00367 ************************************************************************/ 00368 parse_status_t parser_parse_responseline(INOUT http_parser_t *parser); 00369 00370 /************************************************************************ 00371 * Function: parser_parse_headers 00372 * 00373 * Parameters: 00374 * INOUT http_parser_t* parser ; HTTP Parser object 00375 * 00376 * Description: Get HTTP Method, URL location and version information. 00377 * 00378 * Returns: 00379 * PARSE_OK 00380 * PARSE_SUCCESS 00381 * PARSE_FAILURE 00382 ************************************************************************/ 00383 parse_status_t parser_parse_headers(INOUT http_parser_t *parser); 00384 00385 /************************************************************************ 00386 * Function: parser_parse_entity 00387 * 00388 * Parameters: 00389 * INOUT http_parser_t* parser ; HTTP Parser object 00390 * 00391 * Description: Determines method to read entity 00392 * 00393 * Returns: 00394 * PARSE_OK 00395 * PARSE_FAILURE 00396 * PARSE_COMPLETE -- no more reading to do 00397 ************************************************************************/ 00398 parse_status_t parser_parse_entity(INOUT http_parser_t *parser); 00399 00400 /************************************************************************ 00401 * Function: parser_get_entity_read_method 00402 * 00403 * Parameters: 00404 * INOUT http_parser_t* parser ; HTTP Parser object 00405 * 00406 * Description: Determines method to read entity 00407 * 00408 * Returns: 00409 * PARSE_OK 00410 * PARSE_FAILURE 00411 * PARSE_COMPLETE -- no more reading to do 00412 ************************************************************************/ 00413 parse_status_t parser_get_entity_read_method( INOUT http_parser_t* parser ); 00414 00415 /************************************************************************ 00416 * Function: parser_append 00417 * 00418 * Parameters: 00419 * INOUT http_parser_t* parser ; HTTP Parser Object 00420 * IN const char* buf ; buffer to be appended to the parser 00421 * buffer 00422 * IN size_t buf_length ; Size of the buffer 00423 * 00424 * Description: The parser function. Depending on the position of the 00425 * parser object the actual parsing function is invoked 00426 * 00427 * Returns: 00428 * void 00429 ************************************************************************/ 00430 parse_status_t parser_append( INOUT http_parser_t* parser, 00431 IN const char* buf, 00432 IN size_t buf_length ); 00433 00434 /************************************************************************ 00435 * Function: matchstr 00436 * 00437 * Parameters: 00438 * IN char *str ; String to be matched 00439 * IN size_t slen ; Length of the string 00440 * IN const char* fmt ; Pattern format 00441 * ... 00442 * 00443 * Description: Matches a variable parameter list with a string 00444 * and takes actions based on the data type specified. 00445 * 00446 * Returns: 00447 * PARSE_OK 00448 * PARSE_NO_MATCH -- failure to match pattern 'fmt' 00449 * PARSE_FAILURE -- 'str' is bad input 00450 ************************************************************************/ 00451 int matchstr( IN char *str, IN size_t slen, IN const char* fmt, ... ); 00452 00453 // ==================================================== 00454 // misc functions 00455 00456 00457 /************************************************************************ 00458 * Function: raw_to_int 00459 * 00460 * Parameters: 00461 * IN memptr* raw_value ; Buffer to be converted 00462 * IN int base ; Base to use for conversion 00463 * 00464 * Description: Converts raw character data to long-integer value 00465 * 00466 * Returns: 00467 * off_t 00468 ************************************************************************/ 00469 off_t raw_to_int( IN memptr* raw_value, int base ); 00470 00471 /************************************************************************ 00472 * Function: raw_find_str 00473 * 00474 * Parameters: 00475 * IN memptr* raw_value ; Buffer containg the string 00476 * IN const char* str ; Substring to be found 00477 * 00478 * Description: Find a substring from raw character string buffer 00479 * 00480 * Returns: 00481 * int - index at which the substring is found. 00482 ************************************************************************/ 00483 int raw_find_str( IN memptr* raw_value, IN const char* str ); 00484 00485 /************************************************************************ 00486 * Function: method_to_str 00487 * 00488 * Parameters: 00489 * IN http_method_t method ; HTTP method 00490 * 00491 * Description: A wrapper function that maps a method id to a method 00492 * nameConverts a http_method id stored in the HTTP Method 00493 * 00494 * Returns: 00495 * const char* ptr - Ptr to the HTTP Method * 00496 ************************************************************************/ 00497 const char* method_to_str( IN http_method_t method ); 00498 00499 /************************************************************************ 00500 * Function: print_http_headers 00501 * 00502 * Parameters: 00503 * http_message_t* hmsg ; HTTP Message object 00504 * 00505 * Description: 00506 * 00507 * Returns: 00508 * void 00509 ************************************************************************/ 00510 void print_http_headers( IN http_message_t* hmsg ); 00511 00512 #ifdef __cplusplus 00513 } // extern "C" 00514 #endif // __cplusplus 00515 00516 00517 #endif // GENLIB_NET_HTTP_HTTPPARSER_H
1.6.1