2 PowerDNS Versatile Database Driven Nameserver
3 Copyright (C) 2002-2012 PowerDNS.COM BV
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
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.
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.
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
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"
35 class HttpRequest : public YaHTTP::Request {
37 HttpRequest() : YaHTTP::Request(), accept_json(false), accept_html(false), complete(false) { };
42 void json(rapidjson::Document& document);
45 class HttpResponse: public YaHTTP::Response {
47 HttpResponse() : YaHTTP::Response() { };
48 HttpResponse(const YaHTTP::Response &resp) : YaHTTP::Response(resp) { };
50 void setBody(rapidjson::Document& document);
57 HttpException(int status) : d_response()
59 d_response.status = status;
62 HttpResponse response()
68 HttpResponse d_response;
71 class HttpBadRequestException : public HttpException {
73 HttpBadRequestException() : HttpException(400) { };
76 class HttpUnauthorizedException : public HttpException {
78 HttpUnauthorizedException() : HttpException(401)
80 d_response.headers["WWW-Authenticate"] = "Basic realm=\"PowerDNS\"";
84 class HttpNotFoundException : public HttpException {
86 HttpNotFoundException() : HttpException(404) { };
89 class HttpMethodNotAllowedException : public HttpException {
91 HttpMethodNotAllowedException() : HttpException(405) { };
94 class HttpInternalServerErrorException : public HttpException {
96 HttpInternalServerErrorException() : HttpException(500) { };
99 class ApiException : public runtime_error
102 ApiException(const string& what) : runtime_error(what) {
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();
115 ComboAddress d_local;
118 return d_server_socket.accept();
122 Socket d_server_socket;
125 class WebServer : public boost::noncopyable
128 WebServer(const string &listenaddress, int port, const string &password="");
132 void serveConnection(Socket *client);
133 HttpResponse handleRequest(HttpRequest request);
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);
140 static char B64Decode1(char cInChar);
141 static int B64Decode(const std::string& strInput, std::string& strOutput);
143 virtual Server* createServer() {
144 return new Server(d_listenaddress, d_port);
147 string d_listenaddress;
153 #endif /* WEBSERVER_HH */