From ab14ef998c76b5efe8fcc807bb96c2c4ffbb982f Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Wed, 13 Mar 2019 17:46:27 +0100 Subject: [PATCH] auth: Parse the HTTP URL when the remote backend is initialized (cherry picked from commit a11a87c8eacb146b2c4a036f9a0817d704c8e925) --- modules/remotebackend/httpconnector.cc | 83 +++++++++++++++----------- modules/remotebackend/remotebackend.hh | 2 + 2 files changed, 49 insertions(+), 36 deletions(-) diff --git a/modules/remotebackend/httpconnector.cc b/modules/remotebackend/httpconnector.cc index 7974c557c..ea9afa42c 100644 --- a/modules/remotebackend/httpconnector.cc +++ b/modules/remotebackend/httpconnector.cc @@ -35,7 +35,22 @@ #endif HTTPConnector::HTTPConnector(std::map options) { + + if (options.find("url") == options.end()) { + throw PDNSException("Cannot find 'url' option in the remote backend HTTP connector's parameters"); + } + this->d_url = options.find("url")->second; + + try { + YaHTTP::URL url(d_url); + d_host = url.host; + d_port = url.port; + } + catch(const std::exception& e) { + throw PDNSException("Error parsing the 'url' option provided to the remote backend HTTP connector: " + std::string(e.what())); + } + if (options.find("url-suffix") != options.end()) { this->d_url_suffix = options.find("url-suffix")->second; } else { @@ -334,45 +349,41 @@ int HTTPConnector::send_message(const Json& input) { delete this->d_socket; this->d_socket = NULL; - if (req.url.protocol == "unix") { - // connect using unix socket - } else { - // connect using tcp - struct addrinfo *gAddr, *gAddrPtr, hints; - std::string sPort = std::to_string(req.url.port); - memset(&hints,0,sizeof hints); - hints.ai_family = AF_UNSPEC; - hints.ai_flags = AI_ADDRCONFIG; - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = 6; // tcp - if ((ec = getaddrinfo(req.url.host.c_str(), sPort.c_str(), &hints, &gAddr)) == 0) { - // try to connect to each address. - gAddrPtr = gAddr; + // connect using tcp + struct addrinfo *gAddr, *gAddrPtr, hints; + std::string sPort = std::to_string(d_port); + memset(&hints,0,sizeof hints); + hints.ai_family = AF_UNSPEC; + hints.ai_flags = AI_ADDRCONFIG; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = 6; // tcp + if ((ec = getaddrinfo(d_host.c_str(), sPort.c_str(), &hints, &gAddr)) == 0) { + // try to connect to each address. + gAddrPtr = gAddr; - while(gAddrPtr) { - try { - d_socket = new Socket(gAddrPtr->ai_family, gAddrPtr->ai_socktype, gAddrPtr->ai_protocol); - d_addr.setSockaddr(gAddrPtr->ai_addr, gAddrPtr->ai_addrlen); - d_socket->connect(d_addr); - d_socket->setNonBlocking(); - d_socket->writenWithTimeout(out.str().c_str(), out.str().size(), timeout); - rv = 1; - } catch (NetworkError& ne) { - L< -1) break; - delete d_socket; - d_socket = NULL; - gAddrPtr = gAddrPtr->ai_next; - + while(gAddrPtr) { + try { + d_socket = new Socket(gAddrPtr->ai_family, gAddrPtr->ai_socktype, gAddrPtr->ai_protocol); + d_addr.setSockaddr(gAddrPtr->ai_addr, gAddrPtr->ai_addrlen); + d_socket->connect(d_addr); + d_socket->setNonBlocking(); + d_socket->writenWithTimeout(out.str().c_str(), out.str().size(), timeout); + rv = 1; + } catch (NetworkError& ne) { + L< -1) break; + delete d_socket; + d_socket = NULL; + gAddrPtr = gAddrPtr->ai_next; + } + freeaddrinfo(gAddr); + } else { + L<