return false;
}
-static void apiWrapper(WebServer::HandlerFunction handler, HttpRequest* req, HttpResponse* resp, const string &apikey) {
+void WebServer::apiWrapper(WebServer::HandlerFunction handler, HttpRequest* req, HttpResponse* resp) {
if (optionsHandler(req, resp)) return;
resp->headers["access-control-allow-origin"] = "*";
- if (apikey.empty()) {
+ if (d_apikey.empty()) {
g_log<<Logger::Error<<req->logprefix<<"HTTP API Request \"" << req->url.path << "\": Authentication failed, API Key missing in config" << endl;
throw HttpUnauthorizedException("X-API-Key");
}
- bool auth_ok = req->compareHeader("x-api-key", apikey) || req->getvars["api-key"] == apikey;
+
+ bool auth_ok = req->compareHeader("x-api-key", d_apikey) || req->getvars["api-key"] == d_apikey;
if (!auth_ok) {
g_log<<Logger::Error<<req->logprefix<<"HTTP Request \"" << req->url.path << "\": Authentication by API Key failed" << endl;
}
void WebServer::registerApiHandler(const string& url, HandlerFunction handler) {
- HandlerFunction f = boost::bind(&apiWrapper, handler, _1, _2, d_apikey);
+ HandlerFunction f = boost::bind(&WebServer::apiWrapper, this, handler, _1, _2);
registerBareHandler(url, f);
- d_registerApiHandlerCalled = true;
}
-static void webWrapper(WebServer::HandlerFunction handler, HttpRequest* req, HttpResponse* resp, const string &password) {
- if (!password.empty()) {
- bool auth_ok = req->compareAuthorization(password);
+void WebServer::webWrapper(WebServer::HandlerFunction handler, HttpRequest* req, HttpResponse* resp) {
+ if (!d_webserverPassword.empty()) {
+ bool auth_ok = req->compareAuthorization(d_webserverPassword);
if (!auth_ok) {
g_log<<Logger::Debug<<req->logprefix<<"HTTP Request \"" << req->url.path << "\": Web Authentication failed" << endl;
throw HttpUnauthorizedException("Basic");
}
void WebServer::registerWebHandler(const string& url, HandlerFunction handler) {
- HandlerFunction f = boost::bind(&webWrapper, handler, _1, _2, d_webserverPassword);
+ HandlerFunction f = boost::bind(&WebServer::webWrapper, this, handler, _1, _2);
registerBareHandler(url, f);
}
virtual ~WebServer() { };
void setApiKey(const string &apikey) {
- if (d_registerApiHandlerCalled) {
- throw PDNSException("registerApiHandler has been called, can not change apikey");
- }
d_apikey = apikey;
}
void setPassword(const string &password) {
- if (d_registerWebHandlerCalled) {
- throw PDNSException("registerWebHandler has been called, can not change password");
- }
d_webserverPassword = password;
}
std::shared_ptr<Server> d_server;
std::string d_apikey;
- bool d_registerApiHandlerCalled{false};
-
+ void apiWrapper(WebServer::HandlerFunction handler, HttpRequest* req, HttpResponse* resp);
std::string d_webserverPassword;
- bool d_registerWebHandlerCalled{false};
+ void webWrapper(WebServer::HandlerFunction handler, HttpRequest* req, HttpResponse* resp);
NetmaskGroup d_acl;