28 #include <boost/algorithm/string.hpp> 29 #include <boost/regex.hpp> 35 namespace autoconfig {
46 Url(
const std::string& url)
49 static const boost::regex protocolExp(
"(\\w+\\d?(\\+\\w+)?)://([^/]*)(\\/[^?]*)?");
50 boost::smatch protocolMatch;
51 if (!boost::regex_match(url, protocolMatch, protocolExp)) {
54 m_scheme = protocolMatch[1];
55 const std::string& authority = protocolMatch[3];
56 m_path = protocolMatch[4];
59 static const boost::regex v6Exp(
"^\\[([a-fA-F0-9:]+)\\](?:\\:(\\d+))?$");
61 static const boost::regex v4MappedV6Exp(
"^\\[::ffff:(\\d+(?:\\.\\d+){3})\\](?:\\:(\\d+))?$");
63 static const boost::regex v4HostExp(
"^([^:]+)(?:\\:(\\d+))?$");
65 if (authority.empty()) {
70 bool isV6 = boost::regex_match(authority, match, v6Exp);
72 boost::regex_match(authority, match, v4MappedV6Exp) ||
73 boost::regex_match(authority, match, v4HostExp)) {
122 std::string m_scheme;
128 class HttpException :
public std::runtime_error
132 HttpException(
const std::string& what)
133 :
std::runtime_error(what)
144 NdnFchDiscovery::doStart()
147 using namespace boost::asio::ip;
148 tcp::iostream requestStream;
150 requestStream.expires_from_now(boost::posix_time::milliseconds(3000));
153 if (!url.isValid()) {
154 BOOST_THROW_EXCEPTION(HttpException(
"Invalid NDN-FCH URL: " + m_url));
157 if (!boost::iequals(url.getScheme(),
"http")) {
158 BOOST_THROW_EXCEPTION(HttpException(
"Only http:// NDN-FCH URLs are supported"));
161 requestStream.connect(url.getHost(), url.getPort());
163 if (!requestStream) {
164 BOOST_THROW_EXCEPTION(HttpException(
"HTTP connection error to " + m_url));
167 requestStream <<
"GET " << url.getPath() <<
" HTTP/1.0\r\n";
168 requestStream <<
"Host: " << url.getHost() <<
":" << url.getPort() <<
"\r\n";
169 requestStream <<
"Accept: */*\r\n";
170 requestStream <<
"Cache-Control: no-cache\r\n";
171 requestStream <<
"Connection: close\r\n\r\n";
172 requestStream.flush();
174 std::string statusLine;
175 std::getline(requestStream, statusLine);
176 if (!requestStream) {
177 BOOST_THROW_EXCEPTION(HttpException(
"HTTP communication error"));
180 std::stringstream responseStream(statusLine);
181 std::string httpVersion;
182 responseStream >> httpVersion;
183 unsigned int statusCode;
184 responseStream >> statusCode;
185 std::string statusMessage;
187 std::getline(responseStream, statusMessage);
188 if (!static_cast<bool>(requestStream) || httpVersion.substr(0, 5) !=
"HTTP/") {
189 BOOST_THROW_EXCEPTION(HttpException(
"HTTP communication error"));
191 if (statusCode != 200) {
192 boost::trim(statusMessage);
193 BOOST_THROW_EXCEPTION(HttpException(
"HTTP request failed: " + std::to_string(statusCode) +
" " + statusMessage));
196 while (std::getline(requestStream, header) && header !=
"\r")
200 requestStream >> hubHost;
202 if (hubHost.empty()) {
203 BOOST_THROW_EXCEPTION(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...