]> arthur.barton.de Git - netdata.git/blob - src/web_client.h
c5a448e0b850b828c85f97e00a2bac18a0c5f565
[netdata.git] / src / web_client.h
1
2 #ifdef NETDATA_WITH_ZLIB
3 #include <zlib.h>
4 #endif
5
6 #include <sys/time.h>
7 #include <string.h>
8 #include <sys/types.h>
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <arpa/inet.h>
12 #include <netdb.h>
13
14 #include "web_buffer.h"
15 #include "dictionary.h"
16
17 #define DEFAULT_DISCONNECT_IDLE_WEB_CLIENTS_AFTER_SECONDS 60
18 extern int web_client_timeout;
19 extern int web_enable_gzip;
20
21 #ifndef NETDATA_WEB_CLIENT_H
22 #define NETDATA_WEB_CLIENT_H 1
23
24 #define WEB_CLIENT_MODE_NORMAL          0
25 #define WEB_CLIENT_MODE_FILECOPY        1
26 #define WEB_CLIENT_MODE_OPTIONS         2
27
28 #define URL_MAX 8192
29 #define ZLIB_CHUNK      16384
30 #define HTTP_RESPONSE_HEADER_SIZE 4096
31 #define COOKIE_MAX 1024
32 #define ORIGIN_MAX 1024
33
34 struct response {
35         BUFFER *header;                                 // our response header
36         BUFFER *header_output;                  // internal use
37         BUFFER *data;                                   // our response data buffer
38
39         int code;                                               // the HTTP response code
40
41         size_t rlen;                                    // if non-zero, the excepted size of ifd (input of firecopy)
42         size_t sent;                                    // current data length sent to output
43
44         int zoutput;                                    // if set to 1, web_client_send() will send compressed data
45 #ifdef NETDATA_WITH_ZLIB
46         z_stream zstream;                               // zlib stream for sending compressed output to client
47         Bytef zbuffer[ZLIB_CHUNK];              // temporary buffer for storing compressed output
48         size_t zsent;                                   // the compressed bytes we have sent to the client
49         size_t zhave;                                   // the compressed bytes that we have received from zlib
50         int zinitialized:1;
51 #endif
52
53 };
54
55 struct web_client {
56         unsigned long long id;
57
58         uint8_t obsolete:1;                                     // if set to 1, the listener will remove this client
59                                                                                 // after setting this to 1, you should not touch
60                                                                                 // this web_client
61
62         uint8_t dead:1;                                         // if set to 1, this client is dead
63
64         uint8_t keepalive:1;                            // if set to 1, the web client will be re-used
65         uint8_t enable_gzip:1;                          // if set to 1, the response will be compressed
66
67         uint8_t mode:3;                                         // the operational mode of the client
68
69         uint8_t wait_receive:1;                         // 1 = we are waiting more input data
70         uint8_t wait_send:1;                            // 1 = we have data to send to the client
71
72 #ifdef TCP_CORK
73         int tcp_cork;                                           // 1 = we have a cork on the socket
74 #endif /* TCP_CORK */
75
76         int ifd;
77         int ofd;
78
79         char client_ip[NI_MAXHOST+1];
80         char client_port[NI_MAXSERV+1];
81
82         char decoded_url[URL_MAX + 1];  // we decode the URL in this buffer
83         char last_url[URL_MAX+1];               // we keep a copy of the decoded URL here
84
85         struct timeval tv_in, tv_ready;
86
87         char cookie1[COOKIE_MAX+1];
88         char cookie2[COOKIE_MAX+1];
89         char origin[ORIGIN_MAX+1];
90
91         struct sockaddr_storage clientaddr;
92         struct response response;
93
94         size_t stats_received_bytes;
95         size_t stats_sent_bytes;
96
97         pthread_t thread;                               // the thread servicing this client
98
99         struct web_client *prev;
100         struct web_client *next;
101 };
102
103 #define WEB_CLIENT_IS_DEAD(w) (w)->dead=1
104
105 extern struct web_client *web_clients;
106
107 extern uid_t web_files_uid(void);
108 extern uid_t web_files_gid(void);
109
110 extern struct web_client *web_client_create(int listener);
111 extern struct web_client *web_client_free(struct web_client *w);
112 extern ssize_t web_client_send(struct web_client *w);
113 extern ssize_t web_client_receive(struct web_client *w);
114 extern void web_client_process(struct web_client *w);
115 extern void web_client_reset(struct web_client *w);
116
117 extern void *web_client_main(void *ptr);
118
119 #endif