web_client.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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_FORBIDDEN 403
  17. #define HTTP_RESP_NOT_FOUND 404
  18. #define HTTP_RESP_PRECOND_FAIL 412
  19. // HTTP_CODES 5XX Server Errors
  20. #define HTTP_RESP_INTERNAL_SERVER_ERROR 500
  21. #define HTTP_RESP_BACKEND_FETCH_FAILED 503
  22. extern int respect_web_browser_do_not_track_policy;
  23. extern char *web_x_frame_options;
  24. typedef enum web_client_mode {
  25. WEB_CLIENT_MODE_NORMAL = 0,
  26. WEB_CLIENT_MODE_FILECOPY = 1,
  27. WEB_CLIENT_MODE_OPTIONS = 2,
  28. WEB_CLIENT_MODE_STREAM = 3
  29. } WEB_CLIENT_MODE;
  30. typedef enum {
  31. HTTP_VALIDATION_OK,
  32. HTTP_VALIDATION_NOT_SUPPORTED,
  33. HTTP_VALIDATION_MALFORMED_URL,
  34. #ifdef ENABLE_HTTPS
  35. HTTP_VALIDATION_INCOMPLETE,
  36. HTTP_VALIDATION_REDIRECT
  37. #else
  38. HTTP_VALIDATION_INCOMPLETE
  39. #endif
  40. } HTTP_VALIDATION;
  41. typedef enum web_client_flags {
  42. WEB_CLIENT_FLAG_DEAD = 1 << 1, // if set, this client is dead
  43. WEB_CLIENT_FLAG_KEEPALIVE = 1 << 2, // if set, the web client will be re-used
  44. WEB_CLIENT_FLAG_WAIT_RECEIVE = 1 << 3, // if set, we are waiting more input data
  45. WEB_CLIENT_FLAG_WAIT_SEND = 1 << 4, // if set, we have data to send to the client
  46. WEB_CLIENT_FLAG_DO_NOT_TRACK = 1 << 5, // if set, we should not set cookies on this client
  47. WEB_CLIENT_FLAG_TRACKING_REQUIRED = 1 << 6, // if set, we need to send cookies
  48. WEB_CLIENT_FLAG_TCP_CLIENT = 1 << 7, // if set, the client is using a TCP socket
  49. WEB_CLIENT_FLAG_UNIX_CLIENT = 1 << 8, // if set, the client is using a UNIX socket
  50. WEB_CLIENT_FLAG_DONT_CLOSE_SOCKET = 1 << 9, // don't close the socket when cleaning up (static-threaded web server)
  51. WEB_CLIENT_CHUNKED_TRANSFER = 1 << 10, // chunked transfer (used with zlib compression)
  52. } WEB_CLIENT_FLAGS;
  53. //#ifdef HAVE_C___ATOMIC
  54. //#define web_client_flag_check(w, flag) (__atomic_load_n(&((w)->flags), __ATOMIC_SEQ_CST) & flag)
  55. //#define web_client_flag_set(w, flag) __atomic_or_fetch(&((w)->flags), flag, __ATOMIC_SEQ_CST)
  56. //#define web_client_flag_clear(w, flag) __atomic_and_fetch(&((w)->flags), ~flag, __ATOMIC_SEQ_CST)
  57. //#else
  58. #define web_client_flag_check(w, flag) ((w)->flags & (flag))
  59. #define web_client_flag_set(w, flag) (w)->flags |= flag
  60. #define web_client_flag_clear(w, flag) (w)->flags &= ~flag
  61. //#endif
  62. #define WEB_CLIENT_IS_DEAD(w) web_client_flag_set(w, WEB_CLIENT_FLAG_DEAD)
  63. #define web_client_check_dead(w) web_client_flag_check(w, WEB_CLIENT_FLAG_DEAD)
  64. #define web_client_has_keepalive(w) web_client_flag_check(w, WEB_CLIENT_FLAG_KEEPALIVE)
  65. #define web_client_enable_keepalive(w) web_client_flag_set(w, WEB_CLIENT_FLAG_KEEPALIVE)
  66. #define web_client_disable_keepalive(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_KEEPALIVE)
  67. #define web_client_has_donottrack(w) web_client_flag_check(w, WEB_CLIENT_FLAG_DO_NOT_TRACK)
  68. #define web_client_enable_donottrack(w) web_client_flag_set(w, WEB_CLIENT_FLAG_DO_NOT_TRACK)
  69. #define web_client_disable_donottrack(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_DO_NOT_TRACK)
  70. #define web_client_has_tracking_required(w) web_client_flag_check(w, WEB_CLIENT_FLAG_TRACKING_REQUIRED)
  71. #define web_client_enable_tracking_required(w) web_client_flag_set(w, WEB_CLIENT_FLAG_TRACKING_REQUIRED)
  72. #define web_client_disable_tracking_required(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_TRACKING_REQUIRED)
  73. #define web_client_has_wait_receive(w) web_client_flag_check(w, WEB_CLIENT_FLAG_WAIT_RECEIVE)
  74. #define web_client_enable_wait_receive(w) web_client_flag_set(w, WEB_CLIENT_FLAG_WAIT_RECEIVE)
  75. #define web_client_disable_wait_receive(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_WAIT_RECEIVE)
  76. #define web_client_has_wait_send(w) web_client_flag_check(w, WEB_CLIENT_FLAG_WAIT_SEND)
  77. #define web_client_enable_wait_send(w) web_client_flag_set(w, WEB_CLIENT_FLAG_WAIT_SEND)
  78. #define web_client_disable_wait_send(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_WAIT_SEND)
  79. #define web_client_set_tcp(w) web_client_flag_set(w, WEB_CLIENT_FLAG_TCP_CLIENT)
  80. #define web_client_set_unix(w) web_client_flag_set(w, WEB_CLIENT_FLAG_UNIX_CLIENT)
  81. #define web_client_check_unix(w) web_client_flag_check(w, WEB_CLIENT_FLAG_UNIX_CLIENT)
  82. #define web_client_check_tcp(w) web_client_flag_check(w, WEB_CLIENT_FLAG_TCP_CLIENT)
  83. #define web_client_is_corkable(w) web_client_flag_check(w, WEB_CLIENT_FLAG_TCP_CLIENT)
  84. #define NETDATA_WEB_REQUEST_URL_SIZE 8192
  85. #define NETDATA_WEB_RESPONSE_ZLIB_CHUNK_SIZE 16384
  86. #define NETDATA_WEB_RESPONSE_HEADER_SIZE 4096
  87. #define NETDATA_WEB_REQUEST_COOKIE_SIZE 1024
  88. #define NETDATA_WEB_REQUEST_ORIGIN_HEADER_SIZE 1024
  89. #define NETDATA_WEB_RESPONSE_INITIAL_SIZE 16384
  90. #define NETDATA_WEB_REQUEST_RECEIVE_SIZE 16384
  91. #define NETDATA_WEB_REQUEST_MAX_SIZE 16384
  92. struct response {
  93. BUFFER *header; // our response header
  94. BUFFER *header_output; // internal use
  95. BUFFER *data; // our response data buffer
  96. int code; // the HTTP response code
  97. size_t rlen; // if non-zero, the excepted size of ifd (input of firecopy)
  98. size_t sent; // current data length sent to output
  99. int zoutput; // if set to 1, web_client_send() will send compressed data
  100. #ifdef NETDATA_WITH_ZLIB
  101. z_stream zstream; // zlib stream for sending compressed output to client
  102. Bytef zbuffer[NETDATA_WEB_RESPONSE_ZLIB_CHUNK_SIZE]; // temporary buffer for storing compressed output
  103. size_t zsent; // the compressed bytes we have sent to the client
  104. size_t zhave; // the compressed bytes that we have received from zlib
  105. unsigned int zinitialized : 1;
  106. #endif /* NETDATA_WITH_ZLIB */
  107. };
  108. struct web_client {
  109. unsigned long long id;
  110. WEB_CLIENT_FLAGS flags; // status flags for the client
  111. WEB_CLIENT_MODE mode; // the operational mode of the client
  112. WEB_CLIENT_ACL acl; // the access list of the client
  113. int port_acl; // the operations permitted on the port the client connected to
  114. char *auth_bearer_token; // the Bearer auth token (if sent)
  115. size_t header_parse_tries;
  116. size_t header_parse_last_size;
  117. int tcp_cork; // 1 = we have a cork on the socket
  118. int ifd;
  119. int ofd;
  120. char client_ip[INET6_ADDRSTRLEN]; // Defined buffer sizes include null-terminators
  121. char client_port[NI_MAXSERV];
  122. char server_host[NI_MAXHOST];
  123. char client_host[NI_MAXHOST];
  124. char forwarded_host[NI_MAXHOST]; //Used with proxy
  125. char decoded_url[NETDATA_WEB_REQUEST_URL_SIZE + 1]; // we decode the URL in this buffer
  126. char decoded_query_string[NETDATA_WEB_REQUEST_URL_SIZE + 1]; // we decode the Query String in this buffer
  127. char last_url[NETDATA_WEB_REQUEST_URL_SIZE + 1]; // we keep a copy of the decoded URL here
  128. size_t url_path_length;
  129. char separator; // This value can be either '?' or 'f'
  130. char *url_search_path; //A pointer to the search path sent by the client
  131. struct timeval tv_in, tv_ready;
  132. char cookie1[NETDATA_WEB_REQUEST_COOKIE_SIZE + 1];
  133. char cookie2[NETDATA_WEB_REQUEST_COOKIE_SIZE + 1];
  134. char origin[NETDATA_WEB_REQUEST_ORIGIN_HEADER_SIZE + 1];
  135. char *user_agent;
  136. struct response response;
  137. size_t stats_received_bytes;
  138. size_t stats_sent_bytes;
  139. // cache of web_client allocations
  140. struct web_client *prev; // maintain a linked list of web clients
  141. struct web_client *next; // for the web servers that need it
  142. // MULTI-THREADED WEB SERVER MEMBERS
  143. netdata_thread_t thread; // the thread servicing this client
  144. volatile int running; // 1 when the thread runs, 0 otherwise
  145. // STATIC-THREADED WEB SERVER MEMBERS
  146. size_t pollinfo_slot; // POLLINFO slot of the web client
  147. size_t pollinfo_filecopy_slot; // POLLINFO slot of the file read
  148. #ifdef ENABLE_HTTPS
  149. struct netdata_ssl ssl;
  150. #endif
  151. };
  152. extern int web_client_permission_denied(struct web_client *w);
  153. extern ssize_t web_client_send(struct web_client *w);
  154. extern ssize_t web_client_receive(struct web_client *w);
  155. extern ssize_t web_client_read_file(struct web_client *w);
  156. extern void web_client_process_request(struct web_client *w);
  157. extern void web_client_request_done(struct web_client *w);
  158. extern void buffer_data_options2string(BUFFER *wb, uint32_t options);
  159. extern int mysendfile(struct web_client *w, char *filename);
  160. extern void web_client_build_http_header(struct web_client *w);
  161. extern char *strip_control_characters(char *url);
  162. #include "daemon/common.h"
  163. #endif