httpreadwrite.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*******************************************************************************
  2. *
  3. * Copyright (c) 2000-2003 Intel Corporation
  4. * All rights reserved.
  5. * Copyright (c) 2012 France Telecom All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * - Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * - Neither name of Intel Corporation nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  27. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. ******************************************************************************/
  32. #ifndef GENLIB_NET_HTTP_HTTPREADWRITE_H
  33. #define GENLIB_NET_HTTP_HTTPREADWRITE_H
  34. /*
  35. * \file
  36. */
  37. #include "config.h"
  38. #include "upnputil.h"
  39. #include "sock.h"
  40. #include "httpparser.h"
  41. /*! timeout in secs. */
  42. #define HTTP_DEFAULT_TIMEOUT 30
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. #ifdef WIN32
  47. struct tm *http_gmtime_r(const time_t *clock, struct tm *result);
  48. #else
  49. #define http_gmtime_r gmtime_r
  50. #endif
  51. int http_CancelHttpGet(IN void *Handle);
  52. /*!
  53. * \brief Validates URL.
  54. *
  55. * \return
  56. * \li \c UPNP_E_INVALID_URL
  57. * \li \c UPNP_E_SUCCESS
  58. */
  59. int http_FixUrl(
  60. /*! [in] URL to be validated and fixed. */
  61. uri_type *url,
  62. /*! [out] URL after being fixed. */
  63. uri_type *fixed_url);
  64. /*!
  65. * \brief Parses URL and then validates URL.
  66. *
  67. * \return
  68. * \li \c UPNP_E_INVALID_URL
  69. * \li \c UPNP_E_SUCCESS
  70. */
  71. int http_FixStrUrl(
  72. /*! [in] Character string as a URL. */
  73. const char *urlstr,
  74. /*! [in] Length of the character string. */
  75. size_t urlstrlen,
  76. /*! [out] Fixed and corrected URL. */
  77. uri_type *fixed_url);
  78. /*!
  79. * \brief Gets destination address from URL and then connects to the
  80. * remote end.
  81. *
  82. * \return Socket descriptor on success, or on error:
  83. * \li \c UPNP_E_OUTOF_SOCKET
  84. * \li \c UPNP_E_SOCKET_CONNECT
  85. */
  86. SOCKET http_Connect(
  87. /*! [in] URL containing destination information. */
  88. uri_type *destination_url,
  89. /*! [out] Fixed and corrected URL. */
  90. uri_type *url);
  91. /************************************************************************
  92. * Function: http_RecvMessage
  93. *
  94. * Parameters:
  95. * IN SOCKINFO *info; Socket information object
  96. * OUT http_parser_t* parser; HTTP parser object
  97. * IN http_method_t request_method; HTTP request method
  98. * IN OUT int* timeout_secs; time out
  99. * OUT int* http_error_code; HTTP error code returned
  100. *
  101. * Description:
  102. * Get the data on the socket and take actions based on the read data
  103. * to modify the parser objects buffer. If an error is reported while
  104. * parsing the data, the error code is passed in the http_errr_code
  105. * parameter
  106. *
  107. * Returns:
  108. * UPNP_E_BAD_HTTPMSG
  109. * UPNP_E_SUCCESS
  110. ************************************************************************/
  111. int http_RecvMessage( IN SOCKINFO *info, OUT http_parser_t* parser,
  112. IN http_method_t request_method,
  113. IN OUT int* timeout_secs,
  114. OUT int* http_error_code );
  115. /*!
  116. * \brief Sends a message to the destination based on the format parameter.
  117. *
  118. * fmt types:
  119. * \li \c 'f': arg = "const char *" file name
  120. * \li \c 'b': arg1 = "const char *" mem_buffer; arg2 = "size_t" buffer length.
  121. * \li \c 'I': arg = "struct SendInstruction *"
  122. *
  123. * E.g.:
  124. \verbatim
  125. char *buf = "POST /xyz.cgi http/1.1\r\n\r\n";
  126. char *filename = "foo.dat";
  127. int status = http_SendMessage(tcpsock, "bf",
  128. buf, strlen(buf), // args for memory buffer
  129. filename); // arg for file
  130. \endverbatim
  131. *
  132. * \return
  133. * \li \c UPNP_E_OUTOF_MEMORY
  134. * \li \c UPNP_E_FILE_READ_ERROR
  135. * \li \c UPNP_E_SUCCESS
  136. */
  137. int http_SendMessage(
  138. /* [in] Socket information object. */
  139. SOCKINFO *info,
  140. /* [in,out] Time out value. */
  141. int* timeout_secs,
  142. /* [in] Pattern format to take actions upon. */
  143. const char* fmt,
  144. /* [in] Variable parameter list. */
  145. ...);
  146. /************************************************************************
  147. * Function: http_RequestAndResponse
  148. *
  149. * Parameters:
  150. * IN uri_type* destination; Destination URI object which contains
  151. * remote IP address among other elements
  152. * IN const char* request; Request to be sent
  153. * IN size_t request_length; Length of the request
  154. * IN http_method_t req_method; HTTP Request method
  155. * IN int timeout_secs; time out value
  156. * OUT http_parser_t* response; Parser object to receive the repsonse
  157. *
  158. * Description:
  159. * Initiates socket, connects to the destination, sends a
  160. * request and waits for the response from the remote end
  161. *
  162. * Returns:
  163. * UPNP_E_SOCKET_ERROR
  164. * UPNP_E_SOCKET_CONNECT
  165. * Error Codes returned by http_SendMessage
  166. * Error Codes returned by http_RecvMessage
  167. ************************************************************************/
  168. int http_RequestAndResponse(
  169. IN uri_type* destination,
  170. IN const char* request,
  171. IN size_t request_length,
  172. IN http_method_t req_method,
  173. IN int timeout_secs,
  174. OUT http_parser_t* response );
  175. /************************************************************************
  176. * return codes:
  177. * 0 -- success
  178. * UPNP_E_OUTOF_MEMORY
  179. * UPNP_E_TIMEDOUT
  180. * UPNP_E_BAD_REQUEST
  181. * UPNP_E_BAD_RESPONSE
  182. * UPNP_E_INVALID_URL
  183. * UPNP_E_SOCKET_READ
  184. * UPNP_E_SOCKET_WRITE
  185. ************************************************************************/
  186. /************************************************************************
  187. * Function: http_Download
  188. *
  189. * Parameters:
  190. * IN const char* url_str; String as a URL
  191. * IN int timeout_secs; time out value
  192. * OUT char** document; buffer to store the document extracted
  193. * from the donloaded message.
  194. * OUT size_t* doc_length; length of the extracted document
  195. * OUT char* content_type; Type of content
  196. *
  197. * Description:
  198. * Download the document message and extract the document
  199. * from the message.
  200. *
  201. * Return: int
  202. * UPNP_E_SUCCESS
  203. * UPNP_E_INVALID_URL
  204. ************************************************************************/
  205. int http_Download(
  206. IN const char* url,
  207. IN int timeout_secs,
  208. OUT char** document,
  209. OUT size_t *doc_length,
  210. OUT char* content_type );
  211. /************************************************************************
  212. * Function: http_WriteHttpPost
  213. *
  214. * Parameters:
  215. * IN void *Handle: Handle to the http post object
  216. * IN char *buf: Buffer to send to peer, if format used
  217. * is not UPNP_USING_CHUNKED,
  218. * IN size_t *size: Size of the data to be sent.
  219. * IN int timeout: time out value
  220. *
  221. * Description:
  222. * Formats data if format used is UPNP_USING_CHUNKED.
  223. * Writes data on the socket connected to the peer.
  224. *
  225. * Return: int
  226. * UPNP_E_SUCCESS - On Success
  227. * UPNP_E_INVALID_PARAM - Invalid Parameter
  228. * -1 - On Socket Error.
  229. ************************************************************************/
  230. int http_WriteHttpPost(IN void *Handle,
  231. IN char *buf,
  232. IN size_t *size,
  233. IN int timeout);
  234. /************************************************************************
  235. * Function: http_CloseHttpPost
  236. *
  237. * Parameters:
  238. * IN void *Handle; Handle to the http post object
  239. * IN OUT int *httpStatus; HTTP status returned on receiving a
  240. * response message
  241. * IN int timeout; time out value
  242. *
  243. * Description:
  244. * Sends remaining data if using UPNP_USING_CHUNKED
  245. * format. Receives any more messages. Destroys socket and any socket
  246. * associated memory. Frees handle associated with the HTTP POST msg.
  247. *
  248. * Return: int
  249. * UPNP_E_SUCCESS - On success
  250. * UPNP_E_INVALID_PARAM - Invalid Parameter
  251. ************************************************************************/
  252. int http_CloseHttpPost(IN void *Handle,
  253. IN OUT int *httpStatus,
  254. IN int timeout);
  255. /************************************************************************
  256. * Function: http_OpenHttpPost
  257. *
  258. * Parameters:
  259. * IN const char *url_str; String as a URL
  260. * IN OUT void **Handle; Pointer to buffer to store HTTP
  261. * post handle
  262. * IN const char *contentType; Type of content
  263. * IN int contentLength; length of content
  264. * IN int timeout; time out value
  265. *
  266. * Description:
  267. * Makes the HTTP POST message, connects to the peer,
  268. * sends the HTTP POST request. Adds the post handle to buffer of
  269. * such handles
  270. *
  271. * Return : int;
  272. * UPNP_E_SUCCESS - On success
  273. * UPNP_E_INVALID_PARAM - Invalid Parameter
  274. * UPNP_E_OUTOF_MEMORY
  275. * UPNP_E_SOCKET_ERROR
  276. * UPNP_E_SOCKET_CONNECT
  277. ************************************************************************/
  278. int http_OpenHttpPost(IN const char *url_str,
  279. IN OUT void **Handle,
  280. IN const char *contentType,
  281. IN int contentLength,
  282. IN int timeout);
  283. /************************************************************************
  284. * Function: http_ReadHttpGet
  285. *
  286. * Parameters:
  287. * IN void *Handle; Handle to the HTTP get object
  288. * IN OUT char *buf; Buffer to get the read and parsed data
  289. * IN OUT size_t *size; Size of the buffer passed
  290. * IN int timeout; time out value
  291. *
  292. * Description:
  293. * Parses already existing data, then gets new data.
  294. * Parses and extracts information from the new data.
  295. *
  296. * Return: int
  297. * UPNP_E_SUCCESS - On success
  298. * UPNP_E_INVALID_PARAM - Invalid Parameter
  299. * UPNP_E_BAD_RESPONSE
  300. * UPNP_E_BAD_HTTPMSG
  301. * UPNP_E_CANCELED
  302. ************************************************************************/
  303. int http_ReadHttpGet(
  304. IN void *Handle,
  305. IN OUT char *buf,
  306. IN OUT size_t *size,
  307. IN int timeout);
  308. /************************************************************************
  309. * Function: http_HttpGetProgress
  310. *
  311. * Parameters:
  312. * IN void *Handle; Handle to the HTTP get object
  313. * OUT size_t *length; Buffer to get the read and parsed data
  314. * OUT size_t *total; Size of tge buffer passed
  315. *
  316. * Description:
  317. * Extracts information from the Handle to the HTTP get object.
  318. *
  319. * Return: int
  320. * UPNP_E_SUCCESS - On Sucess
  321. * UPNP_E_INVALID_PARAM - Invalid Parameter
  322. ************************************************************************/
  323. int http_HttpGetProgress(
  324. IN void *Handle,
  325. OUT size_t *length,
  326. OUT size_t *total);
  327. /************************************************************************
  328. * Function: http_CloseHttpGet
  329. *
  330. * Parameters:
  331. * IN void *Handle; Handle to HTTP get object
  332. *
  333. * Description:
  334. * Clears the handle allocated for the HTTP GET operation
  335. * Clears socket states and memory allocated for socket operations.
  336. *
  337. * Return: int
  338. * UPNP_E_SUCCESS - On Success
  339. * UPNP_E_INVALID_PARAM - Invalid Parameter
  340. ************************************************************************/
  341. int http_CloseHttpGet(IN void *Handle);
  342. /*!
  343. * \brief Makes the HTTP GET message, connects to the peer,
  344. * sends the HTTP GET request, gets the response and parses the response.
  345. *
  346. * If a proxy URL is defined then the connection is made there.
  347. *
  348. * \return integer
  349. * \li \c UPNP_E_SUCCESS - On Success
  350. * \li \c UPNP_E_INVALID_PARAM - Invalid Paramters
  351. * \li \c UPNP_E_OUTOF_MEMORY
  352. * \li \c UPNP_E_SOCKET_ERROR
  353. * \li \c UPNP_E_BAD_RESPONSE
  354. */
  355. int http_OpenHttpGet(
  356. /* [in] String as a URL. */
  357. const char *url_str,
  358. /* [in,out] Pointer to buffer to store HTTP post handle. */
  359. void **Handle,
  360. /* [in,out] Type of content. */
  361. char **contentType,
  362. /* [out] length of content. */
  363. int *contentLength,
  364. /* [out] HTTP status returned on receiving a response message. */
  365. int *httpStatus,
  366. /* [in] time out value. */
  367. int timeout);
  368. /*!
  369. * \brief Makes the HTTP GET message, connects to the peer,
  370. * sends the HTTP GET request, gets the response and parses the response.
  371. *
  372. * If a proxy URL is defined then the connection is made there.
  373. *
  374. * \return integer
  375. * \li \c UPNP_E_SUCCESS - On Success
  376. * \li \c UPNP_E_INVALID_PARAM - Invalid Paramters
  377. * \li \c UPNP_E_OUTOF_MEMORY
  378. * \li \c UPNP_E_SOCKET_ERROR
  379. * \li \c UPNP_E_BAD_RESPONSE
  380. */
  381. int http_OpenHttpGetProxy(
  382. /* [in] String as a URL. */
  383. const char *url_str,
  384. /* [in] String as a URL. */
  385. const char *proxy_str,
  386. /* [in,out] Pointer to buffer to store HTTP post handle. */
  387. void **Handle,
  388. /* [in,out] Type of content. */
  389. char **contentType,
  390. /* [out] length of content. */
  391. int *contentLength,
  392. /* [out] HTTP status returned on receiving a response message. */
  393. int *httpStatus,
  394. /* [in] time out value. */
  395. int timeout);
  396. /************************************************************************
  397. * Function: http_SendStatusResponse
  398. *
  399. * Parameters:
  400. * IN SOCKINFO *info; Socket information object
  401. * IN int http_status_code; error code returned while making
  402. * or sending the response message
  403. * IN int request_major_version; request major version
  404. * IN int request_minor_version; request minor version
  405. *
  406. * Description:
  407. * Generate a response message for the status query and send the
  408. * status response.
  409. *
  410. * Return: int
  411. * 0 -- success
  412. * UPNP_E_OUTOF_MEMORY
  413. * UPNP_E_SOCKET_WRITE
  414. * UPNP_E_TIMEDOUT
  415. ************************************************************************/
  416. int http_SendStatusResponse(
  417. IN SOCKINFO *info,
  418. IN int http_status_code,
  419. IN int request_major_version,
  420. IN int request_minor_version );
  421. /*!
  422. * \brief Generate an HTTP message based on the format that is specified in
  423. * the input parameters.
  424. *
  425. \verbatim
  426. Format types:
  427. 'B': arg = int status_code -- appends content-length, content-type and HTML body for given code.
  428. 'b': arg1 = const char *buf;
  429. arg2 = size_t buf_length memory ptr
  430. 'C': (no args) -- appends a HTTP CONNECTION: close header depending on major, minor version.
  431. 'c': (no args) -- appends CRLF "\r\n"
  432. 'D': (no args) -- appends HTTP DATE: header
  433. 'd': arg = int number -- appends decimal number
  434. 'G': arg = range information -- add range header
  435. 'h': arg = off_t number -- appends off_t number
  436. 'K': (no args) -- add chunky header
  437. 'L': arg = language information -- add Content-Language header if Accept-Language header is not empty and if
  438. WEB_SERVER_CONTENT_LANGUAGE is not empty
  439. 'N': arg1 = off_t content_length -- content-length header
  440. 'q': arg1 = http_method_t -- request start line and HOST header
  441. arg2 = (uri_type *)
  442. 'Q': arg1 = http_method_t; -- start line of request
  443. arg2 = char* url;
  444. arg3 = size_t url_length
  445. 'R': arg = int status_code -- adds a response start line
  446. 'S': (no args) -- appends HTTP SERVER: header
  447. 's': arg = const char * -- C_string
  448. 'T': arg = char * content_type; -- format e.g: "text/html"; content-type header
  449. 't': arg = time_t * gmt_time -- appends time in RFC 1123 fmt
  450. 'U': (no args) -- appends HTTP USER-AGENT: header
  451. 'X': arg = const char * -- useragent; "redsonic" HTTP X-User-Agent: useragent
  452. \endverbatim
  453. *
  454. * \return
  455. * \li \c 0 - On Success
  456. * \li \c UPNP_E_OUTOF_MEMORY
  457. * \li \c UPNP_E_INVALID_URL
  458. */
  459. int http_MakeMessage(
  460. /* [in,out] Buffer with the contents of the message. */
  461. INOUT membuffer* buf,
  462. /* [in] HTTP major version. */
  463. IN int http_major_version,
  464. /* [in] HTTP minor version. */
  465. IN int http_minor_version,
  466. /* [in] Pattern format. */
  467. IN const char* fmt,
  468. /* [in] Format arguments. */
  469. ... );
  470. /************************************************************************
  471. * Function: http_CalcResponseVersion
  472. *
  473. * Parameters:
  474. * IN int request_major_vers; Request major version
  475. * IN int request_minor_vers; Request minor version
  476. * OUT int* response_major_vers; Response mojor version
  477. * OUT int* response_minor_vers; Response minor version
  478. *
  479. * Description:
  480. * Calculate HTTP response versions based on the request versions.
  481. *
  482. * Return: void
  483. ************************************************************************/
  484. void http_CalcResponseVersion(
  485. IN int request_major_vers,
  486. IN int request_minor_vers,
  487. OUT int* response_major_vers,
  488. OUT int* response_minor_vers );
  489. /************************************************************************
  490. * Function: http_OpenHttpGetEx
  491. *
  492. * Parameters:
  493. * IN const char *url_str; String as a URL
  494. * IN OUT void **Handle; Pointer to buffer to store HTTP
  495. * post handle
  496. * IN OUT char **contentType; Type of content
  497. * OUT int *contentLength; length of content
  498. * OUT int *httpStatus; HTTP status returned on receiving a
  499. * response message
  500. * IN int timeout; time out value
  501. *
  502. * Description:
  503. * Makes the HTTP GET message, connects to the peer,
  504. * sends the HTTP GET request, gets the response and parses the
  505. * response.
  506. *
  507. * Return: int
  508. * UPNP_E_SUCCESS - On Success
  509. * UPNP_E_INVALID_PARAM - Invalid Paramters
  510. * UPNP_E_OUTOF_MEMORY
  511. * UPNP_E_SOCKET_ERROR
  512. * UPNP_E_BAD_RESPONSE
  513. ************************************************************************/
  514. int http_OpenHttpGetEx(IN const char *url_str,
  515. IN OUT void **Handle,
  516. IN OUT char **contentType,
  517. OUT int *contentLength,
  518. OUT int *httpStatus,
  519. IN int lowRange,
  520. IN int highRange,
  521. IN int timeout);
  522. /************************************************************************
  523. * Function: get_sdk_info
  524. *
  525. * Parameters:
  526. * OUT char *info; buffer to store the operating system information
  527. * IN size_t infoSize; size of buffer
  528. *
  529. * Description:
  530. * Returns the server information for the operating system
  531. *
  532. * Return:
  533. * UPNP_INLINE void
  534. ************************************************************************/
  535. void get_sdk_info( OUT char *info, IN size_t infoSize );
  536. #ifdef __cplusplus
  537. } /* #extern "C" */
  538. #endif
  539. #endif /* GENLIB_NET_HTTP_HTTPREADWRITE_H */