]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/webserver.hh
Webserver: convert to new yahttp api and router
[thirdparty/pdns.git] / pdns / webserver.hh
1 /*
2 PowerDNS Versatile Database Driven Nameserver
3 Copyright (C) 2002-2012 PowerDNS.COM BV
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2
7 as published by the Free Software Foundation
8
9 Additionally, the license of this program contains a special
10 exception which allows to distribute the program in binary form when
11 it is linked against OpenSSL.
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 St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22 #ifndef WEBSERVER_HH
23 #define WEBSERVER_HH
24 #include <map>
25 #include <string>
26 #include <list>
27 #include <boost/utility.hpp>
28 #include <yahttp/yahttp.hpp>
29 #include "rapidjson/document.h"
30 #include "rapidjson/stringbuffer.h"
31 #include "rapidjson/writer.h"
32 #include "namespaces.hh"
33 #include "sstuff.hh"
34
35 class HttpRequest : public YaHTTP::Request {
36 public:
37 HttpRequest() : YaHTTP::Request(), accept_json(false), accept_html(false), complete(false) { };
38
39 bool accept_json;
40 bool accept_html;
41 bool complete;
42 void json(rapidjson::Document& document);
43 };
44
45 class HttpResponse: public YaHTTP::Response {
46 public:
47 HttpResponse() : YaHTTP::Response() { };
48 HttpResponse(const YaHTTP::Response &resp) : YaHTTP::Response(resp) { };
49
50 void setBody(rapidjson::Document& document);
51 };
52
53
54 class HttpException
55 {
56 public:
57 HttpException(int status) : d_response()
58 {
59 d_response.status = status;
60 };
61
62 HttpResponse response()
63 {
64 return d_response;
65 }
66
67 protected:
68 HttpResponse d_response;
69 };
70
71 class HttpBadRequestException : public HttpException {
72 public:
73 HttpBadRequestException() : HttpException(400) { };
74 };
75
76 class HttpUnauthorizedException : public HttpException {
77 public:
78 HttpUnauthorizedException() : HttpException(401)
79 {
80 d_response.headers["WWW-Authenticate"] = "Basic realm=\"PowerDNS\"";
81 }
82 };
83
84 class HttpNotFoundException : public HttpException {
85 public:
86 HttpNotFoundException() : HttpException(404) { };
87 };
88
89 class HttpMethodNotAllowedException : public HttpException {
90 public:
91 HttpMethodNotAllowedException() : HttpException(405) { };
92 };
93
94 class HttpInternalServerErrorException : public HttpException {
95 public:
96 HttpInternalServerErrorException() : HttpException(500) { };
97 };
98
99 class ApiException : public runtime_error
100 {
101 public:
102 ApiException(const string& what) : runtime_error(what) {
103 }
104 };
105
106 class Server
107 {
108 public:
109 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) {
110 d_server_socket.setReuseAddr();
111 d_server_socket.bind(d_local);
112 d_server_socket.listen();
113 }
114
115 ComboAddress d_local;
116
117 Socket *accept() {
118 return d_server_socket.accept();
119 }
120
121 protected:
122 Socket d_server_socket;
123 };
124
125 class WebServer : public boost::noncopyable
126 {
127 public:
128 WebServer(const string &listenaddress, int port, const string &password="");
129 void bind();
130 void go();
131
132 void serveConnection(Socket *client);
133 HttpResponse handleRequest(HttpRequest request);
134
135 typedef boost::function<void(HttpRequest* req, HttpResponse* resp)> HandlerFunction;
136 void registerHandler(const string& url, HandlerFunction handler);
137 void registerApiHandler(const string& url, HandlerFunction handler);
138
139 protected:
140 static char B64Decode1(char cInChar);
141 static int B64Decode(const std::string& strInput, std::string& strOutput);
142
143 virtual Server* createServer() {
144 return new Server(d_listenaddress, d_port);
145 }
146
147 string d_listenaddress;
148 int d_port;
149 string d_password;
150 Server* d_server;
151 };
152
153 #endif /* WEBSERVER_HH */