2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
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.
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.
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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <boost/utility.hpp>
28 #include <yahttp/yahttp.hpp>
30 #include "namespaces.hh"
35 class HttpRequest : public YaHTTP::Request {
37 HttpRequest() : YaHTTP::Request(), accept_json(false), accept_html(false), complete(false) { };
44 // checks password _only_.
45 bool compareAuthorization(const string &expected_password);
46 bool compareHeader(const string &header_name, const string &expected_value);
49 class HttpResponse: public YaHTTP::Response {
51 HttpResponse() : YaHTTP::Response() { };
52 HttpResponse(const YaHTTP::Response &resp) : YaHTTP::Response(resp) { };
54 void setBody(const json11::Json& document);
55 void setErrorResult(const std::string& message, const int status);
56 void setSuccessResult(const std::string& message, const int status = 200);
63 HttpException(int status) : d_response()
65 d_response.status = status;
68 HttpResponse response()
74 HttpResponse d_response;
77 class HttpBadRequestException : public HttpException {
79 HttpBadRequestException() : HttpException(400) { };
82 class HttpUnauthorizedException : public HttpException {
84 HttpUnauthorizedException(string const &scheme) : HttpException(401)
86 d_response.headers["WWW-Authenticate"] = scheme + " realm=\"PowerDNS\"";
90 class HttpForbiddenException : public HttpException {
92 HttpForbiddenException() : HttpException(403) { };
95 class HttpNotFoundException : public HttpException {
97 HttpNotFoundException() : HttpException(404) { };
100 class HttpMethodNotAllowedException : public HttpException {
102 HttpMethodNotAllowedException() : HttpException(405) { };
105 class HttpInternalServerErrorException : public HttpException {
107 HttpInternalServerErrorException() : HttpException(500) { };
110 class ApiException : public runtime_error
113 ApiException(const string& what) : runtime_error(what) {
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) {
121 d_server_socket.setReuseAddr();
122 d_server_socket.bind(d_local);
123 d_server_socket.listen();
125 virtual ~Server() { };
127 ComboAddress d_local;
129 std::shared_ptr<Socket> accept() {
130 return std::shared_ptr<Socket>(d_server_socket.accept());
134 Socket d_server_socket;
137 class WebServer : public boost::noncopyable
140 WebServer(const string &listenaddress, int port);
141 virtual ~WebServer() { };
145 void serveConnection(std::shared_ptr<Socket> client) const;
146 void handleRequest(HttpRequest& request, HttpResponse& resp) const;
148 typedef boost::function<void(HttpRequest* req, HttpResponse* resp)> HandlerFunction;
149 void registerApiHandler(const string& url, HandlerFunction handler);
150 void registerWebHandler(const string& url, HandlerFunction handler);
153 void registerBareHandler(const string& url, HandlerFunction handler);
155 virtual std::shared_ptr<Server> createServer() {
156 return std::make_shared<Server>(d_listenaddress, d_port);
159 string d_listenaddress;
162 std::shared_ptr<Server> d_server;
165 #endif /* WEBSERVER_HH */