• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PowerDNS / pdns / 9759852662

02 Jul 2024 11:09AM UTC coverage: 64.672% (-0.009%) from 64.681%
9759852662

push

github

web-flow
Merge pull request #14396 from omoerbeek/tidy-20240627

Tidy sstuf.hh and shuffle.??

37193 of 88268 branches covered (42.14%)

Branch coverage included in aggregate %.

72 of 126 new or added lines in 6 files covered. (57.14%)

86 existing lines in 13 files now uncovered.

124711 of 162077 relevant lines covered (76.95%)

4947384.09 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

45.78
/modules/remotebackend/httpconnector.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 "remotebackend.hh"
26
#include <sys/socket.h>
27
#include <unistd.h>
28
#include <fcntl.h>
29

30
#include <sstream>
31
#include "pdns/lock.hh"
32

33
#ifndef UNIX_PATH_MAX
34
#define UNIX_PATH_MAX 108
35
#endif
36

37
HTTPConnector::HTTPConnector(std::map<std::string, std::string> options) :
38
  d_socket(nullptr)
39
{
25✔
40

41
  if (options.find("url") == options.end()) {
25!
42
    throw PDNSException("Cannot find 'url' option in the remote backend HTTP connector's parameters");
×
43
  }
×
44

45
  this->d_url = options.find("url")->second;
25✔
46

47
  try {
25✔
48
    YaHTTP::URL url(d_url);
25✔
49
    d_host = url.host;
25✔
50
    d_port = url.port;
25✔
51
  }
25✔
52
  catch (const std::exception& e) {
25✔
53
    throw PDNSException("Error parsing the 'url' option provided to the remote backend HTTP connector: " + std::string(e.what()));
×
54
  }
×
55

56
  if (options.find("url-suffix") != options.end()) {
25!
57
    this->d_url_suffix = options.find("url-suffix")->second;
×
58
  }
×
59
  else {
25✔
60
    this->d_url_suffix = "";
25✔
61
  }
25✔
62
  this->timeout = 2;
25✔
63
  this->d_post = false;
25✔
64
  this->d_post_json = false;
25✔
65

66
  if (options.find("timeout") != options.end()) {
25✔
67
    this->timeout = std::stoi(options.find("timeout")->second) / 1000;
14✔
68
  }
14✔
69
  if (options.find("post") != options.end()) {
25!
70
    std::string val = options.find("post")->second;
×
71
    if (val == "yes" || val == "true" || val == "on" || val == "1") {
×
72
      this->d_post = true;
×
73
    }
×
74
  }
×
75
  if (options.find("post_json") != options.end()) {
25!
76
    std::string val = options.find("post_json")->second;
×
77
    if (val == "yes" || val == "true" || val == "on" || val == "1") {
×
78
      this->d_post_json = true;
×
79
    }
×
80
  }
×
81
}
25✔
82

83
HTTPConnector::~HTTPConnector() = default;
21✔
84

85
void HTTPConnector::addUrlComponent(const Json& parameters, const string& element, std::stringstream& ss)
86
{
1,009✔
87
  std::string sparam;
1,009✔
88
  if (parameters[element] != Json()) {
1,009✔
89
    ss << "/" << YaHTTP::Utility::encodeURL(asString(parameters[element]), false);
257✔
90
  }
257✔
91
}
1,009✔
92

93
std::string HTTPConnector::buildMemberListArgs(const std::string& prefix, const Json& args)
94
{
×
95
  std::stringstream stream;
×
96

97
  for (const auto& pair : args.object_items()) {
×
98
    stream << prefix << "[" << YaHTTP::Utility::encodeURL(pair.first, false) << "]=";
×
99
    if (pair.second.is_bool()) {
×
100
      stream << (pair.second.bool_value() ? "1" : "0");
×
101
    }
×
102
    else if (!pair.second.is_null()) {
×
103
      stream << YaHTTP::Utility::encodeURL(HTTPConnector::asString(pair.second), false);
×
104
    }
×
105
    stream << "&";
×
106
  }
×
107

108
  return stream.str().substr(0, stream.str().size() - 1); // snip the trailing &
×
109
}
×
110

