wm_http_client.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /**
  2. * @file wm_http_client.h
  3. *
  4. * @brief Http client APIs
  5. *
  6. * @author wanghf
  7. *
  8. * Copyright (c) 2014 Winner Microelectronics Co., Ltd.
  9. */
  10. #ifndef WM_HTTP_CLIENT_H
  11. #define WM_HTTP_CLIENT_H
  12. #include "wm_config.h"
  13. #include "wm_type_def.h"
  14. #ifdef BOOL
  15. #undef BOOL
  16. #endif
  17. #ifdef UCHAR
  18. #undef UCHAR
  19. #endif
  20. #ifdef CHAR
  21. #undef CHAR
  22. #endif
  23. #ifdef UINT16
  24. #undef UINT16
  25. #endif
  26. #ifdef INT16
  27. #undef INT16
  28. #endif
  29. #ifdef UINT32
  30. #undef UINT32
  31. #endif
  32. #ifdef INT32
  33. #undef INT32
  34. #endif
  35. #ifdef UINT64
  36. #undef UINT64
  37. #endif
  38. #ifdef INT64
  39. #undef INT64
  40. #endif
  41. #ifdef ULONG
  42. #undef ULONG
  43. #endif
  44. #ifdef LONG
  45. #undef LONG
  46. #endif
  47. #define VOID void
  48. typedef int BOOL;
  49. typedef unsigned char UCHAR;
  50. //typedef signed char CHAR;
  51. typedef char CHAR;
  52. typedef unsigned short UINT16;
  53. typedef signed short INT16;
  54. typedef unsigned int UINT32;
  55. typedef signed int INT32;
  56. typedef unsigned long long UINT64;
  57. typedef long long INT64;
  58. typedef unsigned long ULONG;
  59. typedef signed long LONG;
  60. /* HTTP Status, API Return codes */
  61. /** HTTP Success status */
  62. #define HTTP_CLIENT_SUCCESS 0
  63. /** Unknown error */
  64. #define HTTP_CLIENT_UNKNOWN_ERROR 1
  65. /** an Invalid handle or possible bad pointer was passed to a function */
  66. #define HTTP_CLIENT_ERROR_INVALID_HANDLE 2
  67. /** Buffer too small or a failure while in memory allocation */
  68. #define HTTP_CLIENT_ERROR_NO_MEMORY 3
  69. /** an attempt to use an invalid socket handle was made */
  70. #define HTTP_CLIENT_ERROR_SOCKET_INVALID 4
  71. /** Can't send socket parameters */
  72. #define HTTP_CLIENT_ERROR_SOCKET_CANT_SET 5
  73. /** Error while resolving host name */
  74. #define HTTP_CLIENT_ERROR_SOCKET_RESOLVE 6
  75. /** Error while connecting to the remote server */
  76. #define HTTP_CLIENT_ERROR_SOCKET_CONNECT 7
  77. /** socket time out error */
  78. #define HTTP_CLIENT_ERROR_SOCKET_TIME_OUT 8
  79. /** Error while receiving data */
  80. #define HTTP_CLIENT_ERROR_SOCKET_RECV 9
  81. /** Error while sending data */
  82. #define HTTP_CLIENT_ERROR_SOCKET_SEND 10
  83. /** Error while receiving the remote HTTP headers */
  84. #define HTTP_CLIENT_ERROR_HEADER_RECV 11
  85. /** Could not find element within header */
  86. #define HTTP_CLIENT_ERROR_HEADER_NOT_FOUND 12
  87. /** The headers search clue was too large for the internal API buffer */
  88. #define HTTP_CLIENT_ERROR_HEADER_BIG_CLUE 13
  89. /** No content length was specified for the outgoing data. the caller should
  90. specify chunking mode in the session creation */
  91. #define HTTP_CLIENT_ERROR_HEADER_NO_LENGTH 14
  92. /** The HTTP chunk token that was received from the server was too big and possibly wrong */
  93. #define HTTP_CLIENT_ERROR_CHUNK_TOO_BIG 15
  94. /** Could not authenticate with the remote host */
  95. #define HTTP_CLIENT_ERROR_AUTH_HOST 16
  96. /** Could not authenticate with the remote proxy */
  97. #define HTTP_CLIENT_ERROR_AUTH_PROXY 17
  98. /** Bad or not supported HTTP verb was passed to a function */
  99. #define HTTP_CLIENT_ERROR_BAD_VERB 18
  100. /** a function received a parameter that was too large */
  101. #define HTTP_CLIENT_ERROR_LONG_INPUT 19
  102. /** The session state prevents the current function from proceeding */
  103. #define HTTP_CLIENT_ERROR_BAD_STATE 20
  104. /** Could not parse the chunk length while in chunked transfer */
  105. #define HTTP_CLIENT_ERROR_CHUNK 21
  106. /** Could not parse curtail elements from the URL (such as the host name, HTTP prefix act') */
  107. #define HTTP_CLIENT_ERROR_BAD_URL 22
  108. /** Could not detect key elements in the received headers */
  109. #define HTTP_CLIENT_ERROR_BAD_HEADER 23
  110. /** Error while attempting to resize a buffer */
  111. #define HTTP_CLIENT_ERROR_BUFFER_RSIZE 24
  112. /** Authentication schema is not supported */
  113. #define HTTP_CLIENT_ERROR_BAD_AUTH 25
  114. /** The selected authentication schema does not match the server response */
  115. #define HTTP_CLIENT_ERROR_AUTH_MISMATCH 26
  116. /** an element was missing while parsing the digest authentication challenge */
  117. #define HTTP_CLIENT_ERROR_NO_DIGEST_TOKEN 27
  118. /** Digest algorithem could be MD5 or MD5-sess other types are not supported */
  119. #define HTTP_CLIENT_ERROR_NO_DIGEST_ALG 28
  120. /** Binding error */
  121. #define HTTP_CLIENT_ERROR_SOCKET_BIND 29
  122. /** Tls negotiation error */
  123. #define HTTP_CLIENT_ERROR_TLS_NEGO 30
  124. /** Feature is not (yet) implemented */
  125. #define HTTP_CLIENT_ERROR_NOT_IMPLEMENTED 64
  126. /** Connection closed. */
  127. #define HTTP_CLIENT_ERROR_CONNECTION_CLOSE 500
  128. /** HTTP end of stream message */
  129. #define HTTP_CLIENT_EOS 1000
  130. // HTTP Session flags (Public flags)
  131. #define HTTP_CLIENT_FLAG_KEEP_ALIVE 0x00000001 // Set the keep alive header
  132. #define HTTP_CLIENT_FLAG_SEND_CHUNKED 0x00000002 // The outgoing should chunked
  133. #define HTTP_CLIENT_FLAG_NO_CACHE 0x00000004 // Set the no cache header
  134. #define HTTP_CLIENT_FLAG_ASYNC 0x00000008 // Currently not implemented
  135. #define HTTP_CLIENT_FLAG_MULTIPART_FORM 0x00000010 // The outgoing should multipart/form-data
  136. // HTTP Type Definitions
  137. typedef UINT32 HTTP_SESSION_HANDLE;
  138. typedef UINT32 HTTP_CLIENT_SESSION_FLAGS;
  139. /******************************************************************************
  140. *
  141. * Section : HTTP API structures
  142. *
  143. ******************************************************************************/
  144. /* HTTP Type Definitions */
  145. /** http seesion handle */
  146. typedef u32 tls_http_session_handle_t;
  147. /** http seesion flags */
  148. typedef u32 tls_http_session_flags_t;
  149. /** HTTP Supported authentication methods */
  150. typedef enum _HTTP_AUTH_SCHEMA
  151. {
  152. AuthSchemaNone = 0,
  153. AuthSchemaBasic,
  154. AuthSchemaDigest,
  155. AuthSchemaKerberos,
  156. AuthNotSupported
  157. } HTTP_AUTH_SCHEMA;
  158. /** HTTP supported verbs */
  159. typedef enum _HTTP_VERB
  160. {
  161. VerbGet = 0,
  162. VerbHead,
  163. VerbPost,
  164. VerbPut,
  165. VerbFwup,
  166. VerbNotSupported
  167. // Note: others verb such as connect and put are currently not supported
  168. } HTTP_VERB;
  169. /** Data structure that the caller can request at any time that will include
  170. some information regarding the session */
  171. typedef struct _HTTP_CLIENT
  172. {
  173. UINT32 HTTPStatusCode; // HTTP Status code (200 OK)
  174. UINT32 RequestBodyLengthSent; // Total bytes sent (body only)
  175. UINT32 ResponseBodyLengthReceived; // Total bytes received (body only)
  176. UINT32 TotalResponseBodyLength; // as extracted from the 揷ontent-length" header
  177. UINT32 HttpState;
  178. } HTTP_CLIENT;
  179. /** HTTP parameters */
  180. typedef struct _HTTPParameters
  181. {
  182. CHAR* Uri;
  183. CHAR* ProxyHost;
  184. UINT32 UseProxy ;
  185. UINT32 ProxyPort;
  186. UINT32 Verbose;
  187. CHAR* UserName;
  188. CHAR* Password;
  189. HTTP_AUTH_SCHEMA AuthType;
  190. } HTTPParameters;
  191. #if TLS_CONFIG_HTTP_CLIENT_TASK
  192. /** the callback function of http clent for received */
  193. typedef void (*http_client_recv_callback_fn)(HTTP_SESSION_HANDLE pSession, CHAR * data, UINT32 totallen, UINT32 datalen);
  194. /** the callback function of http clent for err */
  195. typedef void (*http_client_err_callback_fn)(HTTP_SESSION_HANDLE pSession, int err);
  196. /** message of the http client */
  197. typedef struct _http_client_msg
  198. {
  199. HTTP_SESSION_HANDLE pSession;
  200. HTTPParameters param;
  201. HTTP_VERB method;
  202. CHAR* sendData;
  203. UINT32 dataLen;
  204. http_client_recv_callback_fn recv_fn;
  205. http_client_err_callback_fn err_fn;
  206. } http_client_msg;
  207. #endif
  208. /**
  209. * @defgroup APP_APIs APP APIs
  210. * @brief APP APIs
  211. */
  212. /**
  213. * @addtogroup APP_APIs
  214. * @{
  215. */
  216. /**
  217. * @defgroup HTTPC_APIs HTTPC APIs
  218. * @brief HTTP client APIs
  219. */
  220. /**
  221. * @addtogroup HTTPC_APIs
  222. * @{
  223. */
  224. /******************************************************************************
  225. *
  226. * Section : HTTP API public interface
  227. *
  228. ******************************************************************************/
  229. /**
  230. * @brief Allocate memory for a new HTTP Session
  231. *
  232. * @param[in] Flags HTTP Session internal API flags, 0 should be passed here
  233. *
  234. * @retval 0 failed
  235. * @retval other HTTP Session handle
  236. *
  237. * @note None
  238. */
  239. HTTP_SESSION_HANDLE HTTPClientOpenRequest (HTTP_CLIENT_SESSION_FLAGS Flags);
  240. /**
  241. * @brief Closes the active connection and free the corresponding memory
  242. *
  243. * @param[in] *pSession HTTP Session handle
  244. *
  245. * @retval HTTP_CLIENT_SUCCESS success
  246. * @retval other failed
  247. *
  248. * @note None
  249. */
  250. UINT32 HTTPClientCloseRequest (HTTP_SESSION_HANDLE *pSession);
  251. /**
  252. * @brief Sets the HTTP authentication schema
  253. *
  254. * @param[in] pSession HTTP Session handle
  255. * @param[in] AuthSchema HTTP Supported authentication methods
  256. * @param[in] *pReserved Reserved parameter
  257. *
  258. * @retval HTTP_CLIENT_SUCCESS success
  259. * @retval other failed
  260. *
  261. * @note None
  262. */
  263. UINT32 HTTPClientSetAuth (HTTP_SESSION_HANDLE pSession, HTTP_AUTH_SCHEMA AuthSchema, void *pReserved);
  264. /**
  265. * @brief Sets credentials for the target host
  266. *
  267. * @param[in] pSession HTTP Session handle
  268. * @param[in] *pUserName User name
  269. * @param[in] *pPassword Password
  270. *
  271. * @retval HTTP_CLIENT_SUCCESS success
  272. * @retval other failed
  273. *
  274. * @note None
  275. */
  276. UINT32 HTTPClientSetCredentials (HTTP_SESSION_HANDLE pSession, CHAR *pUserName, CHAR *pPassword);
  277. /**
  278. * @brief Sets all the proxy related parameters
  279. *
  280. * @param[in] pSession HTTP Session handle
  281. * @param[in] *pProxyName The host name
  282. * @param[in] nPort The proxy port number
  283. * @param[in] *pUserName User name for proxy authentication (can be null)
  284. * @param[in] *pPassword User password for proxy authentication (can be null)
  285. *
  286. * @retval HTTP_CLIENT_SUCCESS success
  287. * @retval other failed
  288. *
  289. * @note None
  290. */
  291. UINT32 HTTPClientSetProxy (HTTP_SESSION_HANDLE pSession, CHAR *pProxyName, UINT16 nPort, CHAR *pUserName, CHAR *pPassword);
  292. /**
  293. * @brief Sets the HTTP verb for the outgoing request
  294. *
  295. * @param[in] pSession HTTP Session handle
  296. * @param[in] HttpVerb HTTP supported verbs
  297. *
  298. * @retval HTTP_CLIENT_SUCCESS success
  299. * @retval other failed
  300. *
  301. * @note None
  302. */
  303. UINT32 HTTPClientSetVerb (HTTP_SESSION_HANDLE pSession, HTTP_VERB HttpVerb);
  304. /**
  305. * @brief Add headers into the outgoing request
  306. *
  307. * @param[in] pSession HTTP Session
  308. * @param[in] *pHeaderName The Header name
  309. * @param[in] *pHeaderData The header data
  310. * @param[in] nInsert Reserved, could be any
  311. *
  312. * @retval HTTP_CLIENT_SUCCESS success
  313. * @retval other failed
  314. *
  315. * @note None
  316. */
  317. UINT32 HTTPClientAddRequestHeaders (HTTP_SESSION_HANDLE pSession, CHAR *pHeaderName, CHAR *pHeaderData, BOOL nInsert);
  318. /**
  319. * @brief This function builds the request headers, performs a DNS resolution,
  320. * opens the connection (if it was not opened yet by a previous request
  321. * or if it has closed) and sends the request headers
  322. *
  323. * @param[in] pSession HTTP Session handle
  324. * @param[in] *pUrl The requested URL
  325. * @param[in] *pData Data to post to the server
  326. * @param[in] nDataLength Length of posted data
  327. * @param[in] TotalLength Valid only when http method is post
  328. * TRUE: Post data to http server.
  329. * FALSE: In a post request without knowing the total
  330. * length in advance so return error or use chunking.
  331. * @param[in] nTimeout Operation timeout
  332. * @param[in] nClientPort Client side port 0 for none
  333. *
  334. * @retval HTTP_CLIENT_SUCCESS success
  335. * @retval other failed
  336. *
  337. * @note None
  338. */
  339. UINT32 HTTPClientSendRequest (HTTP_SESSION_HANDLE pSession, CHAR *pUrl, VOID *pData, UINT32 nDataLength, BOOL TotalLength, UINT32 nTimeout,UINT32 nClientPort);
  340. /**
  341. * @brief Write data to the remote server
  342. *
  343. * @param[in] pSession HTTP Session handle
  344. * @param[in] *pBuffer Data to write to the server
  345. * @param[in] nBufferLength Length of wtitten data
  346. * @param[in] nTimeout Timeout for the operation
  347. *
  348. * @retval HTTP_CLIENT_SUCCESS success
  349. * @retval other failed
  350. *
  351. * @note None
  352. */
  353. UINT32 HTTPClientWriteData (HTTP_SESSION_HANDLE pSession, VOID *pBuffer, UINT32 nBufferLength, UINT32 nTimeout);
  354. /**
  355. * @brief Receives the response header on the connection and parses it.
  356. * Performs any required authentication.
  357. *
  358. * @param[in] pSession HTTP Session handle
  359. * @param[in] nTimeout Timeout for the operation
  360. *
  361. * @retval HTTP_CLIENT_SUCCESS success
  362. * @retval other failed
  363. *
  364. * @note None
  365. */
  366. UINT32 HTTPClientRecvResponse (HTTP_SESSION_HANDLE pSession, UINT32 nTimeout);
  367. /**
  368. * @brief Read data from the server. Parse out the chunks data
  369. *
  370. * @param[in] pSession HTTP Session handle
  371. * @param[out] *pBuffer A pointer to a buffer that will be filled with the servers response
  372. * @param[in] nBytesToRead The size of the buffer (numbers of bytes to read)
  373. * @param[in] nTimeout Operation timeout in seconds
  374. * @param[out] *nBytesRecived Count of the bytes that were received in this operation
  375. *
  376. * @retval HTTP_CLIENT_SUCCESS success
  377. * @retval other failed
  378. *
  379. * @note None
  380. */
  381. UINT32 HTTPClientReadData (HTTP_SESSION_HANDLE pSession, VOID *pBuffer, UINT32 nBytesToRead, UINT32 nTimeout, UINT32 *nBytesRecived);
  382. /**
  383. * @brief Fill the users structure with the session information
  384. *
  385. * @param[in] pSession HTTP Session handle
  386. * @param[out] *HTTPClient The session information
  387. *
  388. * @retval HTTP_CLIENT_SUCCESS success
  389. * @retval other failed
  390. *
  391. * @note None
  392. */
  393. UINT32 HTTPClientGetInfo (HTTP_SESSION_HANDLE pSession, HTTP_CLIENT *HTTPClient);
  394. /**
  395. * @brief Initiate the headr searching functions and find the first header
  396. *
  397. * @param[in] pSession HTTP Session handle
  398. * @param[in] *pSearchClue Search clue
  399. * @param[out] *pHeaderBuffer A pointer to a buffer that will be filled with the header name and value
  400. * @param[out] *nLength Count of the bytes that were received in this operation
  401. *
  402. * @retval HTTP_CLIENT_SUCCESS success
  403. * @retval other failed
  404. *
  405. * @note None
  406. */
  407. UINT32 HTTPClientFindFirstHeader (HTTP_SESSION_HANDLE pSession, CHAR *pSearchClue,CHAR *pHeaderBuffer, UINT32 *nLength);
  408. /**
  409. * @brief Find the next header.
  410. *
  411. * @param[in] pSession HTTP Session handle
  412. * @param[out] *pHeaderBuffer A pointer to a buffer that will be filled with the header name and value
  413. * @param[out] *nLength Count of the bytes that were received in this operation
  414. *
  415. * @retval HTTP_CLIENT_SUCCESS success
  416. * @retval other failed
  417. *
  418. * @note None
  419. */
  420. UINT32 HTTPClientGetNextHeader (HTTP_SESSION_HANDLE pSession, CHAR *pHeaderBuffer, UINT32 *nLength);
  421. /**
  422. * @brief Terminate a headers search session
  423. *
  424. * @param[in] pSession HTTP Session handle
  425. *
  426. * @retval HTTP_CLIENT_SUCCESS success
  427. * @retval other failed
  428. *
  429. * @note None
  430. */
  431. UINT32 HTTPClientFindCloseHeader (HTTP_SESSION_HANDLE pSession);
  432. #if TLS_CONFIG_HTTP_CLIENT_TASK
  433. /**
  434. * @brief initialize task of the http client
  435. *
  436. * @param None
  437. *
  438. * @retval WM_SUCCESS success
  439. * @retval WM_FAILED failed
  440. *
  441. * @note None
  442. */
  443. int http_client_task_init(void);
  444. /**
  445. * @brief post message to the task of http client
  446. *
  447. * @param[in] msg pointer to the message
  448. *
  449. * @retval ERR_OK success
  450. * @retval other failed
  451. *
  452. * @note None
  453. */
  454. int http_client_post(http_client_msg * msg);
  455. #endif /* TLS_CONFIG_HTTP_CLIENT_TASK */
  456. /**
  457. * @}
  458. */
  459. /**
  460. * @}
  461. */
  462. #endif /* WM_HTTP_CLIENT_H */