wm_http_client.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. /** HTTP end of stream message */
  127. #define HTTP_CLIENT_EOS 1000
  128. // HTTP Session flags (Public flags)
  129. #define HTTP_CLIENT_FLAG_KEEP_ALIVE 0x00000001 // Set the keep alive header
  130. #define HTTP_CLIENT_FLAG_SEND_CHUNKED 0x00000002 // The outgoing should chunked
  131. #define HTTP_CLIENT_FLAG_NO_CACHE 0x00000004 // Set the no cache header
  132. #define HTTP_CLIENT_FLAG_ASYNC 0x00000008 // Currently not implemented
  133. #define HTTP_CLIENT_FLAG_MULTIPART_FORM 0x00000010 // The outgoing should multipart/form-data
  134. // HTTP Type Definitions
  135. typedef UINT32 HTTP_SESSION_HANDLE;
  136. typedef UINT32 HTTP_CLIENT_SESSION_FLAGS;
  137. /******************************************************************************
  138. *
  139. * Section : HTTP API structures
  140. *
  141. ******************************************************************************/
  142. /* HTTP Type Definitions */
  143. /** http seesion handle */
  144. typedef u32 tls_http_session_handle_t;
  145. /** http seesion flags */
  146. typedef u32 tls_http_session_flags_t;
  147. /** HTTP Supported authentication methods */
  148. typedef enum _HTTP_AUTH_SCHEMA
  149. {
  150. AuthSchemaNone = 0,
  151. AuthSchemaBasic,
  152. AuthSchemaDigest,
  153. AuthSchemaKerberos,
  154. AuthNotSupported
  155. } HTTP_AUTH_SCHEMA;
  156. /** HTTP supported verbs */
  157. typedef enum _HTTP_VERB
  158. {
  159. VerbGet = 0,
  160. VerbHead,
  161. VerbPost,
  162. VerbPut,
  163. VerbFwup,
  164. VerbNotSupported
  165. // Note: others verb such as connect and put are currently not supported
  166. } HTTP_VERB;
  167. /** Data structure that the caller can request at any time that will include
  168. some information regarding the session */
  169. typedef struct _HTTP_CLIENT
  170. {
  171. UINT32 HTTPStatusCode; // HTTP Status code (200 OK)
  172. UINT32 RequestBodyLengthSent; // Total bytes sent (body only)
  173. UINT32 ResponseBodyLengthReceived; // Total bytes received (body only)
  174. UINT32 TotalResponseBodyLength; // as extracted from the “content-length" header
  175. UINT32 HttpState;
  176. } HTTP_CLIENT;
  177. /** HTTP parameters */
  178. typedef struct _HTTPParameters
  179. {
  180. CHAR* Uri;
  181. CHAR* ProxyHost;
  182. UINT32 UseProxy ;
  183. UINT32 ProxyPort;
  184. UINT32 Verbose;
  185. CHAR* UserName;
  186. CHAR* Password;
  187. HTTP_AUTH_SCHEMA AuthType;
  188. } HTTPParameters;
  189. #if TLS_CONFIG_HTTP_CLIENT_TASK
  190. /** the callback function of http clent for received */
  191. typedef void (*http_client_recv_callback_fn)(HTTP_SESSION_HANDLE pSession, CHAR * data, UINT32 totallen, UINT32 datalen);
  192. /** the callback function of http clent for err */
  193. typedef void (*http_client_err_callback_fn)(HTTP_SESSION_HANDLE pSession, int err);
  194. /** message of the http client */
  195. typedef struct _http_client_msg
  196. {
  197. HTTP_SESSION_HANDLE pSession;
  198. HTTPParameters param;
  199. HTTP_VERB method;
  200. CHAR* sendData;
  201. UINT32 dataLen;
  202. http_client_recv_callback_fn recv_fn;
  203. http_client_err_callback_fn err_fn;
  204. } http_client_msg;
  205. #endif
  206. /**
  207. * @defgroup APP_APIs APP APIs
  208. * @brief APP APIs
  209. */
  210. /**
  211. * @addtogroup APP_APIs
  212. * @{
  213. */
  214. /**
  215. * @defgroup HTTPC_APIs HTTPC APIs
  216. * @brief HTTP client APIs
  217. */
  218. /**
  219. * @addtogroup HTTPC_APIs
  220. * @{
  221. */
  222. /******************************************************************************
  223. *
  224. * Section : HTTP API public interface
  225. *
  226. ******************************************************************************/
  227. /**
  228. * @brief Allocate memory for a new HTTP Session
  229. *
  230. * @param[in] Flags HTTP Session internal API flags, 0 should be passed here
  231. *
  232. * @retval 0 failed
  233. * @retval other HTTP Session handle
  234. *
  235. * @note None
  236. */
  237. HTTP_SESSION_HANDLE HTTPClientOpenRequest (HTTP_CLIENT_SESSION_FLAGS Flags);
  238. /**
  239. * @brief Closes the active connection and free the corresponding memory
  240. *
  241. * @param[in] *pSession HTTP Session handle
  242. *
  243. * @retval HTTP_CLIENT_SUCCESS success
  244. * @retval other failed
  245. *
  246. * @note None
  247. */
  248. UINT32 HTTPClientCloseRequest (HTTP_SESSION_HANDLE *pSession);
  249. /**
  250. * @brief Sets the HTTP authentication schema
  251. *
  252. * @param[in] pSession HTTP Session handle
  253. * @param[in] AuthSchema HTTP Supported authentication methods
  254. * @param[in] *pReserved Reserved parameter
  255. *
  256. * @retval HTTP_CLIENT_SUCCESS success
  257. * @retval other failed
  258. *
  259. * @note None
  260. */
  261. UINT32 HTTPClientSetAuth (HTTP_SESSION_HANDLE pSession, HTTP_AUTH_SCHEMA AuthSchema, void *pReserved);
  262. /**
  263. * @brief Sets credentials for the target host
  264. *
  265. * @param[in] pSession HTTP Session handle
  266. * @param[in] *pUserName User name
  267. * @param[in] *pPassword Password
  268. *
  269. * @retval HTTP_CLIENT_SUCCESS success
  270. * @retval other failed
  271. *
  272. * @note None
  273. */
  274. UINT32 HTTPClientSetCredentials (HTTP_SESSION_HANDLE pSession, CHAR *pUserName, CHAR *pPassword);
  275. /**
  276. * @brief Sets all the proxy related parameters
  277. *
  278. * @param[in] pSession HTTP Session handle
  279. * @param[in] *pProxyName The host name
  280. * @param[in] nPort The proxy port number
  281. * @param[in] *pUserName User name for proxy authentication (can be null)
  282. * @param[in] *pPassword User password for proxy authentication (can be null)
  283. *
  284. * @retval HTTP_CLIENT_SUCCESS success
  285. * @retval other failed
  286. *
  287. * @note None
  288. */
  289. UINT32 HTTPClientSetProxy (HTTP_SESSION_HANDLE pSession, CHAR *pProxyName, UINT16 nPort, CHAR *pUserName, CHAR *pPassword);
  290. /**
  291. * @brief Sets the HTTP verb for the outgoing request
  292. *
  293. * @param[in] pSession HTTP Session handle
  294. * @param[in] HttpVerb HTTP supported verbs
  295. *
  296. * @retval HTTP_CLIENT_SUCCESS success
  297. * @retval other failed
  298. *
  299. * @note None
  300. */
  301. UINT32 HTTPClientSetVerb (HTTP_SESSION_HANDLE pSession, HTTP_VERB HttpVerb);
  302. /**
  303. * @brief Add headers into the outgoing request
  304. *
  305. * @param[in] pSession HTTP Session
  306. * @param[in] *pHeaderName The Header name
  307. * @param[in] *pHeaderData The header data
  308. * @param[in] nInsert Reserved, could be any
  309. *
  310. * @retval HTTP_CLIENT_SUCCESS success
  311. * @retval other failed
  312. *
  313. * @note None
  314. */
  315. UINT32 HTTPClientAddRequestHeaders (HTTP_SESSION_HANDLE pSession, CHAR *pHeaderName, CHAR *pHeaderData, BOOL nInsert);
  316. /**
  317. * @brief This function builds the request headers, performs a DNS resolution,
  318. * opens the connection (if it was not opened yet by a previous request
  319. * or if it has closed) and sends the request headers
  320. *
  321. * @param[in] pSession HTTP Session handle
  322. * @param[in] *pUrl The requested URL
  323. * @param[in] *pData Data to post to the server
  324. * @param[in] nDataLength Length of posted data
  325. * @param[in] TotalLength Valid only when http method is post
  326. * TRUE: Post data to http server.
  327. * FALSE: In a post request without knowing the total
  328. * length in advance so return error or use chunking.
  329. * @param[in] nTimeout Operation timeout
  330. * @param[in] nClientPort Client side port 0 for none
  331. *
  332. * @retval HTTP_CLIENT_SUCCESS success
  333. * @retval other failed
  334. *
  335. * @note None
  336. */
  337. UINT32 HTTPClientSendRequest (HTTP_SESSION_HANDLE pSession, CHAR *pUrl, VOID *pData, UINT32 nDataLength, BOOL TotalLength, UINT32 nTimeout,UINT32 nClientPort);
  338. /**
  339. * @brief Write data to the remote server
  340. *
  341. * @param[in] pSession HTTP Session handle
  342. * @param[in] *pBuffer Data to write to the server
  343. * @param[in] nBufferLength Length of wtitten data
  344. * @param[in] nTimeout Timeout for the operation
  345. *
  346. * @retval HTTP_CLIENT_SUCCESS success
  347. * @retval other failed
  348. *
  349. * @note None
  350. */
  351. UINT32 HTTPClientWriteData (HTTP_SESSION_HANDLE pSession, VOID *pBuffer, UINT32 nBufferLength, UINT32 nTimeout);
  352. /**
  353. * @brief Receives the response header on the connection and parses it.
  354. * Performs any required authentication.
  355. *
  356. * @param[in] pSession HTTP Session handle
  357. * @param[in] nTimeout Timeout for the operation
  358. *
  359. * @retval HTTP_CLIENT_SUCCESS success
  360. * @retval other failed
  361. *
  362. * @note None
  363. */
  364. UINT32 HTTPClientRecvResponse (HTTP_SESSION_HANDLE pSession, UINT32 nTimeout);
  365. /**
  366. * @brief Read data from the server. Parse out the chunks data
  367. *
  368. * @param[in] pSession HTTP Session handle
  369. * @param[out] *pBuffer A pointer to a buffer that will be filled with the servers response
  370. * @param[in] nBytesToRead The size of the buffer (numbers of bytes to read)
  371. * @param[in] nTimeout Operation timeout in seconds
  372. * @param[out] *nBytesRecived Count of the bytes that were received in this operation
  373. *
  374. * @retval HTTP_CLIENT_SUCCESS success
  375. * @retval other failed
  376. *
  377. * @note None
  378. */
  379. UINT32 HTTPClientReadData (HTTP_SESSION_HANDLE pSession, VOID *pBuffer, UINT32 nBytesToRead, UINT32 nTimeout, UINT32 *nBytesRecived);
  380. /**
  381. * @brief Fill the users structure with the session information
  382. *
  383. * @param[in] pSession HTTP Session handle
  384. * @param[out] *HTTPClient The session information
  385. *
  386. * @retval HTTP_CLIENT_SUCCESS success
  387. * @retval other failed
  388. *
  389. * @note None
  390. */
  391. UINT32 HTTPClientGetInfo (HTTP_SESSION_HANDLE pSession, HTTP_CLIENT *HTTPClient);
  392. /**
  393. * @brief Initiate the headr searching functions and find the first header
  394. *
  395. * @param[in] pSession HTTP Session handle
  396. * @param[in] *pSearchClue Search clue
  397. * @param[out] *pHeaderBuffer A pointer to a buffer that will be filled with the header name and value
  398. * @param[out] *nLength Count of the bytes that were received in this operation
  399. *
  400. * @retval HTTP_CLIENT_SUCCESS success
  401. * @retval other failed
  402. *
  403. * @note None
  404. */
  405. UINT32 HTTPClientFindFirstHeader (HTTP_SESSION_HANDLE pSession, CHAR *pSearchClue,CHAR *pHeaderBuffer, UINT32 *nLength);
  406. /**
  407. * @brief Find the next header.
  408. *
  409. * @param[in] pSession HTTP Session handle
  410. * @param[out] *pHeaderBuffer A pointer to a buffer that will be filled with the header name and value
  411. * @param[out] *nLength Count of the bytes that were received in this operation
  412. *
  413. * @retval HTTP_CLIENT_SUCCESS success
  414. * @retval other failed
  415. *
  416. * @note None
  417. */
  418. UINT32 HTTPClientGetNextHeader (HTTP_SESSION_HANDLE pSession, CHAR *pHeaderBuffer, UINT32 *nLength);
  419. /**
  420. * @brief Terminate a headers search session
  421. *
  422. * @param[in] pSession HTTP Session handle
  423. *
  424. * @retval HTTP_CLIENT_SUCCESS success
  425. * @retval other failed
  426. *
  427. * @note None
  428. */
  429. UINT32 HTTPClientFindCloseHeader (HTTP_SESSION_HANDLE pSession);
  430. #if TLS_CONFIG_HTTP_CLIENT_TASK
  431. /**
  432. * @brief initialize task of the http client
  433. *
  434. * @param None
  435. *
  436. * @retval WM_SUCCESS success
  437. * @retval WM_FAILED failed
  438. *
  439. * @note None
  440. */
  441. int http_client_task_init(void);
  442. /**
  443. * @brief post message to the task of http client
  444. *
  445. * @param[in] msg pointer to the message
  446. *
  447. * @retval ERR_OK success
  448. * @retval other failed
  449. *
  450. * @note None
  451. */
  452. int http_client_post(http_client_msg * msg);
  453. #endif /* TLS_CONFIG_HTTP_CLIENT_TASK */
  454. /**
  455. * @}
  456. */
  457. /**
  458. * @}
  459. */
  460. #endif /* WM_HTTP_CLIENT_H */