111
// builds our request (near-restful)
112
void HTTPConnector::restful_requestbuilder(const std::string& method, const Json& parameters, YaHTTP::Request& req)
113
{
143✔
114
  std::stringstream ss;
143✔
115
  std::string sparam;
143✔
116
  std::string verb;
143✔
117

118
  // special names are qname, name, zonename, kind, others go to headers
119

120
  ss << d_url;
143✔
121

122
  ss << "/" << method;
143✔
123

124
  // add the url components, if found, in following order.
125
  // id must be first due to the fact that the qname/name can be empty
126

127
  addUrlComponent(parameters, "id", ss);
143✔
128
  addUrlComponent(parameters, "domain_id", ss);
143✔
129
  addUrlComponent(parameters, "zonename", ss);
143✔
130
  addUrlComponent(parameters, "qname", ss);
143✔
131
  addUrlComponent(parameters, "name", ss);
143✔
132
  addUrlComponent(parameters, "kind", ss);
143✔
133
  addUrlComponent(parameters, "qtype", ss);
143✔
134

135
  // set the correct type of request based on method
136
  if (method == "activateDomainKey" || method == "deactivateDomainKey" || method == "publishDomainKey" || method == "unpublishDomainKey") {
143!
137
    // create an empty post
138
    req.preparePost();
×
139
    verb = "POST";
×
140
  }
×
141
  else if (method == "setTSIGKey") {
143!
142
    req.POST()["algorithm"] = parameters["algorithm"].string_value();
×
143
    req.POST()["content"] = parameters["content"].string_value();
×
144
    req.preparePost();
×
145
    verb = "PATCH";
×
146
  }
×
147
  else if (method == "deleteTSIGKey") {
143!
148
    verb = "DELETE";
×
149
  }
×
150
  else if (method == "addDomainKey") {
143✔
151
    const Json& param = parameters["key"];
2✔
152
    req.POST()["flags"] = asString(param["flags"]);
2✔
153
    req.POST()["active"] = (param["active"].bool_value() ? "1" : "0");
2!
154
    req.POST()["published"] = (param["published"].bool_value() ? "1" : "0");
2!
155
    req.POST()["content"] = param["content"].string_value();
2✔
156
    req.preparePost();
2✔
157
    verb = "PUT";
2✔
158
  }
2✔
159
  else if (method == "superMasterBackend") {
141!
160
    std::stringstream ss2;
×
161
    addUrlComponent(parameters, "ip", ss);
×
162
    addUrlComponent(parameters, "domain", ss);
×
163
    // then we need to serialize rrset payload into POST
164
    for (size_t index = 0; index < parameters["nsset"].array_items().size(); index++) {
×
165
      ss2 << buildMemberListArgs("nsset[" + std::to_string(index) + "]", parameters["nsset"][index]) << "&";
×
166
    }
×
167
    req.body = ss2.str().substr(0, ss2.str().size() - 1);
×
168
    req.headers["content-type"] = "application/x-www-form-urlencoded; charset=utf-8";
×
169
    req.headers["content-length"] = std::to_string(req.body.size());
×
170
    verb = "POST";
×
171
  }
×
172
  else if (method == "createSlaveDomain") {
141!
173
    addUrlComponent(parameters, "ip", ss);
×
174
    addUrlComponent(parameters, "domain", ss);
×
175
    if (!parameters["account"].is_null() && parameters["account"].is_string()) {
×
176
      req.POST()["account"] = parameters["account"].string_value();
×
177
    }
×
178
    req.preparePost();
×
179
    verb = "PUT";
×
180
  }
×
181
  else if (method == "replaceRRSet") {
141!
182
    std::stringstream ss2;
×
183
    for (size_t index = 0; index < parameters["rrset"].array_items().size(); index++) {
×
184
      ss2 << buildMemberListArgs("rrset[" + std::to_string(index) + "]", parameters["rrset"][index]) << "&";
×
185
    }
×
186
    req.body = ss2.str().substr(0, ss2.str().size() - 1); // remove trailing &
×
187
    req.headers["content-type"] = "application/x-www-form-urlencoded; charset=utf-8";
×
188
    req.headers["content-length"] = std::to_string(req.body.size());
×
189
    verb = "PATCH";
×
190
  }
×
191
  else if (method == "feedRecord") {
141!
192
    addUrlComponent(parameters, "trxid", ss);
×
193
    req.body = buildMemberListArgs("rr", parameters["rr"]);
×
194
    req.headers["content-type"] = "application/x-www-form-urlencoded; charset=utf-8";
×
195
    req.headers["content-length"] = std::to_string(req.body.size());
×
196
    verb = "PATCH";
×
197
  }
×
198
  else if (method == "feedEnts") {
141!
199
    std::stringstream ss2;
×
200
    addUrlComponent(parameters, "trxid", ss);
×
201
    for (const auto& param : parameters["nonterm"].array_items()) {
×
202
      ss2 << "nonterm[]=" << YaHTTP::Utility::encodeURL(param.string_value(), false) << "&";
×
203
    }
×
204
    for (const auto& param : parameters["auth"].array_items()) {
×
205
      ss2 << "auth[]=" << (param["auth"].bool_value() ? "1" : "0") << "&";
×
206
    }
×
207
    req.body = ss2.str().substr(0, ss2.str().size() - 1);
×
208
    req.headers["content-type"] = "application/x-www-form-urlencoded; charset=utf-8";
×
209
    req.headers["content-length"] = std::to_string(req.body.size());
×
210
    verb = "PATCH";
×
211
  }
×
212
  else if (method == "feedEnts3") {
141!
213
    std::stringstream ss2;
×
214
    addUrlComponent(parameters, "domain", ss);
×
215
    addUrlComponent(parameters, "trxid", ss);
×
216
    ss2 << "times=" << parameters["times"].int_value() << "&salt=" << YaHTTP::Utility::encodeURL(parameters["salt"].string_value(), false) << "&narrow=" << (parameters["narrow"].bool_value() ? 1 : 0) << "&";
×
217
    for (const auto& param : parameters["nonterm"].array_items()) {
×
218
      ss2 << "nonterm[]=" << YaHTTP::Utility::encodeURL(param.string_value(), false) << "&";
×
219
    }
×
220
    for (const auto& param : parameters["auth"].array_items()) {
×
221
      ss2 << "auth[]=" << (param["auth"].bool_value() ? "1" : "0") << "&";
×
222
    }
×
223
    req.body = ss2.str().substr(0, ss2.str().size() - 1);
×
224
    req.headers["content-type"] = "application/x-www-form-urlencoded; charset=utf-8";
×
225
    req.headers["content-length"] = std::to_string(req.body.size());
×
226
    verb = "PATCH";
×
227
  }
×
228
  else if (method == "startTransaction") {
141✔
229
    addUrlComponent(parameters, "domain", ss);
4✔
230
    addUrlComponent(parameters, "trxid", ss);
4✔
231
    req.preparePost();
4✔
232
    verb = "POST";
4✔
233
  }
4✔
234
  else if (method == "commitTransaction" || method == "abortTransaction") {
137!
235
    addUrlComponent(parameters, "trxid", ss);
×
236
    req.preparePost();
×
237
    verb = "POST";
×
238
  }
×
239
  else if (method == "setDomainMetadata") {
137!
240
    // copy all metadata values into post
241
    std::stringstream ss2;
×
242
    // this one has values too
243
    if (parameters["value"].is_array()) {
×
244
      for (const auto& val : parameters["value"].array_items()) {
×
245
        ss2 << "value[]=" << YaHTTP::Utility::encodeURL(val.string_value(), false) << "&";
×
246
      }
×
247
    }
×
248
    req.body = ss2.str().substr(0, ss2.str().size() - 1);
×
249
    req.headers["content-type"] = "application/x-www-form-urlencoded; charset=utf-8";
×
250
    req.headers["content-length"] = std::to_string(req.body.size());
×
251
    verb = "PATCH";
×
252
  }
×
253
  else if (method == "removeDomainKey") {
137!
254
    // this one is delete
255
    verb = "DELETE";
×
256
  }
×
257
  else if (method == "setNotified") {
137!
258
    req.POST()["serial"] = asString(parameters["serial"]);
×
259
    req.preparePost();
×
260
    verb = "PATCH";
×
261
  }
×
262
  else if (method == "setStale") {
137!
263
    req.preparePost();
×
264
    verb = "PATCH";
×
265
  }
×
266
  else if (method == "setFresh") {
137!
267
    req.preparePost();
×
268
    verb = "PATCH";
×
269
  }
×
270
  else if (method == "directBackendCmd") {
137✔
271
    req.POST()["query"] = parameters["query"].string_value();
2✔
272
    req.preparePost();
2✔
273
    verb = "POST";
2✔
274
  }
2✔
275
  else if (method == "searchRecords" || method == "searchComments") {
135!
276
    req.GET()["pattern"] = parameters["pattern"].string_value();
×
277
    req.GET()["maxResults"] = std::to_string(parameters["maxResults"].int_value());
×
278
    verb = "GET";
×
279
  }
×
280
  else if (method == "getAllDomains") {
135!
281
    req.GET()["includeDisabled"] = (parameters["include_disabled"].bool_value() ? "true" : "false");
×
282
    verb = "GET";
×
283
  }
×
284
  else {
135✔
285
    // perform normal get
286
    verb = "GET";
135✔
287
  }
135✔
288

289
  // put everything else into headers
290
  for (const auto& pair : parameters.object_items()) {
413✔
291
    std::string member = pair.first;
413✔
292
    // whitelist header parameters
293
    if ((member == "trxid" || member == "local" || member == "remote" || member == "real-remote" || member == "zone-id")) {
413✔
294
      std::string hdr = "x-remotebackend-" + member;
152✔
295
      req.headers[hdr] = asString(pair.second);
152✔
296
    }
152✔
297
  };
413✔
298

299
  // finally add suffix and store url
300
  ss << d_url_suffix;
143✔
301

302
  req.setup(verb, ss.str());
143✔
303
  req.headers["accept"] = "application/json";
143✔
304
}
143✔
305

