web_client.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_WEB_CLIENT_H
  3. #define NETDATA_WEB_CLIENT_H 1
  4. #include "libnetdata/libnetdata.h"
  5. extern int web_enable_gzip, web_gzip_level, web_gzip_strategy;
  6. #define HTTP_REQ_MAX_HEADER_FETCH_TRIES 100
  7. extern int respect_web_browser_do_not_track_policy;
  8. extern char *web_x_frame_options;
  9. typedef enum web_client_mode {
  10. WEB_CLIENT_MODE_GET = 0,
  11. WEB_CLIENT_MODE_POST = 1,
  12. WEB_CLIENT_MODE_FILECOPY = 2,
  13. WEB_CLIENT_MODE_OPTIONS = 3,
  14. WEB_CLIENT_MODE_STREAM = 4,
  15. WEB_CLIENT_MODE_PUT = 5,
  16. WEB_CLIENT_MODE_DELETE = 6,
  17. } WEB_CLIENT_MODE;
  18. typedef enum {
  19. HTTP_VALIDATION_OK,
  20. HTTP_VALIDATION_NOT_SUPPORTED,
  21. HTTP_VALIDATION_TOO_MANY_READ_RETRIES,
  22. HTTP_VALIDATION_EXCESS_REQUEST_DATA,
  23. HTTP_VALIDATION_MALFORMED_URL,
  24. HTTP_VALIDATION_INCOMPLETE,
  25. #ifdef ENABLE_HTTPS
  26. HTTP_VALIDATION_REDIRECT
  27. #endif
  28. } HTTP_VALIDATION;
  29. typedef enum web_client_flags {
  30. WEB_CLIENT_FLAG_DEAD = (1 << 1), // if set, this client is dead
  31. WEB_CLIENT_FLAG_KEEPALIVE = (1 << 2), // if set, the web client will be re-used
  32. WEB_CLIENT_FLAG_WAIT_RECEIVE = (1 << 3), // if set, we are waiting more input data
  33. WEB_CLIENT_FLAG_WAIT_SEND = (1 << 4), // if set, we have data to send to the client
  34. WEB_CLIENT_FLAG_DO_NOT_TRACK = (1 << 5), // if set, we should not set cookies on this client
  35. WEB_CLIENT_FLAG_TRACKING_REQUIRED = (1 << 6), // if set, we need to send cookies
  36. WEB_CLIENT_FLAG_TCP_CLIENT = (1 << 7), // if set, the client is using a TCP socket
  37. WEB_CLIENT_FLAG_UNIX_CLIENT = (1 << 8), // if set, the client is using a UNIX socket
  38. WEB_CLIENT_FLAG_DONT_CLOSE_SOCKET = (1 << 9), // don't close the socket when cleaning up (static-threaded web server)
  39. WEB_CLIENT_CHUNKED_TRANSFER = (1 << 10), // chunked transfer (used with zlib compression)
  40. WEB_CLIENT_FLAG_SSL_WAIT_RECEIVE = (1 << 11), // if set, we are waiting more input data from an ssl conn
  41. WEB_CLIENT_FLAG_SSL_WAIT_SEND = (1 << 12), // if set, we have data to send to the client from an ssl conn
  42. WEB_CLIENT_FLAG_PATH_IS_V0 = (1 << 13), // v0 dashboard found on the path
  43. WEB_CLIENT_FLAG_PATH_IS_V1 = (1 << 14), // v1 dashboard found on the path
  44. WEB_CLIENT_FLAG_PATH_IS_V2 = (1 << 15), // v2 dashboard found on the path
  45. WEB_CLIENT_FLAG_PATH_HAS_TRAILING_SLASH = (1 << 16), // the path has a trailing hash
  46. WEB_CLIENT_FLAG_PATH_HAS_FILE_EXTENSION = (1 << 17), // the path ends with a filename extension
  47. } WEB_CLIENT_FLAGS;
  48. #define WEB_CLIENT_FLAG_PATH_WITH_VERSION (WEB_CLIENT_FLAG_PATH_IS_V0|WEB_CLIENT_FLAG_PATH_IS_V1|WEB_CLIENT_FLAG_PATH_IS_V2)
  49. #define web_client_reset_path_flags(w) (w)->flags &= ~(WEB_CLIENT_FLAG_PATH_WITH_VERSION|WEB_CLIENT_FLAG_PATH_HAS_TRAILING_SLASH|WEB_CLIENT_FLAG_PATH_HAS_FILE_EXTENSION)
  50. #define web_client_flag_check(w, flag) ((w)->flags & (flag))
  51. #define web_client_flag_set(w, flag) (w)->flags |= flag
  52. #define web_client_flag_clear(w, flag) (w)->flags &= ~flag
  53. #define WEB_CLIENT_IS_DEAD(w) web_client_flag_set(w, WEB_CLIENT_FLAG_DEAD)
  54. #define web_client_check_dead(w) web_client_flag_check(w, WEB_CLIENT_FLAG_DEAD)
  55. #define web_client_has_keepalive(w) web_client_flag_check(w, WEB_CLIENT_FLAG_KEEPALIVE)
  56. #define web_client_enable_keepalive(w) web_client_flag_set(w, WEB_CLIENT_FLAG_KEEPALIVE)
  57. #define web_client_disable_keepalive(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_KEEPALIVE)
  58. #define web_client_has_donottrack(w) web_client_flag_check(w, WEB_CLIENT_FLAG_DO_NOT_TRACK)
  59. #define web_client_enable_donottrack(w) web_client_flag_set(w, WEB_CLIENT_FLAG_DO_NOT_TRACK)
  60. #define web_client_disable_donottrack(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_DO_NOT_TRACK)
  61. #define web_client_has_tracking_required(w) web_client_flag_check(w, WEB_CLIENT_FLAG_TRACKING_REQUIRED)
  62. #define web_client_enable_tracking_required(w) web_client_flag_set(w, WEB_CLIENT_FLAG_TRACKING_REQUIRED)
  63. #define web_client_disable_tracking_required(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_TRACKING_REQUIRED)
  64. #define web_client_has_wait_receive(w) web_client_flag_check(w, WEB_CLIENT_FLAG_WAIT_RECEIVE)
  65. #define web_client_enable_wait_receive(w) web_client_flag_set(w, WEB_CLIENT_FLAG_WAIT_RECEIVE)
  66. #define web_client_disable_wait_receive(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_WAIT_RECEIVE)
  67. #define web_client_has_wait_send(w) web_client_flag_check(w, WEB_CLIENT_FLAG_WAIT_SEND)
  68. #define web_client_enable_wait_send(w) web_client_flag_set(w, WEB_CLIENT_FLAG_WAIT_SEND)
  69. #define web_client_disable_wait_send(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_WAIT_SEND)
  70. #define web_client_has_ssl_wait_receive(w) web_client_flag_check(w, WEB_CLIENT_FLAG_SSL_WAIT_RECEIVE)
  71. #define web_client_enable_ssl_wait_receive(w) web_client_flag_set(w, WEB_CLIENT_FLAG_SSL_WAIT_RECEIVE)
  72. #define web_client_disable_ssl_wait_receive(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_SSL_WAIT_RECEIVE)
  73. #define web_client_has_ssl_wait_send(w) web_client_flag_check(w, WEB_CLIENT_FLAG_SSL_WAIT_SEND)
  74. #define web_client_enable_ssl_wait_send(w) web_client_flag_set(w, WEB_CLIENT_FLAG_SSL_WAIT_SEND)
  75. #define web_client_disable_ssl_wait_send(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_SSL_WAIT_SEND)
  76. #define web_client_set_tcp(w) web_client_flag_set(w, WEB_CLIENT_FLAG_TCP_CLIENT)
  77. #define web_client_set_unix(w) web_client_flag_set(w, WEB_CLIENT_FLAG_UNIX_CLIENT)
  78. #define web_client_check_unix(w) web_client_flag_check(w, WEB_CLIENT_FLAG_UNIX_CLIENT)
  79. #define web_client_check_tcp(w) web_client_flag_check(w, WEB_CLIENT_FLAG_TCP_CLIENT)
  80. #define web_client_is_corkable(w) web_client_flag_check(w, WEB_CLIENT_FLAG_TCP_CLIENT)
  81. #define NETDATA_WEB_REQUEST_URL_SIZE 65536 // static allocation
  82. #define NETDATA_WEB_RESPONSE_ZLIB_CHUNK_SIZE 16384
  83. #define NETDATA_WEB_RESPONSE_HEADER_INITIAL_SIZE 4096
  84. #define NETDATA_WEB_RESPONSE_INITIAL_SIZE 8192
  85. #define NETDATA_WEB_REQUEST_INITIAL_SIZE 8192
  86. #define NETDATA_WEB_REQUEST_MAX_SIZE 65536
  87. #define NETDATA_WEB_DECODED_URL_INITIAL_SIZE 512
  88. struct response {
  89. BUFFER *header; // our response header
  90. BUFFER *header_output; // internal use
  91. BUFFER *data; // our response data buffer
  92. short int code; // the HTTP response code
  93. bool has_cookies;
  94. size_t rlen; // if non-zero, the excepted size of ifd (input of firecopy)
  95. size_t sent; // current data length sent to output
  96. bool zoutput; // if set to 1, web_client_send() will send compressed data
  97. bool zinitialized;
  98. z_stream zstream; // zlib stream for sending compressed output to client
  99. size_t zsent; // the compressed bytes we have sent to the client
  100. size_t zhave; // the compressed bytes that we have received from zlib
  101. Bytef zbuffer[NETDATA_WEB_RESPONSE_ZLIB_CHUNK_SIZE]; // temporary buffer for storing compressed output
  102. };
  103. struct web_client;
  104. typedef bool (*web_client_interrupt_t)(struct web_client *, void *data);
  105. struct web_client {
  106. unsigned long long id;
  107. size_t use_count;
  108. uuid_t transaction;
  109. WEB_CLIENT_FLAGS flags; // status flags for the client
  110. WEB_CLIENT_MODE mode; // the operational mode of the client
  111. WEB_CLIENT_ACL acl; // the access list of the client
  112. int port_acl; // the operations permitted on the port the client connected to
  113. size_t header_parse_tries;
  114. size_t header_parse_last_size;
  115. bool tcp_cork;
  116. int ifd;
  117. int ofd;
  118. char client_ip[INET6_ADDRSTRLEN]; // Defined buffer sizes include null-terminators
  119. char client_port[NI_MAXSERV];
  120. char client_host[NI_MAXHOST];
  121. BUFFER *url_as_received; // the entire URL as received, used for logging - DO NOT MODIFY
  122. BUFFER *url_path_decoded; // the path, decoded - it is incrementally parsed and altered
  123. BUFFER *url_query_string_decoded; // the query string, decoded - it is incrementally parsed and altered
  124. // THESE NEED TO BE FREED
  125. char *auth_bearer_token; // the Bearer auth token (if sent)
  126. char *server_host; // the Host: header
  127. char *forwarded_host; // the X-Forwarded-For: header
  128. char *origin; // the Origin: header
  129. char *user_agent; // the User-Agent: header
  130. char *post_payload; // when this request is a POST, this has the payload
  131. size_t post_payload_size; // the size of the buffer allocated for the payload
  132. // the actual contents may be less than the size
  133. // STATIC-THREADED WEB SERVER MEMBERS
  134. size_t pollinfo_slot; // POLLINFO slot of the web client
  135. size_t pollinfo_filecopy_slot; // POLLINFO slot of the file read
  136. #ifdef ENABLE_HTTPS
  137. NETDATA_SSL ssl;
  138. #endif
  139. struct { // A callback to check if the query should be interrupted / stopped
  140. web_client_interrupt_t callback;
  141. void *callback_data;
  142. } interrupt;
  143. struct {
  144. size_t received_bytes;
  145. size_t sent_bytes;
  146. size_t *memory_accounting; // temporary pointer for constructor to use
  147. } statistics;
  148. struct {
  149. usec_t timeout_ut; // timeout if set, or zero
  150. struct timeval tv_in; // request received
  151. struct timeval tv_ready; // request processed - response ready
  152. struct timeval tv_timeout_last_checkpoint; // last checkpoint
  153. } timings;
  154. struct {
  155. struct web_client *prev;
  156. struct web_client *next;
  157. } cache;
  158. struct response response;
  159. };
  160. int web_client_permission_denied(struct web_client *w);
  161. int web_client_bearer_required(struct web_client *w);
  162. ssize_t web_client_send(struct web_client *w);
  163. ssize_t web_client_receive(struct web_client *w);
  164. ssize_t web_client_read_file(struct web_client *w);
  165. void web_client_process_request_from_web_server(struct web_client *w);
  166. void web_client_request_done(struct web_client *w);
  167. void buffer_data_options2string(BUFFER *wb, uint32_t options);
  168. void web_client_build_http_header(struct web_client *w);
  169. void web_client_reuse_from_cache(struct web_client *w);
  170. struct web_client *web_client_create(size_t *statistics_memory_accounting);
  171. void web_client_free(struct web_client *w);
  172. #include "web/api/web_api_v1.h"
  173. #include "web/api/web_api_v2.h"
  174. #include "daemon/common.h"
  175. void web_client_decode_path_and_query_string(struct web_client *w, const char *path_and_query_string);
  176. int web_client_api_request(RRDHOST *host, struct web_client *w, char *url_path_fragment);
  177. const char *web_content_type_to_string(HTTP_CONTENT_TYPE content_type);
  178. void web_client_enable_deflate(struct web_client *w, int gzip);
  179. int web_client_api_request_with_node_selection(RRDHOST *host, struct web_client *w, char *decoded_url_path);
  180. void web_client_timeout_checkpoint_init(struct web_client *w);
  181. void web_client_timeout_checkpoint_set(struct web_client *w, int timeout_ms);
  182. usec_t web_client_timeout_checkpoint(struct web_client *w);
  183. bool web_client_timeout_checkpoint_and_check(struct web_client *w, usec_t *usec_since_last_checkpoint);
  184. usec_t web_client_timeout_checkpoint_response_ready(struct web_client *w, usec_t *usec_since_last_checkpoint);
  185. void web_client_log_completed_request(struct web_client *w, bool update_web_stats);
  186. #endif