]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/ws-auth.hh
Merge pull request #9073 from pieterlexis/runtime-dirs-virtual-hosting
[thirdparty/pdns.git] / pdns / ws-auth.hh
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22 #pragma once
23 #include <string>
24 #include <map>
25 #include <time.h>
26 #include <pthread.h>
27 #include "misc.hh"
28 #include "namespaces.hh"
29
30 class Ewma
31 {
32 public:
33 Ewma() : d_last(0), d_10(0), d_5(0), d_1(0), d_max(0){dt.set();}
34 void submit(int val)
35 {
36 int rate=val-d_last;
37 double difft=dt.udiff()/1000000.0;
38 dt.set();
39
40 d_10=((600.0-difft)*d_10+(difft*rate))/600.0;
41 d_5=((300.0-difft)*d_5+(difft*rate))/300.0;
42 d_1=((60.0-difft)*d_1+(difft*rate))/60.0;
43 d_max=max(d_1,d_max);
44
45 d_last=val;
46 }
47 double get10()
48 {
49 return d_10;
50 }
51 double get5()
52 {
53 return d_5;
54 }
55 double get1()
56 {
57 return d_1;
58 }
59 double getMax()
60 {
61 return d_max;
62 }
63 private:
64 DTime dt;
65 int d_last;
66 double d_10, d_5, d_1, d_max;
67 };
68
69 class WebServer;
70 class HttpRequest;
71 class HttpResponse;
72
73 class AuthWebServer
74 {
75 public:
76 AuthWebServer();
77 void go();
78 static string makePercentage(const double& val);
79
80 private:
81 void indexfunction(HttpRequest* req, HttpResponse* resp);
82 void cssfunction(HttpRequest* req, HttpResponse* resp);
83 void jsonstat(HttpRequest* req, HttpResponse* resp);
84 void registerApiHandler(const string& url, boost::function<void(HttpRequest*, HttpResponse*)> handler);
85 void printvars(ostringstream &ret);
86 void printargs(ostringstream &ret);
87 void webThread();
88 void statThread();
89
90 time_t d_start;
91 double d_min10, d_min5, d_min1;
92 Ewma d_queries, d_cachehits, d_cachemisses;
93 Ewma d_qcachehits, d_qcachemisses;
94 WebServer *d_ws{nullptr};
95 };