https_client.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_HTTPS_CLIENT_H
  3. #define NETDATA_HTTPS_CLIENT_H
  4. #include "libnetdata/libnetdata.h"
  5. #include "mqtt_websockets/c-rbuf/include/ringbuffer.h"
  6. #include "mqtt_websockets/c_rhash/include/c_rhash.h"
  7. typedef enum http_req_type {
  8. HTTP_REQ_GET = 0,
  9. HTTP_REQ_POST,
  10. HTTP_REQ_CONNECT
  11. } http_req_type_t;
  12. typedef struct {
  13. http_req_type_t request_type;
  14. char *host;
  15. int port;
  16. char *url;
  17. time_t timeout_s; //timeout in seconds for the network operation (send/recv)
  18. void *payload;
  19. size_t payload_size;
  20. char *proxy_host;
  21. int proxy_port;
  22. const char *proxy_username;
  23. const char *proxy_password;
  24. } https_req_t;
  25. typedef struct {
  26. int http_code;
  27. void *payload;
  28. size_t payload_size;
  29. } https_req_response_t;
  30. // Non feature complete URL parser
  31. // feel free to extend when needed
  32. // currently implements only what ACLK
  33. // needs
  34. // proto://host[:port]/path
  35. typedef struct {
  36. char *proto;
  37. char *host;
  38. int port;
  39. char* path;
  40. } url_t;
  41. int url_parse(const char *url, url_t *parsed);
  42. void url_t_destroy(url_t *url);
  43. void https_req_response_free(https_req_response_t *res);
  44. void https_req_response_init(https_req_response_t *res);
  45. #define HTTPS_REQ_RESPONSE_T_INITIALIZER \
  46. { \
  47. .http_code = 0, \
  48. .payload = NULL, \
  49. .payload_size = 0 \
  50. }
  51. #define HTTPS_REQ_T_INITIALIZER \
  52. { \
  53. .request_type = HTTP_REQ_GET, \
  54. .host = NULL, \
  55. .port = 443, \
  56. .url = NULL, \
  57. .timeout_s = 30, \
  58. .payload = NULL, \
  59. .payload_size = 0, \
  60. .proxy_host = NULL, \
  61. .proxy_port = 8080 \
  62. }
  63. int https_request(https_req_t *request, https_req_response_t *response);
  64. // we expose previously internal parser as this is usefull also from
  65. // other parts of the code
  66. enum http_parse_state {
  67. HTTP_PARSE_INITIAL = 0,
  68. HTTP_PARSE_HEADERS,
  69. HTTP_PARSE_CONTENT
  70. };
  71. typedef uint32_t parse_ctx_flags_t;
  72. #define HTTP_PARSE_FLAG_DONT_WAIT_FOR_CONTENT ((parse_ctx_flags_t)0x01)
  73. #define HTTP_PARSE_FLAGS_DEFAULT ((parse_ctx_flags_t)0)
  74. typedef struct {
  75. parse_ctx_flags_t flags;
  76. enum http_parse_state state;
  77. int content_length;
  78. int http_code;
  79. c_rhash headers;
  80. // for chunked data only
  81. char *chunked_response;
  82. size_t chunked_response_size;
  83. size_t chunked_response_written;
  84. enum chunked_content_state {
  85. CHUNKED_CONTENT_CHUNK_SIZE = 0,
  86. CHUNKED_CONTENT_CHUNK_DATA,
  87. CHUNKED_CONTENT_CHUNK_END_CRLF,
  88. CHUNKED_CONTENT_FINAL_CRLF
  89. } chunked_content_state;
  90. size_t chunk_size;
  91. size_t chunk_got;
  92. } http_parse_ctx;
  93. void http_parse_ctx_create(http_parse_ctx *ctx);
  94. void http_parse_ctx_destroy(http_parse_ctx *ctx);
  95. typedef enum {
  96. HTTP_PARSE_ERROR = -1,
  97. HTTP_PARSE_NEED_MORE_DATA = 0,
  98. HTTP_PARSE_SUCCESS = 1
  99. } http_parse_rc;
  100. http_parse_rc parse_http_response(rbuf_t buf, http_parse_ctx *parse_ctx);
  101. const char *get_http_header_by_name(http_parse_ctx *ctx, const char *name);
  102. #endif /* NETDATA_HTTPS_CLIENT_H */