]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/ws-recursor.cc
auth: switch circleci mssql image
[thirdparty/pdns.git] / pdns / ws-recursor.cc
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
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.
8 *
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.
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
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include "ws-recursor.hh"
26 #include "json.hh"
27
28 #include <string>
29 #include "namespaces.hh"
30 #include <iostream>
31 #include "iputils.hh"
32 #include "rec_channel.hh"
33 #include "arguments.hh"
34 #include "misc.hh"
35 #include "syncres.hh"
36 #include "dnsparser.hh"
37 #include "json11.hpp"
38 #include "webserver.hh"
39 #include "ws-api.hh"
40 #include "logger.hh"
41 #include "ext/incbin/incbin.h"
42 #include "rec-lua-conf.hh"
43 #include "rpzloader.hh"
44 #include "uuid-utils.hh"
45
46 extern thread_local FDMultiplexer* t_fdm;
47
48 using json11::Json;
49
50 void productServerStatisticsFetch(map<string,string>& out)
51 {
52 map<string,string> stats = getAllStatsMap(StatComponent::API);
53 out.swap(stats);
54 }
55
56 boost::optional<uint64_t> productServerStatisticsFetch(const std::string& name)
57 {
58 return getStatByName(name);
59 }
60
61 static void apiWriteConfigFile(const string& filebasename, const string& content)
62 {
63 if (::arg()["api-config-dir"].empty()) {
64 throw ApiException("Config Option \"api-config-dir\" must be set");
65 }
66
67 string filename = ::arg()["api-config-dir"] + "/" + filebasename + ".conf";
68 ofstream ofconf(filename.c_str());
69 if (!ofconf) {
70 throw ApiException("Could not open config fragment file '"+filename+"' for writing: "+stringerror());
71 }
72 ofconf << "# Generated by pdns-recursor REST API, DO NOT EDIT" << endl;
73 ofconf << content << endl;
74 ofconf.close();
75 }
76
77 static void apiServerConfigAllowFrom(HttpRequest* req, HttpResponse* resp)
78 {
79 if (req->method == "PUT") {
80 Json document = req->json();
81
82 auto jlist = document["value"];
83 if (!jlist.is_array()) {
84 throw ApiException("'value' must be an array");
85 }
86
87 NetmaskGroup nmg;
88 for (auto value : jlist.array_items()) {
89 try {
90 nmg.addMask(value.string_value());
91 } catch (const NetmaskException &e) {
92 throw ApiException(e.reason);
93 }
94 }
95
96 ostringstream ss;
97
98 // Clear allow-from-file if set, so our changes take effect
99 ss << "allow-from-file=" << endl;
100
101 // Clear allow-from, and provide a "parent" value
102 ss << "allow-from=" << endl;
103 ss << "allow-from+=" << nmg.toString() << endl;
104
105 apiWriteConfigFile("allow-from", ss.str());
106
107 parseACLs();
108
109 // fall through to GET
110 } else if (req->method != "GET") {
111 throw HttpMethodNotAllowedException();
112 }
113
114 // Return currently configured ACLs
115 vector<string> entries;
116 t_allowFrom->toStringVector(&entries);
117
118 resp->setBody(Json::object {
119 { "name", "allow-from" },
120 { "value", entries },
121 });
122 }
123
124 static void fillZone(const DNSName& zonename, HttpResponse* resp)
125 {
126 auto iter = SyncRes::t_sstorage.domainmap->find(zonename);
127 if (iter == SyncRes::t_sstorage.domainmap->end())
128 throw ApiException("Could not find domain '"+zonename.toLogString()+"'");
129
130 const SyncRes::AuthDomain& zone = iter->second;
131
132 Json::array servers;
133 for(const ComboAddress& server : zone.d_servers) {
134 servers.push_back(server.toStringWithPort());
135 }
136
137 Json::array records;
138 for(const SyncRes::AuthDomain::records_t::value_type& dr : zone.d_records) {
139 records.push_back(Json::object {
140 { "name", dr.d_name.toString() },
141 { "type", DNSRecordContent::NumberToType(dr.d_type) },
142 { "ttl", (double)dr.d_ttl },
143 { "content", dr.d_content->getZoneRepresentation() }
144 });
145 }
146
147 // id is the canonical lookup key, which doesn't actually match the name (in some cases)
148 string zoneId = apiZoneNameToId(iter->first);
149 Json::object doc = {
150 { "id", zoneId },
151 { "url", "/api/v1/servers/localhost/zones/" + zoneId },
152 { "name", iter->first.toString() },
153 { "kind", zone.d_servers.empty() ? "Native" : "Forwarded" },
154 { "servers", servers },
155 { "recursion_desired", zone.d_servers.empty() ? false : zone.d_rdForward },
156 { "records", records }
157 };
158
159 resp->setBody(doc);
160 }
161
162 static void doCreateZone(const Json document)
163 {
164 if (::arg()["api-config-dir"].empty()) {
165 throw ApiException("Config Option \"api-config-dir\" must be set");
166 }
167
168 DNSName zonename = apiNameToDNSName(stringFromJson(document, "name"));
169 apiCheckNameAllowedCharacters(zonename.toString());
170
171 string singleIPTarget = document["single_target_ip"].string_value();
172 string kind = toUpper(stringFromJson(document, "kind"));
173 bool rd = boolFromJson(document, "recursion_desired");
174 string confbasename = "zone-" + apiZoneNameToId(zonename);
175
176 if (kind == "NATIVE") {
177 if (rd)
178 throw ApiException("kind=Native and recursion_desired are mutually exclusive");
179 if(!singleIPTarget.empty()) {
180 try {
181 ComboAddress rem(singleIPTarget);
182 if(rem.sin4.sin_family != AF_INET)
183 throw ApiException("");
184 singleIPTarget = rem.toString();
185 }
186 catch(...) {
187 throw ApiException("Single IP target '"+singleIPTarget+"' is invalid");
188 }
189 }
190 string zonefilename = ::arg()["api-config-dir"] + "/" + confbasename + ".zone";
191 ofstream ofzone(zonefilename.c_str());
192 if (!ofzone) {
193 throw ApiException("Could not open '"+zonefilename+"' for writing: "+stringerror());
194 }
195 ofzone << "; Generated by pdns-recursor REST API, DO NOT EDIT" << endl;
196 ofzone << zonename << "\tIN\tSOA\tlocal.zone.\thostmaster."<<zonename<<" 1 1 1 1 1" << endl;
197 if(!singleIPTarget.empty()) {
198 ofzone <<zonename << "\t3600\tIN\tA\t"<<singleIPTarget<<endl;
199 ofzone <<"*."<<zonename << "\t3600\tIN\tA\t"<<singleIPTarget<<endl;
200 }
201 ofzone.close();
202
203 apiWriteConfigFile(confbasename, "auth-zones+=" + zonename.toString() + "=" + zonefilename);
204 } else if (kind == "FORWARDED") {
205 string serverlist;
206 for (auto value : document["servers"].array_items()) {
207 string server = value.string_value();
208 if (server == "") {
209 throw ApiException("Forwarded-to server must not be an empty string");
210 }
211 try {
212 ComboAddress ca = parseIPAndPort(server, 53);
213 if (!serverlist.empty()) {
214 serverlist += ";";
215 }
216 serverlist += ca.toStringWithPort();
217 } catch (const PDNSException &e) {
218 throw ApiException(e.reason);
219 }
220 }
221 if (serverlist == "")
222 throw ApiException("Need at least one upstream server when forwarding");
223
224 if (rd) {
225 apiWriteConfigFile(confbasename, "forward-zones-recurse+=" + zonename.toString() + "=" + serverlist);
226 } else {
227 apiWriteConfigFile(confbasename, "forward-zones+=" + zonename.toString() + "=" + serverlist);
228 }
229 } else {
230 throw ApiException("invalid kind");
231 }
232 }
233
234 static bool doDeleteZone(const DNSName& zonename)
235 {
236 if (::arg()["api-config-dir"].empty()) {
237 throw ApiException("Config Option \"api-config-dir\" must be set");
238 }
239
240 string filename;
241
242 // this one must exist
243 filename = ::arg()["api-config-dir"] + "/zone-" + apiZoneNameToId(zonename) + ".conf";
244 if (unlink(filename.c_str()) != 0) {
245 return false;
246 }
247
248 // .zone file is optional
249 filename = ::arg()["api-config-dir"] + "/zone-" + apiZoneNameToId(zonename) + ".zone";
250 unlink(filename.c_str());
251
252 return true;
253 }
254
255 static void apiServerZones(HttpRequest* req, HttpResponse* resp)
256 {
257 if (req->method == "POST") {
258 if (::arg()["api-config-dir"].empty()) {
259 throw ApiException("Config Option \"api-config-dir\" must be set");
260 }
261
262 Json document = req->json();
263
264 DNSName zonename = apiNameToDNSName(stringFromJson(document, "name"));
265
266 auto iter = SyncRes::t_sstorage.domainmap->find(zonename);
267 if (iter != SyncRes::t_sstorage.domainmap->end())
268 throw ApiException("Zone already exists");
269
270 doCreateZone(document);
271 reloadAuthAndForwards();
272 fillZone(zonename, resp);
273 resp->status = 201;
274 return;
275 }
276
277 if(req->method != "GET")
278 throw HttpMethodNotAllowedException();
279
280 Json::array doc;
281 for(const SyncRes::domainmap_t::value_type& val : *SyncRes::t_sstorage.domainmap) {
282 const SyncRes::AuthDomain& zone = val.second;
283 Json::array servers;
284 for(const ComboAddress& server : zone.d_servers) {
285 servers.push_back(server.toStringWithPort());
286 }
287 // id is the canonical lookup key, which doesn't actually match the name (in some cases)
288 string zoneId = apiZoneNameToId(val.first);
289 doc.push_back(Json::object {
290 { "id", zoneId },
291 { "url", "/api/v1/servers/localhost/zones/" + zoneId },
292 { "name", val.first.toString() },
293 { "kind", zone.d_servers.empty() ? "Native" : "Forwarded" },
294 { "servers", servers },
295 { "recursion_desired", zone.d_servers.empty() ? false : zone.d_rdForward }
296 });
297 }
298 resp->setBody(doc);
299 }
300
301 static void apiServerZoneDetail(HttpRequest* req, HttpResponse* resp)
302 {
303 DNSName zonename = apiZoneIdToName(req->parameters["id"]);
304
305 SyncRes::domainmap_t::const_iterator iter = SyncRes::t_sstorage.domainmap->find(zonename);
306 if (iter == SyncRes::t_sstorage.domainmap->end())
307 throw ApiException("Could not find domain '"+zonename.toLogString()+"'");
308
309 if(req->method == "PUT") {
310 Json document = req->json();
311
312 doDeleteZone(zonename);
313 doCreateZone(document);
314 reloadAuthAndForwards();
315 resp->body = "";
316 resp->status = 204; // No Content, but indicate success
317 }
318 else if(req->method == "DELETE") {
319 if (!doDeleteZone(zonename)) {
320 throw ApiException("Deleting domain failed");
321 }
322
323 reloadAuthAndForwards();
324 // empty body on success
325 resp->body = "";
326 resp->status = 204; // No Content: declare that the zone is gone now
327 } else if(req->method == "GET") {
328 fillZone(zonename, resp);
329 } else {
330 throw HttpMethodNotAllowedException();
331 }
332 }
333
334 static void apiServerSearchData(HttpRequest* req, HttpResponse* resp) {
335 if(req->method != "GET")
336 throw HttpMethodNotAllowedException();
337
338 string q = req->getvars["q"];
339 if (q.empty())
340 throw ApiException("Query q can't be blank");
341
342 Json::array doc;
343 for(const SyncRes::domainmap_t::value_type& val : *SyncRes::t_sstorage.domainmap) {
344 string zoneId = apiZoneNameToId(val.first);
345 string zoneName = val.first.toString();
346 if (pdns_ci_find(zoneName, q) != string::npos) {
347 doc.push_back(Json::object {
348 { "type", "zone" },
349 { "zone_id", zoneId },
350 { "name", zoneName }
351 });
352 }
353
354 // if zone name is an exact match, don't bother with returning all records/comments in it
355 if (val.first == DNSName(q)) {
356 continue;
357 }
358
359 const SyncRes::AuthDomain& zone = val.second;
360
361 for(const SyncRes::AuthDomain::records_t::value_type& rr : zone.d_records) {
362 if (pdns_ci_find(rr.d_name.toString(), q) == string::npos && pdns_ci_find(rr.d_content->getZoneRepresentation(), q) == string::npos)
363 continue;
364
365 doc.push_back(Json::object {
366 { "type", "record" },
367 { "zone_id", zoneId },
368 { "zone_name", zoneName },
369 { "name", rr.d_name.toString() },
370 { "content", rr.d_content->getZoneRepresentation() }
371 });
372 }
373 }
374 resp->setBody(doc);
375 }
376
377 static void apiServerCacheFlush(HttpRequest* req, HttpResponse* resp) {
378 if(req->method != "PUT")
379 throw HttpMethodNotAllowedException();
380
381 DNSName canon = apiNameToDNSName(req->getvars["domain"]);
382 bool subtree = (req->getvars.count("subtree") > 0 && req->getvars["subtree"].compare("true") == 0);
383
384 int count = broadcastAccFunction<uint64_t>(boost::bind(pleaseWipeCache, canon, subtree));
385 count += broadcastAccFunction<uint64_t>(boost::bind(pleaseWipePacketCache, canon, subtree));
386 count += broadcastAccFunction<uint64_t>(boost::bind(pleaseWipeAndCountNegCache, canon, subtree));
387 resp->setBody(Json::object {
388 { "count", count },
389 { "result", "Flushed cache." }
390 });
391 }
392
393 static void apiServerRPZStats(HttpRequest* req, HttpResponse* resp) {
394 if(req->method != "GET")
395 throw HttpMethodNotAllowedException();
396
397 auto luaconf = g_luaconfs.getLocal();
398 auto numZones = luaconf->dfe.size();
399
400 Json::object ret;
401
402 for (size_t i=0; i < numZones; i++) {
403 auto zone = luaconf->dfe.getZone(i);
404 if (zone == nullptr)
405 continue;
406 auto name = zone->getName();
407 auto stats = getRPZZoneStats(*name);
408 if (stats == nullptr)
409 continue;
410 Json::object zoneInfo = {
411 {"transfers_failed", (double)stats->d_failedTransfers},
412 {"transfers_success", (double)stats->d_successfulTransfers},
413 {"transfers_full", (double)stats->d_fullTransfers},
414 {"records", (double)stats->d_numberOfRecords},
415 {"last_update", (double)stats->d_lastUpdate},
416 {"serial", (double)stats->d_serial},
417 };
418 ret[*name] = zoneInfo;
419 }
420 resp->setBody(ret);
421 }
422
423 #include "htmlfiles.h"
424
425 static void serveStuff(HttpRequest* req, HttpResponse* resp)
426 {
427 resp->headers["Cache-Control"] = "max-age=86400";
428
429 if(req->url.path == "/")
430 req->url.path = "/index.html";
431
432 const string charset = "; charset=utf-8";
433 if(boost::ends_with(req->url.path, ".html"))
434 resp->headers["Content-Type"] = "text/html" + charset;
435 else if(boost::ends_with(req->url.path, ".css"))
436 resp->headers["Content-Type"] = "text/css" + charset;
437 else if(boost::ends_with(req->url.path,".js"))
438 resp->headers["Content-Type"] = "application/javascript" + charset;
439 else if(boost::ends_with(req->url.path, ".png"))
440 resp->headers["Content-Type"] = "image/png";
441
442 resp->headers["X-Content-Type-Options"] = "nosniff";
443 resp->headers["X-Frame-Options"] = "deny";
444 resp->headers["X-Permitted-Cross-Domain-Policies"] = "none";
445
446 resp->headers["X-XSS-Protection"] = "1; mode=block";
447 // resp->headers["Content-Security-Policy"] = "default-src 'self'; style-src 'self' 'unsafe-inline'";
448
449 resp->body = g_urlmap[req->url.path.c_str()+1];
450 resp->status = 200;
451 }
452
453
454 RecursorWebServer::RecursorWebServer(FDMultiplexer* fdm)
455 {
456 registerAllStats();
457
458 d_ws = new AsyncWebServer(fdm, arg()["webserver-address"], arg().asNum("webserver-port"));
459 d_ws->setApiKey(arg()["api-key"]);
460 d_ws->setPassword(arg()["webserver-password"]);
461 d_ws->setLogLevel(arg()["webserver-loglevel"]);
462
463 NetmaskGroup acl;
464 acl.toMasks(::arg()["webserver-allow-from"]);
465 d_ws->setACL(acl);
466
467 d_ws->bind();
468
469 // legacy dispatch
470 d_ws->registerApiHandler("/jsonstat", boost::bind(&RecursorWebServer::jsonstat, this, _1, _2), true);
471 d_ws->registerApiHandler("/api/v1/servers/localhost/cache/flush", &apiServerCacheFlush);
472 d_ws->registerApiHandler("/api/v1/servers/localhost/config/allow-from", &apiServerConfigAllowFrom);
473 d_ws->registerApiHandler("/api/v1/servers/localhost/config", &apiServerConfig);
474 d_ws->registerApiHandler("/api/v1/servers/localhost/rpzstatistics", &apiServerRPZStats);
475 d_ws->registerApiHandler("/api/v1/servers/localhost/search-data", &apiServerSearchData);
476 d_ws->registerApiHandler("/api/v1/servers/localhost/statistics", &apiServerStatistics, true);
477 d_ws->registerApiHandler("/api/v1/servers/localhost/zones/<id>", &apiServerZoneDetail);
478 d_ws->registerApiHandler("/api/v1/servers/localhost/zones", &apiServerZones);
479 d_ws->registerApiHandler("/api/v1/servers/localhost", &apiServerDetail, true);
480 d_ws->registerApiHandler("/api/v1/servers", &apiServer);
481 d_ws->registerApiHandler("/api", &apiDiscovery);
482
483 for(const auto& u : g_urlmap)
484 d_ws->registerWebHandler("/"+u.first, serveStuff);
485 d_ws->registerWebHandler("/", serveStuff);
486 d_ws->go();
487 }
488
489 void RecursorWebServer::jsonstat(HttpRequest* req, HttpResponse *resp)
490 {
491 string command;
492
493 if(req->getvars.count("command")) {
494 command = req->getvars["command"];
495 req->getvars.erase("command");
496 }
497
498 map<string, string> stats;
499 if(command == "get-query-ring") {
500 typedef pair<DNSName,uint16_t> query_t;
501 vector<query_t> queries;
502 bool filter=!req->getvars["public-filtered"].empty();
503
504 if(req->getvars["name"]=="servfail-queries")
505 queries=broadcastAccFunction<vector<query_t> >(pleaseGetServfailQueryRing);
506 else if(req->getvars["name"]=="bogus-queries")
507 queries=broadcastAccFunction<vector<query_t> >(pleaseGetBogusQueryRing);
508 else if(req->getvars["name"]=="queries")
509 queries=broadcastAccFunction<vector<query_t> >(pleaseGetQueryRing);
510
511 typedef map<query_t,unsigned int> counts_t;
512 counts_t counts;
513 unsigned int total=0;
514 for(const query_t& q : queries) {
515 total++;
516 if(filter)
517 counts[make_pair(getRegisteredName(q.first), q.second)]++;
518 else
519 counts[make_pair(q.first, q.second)]++;
520 }
521
522 typedef std::multimap<int, query_t> rcounts_t;
523 rcounts_t rcounts;
524
525 for(counts_t::const_iterator i=counts.begin(); i != counts.end(); ++i)
526 rcounts.insert(make_pair(-i->second, i->first));
527
528 Json::array entries;
529 unsigned int tot=0, totIncluded=0;
530 for(const rcounts_t::value_type& q : rcounts) {
531 totIncluded-=q.first;
532 entries.push_back(Json::array {
533 -q.first, q.second.first.toLogString(), DNSRecordContent::NumberToType(q.second.second)
534 });
535 if(tot++>=100)
536 break;
537 }
538 if(queries.size() != totIncluded) {
539 entries.push_back(Json::array {
540 (int)(queries.size() - totIncluded), "", ""
541 });
542 }
543 resp->setBody(Json::object { { "entries", entries } });
544 return;
545 }
546 else if(command == "get-remote-ring") {
547 vector<ComboAddress> queries;
548 if(req->getvars["name"]=="remotes")
549 queries=broadcastAccFunction<vector<ComboAddress> >(pleaseGetRemotes);
550 else if(req->getvars["name"]=="servfail-remotes")
551 queries=broadcastAccFunction<vector<ComboAddress> >(pleaseGetServfailRemotes);
552 else if(req->getvars["name"]=="bogus-remotes")
553 queries=broadcastAccFunction<vector<ComboAddress> >(pleaseGetBogusRemotes);
554 else if(req->getvars["name"]=="large-answer-remotes")
555 queries=broadcastAccFunction<vector<ComboAddress> >(pleaseGetLargeAnswerRemotes);
556 else if(req->getvars["name"]=="timeouts")
557 queries=broadcastAccFunction<vector<ComboAddress> >(pleaseGetTimeouts);
558
559 typedef map<ComboAddress,unsigned int,ComboAddress::addressOnlyLessThan> counts_t;
560 counts_t counts;
561 unsigned int total=0;
562 for(const ComboAddress& q : queries) {
563 total++;
564 counts[q]++;
565 }
566
567 typedef std::multimap<int, ComboAddress> rcounts_t;
568 rcounts_t rcounts;
569
570 for(counts_t::const_iterator i=counts.begin(); i != counts.end(); ++i)
571 rcounts.insert(make_pair(-i->second, i->first));
572
573 Json::array entries;
574 unsigned int tot=0, totIncluded=0;
575 for(const rcounts_t::value_type& q : rcounts) {
576 totIncluded-=q.first;
577 entries.push_back(Json::array {
578 -q.first, q.second.toString()
579 });
580 if(tot++>=100)
581 break;
582 }
583 if(queries.size() != totIncluded) {
584 entries.push_back(Json::array {
585 (int)(queries.size() - totIncluded), ""
586 });
587 }
588
589 resp->setBody(Json::object { { "entries", entries } });
590 return;
591 } else {
592 resp->setErrorResult("Command '"+command+"' not found", 404);
593 }
594 }
595
596
597 void AsyncServerNewConnectionMT(void *p) {
598 AsyncServer *server = (AsyncServer*)p;
599
600 try {
601 auto socket = server->accept(); // this is actually a shared_ptr
602 if (socket) {
603 server->d_asyncNewConnectionCallback(socket);
604 }
605 } catch (NetworkError &e) {
606 // we're running in a shared process/thread, so can't just terminate/abort.
607 g_log<<Logger::Warning<<"Network error in web thread: "<<e.what()<<endl;
608 return;
609 }
610 catch (...) {
611 g_log<<Logger::Warning<<"Unknown error in web thread"<<endl;
612
613 return;
614 }
615
616 }
617
618 void AsyncServer::asyncWaitForConnections(FDMultiplexer* fdm, const newconnectioncb_t& callback)
619 {
620 d_asyncNewConnectionCallback = callback;
621 fdm->addReadFD(d_server_socket.getHandle(), boost::bind(&AsyncServer::newConnection, this));
622 }
623
624 void AsyncServer::newConnection()
625 {
626 getMT()->makeThread(&AsyncServerNewConnectionMT, this);
627 }
628
629 // This is an entry point from FDM, so it needs to catch everything.
630 void AsyncWebServer::serveConnection(std::shared_ptr<Socket> client) const {
631 const string logprefix = d_logprefix + to_string(getUniqueID()) + " ";
632
633 HttpRequest req(logprefix);
634 HttpResponse resp;
635 ComboAddress remote;
636 string reply;
637
638 try {
639 YaHTTP::AsyncRequestLoader yarl;
640 yarl.initialize(&req);
641 client->setNonBlocking();
642
643 string data;
644 try {
645 while(!req.complete) {
646 int bytes = arecvtcp(data, 16384, client.get(), true);
647 if (bytes > 0) {
648 req.complete = yarl.feed(data);
649 } else {
650 // read error OR EOF
651 break;
652 }
653 }
654 yarl.finalize();
655 } catch (YaHTTP::ParseError &e) {
656 // request stays incomplete
657 g_log<<Logger::Warning<<logprefix<<"Unable to parse request: "<<e.what()<<endl;
658 }
659
660 if (d_loglevel >= WebServer::LogLevel::None) {
661 client->getRemote(remote);
662 }
663
664 logRequest(req, remote);
665
666 WebServer::handleRequest(req, resp);
667 ostringstream ss;
668 resp.write(ss);
669 reply = ss.str();
670
671 logResponse(resp, remote, logprefix);
672
673 // now send the reply
674 if (asendtcp(reply, client.get()) == -1 || reply.empty()) {
675 g_log<<Logger::Error<<logprefix<<"Failed sending reply to HTTP client"<<endl;
676 }
677 }
678 catch(PDNSException &e) {
679 g_log<<Logger::Error<<logprefix<<"Exception: "<<e.reason<<endl;
680 }
681 catch(std::exception &e) {
682 if(strstr(e.what(), "timeout")==0)
683 g_log<<Logger::Error<<logprefix<<"STL Exception: "<<e.what()<<endl;
684 }
685 catch(...) {
686 g_log<<Logger::Error<<logprefix<<"Unknown exception"<<endl;
687 }
688
689 if (d_loglevel >= WebServer::LogLevel::Normal) {
690 g_log<<Logger::Notice<<logprefix<<remote<<" \""<<req.method<<" "<<req.url.path<<" HTTP/"<<req.versionStr(req.version)<<"\" "<<resp.status<<" "<<reply.size()<<endl;
691 }
692 }
693
694 void AsyncWebServer::go() {
695 if (!d_server)
696 return;
697 auto server = std::dynamic_pointer_cast<AsyncServer>(d_server);
698 if (!server)
699 return;
700 server->asyncWaitForConnections(d_fdm, boost::bind(&AsyncWebServer::serveConnection, this, _1));
701 }