306
void HTTPConnector::post_requestbuilder(const Json& input, YaHTTP::Request& req)
307
{
×
308
  if (this->d_post_json) {
×
309
    std::string out = input.dump();
×
310
    req.setup("POST", d_url);
×
311
    // simple case, POST JSON into url. nothing fancy.
312
    req.headers["Content-Type"] = "text/javascript; charset=utf-8";
×
313
    req.headers["Content-Length"] = std::to_string(out.size());
×
314
    req.headers["accept"] = "application/json";
×
315
    req.body = out;
×
316
  }
×
317
  else {
×
318
    std::stringstream url;
×
319
    std::stringstream content;
×
320
    // call url/method.suffix
321
    url << d_url << "/" << input["method"].string_value() << d_url_suffix;
×
322
    req.setup("POST", url.str());
×
323
    // then build content
324
    req.POST()["parameters"] = input["parameters"].dump();
×
325
    req.preparePost();
×
326
    req.headers["accept"] = "application/json";
×
327
  }
×
328
}
×
329

330
int HTTPConnector::send_message(const Json& input)
331
{
143✔
332
  int rv = 0;
143✔
333
  int ec = 0;
143✔
334
  int fd = 0;
143✔
335

336
  std::vector<std::string> members;
143✔
337
  std::string method;
143✔
338
  std::ostringstream out;
143✔
339

340
  // perform request
341
  YaHTTP::Request req;
143✔
342

343
  if (d_post) {
143!
344
    post_requestbuilder(input, req);
×
345
  }
×
346
  else {
143✔
347
    restful_requestbuilder(input["method"].string_value(), input["parameters"], req);
143✔
348
  }
143✔
349

350
  rv = -1;
143✔
351
  req.headers["connection"] = "Keep-Alive"; // see if we can streamline requests (not needed, strictly speaking)
143✔
352

353
  out << req;
143✔
354

355
  // try sending with current socket, if it fails retry with new socket
356
  if (this->d_socket != nullptr) {
143✔
357
    fd = this->d_socket->getHandle();
125✔
358
    // there should be no data waiting
359
    if (waitForRWData(fd, true, 0, 1000) < 1) {
125!
360
      try {
×
361
        d_socket->writenWithTimeout(out.str().c_str(), out.str().size(), timeout);
×
362
        rv = 1;
×
363
      }
×
364
      catch (NetworkError& ne) {
×
365
        g_log << Logger::Error << "While writing to HTTP endpoint " << d_addr.toStringWithPort() << ": " << ne.what() << std::endl;
×
366
      }
×
367
      catch (...) {
×
368
        g_log << Logger::Error << "While writing to HTTP endpoint " << d_addr.toStringWithPort() << ": exception caught" << std::endl;
×
369
      }
×
370
    }
×
371
  }
125✔
372

373
  if (rv == 1) {
143!
374
    return rv;
×
375
  }
×
376

377
  this->d_socket.reset();
143✔
378

379
  // connect using tcp
380
  struct addrinfo* gAddr = nullptr;
143✔
381
  struct addrinfo* gAddrPtr = nullptr;
143✔
382
  struct addrinfo hints
143✔
383
  {
143✔
384
  };
143✔
385
  std::string sPort = std::to_string(d_port);
143✔
386
  memset(&hints, 0, sizeof hints);
143✔
387
  hints.ai_family = AF_UNSPEC;
143✔
388
  hints.ai_flags = AI_ADDRCONFIG;
143✔
389
  hints.ai_socktype = SOCK_STREAM;
143✔
390
  hints.ai_protocol = IPPROTO_TCP;
143✔
391
  if ((ec = getaddrinfo(d_host.c_str(), sPort.c_str(), &hints, &gAddr)) == 0) {
143!
392
    // try to connect to each address.
393
    gAddrPtr = gAddr;
143✔
394

395
    while (gAddrPtr != nullptr) {
286!
396
      try {
286✔
397
        d_socket = std::make_unique<Socket>(gAddrPtr->ai_family, gAddrPtr->ai_socktype, gAddrPtr->ai_protocol);
286✔
398
        d_addr.setSockaddr(gAddrPtr->ai_addr, gAddrPtr->ai_addrlen);
286✔
399
        d_socket->connect(d_addr);
286✔
400
        d_socket->setNonBlocking();
286✔
401
        d_socket->writenWithTimeout(out.str().c_str(), out.str().size(), timeout);
286✔
402
        rv = 1;
286✔
403
      }
286✔
404
      catch (NetworkError& ne) {
286✔
405
        g_log << Logger::Error << "While writing to HTTP endpoint " << d_addr.toStringWithPort() << ": " << ne.what() << std::endl;
143✔
406
      }
143✔
407
      catch (...) {
286✔
408
        g_log << Logger::Error << "While writing to HTTP endpoint " << d_addr.toStringWithPort() << ": exception caught" << std::endl;
×
409
      }
×
410

411
      if (rv > -1) {
286✔
412
        break;
143✔
413
      }
143✔
414
      d_socket.reset();
143✔
415
      gAddrPtr = gAddrPtr->ai_next;
143✔
416
    }
143✔
417
    freeaddrinfo(gAddr);
143✔
418
  }
143✔
419
  else {
×
420
    g_log << Logger::Error << "Unable to resolve " << d_host << ": " << gai_strerror(ec) << std::endl;
×
421
  }
×
422

423
  return rv;
143✔
424
}
143✔
425

