web_client.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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,
  7. web_gzip_level,
  8. web_gzip_strategy;
  9. #endif /* NETDATA_WITH_ZLIB */
  10. extern int respect_web_browser_do_not_track_policy;
  11. extern char *web_x_frame_options;
  12. typedef enum web_client_mode {
  13. WEB_CLIENT_MODE_NORMAL = 0,
  14. WEB_CLIENT_MODE_FILECOPY = 1,
  15. WEB_CLIENT_MODE_OPTIONS = 2,
  16. WEB_CLIENT_MODE_STREAM = 3
  17. } WEB_CLIENT_MODE;
  18. typedef enum web_client_flags {
  19. WEB_CLIENT_FLAG_DEAD = 1 << 1, // if set, this client is dead
  20. WEB_CLIENT_FLAG_KEEPALIVE = 1 << 2, // if set, the web client will be re-used
  21. WEB_CLIENT_FLAG_WAIT_RECEIVE = 1 << 3, // if set, we are waiting more input data
  22. WEB_CLIENT_FLAG_WAIT_SEND = 1 << 4, // if set, we have data to send to the client
  23. WEB_CLIENT_FLAG_DO_NOT_TRACK = 1 << 5, // if set, we should not set cookies on this client
  24. WEB_CLIENT_FLAG_TRACKING_REQUIRED = 1 << 6, // if set, we need to send cookies
  25. WEB_CLIENT_FLAG_TCP_CLIENT = 1 << 7, // if set, the client is using a TCP socket
  26. WEB_CLIENT_FLAG_UNIX_CLIENT = 1 << 8, // if set, the client is using a UNIX socket
  27. WEB_CLIENT_FLAG_DONT_CLOSE_SOCKET = 1 << 9, // don't close the socket when cleaning up (static-threaded web server)
  28. } WEB_CLIENT_FLAGS;
  29. //#ifdef HAVE_C___ATOMIC
  30. //#define web_client_flag_check(w, flag) (__atomic_load_n(&((w)->flags), __ATOMIC_SEQ_CST) & flag)
  31. //#define web_client_flag_set(w, flag) __atomic_or_fetch(&((w)->flags), flag, __ATOMIC_SEQ_CST)
  32. //#define web_client_flag_clear(w, flag) __atomic_and_fetch(&((w)->flags), ~flag, __ATOMIC_SEQ_CST)
  33. //#else
  34. #define web_client_flag_check(w, flag) ((w)->flags & (flag))
  35. #define web_client_flag_set(w, flag) (w)->flags |= flag
  36. #define web_client_flag_clear(w, flag) (w)->flags &= ~flag
  37. //#endif
  38. #define WEB_CLIENT_IS_DEAD(w) web_client_flag_set(w, WEB_CLIENT_FLAG_DEAD)
  39. #define web_client_check_dead(w) web_client_flag_check(w, WEB_CLIENT_FLAG_DEAD)
  40. #define web_client_has_keepalive(w) web_client_flag_check(w, WEB_CLIENT_FLAG_KEEPALIVE)
  41. #define web_client_enable_keepalive(w) web_client_flag_set(w, WEB_CLIENT_FLAG_KEEPALIVE)
  42. #define web_client_disable_keepalive(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_KEEPALIVE)
  43. #define web_client_has_donottrack(w) web_client_flag_check(w, WEB_CLIENT_FLAG_DO_NOT_TRACK)
  44. #define web_client_enable_donottrack(w) web_client_flag_set(w, WEB_CLIENT_FLAG_DO_NOT_TRACK)
  45. #define web_client_disable_donottrack(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_DO_NOT_TRACK)
  46. #define web_client_has_tracking_required(w) web_client_flag_check(w, WEB_CLIENT_FLAG_TRACKING_REQUIRED)
  47. #define web_client_enable_tracking_required(w) web_client_flag_set(w, WEB_CLIENT_FLAG_TRACKING_REQUIRED)
  48. #define web_client_disable_tracking_required(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_TRACKING_REQUIRED)
  49. #define web_client_has_wait_receive(w) web_client_flag_check(w, WEB_CLIENT_FLAG_WAIT_RECEIVE)
  50. #define web_client_enable_wait_receive(w) web_client_flag_set(w, WEB_CLIENT_FLAG_WAIT_RECEIVE)
  51. #define web_client_disable_wait_receive(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_WAIT_RECEIVE)
  52. #define web_client_has_wait_send(w) web_client_flag_check(w, WEB_CLIENT_FLAG_WAIT_SEND)
  53. #define web_client_enable_wait_send(w) web_client_flag_set(w, WEB_CLIENT_FLAG_WAIT_SEND)
  54. #define web_client_disable_wait_send(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_WAIT_SEND)
  55. #define web_client_set_tcp(w) web_client_flag_set(w, WEB_CLIENT_FLAG_TCP_CLIENT)
  56. #define web_client_set_unix(w) web_client_flag_set(w, WEB_CLIENT_FLAG_UNIX_CLIENT)
  57. #define web_client_check_unix(w) web_client_flag_check(w, WEB_CLIENT_FLAG_UNIX_CLIENT)
  58. #define web_client_check_tcp(w) web_client_flag_check(w, WEB_CLIENT_FLAG_TCP_CLIENT)
  59. #define web_client_is_corkable(w) web_client_flag_check(w, WEB_CLIENT_FLAG_TCP_CLIENT)
  60. #define NETDATA_WEB_REQUEST_URL_SIZE 8192
  61. #define NETDATA_WEB_RESPONSE_ZLIB_CHUNK_SIZE 16384
  62. #define NETDATA_WEB_RESPONSE_HEADER_SIZE 4096
  63. #define NETDATA_WEB_REQUEST_COOKIE_SIZE 1024
  64. #define NETDATA_WEB_REQUEST_ORIGIN_HEADER_SIZE 1024
  65. #define NETDATA_WEB_RESPONSE_INITIAL_SIZE 16384
  66. #define NETDATA_WEB_REQUEST_RECEIVE_SIZE 16384
  67. #define NETDATA_WEB_REQUEST_MAX_SIZE 16384
  68. struct response {
  69. BUFFER *header; // our response header
  70. BUFFER *header_output; // internal use
  71. BUFFER *data; // our response data buffer
  72. int code; // the HTTP response code
  73. size_t rlen; // if non-zero, the excepted size of ifd (input of firecopy)
  74. size_t sent; // current data length sent to output
  75. int zoutput; // if set to 1, web_client_send() will send compressed data
  76. #ifdef NETDATA_WITH_ZLIB
  77. z_stream zstream; // zlib stream for sending compressed output to client
  78. Bytef zbuffer[NETDATA_WEB_RESPONSE_ZLIB_CHUNK_SIZE]; // temporary buffer for storing compressed output
  79. size_t zsent; // the compressed bytes we have sent to the client
  80. size_t zhave; // the compressed bytes that we have received from zlib
  81. unsigned int zinitialized:1;
  82. #endif /* NETDATA_WITH_ZLIB */
  83. };
  84. struct web_client {
  85. unsigned long long id;
  86. WEB_CLIENT_FLAGS flags; // status flags for the client
  87. WEB_CLIENT_MODE mode; // the operational mode of the client
  88. WEB_CLIENT_ACL acl; // the access list of the client
  89. int port_acl; // the operations permitted on the port the client connected to
  90. char *auth_bearer_token; // the Bearer auth token (if sent)
  91. size_t header_parse_tries;
  92. size_t header_parse_last_size;
  93. int tcp_cork; // 1 = we have a cork on the socket
  94. int ifd;
  95. int ofd;
  96. char client_ip[NI_MAXHOST+1];
  97. char client_port[NI_MAXSERV+1];
  98. char decoded_url[NETDATA_WEB_REQUEST_URL_SIZE + 1]; // we decode the URL in this buffer
  99. char last_url[NETDATA_WEB_REQUEST_URL_SIZE+1]; // we keep a copy of the decoded URL here
  100. struct timeval tv_in, tv_ready;
  101. char cookie1[NETDATA_WEB_REQUEST_COOKIE_SIZE+1];
  102. char cookie2[NETDATA_WEB_REQUEST_COOKIE_SIZE+1];
  103. char origin[NETDATA_WEB_REQUEST_ORIGIN_HEADER_SIZE+1];
  104. char *user_agent;
  105. struct response response;
  106. size_t stats_received_bytes;
  107. size_t stats_sent_bytes;
  108. // cache of web_client allocations
  109. struct web_client *prev; // maintain a linked list of web clients
  110. struct web_client *next; // for the web servers that need it
  111. // MULTI-THREADED WEB SERVER MEMBERS
  112. netdata_thread_t thread; // the thread servicing this client
  113. volatile int running; // 1 when the thread runs, 0 otherwise
  114. // STATIC-THREADED WEB SERVER MEMBERS
  115. size_t pollinfo_slot; // POLLINFO slot of the web client
  116. size_t pollinfo_filecopy_slot; // POLLINFO slot of the file read
  117. };
  118. extern uid_t web_files_uid(void);
  119. extern uid_t web_files_gid(void);
  120. extern int web_client_permission_denied(struct web_client *w);
  121. extern ssize_t web_client_send(struct web_client *w);
  122. extern ssize_t web_client_receive(struct web_client *w);
  123. extern ssize_t web_client_read_file(struct web_client *w);
  124. extern void web_client_process_request(struct web_client *w);
  125. extern void web_client_request_done(struct web_client *w);
  126. extern void buffer_data_options2string(BUFFER *wb, uint32_t options);
  127. extern int mysendfile(struct web_client *w, char *filename);
  128. #include "daemon/common.h"
  129. #endif