Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cpp/src/arrow/flight/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,8 @@ class FlightClient::FlightClientImpl {
std::stringstream grpc_uri;
std::shared_ptr<grpc::ChannelCredentials> creds;
if (scheme == kSchemeGrpc || scheme == kSchemeGrpcTcp || scheme == kSchemeGrpcTls) {
grpc_uri << location.uri_->host() << ":" << location.uri_->port_text();
grpc_uri << arrow::internal::UriEncodeHost(location.uri_->host()) << ':'
<< location.uri_->port_text();

if (scheme == kSchemeGrpcTls) {
if (options.disable_server_verification) {
Expand Down
17 changes: 17 additions & 0 deletions cpp/src/arrow/flight/flight_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,23 @@ TEST(TestFlight, GetPort) {
ASSERT_GT(server->port(), 0);
}

// CI environments don't have an IPv6 interface configured
TEST(TestFlight, DISABLED_IpV6Port) {
Location location, location2;
std::unique_ptr<FlightServerBase> server = ExampleTestServer();

ASSERT_OK(Location::ForGrpcTcp("[::1]", 0, &location));
FlightServerOptions options(location);
ASSERT_OK(server->Init(options));
ASSERT_GT(server->port(), 0);

ASSERT_OK(Location::ForGrpcTcp("[::1]", server->port(), &location2));
std::unique_ptr<FlightClient> client;
ASSERT_OK(FlightClient::Connect(location2, &client));
std::unique_ptr<FlightListing> listing;
ASSERT_OK(client->ListFlights(&listing));
}

TEST(TestFlight, BuilderHook) {
Location location;
std::unique_ptr<FlightServerBase> server = ExampleTestServer();
Expand Down
3 changes: 2 additions & 1 deletion cpp/src/arrow/flight/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,8 @@ Status FlightServerBase::Init(const FlightServerOptions& options) {
const std::string scheme = location.scheme();
if (scheme == kSchemeGrpc || scheme == kSchemeGrpcTcp || scheme == kSchemeGrpcTls) {
std::stringstream address;
address << location.uri_->host() << ':' << location.uri_->port_text();
address << arrow::internal::UriEncodeHost(location.uri_->host()) << ':'
<< location.uri_->port_text();

std::shared_ptr<grpc::ServerCredentials> creds;
if (scheme == kSchemeGrpcTls) {
Expand Down
13 changes: 13 additions & 0 deletions cpp/src/arrow/util/uri.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ std::string UriEscape(const std::string& s) {
return escaped;
}

std::string UriEncodeHost(const std::string& host) {
// Fairly naive check: if it contains a ':', it's IPv6 and needs
// brackets, else it's OK
if (host.find(":") != std::string::npos) {
std::string result = "[";
result += host;
result += ']';
return result;
} else {
return host;
}
}

struct Uri::Impl {
Impl() : string_rep_(""), port_(-1) { memset(&uri_, 0, sizeof(uri_)); }

Expand Down
5 changes: 5 additions & 0 deletions cpp/src/arrow/util/uri.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,10 @@ class ARROW_EXPORT Uri {
ARROW_EXPORT
std::string UriEscape(const std::string& s);

/// Encode a host for use within a URI, such as "localhost",
/// "127.0.0.1", or "[::1]".
ARROW_EXPORT
std::string UriEncodeHost(const std::string& host);

} // namespace internal
} // namespace arrow
6 changes: 6 additions & 0 deletions cpp/src/arrow/util/uri_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ TEST(UriEscape, Basics) {
ASSERT_EQ(UriEscape("/El Niño/"), "%2FEl%20Ni%C3%B1o%2F");
}

TEST(UriEncodeHost, Basics) {
ASSERT_EQ(UriEncodeHost("::1"), "[::1]");
ASSERT_EQ(UriEscape("arrow.apache.org"), "arrow.apache.org");
ASSERT_EQ(UriEscape("192.168.1.1"), "192.168.1.1");
}

TEST(Uri, Empty) {
Uri uri;
ASSERT_EQ(uri.scheme(), "");
Expand Down