web_client.h 12 KB

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