]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/webserver.hh
Update pip etc. for the auth docs as well
[thirdparty/pdns.git] / pdns / webserver.hh
CommitLineData
12c86877 1/*
12471842
PL
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 */
12c86877
BH
22#ifndef WEBSERVER_HH
23#define WEBSERVER_HH
24#include <map>
25#include <string>
232f0877 26#include <list>
3ae143b0
CH
27#include <boost/utility.hpp>
28#include <yahttp/yahttp.hpp>
5938c49f 29#include "json11.hpp"
10f4eea8 30#include "namespaces.hh"
825fa717 31#include "sstuff.hh"
12c86877 32
bbef8f04
CH
33class WebServer;
34
80d59cd1
CH
35class HttpRequest : public YaHTTP::Request {
36public:
825fa717 37 HttpRequest() : YaHTTP::Request(), accept_json(false), accept_html(false), complete(false) { };
80d59cd1 38
80d59cd1
CH
39 bool accept_json;
40 bool accept_html;
825fa717 41 bool complete;
5938c49f 42 json11::Json json();
bbef8f04
CH
43
44 // checks password _only_.
45 bool compareAuthorization(const string &expected_password);
46 bool compareHeader(const string &header_name, const string &expected_value);
80d59cd1
CH
47};
48
49class HttpResponse: public YaHTTP::Response {
50public:
51 HttpResponse() : YaHTTP::Response() { };
80d59cd1 52 HttpResponse(const YaHTTP::Response &resp) : YaHTTP::Response(resp) { };
669822d0 53
5938c49f 54 void setBody(const json11::Json& document);
692829aa
CH
55 void setErrorResult(const std::string& message, const int status);
56 void setSuccessResult(const std::string& message, const int status = 200);
80d59cd1
CH
57};
58
59
33196945
CH
60class HttpException
61{
62public:
80d59cd1 63 HttpException(int status) : d_response()
33196945 64 {
80d59cd1 65 d_response.status = status;
33196945
CH
66 };
67
80d59cd1
CH
68 HttpResponse response()
69 {
70 return d_response;
33196945
CH
71 }
72
80d59cd1
CH
73protected:
74 HttpResponse d_response;
33196945
CH
75};
76
77class HttpBadRequestException : public HttpException {
78public:
80d59cd1 79 HttpBadRequestException() : HttpException(400) { };
33196945
CH
80};
81
82class HttpUnauthorizedException : public HttpException {
83public:
53255086 84 HttpUnauthorizedException(string const &scheme) : HttpException(401)
80d59cd1 85 {
53255086 86 d_response.headers["WWW-Authenticate"] = scheme + " realm=\"PowerDNS\"";
33196945
CH
87 }
88};
89
53255086
PL
90class HttpForbiddenException : public HttpException {
91public:
92 HttpForbiddenException() : HttpException(403) { };
93};
94
33196945
CH
95class HttpNotFoundException : public HttpException {
96public:
80d59cd1 97 HttpNotFoundException() : HttpException(404) { };
33196945
CH
98};
99
100class HttpMethodNotAllowedException : public HttpException {
101public:
80d59cd1 102 HttpMethodNotAllowedException() : HttpException(405) { };
33196945
CH
103};
104
0f67eeda
CH
105class HttpInternalServerErrorException : public HttpException {
106public:
107 HttpInternalServerErrorException() : HttpException(500) { };
108};
109
3ae143b0
CH
110class ApiException : public runtime_error
111{
112public:
113 ApiException(const string& what) : runtime_error(what) {
114 }
115};
02c04144 116
a7650f23
CH
117class Server
118{
119public:
93f4e5ce 120 Server(const string &localaddress, int port) : d_local(localaddress.empty() ? "0.0.0.0" : localaddress, port), d_server_socket(d_local.sin4.sin_family, SOCK_STREAM, 0) {
a7650f23
CH
121 d_server_socket.setReuseAddr();
122 d_server_socket.bind(d_local);
123 d_server_socket.listen();
124 }
125
126 ComboAddress d_local;
127
128 Socket *accept() {
129 return d_server_socket.accept();
130 }
131
132protected:
133 Socket d_server_socket;
134};
135
3ae143b0 136class WebServer : public boost::noncopyable
12c86877
BH
137{
138public:
bbef8f04 139 WebServer(const string &listenaddress, int port);
825fa717 140 void bind();
12c86877 141 void go();
232f0877 142
825fa717 143 void serveConnection(Socket *client);
34c513f9 144 void handleRequest(HttpRequest& request, HttpResponse& resp);
232f0877 145
80d59cd1 146 typedef boost::function<void(HttpRequest* req, HttpResponse* resp)> HandlerFunction;
3ae143b0 147 void registerApiHandler(const string& url, HandlerFunction handler);
bbef8f04 148 void registerWebHandler(const string& url, HandlerFunction handler);
232f0877 149
3ae143b0 150protected:
bbef8f04 151 void registerBareHandler(const string& url, HandlerFunction handler);
232f0877 152
825fa717
CH
153 virtual Server* createServer() {
154 return new Server(d_listenaddress, d_port);
155 }
156
12c86877
BH
157 string d_listenaddress;
158 int d_port;
232f0877 159 string d_password;
96d299db 160 Server* d_server;
12c86877 161};
3ae143b0 162
12c86877 163#endif /* WEBSERVER_HH */