https_client.h 3.4 KB

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