web_client.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #ifdef NETDATA_WITH_ZLIB
  6. extern int web_enable_gzip, web_gzip_level, web_gzip_strategy;
  7. #endif /* NETDATA_WITH_ZLIB */
  8. // HTTP_CODES 2XX Success
  9. #define HTTP_RESP_OK 200
  10. // HTTP_CODES 3XX Redirections
  11. #define HTTP_RESP_MOVED_PERM 301
  12. #define HTTP_RESP_REDIR_TEMP 307
  13. #define HTTP_RESP_REDIR_PERM 308
  14. // HTTP_CODES 4XX Client Errors
  15. #define HTTP_RESP_BAD_REQUEST 400
  16. #define HTTP_RESP_UNAUTHORIZED 401
  17. #define HTTP_RESP_FORBIDDEN 403
  18. #define HTTP_RESP_NOT_FOUND 404
  19. #define HTTP_RESP_CONFLICT 409
  20. #define HTTP_RESP_PRECOND_FAIL 412
  21. #define HTTP_RESP_CONTENT_TOO_LONG 413
  22. // HTTP_CODES 5XX Server Errors
  23. #define HTTP_RESP_INTERNAL_SERVER_ERROR 500
  24. #define HTTP_RESP_BACKEND_FETCH_FAILED 503
  25. #define HTTP_RESP_SERVICE_UNAVAILABLE 503
  26. #define HTTP_RESP_GATEWAY_TIMEOUT 504
  27. #define HTTP_RESP_BACKEND_RESPONSE_INVALID 591
  28. #define HTTP_REQ_MAX_HEADER_FETCH_TRIES 100
  29. extern int respect_web_browser_do_not_track_policy;
  30. extern char *web_x_frame_options;
  31. typedef enum web_client_mode {
  32. WEB_CLIENT_MODE_GET = 0,
  33. WEB_CLIENT_MODE_POST = 1,
  34. WEB_CLIENT_MODE_FILECOPY = 2,
  35. WEB_CLIENT_MODE_OPTIONS = 3,
  36. WEB_CLIENT_MODE_STREAM = 4,
  37. } WEB_CLIENT_MODE;
  38. typedef enum {
  39. HTTP_VALIDATION_OK,
  40. HTTP_VALIDATION_NOT_SUPPORTED,
  41. HTTP_VALIDATION_TOO_MANY_READ_RETRIES,
  42. HTTP_VALIDATION_EXCESS_REQUEST_DATA,
  43. HTTP_VALIDATION_MALFORMED_URL,
  44. HTTP_VALIDATION_INCOMPLETE,
  45. #ifdef ENABLE_HTTPS
  46. HTTP_VALIDATION_REDIRECT
  47. #endif
  48. } HTTP_VALIDATION;
  49. typedef enum web_client_flags {
  50. WEB_CLIENT_FLAG_DEAD = 1 << 1, // if set, this client is dead
  51. WEB_CLIENT_FLAG_KEEPALIVE = 1 << 2, // if set, the web client will be re-used
  52. WEB_CLIENT_FLAG_WAIT_RECEIVE = 1 << 3, // if set, we are waiting more input data
  53. WEB_CLIENT_FLAG_WAIT_SEND = 1 << 4, // if set, we have data to send to the client
  54. WEB_CLIENT_FLAG_DO_NOT_TRACK = 1 << 5, // if set, we should not set cookies on this client
  55. WEB_CLIENT_FLAG_TRACKING_REQUIRED = 1 << 6, // if set, we need to send cookies
  56. WEB_CLIENT_FLAG_TCP_CLIENT = 1 << 7, // if set, the client is using a TCP socket
  57. WEB_CLIENT_FLAG_UNIX_CLIENT = 1 << 8, // if set, the client is using a UNIX socket
  58. WEB_CLIENT_FLAG_DONT_CLOSE_SOCKET = 1 << 9, // don't close the socket when cleaning up (static-threaded web server)
  59. WEB_CLIENT_CHUNKED_TRANSFER = 1 << 10, // chunked transfer (used with zlib compression)
  60. WEB_CLIENT_FLAG_SSL_WAIT_RECEIVE = 1 << 11, // if set, we are waiting more input data from an ssl conn
  61. WEB_CLIENT_FLAG_SSL_WAIT_SEND = 1 << 12, // if set, we have data to send to the client from an ssl conn
  62. } WEB_CLIENT_FLAGS;
  63. #define web_client_flag_check(w, flag) ((w)->flags & (flag))
  64. #define web_client_flag_set(w, flag) (w)->flags |= flag
  65. #define web_client_flag_clear(w, flag) (w)->flags &= ~flag
  66. #define WEB_CLIENT_IS_DEAD(w) web_client_flag_set(w, WEB_CLIENT_FLAG_DEAD)
  67. #define web_client_check_dead(w) web_client_flag_check(w, WEB_CLIENT_FLAG_DEAD)
  68. #define web_client_has_keepalive(w) web_client_flag_check(w, WEB_CLIENT_FLAG_KEEPALIVE)
  69. #define web_client_enable_keepalive(w) web_client_flag_set(w, WEB_CLIENT_FLAG_KEEPALIVE)
  70. #define web_client_disable_keepalive(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_KEEPALIVE)
  71. #define web_client_has_donottrack(w) web_client_flag_check(w, WEB_CLIENT_FLAG_DO_NOT_TRACK)
  72. #define web_client_enable_donottrack(w) web_client_flag_set(w, WEB_CLIENT_FLAG_DO_NOT_TRACK)
  73. #define web_client_disable_donottrack(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_DO_NOT_TRACK)
  74. #define web_client_has_tracking_required(w) web_client_flag_check(w, WEB_CLIENT_FLAG_TRACKING_REQUIRED)
  75. #define web_client_enable_tracking_required(w) web_client_flag_set(w, WEB_CLIENT_FLAG_TRACKING_REQUIRED)
  76. #define web_client_disable_tracking_required(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_TRACKING_REQUIRED)
  77. #define web_client_has_wait_receive(w) web_client_flag_check(w, WEB_CLIENT_FLAG_WAIT_RECEIVE)
  78. #define web_client_enable_wait_receive(w) web_client_flag_set(w, WEB_CLIENT_FLAG_WAIT_RECEIVE)
  79. #define web_client_disable_wait_receive(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_WAIT_RECEIVE)
  80. #define web_client_has_wait_send(w) web_client_flag_check(w, WEB_CLIENT_FLAG_WAIT_SEND)
  81. #define web_client_enable_wait_send(w) web_client_flag_set(w, WEB_CLIENT_FLAG_WAIT_SEND)
  82. #define web_client_disable_wait_send(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_WAIT_SEND)
  83. #define web_client_has_ssl_wait_receive(w) web_client_flag_check(w, WEB_CLIENT_FLAG_SSL_WAIT_RECEIVE)
  84. #define web_client_enable_ssl_wait_receive(w) web_client_flag_set(w, WEB_CLIENT_FLAG_SSL_WAIT_RECEIVE)
  85. #define web_client_disable_ssl_wait_receive(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_SSL_WAIT_RECEIVE)
  86. #define web_client_has_ssl_wait_send(w) web_client_flag_check(w, WEB_CLIENT_FLAG_SSL_WAIT_SEND)
  87. #define web_client_enable_ssl_wait_send(w) web_client_flag_set(w, WEB_CLIENT_FLAG_SSL_WAIT_SEND)
  88. #define web_client_disable_ssl_wait_send(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_SSL_WAIT_SEND)
  89. #define web_client_set_tcp(w) web_client_flag_set(w, WEB_CLIENT_FLAG_TCP_CLIENT)
  90. #define web_client_set_unix(w) web_client_flag_set(w, WEB_CLIENT_FLAG_UNIX_CLIENT)
  91. #define web_client_check_unix(w) web_client_flag_check(w, WEB_CLIENT_FLAG_UNIX_CLIENT)
  92. #define web_client_check_tcp(w) web_client_flag_check(w, WEB_CLIENT_FLAG_TCP_CLIENT)
  93. #define web_client_is_corkable(w) web_client_flag_check(w, WEB_CLIENT_FLAG_TCP_CLIENT)
  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. struct response {
  102. BUFFER *header; // our response header
  103. BUFFER *header_output; // internal use
  104. BUFFER *data; // our response data buffer
  105. short int code; // the HTTP response code
  106. bool has_cookies;
  107. size_t rlen; // if non-zero, the excepted size of ifd (input of firecopy)
  108. size_t sent; // current data length sent to output
  109. bool zoutput; // if set to 1, web_client_send() will send compressed data
  110. #ifdef NETDATA_WITH_ZLIB
  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. #endif /* NETDATA_WITH_ZLIB */
  117. };
  118. struct web_client;
  119. typedef bool (*web_client_interrupt_t)(struct web_client *, void *data);
  120. struct web_client {
  121. unsigned long long id;
  122. size_t use_count;
  123. WEB_CLIENT_FLAGS flags; // status flags for the client
  124. WEB_CLIENT_MODE mode; // the operational mode of the client
  125. WEB_CLIENT_ACL acl; // the access list of the client
  126. int port_acl; // the operations permitted on the port the client connected to
  127. size_t header_parse_tries;
  128. size_t header_parse_last_size;
  129. bool tcp_cork;
  130. int ifd;
  131. int ofd;
  132. char client_ip[INET6_ADDRSTRLEN]; // Defined buffer sizes include null-terminators
  133. char client_port[NI_MAXSERV];
  134. char client_host[NI_MAXHOST];
  135. BUFFER *url_as_received; // the entire URL as received, used for logging - DO NOT MODIFY
  136. BUFFER *url_path_decoded; // the path, decoded - it is incrementally parsed and altered
  137. BUFFER *url_query_string_decoded; // the query string, decoded - it is incrementally parsed and altered
  138. // THESE NEED TO BE FREED
  139. char *auth_bearer_token; // the Bearer auth token (if sent)
  140. char *server_host; // the Host: header
  141. char *forwarded_host; // the X-Forwarded-For: header
  142. char *origin; // the Origin: header
  143. char *user_agent; // the User-Agent: header
  144. char *post_payload; // when this request is a POST, this has the payload
  145. size_t post_payload_size; // the size of the buffer allocated for the payload
  146. // the actual contents may be less than the size
  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. struct netdata_ssl ssl;
  152. #endif
  153. struct { // A callback to check if the query should be interrupted / stopped
  154. web_client_interrupt_t callback;
  155. void *callback_data;
  156. } interrupt;
  157. struct {
  158. size_t received_bytes;
  159. size_t sent_bytes;
  160. size_t *memory_accounting; // temporary pointer for constructor to use
  161. } statistics;
  162. struct {
  163. usec_t timeout_ut; // timeout if set, or zero
  164. struct timeval tv_in; // request received
  165. struct timeval tv_ready; // request processed - response ready
  166. struct timeval tv_timeout_last_checkpoint; // last checkpoint
  167. } timings;
  168. struct {
  169. struct web_client *prev;
  170. struct web_client *next;
  171. } cache;
  172. struct response response;
  173. };
  174. int web_client_permission_denied(struct web_client *w);
  175. ssize_t web_client_send(struct web_client *w);
  176. ssize_t web_client_receive(struct web_client *w);
  177. ssize_t web_client_read_file(struct web_client *w);
  178. void web_client_process_request(struct web_client *w);
  179. void web_client_request_done(struct web_client *w);
  180. void buffer_data_options2string(BUFFER *wb, uint32_t options);
  181. int mysendfile(struct web_client *w, char *filename);
  182. void web_client_build_http_header(struct web_client *w);
  183. char *strip_control_characters(char *url);
  184. int web_client_socket_is_now_used_for_streaming(struct web_client *w);
  185. void web_client_zero(struct web_client *w);
  186. struct web_client *web_client_create(size_t *statistics_memory_accounting);
  187. void web_client_free(struct web_client *w);
  188. #ifdef ENABLE_HTTPS
  189. void web_client_reuse_ssl(struct web_client *w);
  190. #endif
  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. const char *web_content_type_to_string(HTTP_CONTENT_TYPE content_type);
  197. void web_client_enable_deflate(struct web_client *w, int gzip);
  198. int web_client_api_request_with_node_selection(RRDHOST *host, struct web_client *w, char *decoded_url_path);
  199. void web_client_timeout_checkpoint_init(struct web_client *w);
  200. void web_client_timeout_checkpoint_set(struct web_client *w, int timeout_ms);
  201. usec_t web_client_timeout_checkpoint(struct web_client *w);
  202. bool web_client_timeout_checkpoint_and_check(struct web_client *w, usec_t *usec_since_last_checkpoint);
  203. usec_t web_client_timeout_checkpoint_response_ready(struct web_client *w, usec_t *usec_since_last_checkpoint);
  204. #endif