utils_httpc.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (C) 2012-2019 UCloud. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #ifndef C_SDK_UTILS_HTTPC_H_
  16. #define C_SDK_UTILS_HTTPC_H_
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. #include <stdbool.h>
  21. #include "utils_net.h"
  22. typedef enum {
  23. HTTP_GET,
  24. HTTP_POST,
  25. HTTP_PUT,
  26. HTTP_DELETE,
  27. HTTP_HEAD
  28. } HTTP_Request_Method;
  29. /** @brief This structure defines the http_client_t structure. */
  30. typedef struct {
  31. int remote_port; /**< HTTP or HTTPS port. */
  32. utils_network_t net;
  33. int response_code; /**< Response code. */
  34. char *header; /**< Custom header. */
  35. char *auth_user; /**< Username for basic authentication. */
  36. char *auth_password; /**< Password for basic authentication. */
  37. } http_client_t;
  38. /** @brief This structure defines the HTTP data structure. */
  39. typedef struct {
  40. int is_more; /**< Indicates if more data needs to be retrieved. */
  41. int is_chunked; /**< Response data is encoded in portions/chunks.*/
  42. int retrieve_len; /**< Content length to be retrieved. */
  43. int response_content_len; /**< Response content length. */
  44. int response_received_len; /**< Response have received length. */
  45. int post_buf_len; /**< Post data length. */
  46. int response_buf_len; /**< Response buffer length. */
  47. char *post_content_type; /**< Content type of the post data. */
  48. unsigned char *post_buf; /**< User data to be posted. */
  49. char *response_buf; /**< Buffer to store the response data. */
  50. } http_client_data_t;
  51. int http_client_connect(http_client_t *client, const char *url, int port, const char *ca_crt);
  52. int http_client_common(http_client_t *client, const char *url, int port, const char *ca_crt,
  53. HTTP_Request_Method method, http_client_data_t *client_data, uint32_t timeout_ms);
  54. int http_client_recv_data(http_client_t *client, uint32_t timeout_ms, http_client_data_t *client_data);
  55. void http_client_close(http_client_t *client);
  56. int _http_send_user_data(http_client_t *client, http_client_data_t *client_data, uint32_t timeout_ms);
  57. void http_client_file_md5(char* file_path, char *output);
  58. void http_client_buffer_md5(char* buffer, uint32_t buffer_len, char *output);
  59. int _http_send_request(http_client_t *client, const char *url, HTTP_Request_Method method, uint32_t size_fetched, size_t range_len,
  60. http_client_data_t *client_data, uint32_t timeout_ms);
  61. #ifdef __cplusplus
  62. }
  63. #endif
  64. #endif /* C_SDK_UTILS_HTTPC_H_ */