netdata
web_client.h
1 // SPDX-License-Identifier: GPL-3.0+
2 
3 #ifndef NETDATA_WEB_CLIENT_H
4 #define NETDATA_WEB_CLIENT_H 1
5 
6 #ifdef NETDATA_WITH_ZLIB
7 extern int web_enable_gzip,
8  web_gzip_level,
9  web_gzip_strategy;
10 #endif /* NETDATA_WITH_ZLIB */
11 
12 extern int respect_web_browser_do_not_track_policy;
13 extern char *web_x_frame_options;
14 
15 typedef enum web_client_mode {
16  WEB_CLIENT_MODE_NORMAL = 0,
17  WEB_CLIENT_MODE_FILECOPY = 1,
18  WEB_CLIENT_MODE_OPTIONS = 2,
19  WEB_CLIENT_MODE_STREAM = 3
20 } WEB_CLIENT_MODE;
21 
22 typedef enum web_client_flags {
23  WEB_CLIENT_FLAG_DEAD = 1 << 1, // if set, this client is dead
24 
25  WEB_CLIENT_FLAG_KEEPALIVE = 1 << 2, // if set, the web client will be re-used
26 
27  WEB_CLIENT_FLAG_WAIT_RECEIVE = 1 << 3, // if set, we are waiting more input data
28  WEB_CLIENT_FLAG_WAIT_SEND = 1 << 4, // if set, we have data to send to the client
29 
30  WEB_CLIENT_FLAG_DO_NOT_TRACK = 1 << 5, // if set, we should not set cookies on this client
31  WEB_CLIENT_FLAG_TRACKING_REQUIRED = 1 << 6, // if set, we need to send cookies
32 
33  WEB_CLIENT_FLAG_TCP_CLIENT = 1 << 7, // if set, the client is using a TCP socket
34  WEB_CLIENT_FLAG_UNIX_CLIENT = 1 << 8, // if set, the client is using a UNIX socket
35 
36  WEB_CLIENT_FLAG_DONT_CLOSE_SOCKET = 1 << 9, // don't close the socket when cleaning up (static-threaded web server)
37 } WEB_CLIENT_FLAGS;
38 
39 //#ifdef HAVE_C___ATOMIC
40 //#define web_client_flag_check(w, flag) (__atomic_load_n(&((w)->flags), __ATOMIC_SEQ_CST) & flag)
41 //#define web_client_flag_set(w, flag) __atomic_or_fetch(&((w)->flags), flag, __ATOMIC_SEQ_CST)
42 //#define web_client_flag_clear(w, flag) __atomic_and_fetch(&((w)->flags), ~flag, __ATOMIC_SEQ_CST)
43 //#else
44 #define web_client_flag_check(w, flag) ((w)->flags & (flag))
45 #define web_client_flag_set(w, flag) (w)->flags |= flag
46 #define web_client_flag_clear(w, flag) (w)->flags &= ~flag
47 //#endif
48 
49 #define WEB_CLIENT_IS_DEAD(w) web_client_flag_set(w, WEB_CLIENT_FLAG_DEAD)
50 #define web_client_check_dead(w) web_client_flag_check(w, WEB_CLIENT_FLAG_DEAD)
51 
52 #define web_client_has_keepalive(w) web_client_flag_check(w, WEB_CLIENT_FLAG_KEEPALIVE)
53 #define web_client_enable_keepalive(w) web_client_flag_set(w, WEB_CLIENT_FLAG_KEEPALIVE)
54 #define web_client_disable_keepalive(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_KEEPALIVE)
55 
56 #define web_client_has_donottrack(w) web_client_flag_check(w, WEB_CLIENT_FLAG_DO_NOT_TRACK)
57 #define web_client_enable_donottrack(w) web_client_flag_set(w, WEB_CLIENT_FLAG_DO_NOT_TRACK)
58 #define web_client_disable_donottrack(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_DO_NOT_TRACK)
59 
60 #define web_client_has_tracking_required(w) web_client_flag_check(w, WEB_CLIENT_FLAG_TRACKING_REQUIRED)
61 #define web_client_enable_tracking_required(w) web_client_flag_set(w, WEB_CLIENT_FLAG_TRACKING_REQUIRED)
62 #define web_client_disable_tracking_required(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_TRACKING_REQUIRED)
63 
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 
68 #define web_client_has_wait_send(w) web_client_flag_check(w, WEB_CLIENT_FLAG_WAIT_SEND)
69 #define web_client_enable_wait_send(w) web_client_flag_set(w, WEB_CLIENT_FLAG_WAIT_SEND)
70 #define web_client_disable_wait_send(w) web_client_flag_clear(w, WEB_CLIENT_FLAG_WAIT_SEND)
71 
72 #define web_client_set_tcp(w) web_client_flag_set(w, WEB_CLIENT_FLAG_TCP_CLIENT)
73 #define web_client_set_unix(w) web_client_flag_set(w, WEB_CLIENT_FLAG_UNIX_CLIENT)
74 #define web_client_check_unix(w) web_client_flag_check(w, WEB_CLIENT_FLAG_UNIX_CLIENT)
75 #define web_client_check_tcp(w) web_client_flag_check(w, WEB_CLIENT_FLAG_TCP_CLIENT)
76 
77 #define web_client_is_corkable(w) web_client_flag_check(w, WEB_CLIENT_FLAG_TCP_CLIENT)
78 
79 #define NETDATA_WEB_REQUEST_URL_SIZE 8192
80 #define NETDATA_WEB_RESPONSE_ZLIB_CHUNK_SIZE 16384
81 #define NETDATA_WEB_RESPONSE_HEADER_SIZE 4096
82 #define NETDATA_WEB_REQUEST_COOKIE_SIZE 1024
83 #define NETDATA_WEB_REQUEST_ORIGIN_HEADER_SIZE 1024
84 #define NETDATA_WEB_RESPONSE_INITIAL_SIZE 16384
85 #define NETDATA_WEB_REQUEST_RECEIVE_SIZE 16384
86 #define NETDATA_WEB_REQUEST_MAX_SIZE 16384
87 
88 struct response {
89  BUFFER *header; // our response header
90  BUFFER *header_output; // internal use
91  BUFFER *data; // our response data buffer
92 
93  int code; // the HTTP response code
94 
95  size_t rlen; // if non-zero, the excepted size of ifd (input of firecopy)
96  size_t sent; // current data length sent to output
97 
98  int zoutput; // if set to 1, web_client_send() will send compressed data
99 #ifdef NETDATA_WITH_ZLIB
100  z_stream zstream; // zlib stream for sending compressed output to client
101  Bytef zbuffer[NETDATA_WEB_RESPONSE_ZLIB_CHUNK_SIZE]; // temporary buffer for storing compressed output
102  size_t zsent; // the compressed bytes we have sent to the client
103  size_t zhave; // the compressed bytes that we have received from zlib
104  unsigned int zinitialized:1;
105 #endif /* NETDATA_WITH_ZLIB */
106 
107 };
108 
109 typedef enum web_client_acl {
110  WEB_CLIENT_ACL_NONE = 0,
111  WEB_CLIENT_ACL_NOCHECK = 0,
112  WEB_CLIENT_ACL_DASHBOARD = 1 << 0,
113  WEB_CLIENT_ACL_REGISTRY = 1 << 1,
114  WEB_CLIENT_ACL_BADGE = 1 << 2
115 } WEB_CLIENT_ACL;
116 
117 #define web_client_can_access_dashboard(w) ((w)->acl & WEB_CLIENT_ACL_DASHBOARD)
118 #define web_client_can_access_registry(w) ((w)->acl & WEB_CLIENT_ACL_REGISTRY)
119 #define web_client_can_access_badges(w) ((w)->acl & WEB_CLIENT_ACL_BADGE)
120 
121 #define web_client_can_access_stream(w) \
122  (!web_allow_streaming_from || simple_pattern_matches(web_allow_streaming_from, (w)->client_ip))
123 
124 #define web_client_can_access_netdataconf(w) \
125  (!web_allow_netdataconf_from || simple_pattern_matches(web_allow_netdataconf_from, (w)->client_ip))
126 
127 struct web_client {
128  unsigned long long id;
129 
130  WEB_CLIENT_FLAGS flags; // status flags for the client
131  WEB_CLIENT_MODE mode; // the operational mode of the client
132  WEB_CLIENT_ACL acl; // the access list of the client
133 
134  size_t header_parse_tries;
135  size_t header_parse_last_size;
136 
137  int tcp_cork; // 1 = we have a cork on the socket
138 
139  int ifd;
140  int ofd;
141 
142  char client_ip[NI_MAXHOST+1];
143  char client_port[NI_MAXSERV+1];
144 
145  char decoded_url[NETDATA_WEB_REQUEST_URL_SIZE + 1]; // we decode the URL in this buffer
146  char last_url[NETDATA_WEB_REQUEST_URL_SIZE+1]; // we keep a copy of the decoded URL here
147 
148  struct timeval tv_in, tv_ready;
149 
150  char cookie1[NETDATA_WEB_REQUEST_COOKIE_SIZE+1];
151  char cookie2[NETDATA_WEB_REQUEST_COOKIE_SIZE+1];
152  char origin[NETDATA_WEB_REQUEST_ORIGIN_HEADER_SIZE+1];
153  char *user_agent;
154 
155  struct response response;
156 
157  size_t stats_received_bytes;
158  size_t stats_sent_bytes;
159 
160  // cache of web_client allocations
161  struct web_client *prev; // maintain a linked list of web clients
162  struct web_client *next; // for the web servers that need it
163 
164  // MULTI-THREADED WEB SERVER MEMBERS
165  netdata_thread_t thread; // the thread servicing this client
166  volatile int running; // 1 when the thread runs, 0 otherwise
167 
168  // STATIC-THREADED WEB SERVER MEMBERS
169  size_t pollinfo_slot; // POLLINFO slot of the web client
170  size_t pollinfo_filecopy_slot; // POLLINFO slot of the file read
171 };
172 
173 extern uid_t web_files_uid(void);
174 extern uid_t web_files_gid(void);
175 
176 extern int web_client_permission_denied(struct web_client *w);
177 
178 extern ssize_t web_client_send(struct web_client *w);
179 extern ssize_t web_client_receive(struct web_client *w);
180 extern ssize_t web_client_read_file(struct web_client *w);
181 
182 extern void web_client_process_request(struct web_client *w);
183 extern void web_client_request_done(struct web_client *w);
184 
185 extern int web_client_api_request_v1_data_group(char *name, int def);
186 extern const char *group_method2string(int group);
187 
188 extern void buffer_data_options2string(BUFFER *wb, uint32_t options);
189 
190 extern int mysendfile(struct web_client *w, char *filename);
191 
192 #endif
Definition: web_buffer.h:8
Definition: web_client.h:127
Definition: web_client.h:88