28 #include <boost/algorithm/string.hpp> 29 #include <boost/asio/ip/tcp.hpp> 36 namespace autoconfig {
47 Url(
const std::string& url)
50 static const std::regex protocolExp(
"(\\w+\\d?(\\+\\w+)?)://([^/]*)(\\/[^?]*)?");
51 std::smatch protocolMatch;
52 if (!std::regex_match(url, protocolMatch, protocolExp)) {
55 m_scheme = protocolMatch[1];
56 std::string authority = protocolMatch[3];
57 m_path = protocolMatch[4];
60 static const std::regex v6Exp(
"^\\[([a-fA-F0-9:]+)\\](?:\\:(\\d+))?$");
62 static const std::regex v4MappedV6Exp(
"^\\[::ffff:(\\d+(?:\\.\\d+){3})\\](?:\\:(\\d+))?$");
64 static const std::regex v4HostExp(
"^([^:]+)(?:\\:(\\d+))?$");
66 if (authority.empty()) {
71 bool isV6 = std::regex_match(authority, match, v6Exp);
73 std::regex_match(authority, match, v4MappedV6Exp) ||
74 std::regex_match(authority, match, v4HostExp)) {
123 std::string m_scheme;
129 class HttpException :
public std::runtime_error
133 HttpException(
const std::string& what)
134 :
std::runtime_error(what)
145 NdnFchDiscovery::doStart()
148 boost::asio::ip::tcp::iostream requestStream;
149 #if BOOST_VERSION >= 106700 150 requestStream.expires_after(std::chrono::seconds(3));
152 requestStream.expires_from_now(boost::posix_time::seconds(3));
153 #endif // BOOST_VERSION >= 106700 156 if (!url.isValid()) {
157 NDN_THROW(HttpException(
"Invalid NDN-FCH URL: " + m_url));
159 if (!boost::iequals(url.getScheme(),
"http")) {
160 NDN_THROW(HttpException(
"Only http:// NDN-FCH URLs are supported"));
163 requestStream.connect(url.getHost(), url.getPort());
164 if (!requestStream) {
165 NDN_THROW(HttpException(
"HTTP connection error to " + m_url));
168 requestStream <<
"GET " << url.getPath() <<
" HTTP/1.0\r\n";
169 requestStream <<
"Host: " << url.getHost() <<
":" << url.getPort() <<
"\r\n";
170 requestStream <<
"Accept: */*\r\n";
171 requestStream <<
"Cache-Control: no-cache\r\n";
172 requestStream <<
"Connection: close\r\n\r\n";
173 requestStream.flush();
175 std::string statusLine;
176 std::getline(requestStream, statusLine);
177 if (!requestStream) {
178 NDN_THROW(HttpException(
"HTTP communication error"));
181 std::stringstream responseStream(statusLine);
182 std::string httpVersion;
183 responseStream >> httpVersion;
184 unsigned int statusCode;
185 responseStream >> statusCode;
186 std::string statusMessage;
188 std::getline(responseStream, statusMessage);
189 if (!static_cast<bool>(requestStream) || httpVersion.substr(0, 5) !=
"HTTP/") {
190 NDN_THROW(HttpException(
"HTTP communication error"));
192 if (statusCode != 200) {
193 boost::trim(statusMessage);
194 NDN_THROW(HttpException(
"HTTP request failed: " + to_string(statusCode) +
" " + statusMessage));
197 while (std::getline(requestStream, header) && header !=
"\r")
201 requestStream >> hubHost;
202 if (hubHost.empty()) {
203 NDN_THROW(HttpException(
"NDN-FCH did not return hub host"));
208 catch (
const std::runtime_error& e) {
209 this->
fail(e.what());
Copyright (c) 2014-2017, Regents of the University of California, Arizona Board of Regents...