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