Skip to content

Commit f238f87

Browse files
author
Ajay Kannan
committed
Remove scheme from localhost
1 parent 9463c55 commit f238f87

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,16 @@ public DefaultDatastoreRpc(DatastoreOptions options) {
5151
.projectId(options.projectId())
5252
.initializer(options.httpRequestInitializer());
5353
if (isLocalHost(options.host())) {
54-
clientBuilder = clientBuilder.localHost(options.host());
54+
clientBuilder = clientBuilder.localHost(removeScheme(options.host()));
5555
} else if (!options.host()
5656
.equals(com.google.datastore.v1beta3.client.DatastoreFactory.DEFAULT_HOST)) {
5757
String fullURL = options.host();
5858
if (fullURL.charAt(fullURL.length() - 1) != '/') {
5959
fullURL = fullURL + '/';
6060
}
61-
fullURL = fullURL + "datastore/v1beta3/projects/" + options.projectId();
61+
fullURL = fullURL + "datastore/"
62+
+ com.google.datastore.v1beta3.client.DatastoreFactory.VERSION + "/projects/"
63+
+ options.projectId();
6264
clientBuilder = clientBuilder.projectId(null).projectEndpoint(fullURL);
6365
}
6466
client = com.google.datastore.v1beta3.client.DatastoreFactory.get()
@@ -73,9 +75,7 @@ private static boolean isLocalHost(String host) {
7375
normalizedHost = "http://" + normalizedHost;
7476
}
7577
InetAddress hostAddr = InetAddress.getByName(new URL(normalizedHost).getHost());
76-
if (hostAddr.isAnyLocalAddress() || hostAddr.isLoopbackAddress()) {
77-
return true;
78-
}
78+
return hostAddr.isAnyLocalAddress() || hostAddr.isLoopbackAddress();
7979
} catch (UnknownHostException | MalformedURLException e) {
8080
// ignore
8181
}
@@ -87,6 +87,15 @@ private static boolean includesScheme(String url) {
8787
return url.startsWith("http://") || url.startsWith("https://");
8888
}
8989

90+
private static String removeScheme(String url) {
91+
if (url.startsWith("https://")) {
92+
return url.substring("https://".length());
93+
} else if (url.startsWith("http://")) {
94+
return url.substring("http://".length());
95+
}
96+
return url;
97+
}
98+
9099
private static DatastoreRpcException translate(
91100
com.google.datastore.v1beta3.client.DatastoreException exception) {
92101
String reasonStr = "";

0 commit comments

Comments
 (0)