]>
Commit | Line | Data |
---|---|---|
12c86877 BH |
1 | /* |
2 | PowerDNS Versatile Database Driven Nameserver | |
a2ce158c | 3 | Copyright (C) 2002-2012 PowerDNS.COM BV |
12c86877 BH |
4 | |
5 | This program is free software; you can redistribute it and/or modify | |
22dc646a BH |
6 | it under the terms of the GNU General Public License version 2 |
7 | as published by the Free Software Foundation | |
f782fe38 MH |
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. | |
12c86877 BH |
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 | |
06bd9ccf | 20 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
12c86877 BH |
21 | */ |
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> | |
6ec5e728 CH |
29 | #include "rapidjson/document.h" |
30 | #include "rapidjson/stringbuffer.h" | |
31 | #include "rapidjson/writer.h" | |
10f4eea8 | 32 | #include "namespaces.hh" |
6ec5e728 | 33 | |
96d299db | 34 | class Server; |
232f0877 | 35 | class Session; |
12c86877 | 36 | |
80d59cd1 CH |
37 | class HttpRequest : public YaHTTP::Request { |
38 | public: | |
39 | HttpRequest() : YaHTTP::Request(), accept_json(false), accept_html(false) { }; | |
40 | ||
41 | map<string,string> path_parameters; | |
42 | bool accept_json; | |
43 | bool accept_html; | |
6ec5e728 | 44 | void json(rapidjson::Document& document); |
80d59cd1 CH |
45 | }; |
46 | ||
47 | class HttpResponse: public YaHTTP::Response { | |
48 | public: | |
49 | HttpResponse() : YaHTTP::Response() { }; | |
50 | HttpResponse(const YaHTTP::Request &req) : YaHTTP::Response(req) { }; | |
51 | HttpResponse(const YaHTTP::Response &resp) : YaHTTP::Response(resp) { }; | |
52 | }; | |
53 | ||
54 | ||
33196945 CH |
55 | class HttpException |
56 | { | |
57 | public: | |
80d59cd1 | 58 | HttpException(int status) : d_response() |
33196945 | 59 | { |
80d59cd1 | 60 | d_response.status = status; |
33196945 CH |
61 | }; |
62 | ||
80d59cd1 CH |
63 | HttpResponse response() |
64 | { | |
65 | return d_response; | |
33196945 CH |
66 | } |
67 | ||
80d59cd1 CH |
68 | protected: |
69 | HttpResponse d_response; | |
33196945 CH |
70 | }; |
71 | ||
72 | class HttpBadRequestException : public HttpException { | |
73 | public: | |
80d59cd1 | 74 | HttpBadRequestException() : HttpException(400) { }; |
33196945 CH |
75 | }; |
76 | ||
77 | class HttpUnauthorizedException : public HttpException { | |
78 | public: | |
80d59cd1 CH |
79 | HttpUnauthorizedException() : HttpException(401) |
80 | { | |
81 | d_response.headers["WWW-Authenticate"] = "Basic realm=\"PowerDNS\""; | |
33196945 CH |
82 | } |
83 | }; | |
84 | ||
85 | class HttpNotFoundException : public HttpException { | |
86 | public: | |
80d59cd1 | 87 | HttpNotFoundException() : HttpException(404) { }; |
33196945 CH |
88 | }; |
89 | ||
90 | class HttpMethodNotAllowedException : public HttpException { | |
91 | public: | |
80d59cd1 | 92 | HttpMethodNotAllowedException() : HttpException(405) { }; |
33196945 CH |
93 | }; |
94 | ||
3ae143b0 CH |
95 | class ApiException : public runtime_error |
96 | { | |
97 | public: | |
98 | ApiException(const string& what) : runtime_error(what) { | |
99 | } | |
100 | }; | |
02c04144 | 101 | |
3ae143b0 | 102 | class WebServer : public boost::noncopyable |
12c86877 BH |
103 | { |
104 | public: | |
105 | WebServer(const string &listenaddress, int port, const string &password=""); | |
106 | void go(); | |
232f0877 | 107 | |
3ae143b0 | 108 | void serveConnection(Session client); |
80d59cd1 | 109 | HttpResponse handleRequest(HttpRequest request); |
232f0877 | 110 | |
80d59cd1 | 111 | typedef boost::function<void(HttpRequest* req, HttpResponse* resp)> HandlerFunction; |
232f0877 CH |
112 | struct HandlerRegistration { |
113 | std::list<string> urlParts; | |
114 | std::list<string> paramNames; | |
f6154a3b | 115 | HandlerFunction handler; |
232f0877 CH |
116 | }; |
117 | ||
f6154a3b | 118 | void registerHandler(const string& url, HandlerFunction handler); |
3ae143b0 | 119 | void registerApiHandler(const string& url, HandlerFunction handler); |
232f0877 | 120 | |
3ae143b0 | 121 | protected: |
12c86877 BH |
122 | static char B64Decode1(char cInChar); |
123 | static int B64Decode(const std::string& strInput, std::string& strOutput); | |
232f0877 CH |
124 | bool route(const std::string& url, std::map<std::string, std::string>& urlArgs, HandlerFunction** handler); |
125 | ||
12c86877 BH |
126 | string d_listenaddress; |
127 | int d_port; | |
232f0877 | 128 | std::list<HandlerRegistration> d_handlers; |
232f0877 | 129 | string d_password; |
96d299db | 130 | Server* d_server; |
12c86877 | 131 | }; |
3ae143b0 CH |
132 | |
133 | class FDMultiplexer; | |
134 | ||
135 | class AsyncWebServer : public WebServer | |
136 | { | |
137 | public: | |
138 | AsyncWebServer(FDMultiplexer* fdm, const string &listenaddress, int port, const string &password="") : | |
139 | WebServer(listenaddress, port, password), d_fdm(fdm) { }; | |
140 | void go(); | |
141 | ||
142 | private: | |
143 | FDMultiplexer* d_fdm; | |
144 | ||
145 | void newConnection(Session session); | |
146 | void serveConnection(Session session); | |
147 | }; | |
148 | ||
12c86877 | 149 | #endif /* WEBSERVER_HH */ |