1 /* 2 * Copyright (c) 2022 Winner Microelectronics Co., Ltd. All rights reserved. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef WM_HTTP_CLIENT_H 16 #define WM_HTTP_CLIENT_H 17 18 #include "wm_config.h" 19 #include "wm_type_def.h" 20 #ifdef BOOL 21 #undef BOOL 22 #endif 23 #ifdef UCHAR 24 #undef UCHAR 25 #endif 26 #ifdef CHAR 27 #undef CHAR 28 #endif 29 #ifdef UINT16 30 #undef UINT16 31 #endif 32 #ifdef INT16 33 #undef INT16 34 #endif 35 #ifdef UINT32 36 #undef UINT32 37 #endif 38 #ifdef INT32 39 #undef INT32 40 #endif 41 #ifdef UINT64 42 #undef UINT64 43 #endif 44 #ifdef INT64 45 #undef INT64 46 #endif 47 #ifdef ULONG 48 #undef ULONG 49 #endif 50 #ifdef LONG 51 #undef LONG 52 #endif 53 #define VOID void 54 typedef unsigned int BOOL; 55 typedef char CHAR; 56 typedef unsigned short UINT16; 57 typedef unsigned int UINT32; 58 59 /* HTTP Status, API Return codes */ 60 /** HTTP Success status */ 61 #define HTTP_CLIENT_SUCCESS 0 62 /** Unknown error */ 63 #define HTTP_CLIENT_UNKNOWN_ERROR 1 64 /** an Invalid handle or possible bad pointer was passed to a function */ 65 #define HTTP_CLIENT_ERROR_INVALID_HANDLE 2 66 /** Buffer too small or a failure while in memory allocation */ 67 #define HTTP_CLIENT_ERROR_NO_MEMORY 3 68 /** an attempt to use an invalid socket handle was made */ 69 #define HTTP_CLIENT_ERROR_SOCKET_INVALID 4 70 /** Can't send socket parameters */ 71 #define HTTP_CLIENT_ERROR_SOCKET_CANT_SET 5 72 /** Error while resolving host name */ 73 #define HTTP_CLIENT_ERROR_SOCKET_RESOLVE 6 74 /** Error while connecting to the remote server */ 75 #define HTTP_CLIENT_ERROR_SOCKET_CONNECT 7 76 /** socket time out error */ 77 #define HTTP_CLIENT_ERROR_SOCKET_TIME_OUT 8 78 /** Error while receiving data */ 79 #define HTTP_CLIENT_ERROR_SOCKET_RECV 9 80 /** Error while sending data */ 81 #define HTTP_CLIENT_ERROR_SOCKET_SEND 10 82 /** Error while receiving the remote HTTP headers */ 83 #define HTTP_CLIENT_ERROR_HEADER_RECV 11 84 /** Could not find element within header */ 85 #define HTTP_CLIENT_ERROR_HEADER_NOT_FOUND 12 86 /** The headers search clue was too large for the internal API buffer */ 87 #define HTTP_CLIENT_ERROR_HEADER_BIG_CLUE 13 88 /** No content length was specified for the outgoing data. the caller should 89 specify chunking mode in the session creation */ 90 #define HTTP_CLIENT_ERROR_HEADER_NO_LENGTH 14 91 /** The HTTP chunk token that was received from the server was too big and possibly wrong */ 92 #define HTTP_CLIENT_ERROR_CHUNK_TOO_BIG 15 93 /** Could not authenticate with the remote host */ 94 #define HTTP_CLIENT_ERROR_AUTH_HOST 16 95 /** Could not authenticate with the remote proxy */ 96 #define HTTP_CLIENT_ERROR_AUTH_PROXY 17 97 /** Bad or not supported HTTP verb was passed to a function */ 98 #define HTTP_CLIENT_ERROR_BAD_VERB 18 99 /** a function received a parameter that was too large */ 100 #define HTTP_CLIENT_ERROR_LONG_INPUT 19 101 /** The session state prevents the current function from proceeding */ 102 #define HTTP_CLIENT_ERROR_BAD_STATE 20 103 /** Could not parse the chunk length while in chunked transfer */ 104 #define HTTP_CLIENT_ERROR_CHUNK 21 105 /** Could not parse curtail elements from the URL (such as the host name, HTTP prefix act') */ 106 #define HTTP_CLIENT_ERROR_BAD_URL 22 107 /** Could not detect key elements in the received headers */ 108 #define HTTP_CLIENT_ERROR_BAD_HEADER 23 109 /** Error while attempting to resize a buffer */ 110 #define HTTP_CLIENT_ERROR_BUFFER_RSIZE 24 111 /** Authentication schema is not supported */ 112 #define HTTP_CLIENT_ERROR_BAD_AUTH 25 113 /** The selected authentication schema does not match the server response */ 114 #define HTTP_CLIENT_ERROR_AUTH_MISMATCH 26 115 /** an element was missing while parsing the digest authentication challenge */ 116 #define HTTP_CLIENT_ERROR_NO_DIGEST_TOKEN 27 117 /** Digest algorithem could be MD5 or MD5-sess other types are not supported */ 118 #define HTTP_CLIENT_ERROR_NO_DIGEST_ALG 28 119 /** Binding error */ 120 #define HTTP_CLIENT_ERROR_SOCKET_BIND 29 121 /** Tls negotiation error */ 122 #define HTTP_CLIENT_ERROR_TLS_NEGO 30 123 /** Feature is not (yet) implemented */ 124 #define HTTP_CLIENT_ERROR_NOT_IMPLEMENTED 64 125 /** HTTP end of stream message */ 126 #define HTTP_CLIENT_EOS 1000 127 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 135 // HTTP Type Definitions 136 typedef UINT32 HTTP_SESSION_HANDLE; 137 typedef UINT32 HTTP_CLIENT_SESSION_FLAGS; 138 139 /****************************************************************************** 140 * 141 * Section : HTTP API structures 142 * 143 ******************************************************************************/ 144 145 /* HTTP Type Definitions */ 146 /** http seesion handle */ 147 typedef u32 tls_http_session_handle_t; 148 /** http seesion flags */ 149 typedef u32 tls_http_session_flags_t; 150 151 /** HTTP Supported authentication methods */ 152 typedef enum _HTTP_AUTH_SCHEMA { 153 AuthSchemaNone = 0, 154 AuthSchemaBasic, 155 AuthSchemaDigest, 156 AuthSchemaKerberos, 157 AuthNotSupported 158 } HTTP_AUTH_SCHEMA; 159 160 /** HTTP supported verbs */ 161 typedef enum _HTTP_VERB { 162 VerbGet = 0, 163 VerbHead, 164 VerbPost, 165 VerbPut, 166 VerbFwup, 167 VerbNotSupported 168 } HTTP_VERB; 169 170 /** Data structure that the caller can request at any time that will include 171 some information regarding the session */ 172 typedef struct _HTTP_CLIENT { 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 180 /** HTTP parameters */ 181 typedef struct _HTTPParameters { 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 192 #if TLS_CONFIG_HTTP_CLIENT_TASK 193 /** the callback function of http clent for received */ 194 typedef void (*http_client_recv_callback_fn)(HTTP_SESSION_HANDLE pSession, CHAR *data, UINT32 totallen, UINT32 datalen); 195 /** the callback function of http clent for err */ 196 typedef void (*http_client_err_callback_fn)(HTTP_SESSION_HANDLE pSession, int err); 197 198 /** message of the http client */ 199 typedef struct _http_client_msg { 200 HTTP_SESSION_HANDLE pSession; 201 HTTPParameters param; 202 HTTP_VERB method; 203 CHAR* sendData; 204 UINT32 dataLen; 205 http_client_recv_callback_fn recv_fn; 206 http_client_err_callback_fn err_fn; 207 } http_client_msg; 208 #endif 209 210 /** 211 * @defgroup APP_APIs APP APIs 212 * @brief APP APIs 213 */ 214 215 /** 216 * @addtogroup APP_APIs 217 * @{ 218 */ 219 220 /** 221 * @defgroup HTTPC_APIs HTTPC APIs 222 * @brief HTTP client APIs 223 */ 224 225 /** 226 * @addtogroup HTTPC_APIs 227 * @{ 228 */ 229 230 231 /****************************************************************************** 232 * 233 * Section : HTTP API public interface 234 * 235 ******************************************************************************/ 236 237 /** 238 * @brief Allocate memory for a new HTTP Session 239 * 240 * @param[in] Flags HTTP Session internal API flags, 0 should be passed here 241 * 242 * @retval 0 failed 243 * @retval other HTTP Session handle 244 * 245 * @note None 246 */ 247 HTTP_SESSION_HANDLE HTTPClientOpenRequest (HTTP_CLIENT_SESSION_FLAGS Flags); 248 /** 249 * @brief Closes the active connection and free the corresponding memory 250 * 251 * @param[in] *pSession HTTP Session handle 252 * 253 * @retval HTTP_CLIENT_SUCCESS success 254 * @retval other failed 255 * 256 * @note None 257 */ 258 UINT32 HTTPClientCloseRequest (HTTP_SESSION_HANDLE *pSession); 259 /** 260 * @brief Sets the HTTP authentication schema 261 * 262 * @param[in] pSession HTTP Session handle 263 * @param[in] AuthSchema HTTP Supported authentication methods 264 * @param[in] *pReserved Reserved parameter 265 * 266 * @retval HTTP_CLIENT_SUCCESS success 267 * @retval other failed 268 * 269 * @note None 270 */ 271 UINT32 HTTPClientSetAuth (HTTP_SESSION_HANDLE pSession, HTTP_AUTH_SCHEMA AuthSchema, void *pReserved); 272 /** 273 * @brief Sets credentials for the target host 274 * 275 * @param[in] pSession HTTP Session handle 276 * @param[in] *pUserName User name 277 * @param[in] *pPassword Password 278 * 279 * @retval HTTP_CLIENT_SUCCESS success 280 * @retval other failed 281 * 282 * @note None 283 */ 284 UINT32 HTTPClientSetCredentials (HTTP_SESSION_HANDLE pSession, CHAR *pUserName, CHAR *pPassword); 285 /** 286 * @brief Sets all the proxy related parameters 287 * 288 * @param[in] pSession HTTP Session handle 289 * @param[in] *pProxyName The host name 290 * @param[in] nPort The proxy port number 291 * @param[in] *pUserName User name for proxy authentication (can be null) 292 * @param[in] *pPassword User password for proxy authentication (can be null) 293 * 294 * @retval HTTP_CLIENT_SUCCESS success 295 * @retval other failed 296 * 297 * @note None 298 */ 299 UINT32 HTTPClientSetProxy (HTTP_SESSION_HANDLE pSession, CHAR *pProxyName, 300 UINT16 nPort, CHAR *pUserName, CHAR *pPassword); 301 /** 302 * @brief Sets the HTTP verb for the outgoing request 303 * 304 * @param[in] pSession HTTP Session handle 305 * @param[in] HttpVerb HTTP supported verbs 306 * 307 * @retval HTTP_CLIENT_SUCCESS success 308 * @retval other failed 309 * 310 * @note None 311 */ 312 UINT32 HTTPClientSetVerb (HTTP_SESSION_HANDLE pSession, HTTP_VERB HttpVerb); 313 /** 314 * @brief Add headers into the outgoing request 315 * 316 * @param[in] pSession HTTP Session 317 * @param[in] *pHeaderName The Header name 318 * @param[in] *pHeaderData The header data 319 * @param[in] nInsert Reserved, could be any 320 * 321 * @retval HTTP_CLIENT_SUCCESS success 322 * @retval other failed 323 * 324 * @note None 325 */ 326 UINT32 HTTPClientAddRequestHeaders (HTTP_SESSION_HANDLE pSession, 327 CHAR *pHeaderName, CHAR *pHeaderData, BOOL nInsert); 328 /** 329 * @brief This function builds the request headers, performs a DNS resolution, 330 * opens the connection (if it was not opened yet by a previous request 331 * or if it has closed) and sends the request headers 332 * 333 * @param[in] pSession HTTP Session handle 334 * @param[in] *pUrl The requested URL 335 * @param[in] *pData Data to post to the server 336 * @param[in] nDataLength Length of posted data 337 * @param[in] TotalLength Valid only when http method is post 338 * TRUE: Post data to http server. 339 * FALSE: In a post request without knowing the total 340 * length in advance so return error or use chunking. 341 * @param[in] nTimeout Operation timeout 342 * @param[in] nClientPort Client side port 0 for none 343 * 344 * @retval HTTP_CLIENT_SUCCESS success 345 * @retval other failed 346 * 347 * @note None 348 */ 349 UINT32 HTTPClientSendRequest (HTTP_SESSION_HANDLE pSession, CHAR *pUrl, VOID *pData, UINT32 nDataLength, 350 BOOL TotalLength, UINT32 nTimeout, UINT32 nClientPort); 351 /** 352 * @brief Write data to the remote server 353 * 354 * @param[in] pSession HTTP Session handle 355 * @param[in] *pBuffer Data to write to the server 356 * @param[in] nBufferLength Length of wtitten data 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 HTTPClientWriteData (HTTP_SESSION_HANDLE pSession, VOID *pBuffer, 365 UINT32 nBufferLength, UINT32 nTimeout); 366 /** 367 * @brief Receives the response header on the connection and parses it. 368 * Performs any required authentication. 369 * 370 * @param[in] pSession HTTP Session handle 371 * @param[in] nTimeout Timeout for the operation 372 * 373 * @retval HTTP_CLIENT_SUCCESS success 374 * @retval other failed 375 * 376 * @note None 377 */ 378 UINT32 HTTPClientRecvResponse (HTTP_SESSION_HANDLE pSession, UINT32 nTimeout); 379 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, 406 CHAR *pHeaderBuffer, UINT32 *nLength); 407 /** 408 * @brief Find the next header. 409 * 410 * @param[in] pSession HTTP Session handle 411 * @param[out] *pHeaderBuffer A pointer to a buffer that will be filled with the header name and value 412 * @param[out] *nLength Count of the bytes that were received in this operation 413 * 414 * @retval HTTP_CLIENT_SUCCESS success 415 * @retval other failed 416 * 417 * @note None 418 */ 419 UINT32 HTTPClientGetNextHeader (HTTP_SESSION_HANDLE pSession, CHAR *pHeaderBuffer, UINT32 *nLength); 420 /** 421 * @brief Terminate a headers search session 422 * 423 * @param[in] pSession HTTP Session handle 424 * 425 * @retval HTTP_CLIENT_SUCCESS success 426 * @retval other failed 427 * 428 * @note None 429 */ 430 UINT32 HTTPClientFindCloseHeader (HTTP_SESSION_HANDLE pSession); 431 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 /** 446 * @brief post message to the task of http client 447 * 448 * @param[in] msg pointer to the message 449 * 450 * @retval ERR_OK success 451 * @retval other failed 452 * 453 * @note None 454 */ 455 int http_client_post(http_client_msg *msg); 456 #endif /* TLS_CONFIG_HTTP_CLIENT_TASK */ 457 458 /** 459 * @} 460 */ 461 462 /** 463 * @} 464 */ 465 466 #endif /* WM_HTTP_CLIENT_H */ 467 468