void connect(const uri& uri, std::unique_ptr<socket_t>& socket) { tcp::resolver resolver(socket->get_io_service()); tcp::resolver::query query(uri.host(), uri.scheme()); asio::connect(socket->lowest_layer(), resolver.resolve(query)); socket->lowest_layer().set_option(tcp::no_delay(true)); }
bool uri::operator == (const uri &other) const { // Each individual URI component must be decoded before performing comparison. // TFS # 375865 if (this->is_empty() && other.is_empty()) { return true; } else if (this->is_empty() || other.is_empty()) { return false; } else if (this->scheme() != other.scheme()) { // scheme is canonicalized to lowercase return false; } else if(uri::decode(this->user_info()) != uri::decode(other.user_info())) { return false; } else if (uri::decode(this->host()) != uri::decode(other.host())) { // host is canonicalized to lowercase return false; } else if (this->port() != other.port()) { return false; } else if (uri::decode(this->path()) != uri::decode(other.path())) { return false; } else if (uri::decode(this->query()) != uri::decode(other.query())) { return false; } else if (uri::decode(this->fragment()) != uri::decode(other.fragment())) { return false; } return true; }
client connect(const uri& uri) { static asio::io_service service; if (uri.scheme() == "http") { auto socket = std::make_unique<http_socket_t>(service); connect(uri, socket); return std::make_shared<client_impl<http_socket_t>>(std::move(socket), uri.host()); } else { ssl::context context(ssl::context::sslv23_client); context.set_default_verify_paths(); auto socket = std::make_unique<https_socket_t>(service, context); connect(uri, socket); socket->set_verify_mode(ssl::verify_peer); socket->set_verify_callback(ssl::rfc2818_verification(uri.host())); socket->handshake(ssl::stream_base::client); return std::make_shared<client_impl<https_socket_t>>(std::move(socket), uri.host()); } }
const std::string abc_expected = "A:B/C"; const std::string abc_actual = net::uri_decode("A%3aB%2FC"); EXPECT_EQ(abc_expected, abc_actual); const std::string pfi_expected = "ピーFI"; const std::string pfi_actual = net::uri_decode("%E3%83%94%E3%83%BcFI"); EXPECT_TRUE(equal_percent_encoded_string(pfi_expected, pfi_actual)); } TEST(uri, class) { using pfi::network::uri; uri pfi("http://preferred.jp/product/sedue/"); EXPECT_EQ("http", pfi.scheme()); EXPECT_EQ("preferred.jp", pfi.authority()); EXPECT_TRUE(pfi.userinfo().empty()); EXPECT_EQ("preferred.jp", pfi.host()); EXPECT_TRUE(pfi.port().empty()); EXPECT_EQ("/product/sedue/", pfi.path()); EXPECT_TRUE(pfi.query().empty()); EXPECT_TRUE(pfi.fragment().empty()); uri complex("http://*****:*****@a.com:80/aoeu/htns?q=1234&r=5678#n42"); EXPECT_EQ("http", complex.scheme()); EXPECT_EQ("user:[email protected]:80", complex.authority()); EXPECT_EQ("user:pass", complex.userinfo()); EXPECT_EQ("a.com", complex.host()); EXPECT_EQ("80", complex.port()); EXPECT_EQ("/aoeu/htns", complex.path());
inline std::string scheme(const uri &uri_) { return uri_.scheme(); }