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: httpreadwrite.h 2081 2010-03-23 20:18:00Z lww $ 00051 */ 00052 00053 #ifndef GENLIB_NET_HTTP_HTTPREADWRITE_H 00054 #define GENLIB_NET_HTTP_HTTPREADWRITE_H 00055 00056 #include "config.h" 00057 #include "util.h" 00058 #include "sock.h" 00059 #include "httpparser.h" 00060 00061 // timeout in secs 00062 #define HTTP_DEFAULT_TIMEOUT 30 00063 00064 00065 00066 #ifdef __cplusplus 00067 #extern "C" { 00068 #endif 00069 00070 int 00071 http_CancelHttpGet( IN void *Handle ); 00072 00073 /************************************************************************ 00074 * Function: http_FixUrl 00075 * 00076 * Parameters: 00077 * IN uri_type* url ; URL to be validated and fixed 00078 * OUT uri_type* fixed_url ; URL after being fixed. 00079 * 00080 * Description: Validates URL 00081 * 00082 * Returns: 00083 * UPNP_E_INVALID_URL 00084 * UPNP_E_SUCCESS 00085 ************************************************************************/ 00086 int http_FixUrl( IN uri_type* url, OUT uri_type* fixed_url ); 00087 00088 /************************************************************************ 00089 * Function: http_FixStrUrl 00090 * 00091 * Parameters: 00092 * IN char* urlstr ; Character string as a URL 00093 * IN int urlstrlen ; Length of the character string 00094 * OUT uri_type* fixed_url ; Fixed and corrected URL 00095 * 00096 * Description: Parses URL and then validates URL 00097 * 00098 * Returns: 00099 * UPNP_E_INVALID_URL 00100 * UPNP_E_SUCCESS 00101 ************************************************************************/ 00102 int http_FixStrUrl( IN char* urlstr, IN int urlstrlen, OUT uri_type* fixed_url ); 00103 00104 00105 /************************************************************************ 00106 * Function: http_Connect 00107 * 00108 * Parameters: 00109 * IN uri_type* destination_url ; URL containing destination information 00110 * OUT uri_type *url ; Fixed and corrected URL 00111 * 00112 * Description: Gets destination address from URL and then connects to the 00113 * remote end 00114 * 00115 * Returns: 00116 * socket descriptor on sucess 00117 * UPNP_E_OUTOF_SOCKET 00118 * UPNP_E_SOCKET_CONNECT on error 00119 ************************************************************************/ 00120 int http_Connect( IN uri_type* destination_url, OUT uri_type *url ); 00121 00122 /************************************************************************ 00123 * Function: http_RecvMessage 00124 * 00125 * Parameters: 00126 * IN SOCKINFO *info ; Socket information object 00127 * OUT http_parser_t* parser, HTTP parser object 00128 * IN http_method_t request_method ; HTTP request method 00129 * IN OUT int* timeout_secs ; time out 00130 * OUT int* http_error_code ; HTTP error code returned 00131 * 00132 * Description: Get the data on the socket and take actions based on the 00133 * read data to modify the parser objects buffer. If an error is reported 00134 * while parsing the data, the error code is passed in the http_errr_code 00135 * parameter 00136 * 00137 * Returns: 00138 * UPNP_E_BAD_HTTPMSG 00139 * UPNP_E_SUCCESS 00140 ************************************************************************/ 00141 int http_RecvMessage( IN SOCKINFO *info, OUT http_parser_t* parser, 00142 IN http_method_t request_method, 00143 IN OUT int* timeout_secs, 00144 OUT int* http_error_code ); 00145 00146 00147 /************************************************************************ 00148 * Function: http_SendMessage 00149 * 00150 * Parameters: 00151 * IN SOCKINFO *info ; Socket information object 00152 * IN OUT int * TimeOut ; time out value 00153 * IN const char* fmt, ... Pattern format to take actions upon 00154 * 00155 * Description: Sends a message to the destination based on the 00156 * IN const char* fmt parameter 00157 * fmt types: 00158 * 'f': arg = const char * file name 00159 * 'm': arg1 = const char * mem_buffer; arg2= size_t buf_length 00160 * E.g.: 00161 * char *buf = "POST /xyz.cgi http/1.1\r\n\r\n"; 00162 * char *filename = "foo.dat"; 00163 * int status = http_SendMessage( tcpsock, "mf", 00164 * buf, strlen(buf), // args for memory buffer 00165 * filename ); // arg for file 00166 * 00167 * Returns: 00168 * UPNP_E_OUTOF_MEMORY 00169 * UPNP_E_FILE_READ_ERROR 00170 * UPNP_E_SUCCESS 00171 ************************************************************************/ 00172 int http_SendMessage( IN SOCKINFO *info, IN OUT int* timeout_secs, 00173 IN const char* fmt, ... ); 00174 00175 /************************************************************************ 00176 * Function: http_RequestAndResponse 00177 * 00178 * Parameters: 00179 * IN uri_type* destination ; Destination URI object which contains 00180 * remote IP address among other elements 00181 * IN const char* request ; Request to be sent 00182 * IN size_t request_length ; Length of the request 00183 * IN http_method_t req_method ; HTTP Request method 00184 * IN int timeout_secs ; time out value 00185 * OUT http_parser_t* response ; Parser object to receive the repsonse 00186 * 00187 * Description: Initiates socket, connects to the destination, sends a 00188 * request and waits for the response from the remote end 00189 * 00190 * Returns: 00191 * UPNP_E_SOCKET_ERROR 00192 * UPNP_E_SOCKET_CONNECT 00193 * Error Codes returned by http_SendMessage 00194 * Error Codes returned by http_RecvMessage 00195 ************************************************************************/ 00196 int http_RequestAndResponse( IN uri_type* destination, 00197 IN const char* request, IN size_t request_length, 00198 IN http_method_t req_method, 00199 IN int timeout_secs, 00200 OUT http_parser_t* response ); 00201 00202 /************************************************************************ 00203 * return codes: 00204 * 0 -- success 00205 * UPNP_E_OUTOF_MEMORY 00206 * UPNP_E_TIMEDOUT 00207 * UPNP_E_BAD_REQUEST 00208 * UPNP_E_BAD_RESPONSE 00209 * UPNP_E_INVALID_URL 00210 * UPNP_E_SOCKET_READ 00211 * UPNP_E_SOCKET_WRITE 00212 ************************************************************************/ 00213 00214 /************************************************************************ 00215 * Function : http_Download 00216 * 00217 * Parameters : 00218 * IN const char* url_str : String as a URL 00219 * IN int timeout_secs : time out value 00220 * OUT char** document : buffer to store the document extracted 00221 * from the donloaded message. 00222 * OUT int* doc_length : length of the extracted document 00223 * OUT char* content_type : Type of content 00224 * 00225 * Description : Download the document message and extract the document 00226 * from the message. 00227 * 00228 * Return : int; 00229 * UPNP_E_SUCCESS; 00230 * UPNP_E_INVALID_URL; 00231 * 00232 * 00233 * Note : 00234 ************************************************************************/ 00235 int http_Download( IN const char* url, 00236 IN int timeout_secs, 00237 OUT char** document, OUT int* doc_length, 00238 OUT char* content_type ); 00239 00240 00241 /************************************************************************ 00242 * Function : http_WriteHttpPost 00243 * 00244 * Parameters : 00245 * IN void *Handle : Handle to the http post object 00246 * IN char *buf : Buffer to send to peer, if format used 00247 * is not UPNP_USING_CHUNKED, 00248 * IN off_t *size : Size of the data to be sent. 00249 * IN int timeout : time out value 00250 * 00251 * Description : Formats data if format used is UPNP_USING_CHUNKED. 00252 * Writes data on the socket connected to the peer. 00253 * 00254 * Return : int ; 00255 * UPNP_E_SUCCESS - On Success 00256 * UPNP_E_INVALID_PARAM - Invalid Parameter 00257 * -1 - On Socket Error. 00258 * 00259 * Note : 00260 ************************************************************************/ 00261 int http_WriteHttpPost(IN void *Handle, 00262 IN char *buf, 00263 IN off_t *size, 00264 IN int timeout); 00265 00266 /************************************************************************ 00267 * Function : http_CloseHttpPost 00268 * 00269 * Parameters : 00270 * IN void *Handle : Handle to the http post object 00271 * IN OUT int *httpStatus : HTTP status returned on receiving a 00272 * response message 00273 * IN int timeout : time out value 00274 * 00275 * Description : Sends remaining data if using UPNP_USING_CHUNKED 00276 * format. Receives any more messages. Destroys socket and any socket 00277 * associated memory. Frees handle associated with the HTTP POST msg. 00278 * 00279 * Return : int ; 00280 * UPNP_E_SUCCESS - On Sucess ; 00281 * UPNP_E_INVALID_PARAM - Invalid Parameter; 00282 * 00283 * Note : 00284 ************************************************************************/ 00285 int http_CloseHttpPost(IN void *Handle, 00286 IN OUT int *httpStatus, 00287 IN int timeout); 00288 00289 /************************************************************************ 00290 * Function : http_OpenHttpPost 00291 * 00292 * Parameters : 00293 * IN const char *url_str : String as a URL 00294 * IN OUT void **Handle : Pointer to buffer to store HTTP 00295 * post handle 00296 * IN const char *contentType : Type of content 00297 * IN off_t contentLength : length of content 00298 * IN int timeout : time out value 00299 * 00300 * Description : Makes the HTTP POST message, connects to the peer, 00301 * sends the HTTP POST request. Adds the post handle to buffer of 00302 * such handles 00303 * 00304 * Return : int; 00305 * UPNP_E_SUCCESS - On Sucess ; 00306 * UPNP_E_INVALID_PARAM - Invalid Paramter ; 00307 * UPNP_E_OUTOF_MEMORY ; 00308 * UPNP_E_SOCKET_ERROR ; 00309 * UPNP_E_SOCKET_CONNECT ; 00310 * 00311 * Note : 00312 ************************************************************************/ 00313 int http_OpenHttpPost(IN const char *url_str, 00314 IN OUT void **Handle, 00315 IN const char *contentType, 00316 IN off_t contentLength, 00317 IN int timeout); 00318 00319 /************************************************************************ 00320 * Function : http_ReadHttpGet 00321 * 00322 * Parameters : 00323 * IN void *Handle : Handle to the HTTP get object 00324 * IN OUT char *buf : Buffer to get the read and parsed data 00325 * IN OUT off_t *size : Size of tge buffer passed 00326 * IN int timeout : time out value 00327 * 00328 * Description : Parses already existing data, then gets new data. 00329 * Parses and extracts information from the new data. 00330 * 00331 * Return : int ; 00332 * UPNP_E_SUCCESS - On Sucess ; 00333 * UPNP_E_INVALID_PARAM - Invalid Parameter; 00334 * UPNP_E_BAD_RESPONSE ; 00335 * UPNP_E_BAD_HTTPMSG ; 00336 * 00337 * Note : 00338 ************************************************************************/ 00339 int http_ReadHttpGet(IN void *Handle, 00340 IN OUT char *buf, 00341 IN OUT off_t *size, 00342 IN int timeout); 00343 00344 /************************************************************************ 00345 * Function : http_HttpGetProgress 00346 * 00347 * Parameters : 00348 * IN void *Handle : Handle to the HTTP get object 00349 * OUT off_t *length : Buffer to get the read and parsed data 00350 * OUT off_t *total : Size of tge buffer passed 00351 * 00352 * Description : Extracts information from the Handle to the HTTP get 00353 * object. 00354 * 00355 * Return : int ; 00356 * UPNP_E_SUCCESS - On Sucess ; 00357 * UPNP_E_INVALID_PARAM - Invalid Parameter; 00358 * 00359 * Note : 00360 ************************************************************************/ 00361 int http_HttpGetProgress(IN void *Handle, 00362 OUT off_t *length, 00363 OUT off_t *total ); 00364 00365 /************************************************************************ 00366 * Function : http_CloseHttpGet 00367 * 00368 * Parameters : 00369 * IN void *Handle ; Handle to HTTP get object 00370 * 00371 * Description : Clears the handle allocated for the HTTP GET operation 00372 * Clears socket states and memory allocated for socket operations. 00373 * 00374 * Return : int ; 00375 * UPNP_E_SUCCESS - On Success 00376 * UPNP_E_INVALID_PARAM - Invalid Parameter 00377 * 00378 * Note : 00379 ************************************************************************/ 00380 int http_CloseHttpGet(IN void *Handle); 00381 00382 /************************************************************************ 00383 * Function : http_OpenHttpGet 00384 * 00385 * Parameters : 00386 * IN const char *url_str : String as a URL 00387 * IN OUT void **Handle : Pointer to buffer to store HTTP 00388 * post handle 00389 * IN OUT char **contentType : Type of content 00390 * OUT off_t *contentLength : length of content 00391 * OUT int *httpStatus : HTTP status returned on receiving a 00392 * response message 00393 * IN int timeout : time out value 00394 * 00395 * Description : Makes the HTTP GET message, connects to the peer, 00396 * sends the HTTP GET request, gets the response and parses the 00397 * response. 00398 * 00399 * Return : int; 00400 * UPNP_E_SUCCESS - On Success ; 00401 * UPNP_E_INVALID_PARAM - Invalid Paramters ; 00402 * UPNP_E_OUTOF_MEMORY ; 00403 * UPNP_E_SOCKET_ERROR ; 00404 * UPNP_E_BAD_RESPONSE ; 00405 * 00406 * Note : 00407 * 00408 ************************************************************************/ 00409 int http_OpenHttpGet(IN const char *url_str, 00410 IN OUT void **Handle, 00411 IN OUT char **contentType, 00412 OUT off_t *contentLength, 00413 OUT int *httpStatus, 00414 IN int timeout); 00415 00416 /************************************************************************ 00417 * Function : http_OpenHttpGetProxy 00418 * 00419 * Parameters : 00420 * IN const char *url_str : String as a URL 00421 * IN const char *proxy_str : String as a URL to the proxy 00422 * IN OUT void **Handle : Pointer to buffer to store HTTP 00423 * post handle 00424 * IN OUT char **contentType : Type of content 00425 * OUT off_t *contentLength : length of content 00426 * OUT int *httpStatus : HTTP status returned on receiving a 00427 * response message 00428 * IN int timeout : time out value 00429 * 00430 * Description : Makes the HTTP GET message, connects to the peer, 00431 * sends the HTTP GET request, gets the response and parses the 00432 * response. 00433 * 00434 * Return : int; 00435 * UPNP_E_SUCCESS - On Success ; 00436 * UPNP_E_INVALID_PARAM - Invalid Paramters ; 00437 * UPNP_E_OUTOF_MEMORY ; 00438 * UPNP_E_SOCKET_ERROR ; 00439 * UPNP_E_BAD_RESPONSE ; 00440 * 00441 * Note : 00442 * 00443 ************************************************************************/ 00444 int http_OpenHttpGetProxy(IN const char *url_str, 00445 IN const char *proxy_str, 00446 IN OUT void **Handle, 00447 IN OUT char **contentType, 00448 OUT off_t *contentLength, 00449 OUT int *httpStatus, 00450 IN int timeout); 00451 00452 00453 /************************************************************************ 00454 * Function : http_SendStatusResponse 00455 * 00456 * Parameters : 00457 * IN SOCKINFO *info : Socket information object 00458 * IN int http_status_code : error code returned while making 00459 * or sending the response message 00460 * IN int request_major_version : request major version 00461 * IN int request_minor_version : request minor version 00462 * 00463 * Description : Generate a response message for the status query and 00464 * send the status response. 00465 * 00466 * Return : int; 00467 * 0 -- success 00468 * UPNP_E_OUTOF_MEMORY 00469 * UPNP_E_SOCKET_WRITE 00470 * UPNP_E_TIMEDOUT 00471 * 00472 * Note : 00473 ************************************************************************/ 00474 int http_SendStatusResponse( IN SOCKINFO *info, IN int http_status_code, 00475 IN int request_major_version, 00476 IN int request_minor_version ); 00477 00478 00479 /************************************************************************ 00480 * Function : http_MakeMessage 00481 * 00482 * Parameters : 00483 * INOUT membuffer* buf : buffer with the contents of the 00484 * message 00485 * IN int http_major_version : HTTP major version 00486 * IN int http_minor_version : HTTP minor version 00487 * IN const char* fmt : Pattern format 00488 * ... : 00489 * 00490 * Description : Generate an HTTP message based on the format that is 00491 * specified in the input parameters. 00492 * 00493 * fmt types: 00494 * 's': arg = const char* C_string 00495 * 'b': arg1 = const char* buf; arg2 = size_t buf_length 00496 * memory ptr 00497 * 'c': (no args) appends CRLF "\r\n" 00498 * 'd': arg = int number // appends decimal number 00499 * 'h': arg = off_t number // appends off_t number 00500 * 't': arg = time_t * gmt_time // appends time in RFC 1123 fmt 00501 * 'D': (no args) appends HTTP DATE: header 00502 * 'S': (no args) appends HTTP SERVER: header 00503 * 'U': (no args) appends HTTP USER-AGENT: header 00504 * 'C': (no args) appends a HTTP CONNECTION: close header 00505 * depending on major,minor version 00506 * 'N': arg1 = off_t content_length // content-length header 00507 * 'Q': arg1 = http_method_t; arg2 = char* url; 00508 * arg3 = int url_length // start line of request 00509 * 'R': arg = int status_code // adds a response start line 00510 * 'B': arg = int status_code 00511 * appends content-length, content-type and HTML body for given code 00512 * 'T': arg = char * content_type; format e.g: "text/html"; 00513 * content-type header 00514 * 'H': arg = *userHTTPHeaderList 00515 * 'A': arg = const char* C_string 00516 * adds a custom HTTP header to a specific response (filled out 00517 * by the user in the File_Info struct), may be NULL 00518 * 00519 * Return : int; 00520 * 0 - On Success 00521 * UPNP_E_OUTOF_MEMORY 00522 * UPNP_E_INVALID_URL; 00523 * 00524 * Note : 00525 ************************************************************************/ 00526 int http_MakeMessage( INOUT membuffer* buf, 00527 IN int http_major_version, 00528 IN int http_minor_version, 00529 IN const char* fmt, ... ); 00530 00531 /************************************************************************ 00532 * Function : http_CalcResponseVersion 00533 * 00534 * Parameters : 00535 * IN int request_major_vers : Request major version 00536 * IN int request_minor_vers : Request minor version 00537 * OUT int* response_major_vers : Response mojor version 00538 * OUT int* response_minor_vers : Response minor version 00539 * 00540 * Description : Calculate HTTP response versions based on the request 00541 * versions. 00542 * 00543 * Return : void 00544 * 00545 * Note : 00546 ************************************************************************/ 00547 void http_CalcResponseVersion( 00548 IN int request_major_vers, IN int request_minor_vers, 00549 OUT int* response_major_vers, OUT int* response_minor_vers ); 00550 00551 /************************************************************************ 00552 * Function : http_OpenHttpGetEx 00553 * 00554 * Parameters : 00555 * IN const char *url_str : String as a URL 00556 * IN OUT void **Handle : Pointer to buffer to store HTTP 00557 * post handle 00558 * IN OUT char **contentType : Type of content 00559 * OUT off_t *contentLength : length of content 00560 * OUT int *httpStatus : HTTP status returned on receiving a 00561 * response message 00562 * IN int timeout : time out value 00563 * 00564 * Description : Makes the HTTP GET message, connects to the peer, 00565 * sends the HTTP GET request, gets the response and parses the 00566 * response. 00567 * 00568 * Return : int; 00569 * UPNP_E_SUCCESS - On Success ; 00570 * UPNP_E_INVALID_PARAM - Invalid Paramters ; 00571 * UPNP_E_OUTOF_MEMORY ; 00572 * UPNP_E_SOCKET_ERROR ; 00573 * UPNP_E_BAD_RESPONSE ; 00574 * 00575 * Note : 00576 * 00577 ************************************************************************/ 00578 int http_OpenHttpGetEx(IN const char *url_str, 00579 IN OUT void **Handle, 00580 IN OUT char **contentType, 00581 OUT off_t *contentLength, 00582 OUT int *httpStatus, 00583 IN int lowRange, 00584 IN int highRange, 00585 IN int timeout); 00586 00587 /************************************************************************ 00588 * Function : get_sdk_info 00589 * 00590 * Parameters : 00591 * OUT char *info ; 00592 * 00593 * Description : Returns the server information for the operating 00594 * system 00595 * 00596 * Return : void ; 00597 * 00598 * Note : 00599 ************************************************************************/ 00600 void get_sdk_info( OUT char *info ); 00601 00602 #ifdef __cplusplus 00603 } // #extern "C" 00604 #endif 00605 00606 00607 #endif // GENLIB_NET_HTTP_HTTPREADWRITE_H
1.6.1