From: Pieter Lexis Date: Fri, 18 Jan 2019 16:08:19 +0000 (+0100) Subject: webserver: add loglevels and prefixes X-Git-Tag: dnsdist-1.4.0-alpha1~39^2~20 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a53cd863062be4941778883dfd3b4ea70ffda785;p=thirdparty%2Fpdns.git webserver: add loglevels and prefixes - Prefix all loglines with '[webserver]' and a (fake) uuid - Have 3 levels of webserver logging (not configurable atm) --- diff --git a/pdns/webserver.cc b/pdns/webserver.cc index 88ec949a8c..f9f96ce17a 100644 --- a/pdns/webserver.cc +++ b/pdns/webserver.cc @@ -39,12 +39,12 @@ json11::Json HttpRequest::json() { string err; if(this->body.empty()) { - g_log<body, err); if (doc.is_null()) { - g_log<headers["access-control-allow-origin"] = "*"; if (apikey.empty()) { - g_log<url.path << "\": Authentication failed, API Key missing in config" << endl; + g_log<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; if (!auth_ok) { - g_log<url.path << "\": Authentication by API Key failed" << endl; + g_log<logprefix<<"HTTP Request \"" << req->url.path << "\": Authentication by API Key failed" << endl; throw HttpUnauthorizedException("X-API-Key"); } @@ -178,7 +178,7 @@ static void webWrapper(WebServer::HandlerFunction handler, HttpRequest* req, Htt if (!password.empty()) { bool auth_ok = req->compareAuthorization(password); if (!auth_ok) { - g_log<url.path << "\": Web Authentication failed" << endl; + g_log<logprefix<<"HTTP Request \"" << req->url.path << "\": Web Authentication failed" << endl; throw HttpUnauthorizedException("Basic"); } } @@ -204,11 +204,11 @@ void WebServer::handleRequest(HttpRequest& req, HttpResponse& resp) const try { if (!req.complete) { - g_log< client) const -try { - HttpRequest req; - YaHTTP::AsyncRequestLoader yarl; - yarl.initialize(&req); - int timeout = 5; - client->setNonBlocking(); +void WebServer::serveConnection(std::shared_ptr client) const { + const string logprefix = d_logprefix + "<" + "I should be a UUID" + "> "; try { - while(!req.complete) { - int bytes; - char buf[1024]; - bytes = client->readWithTimeout(buf, sizeof(buf), timeout); - if (bytes > 0) { - string data = string(buf, bytes); - req.complete = yarl.feed(data); + HttpRequest req(logprefix); + YaHTTP::AsyncRequestLoader yarl; + yarl.initialize(&req); + int timeout = 5; + client->setNonBlocking(); + + try { + while(!req.complete) { + int bytes; + char buf[1024]; + bytes = client->readWithTimeout(buf, sizeof(buf), timeout); + if (bytes > 0) { + string data = string(buf, bytes); + req.complete = yarl.feed(data); + } else { + // read error OR EOF + break; + } + } + yarl.finalize(); + } catch (YaHTTP::ParseError &e) { + // request stays incomplete + } + + ComboAddress remote; + + if (d_loglevel >= WebServer::LogLevel::None) { + client->getRemote(remote); + } + + if (d_loglevel >= WebServer::LogLevel::Detailed) { + g_log<getRemote(remote); - g_log<= WebServer::LogLevel::Detailed) { + g_log<= WebServer::LogLevel::Common) { + g_log<writenWithTimeout(reply.c_str(), reply.size(), timeout); } - first = true; - for (const auto& h : req.headers) { - if (first) { - first = false; - g_log<writenWithTimeout(reply.c_str(), reply.size(), timeout); -} -catch(PDNSException &e) { - g_log<d_local.toStringWithPort()<d_local.toStringWithPort()<getRemote(remote)) - g_log<