426
int HTTPConnector::recv_message(Json& output)
427
{
143✔
428
  YaHTTP::AsyncResponseLoader arl;
143✔
429
  YaHTTP::Response resp;
143✔
430

431
  if (d_socket == nullptr) {
143!
432
    return -1; // cannot receive :(
×
433
  }
×
434
  std::array<char, 4096> buffer{};
143✔
435
  time_t time0 = 0;
143✔
436

437
  arl.initialize(&resp);
143✔
438

439
  try {
143✔
440
    time0 = time(nullptr);
143✔
441
    while (!arl.ready() && (labs(time(nullptr) - time0) <= timeout)) {
359!
442
      auto readBytes = d_socket->readWithTimeout(buffer.data(), buffer.size(), timeout);
216✔
443
      if (readBytes == 0) {
216!
444
        throw NetworkError("EOF while reading");
×
445
      }
×
446
      arl.feed(std::string(buffer.data(), readBytes));
216✔
447
    }
216✔
448
    // timeout occurred.
449
    if (!arl.ready()) {
143!
450
      throw NetworkError("timeout");
×
451
    }
×
452
  }
143✔
453
  catch (NetworkError& ne) {
143✔
454
    d_socket.reset();
×
455
    throw PDNSException("While reading from HTTP endpoint " + d_addr.toStringWithPort() + ": " + ne.what());
×
456
  }
×
457
  catch (...) {
143✔
458
    d_socket.reset();
×
459
    throw PDNSException("While reading from HTTP endpoint " + d_addr.toStringWithPort() + ": unknown error");
×
460
  }
×
461

462
  arl.finalize();
143✔
463

464
  if ((resp.status < 200 || resp.status >= 400) && resp.status != 404) {
143!
465
    // bad.
466
    throw PDNSException("Received unacceptable HTTP status code " + std::to_string(resp.status) + " from HTTP endpoint " + d_addr.toStringWithPort());
×
467
  }
×
468

469
  std::string err;
143✔
470
  output = Json::parse(resp.body, err);
143✔
471
  if (output != nullptr) {
143!
472
    return static_cast<int>(resp.body.size());
143✔
473
  }
143✔
474
  g_log << Logger::Error << "Cannot parse JSON reply: " << err << endl;
×
475

NEW
476
  return -1;
×
477
}
143✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc