Skip to content
Permalink
Browse files
[NO ISSUE][MISC] Avoid URI.getHost/Port where hostnames may contain u…
…nderscore

Change-Id: Iff58db84cc4d11609e7d7c459003a8a50f058ac6
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/11305
Reviewed-by: Michael Blow <mblow@apache.org>
Reviewed-by: Ian Maxon <imaxon@uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
  • Loading branch information
mblow committed May 4, 2021
1 parent 404e382 commit d28f9addb8d4e0ea80bb3c88f828aa60d5e3783f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
@@ -33,12 +33,14 @@
import org.apache.asterix.common.api.INcApplicationContext;
import org.apache.asterix.common.library.ILibraryManager;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.client.utils.URIUtils;
import org.apache.hyracks.algebricks.common.utils.Pair;
import org.apache.hyracks.api.control.CcId;
import org.apache.hyracks.api.exceptions.ErrorCode;
import org.apache.hyracks.api.exceptions.HyracksDataException;
import org.apache.hyracks.api.io.FileReference;
import org.apache.hyracks.api.service.IControllerService;
import org.apache.hyracks.util.NetworkUtil;
import org.apache.hyracks.util.file.FileUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -51,7 +53,7 @@ public class RetrieveLibrariesTask implements INCLifecycleTask {

public RetrieveLibrariesTask(List<Pair<URI, String>> nodes) {
this.nodes = nodes;
if (nodes.size() <= 0) {
if (nodes.isEmpty()) {
throw new IllegalArgumentException("No nodes specified to retrieve from");
}
}
@@ -62,7 +64,8 @@ public void perform(CcId ccId, IControllerService cs) throws HyracksDataExceptio
boolean success = false;
for (Pair<URI, String> referenceNode : nodes) {
try {
LOGGER.info("Retrieving UDFs from " + referenceNode.getFirst().getHost());
LOGGER.info("Retrieving UDFs from "
+ NetworkUtil.toHostPort(URIUtils.extractHost(referenceNode.getFirst())));
retrieveLibrary(referenceNode.getFirst(), referenceNode.getSecond(), appContext);
success = true;
break;
@@ -33,6 +33,7 @@
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
@@ -78,7 +79,7 @@ public void uninstall(URI path, Pair<String, String> credentials) throws IOExcep

private HttpClientContext createHttpClientContext(URI path, Pair<String, String> credentials) {
HttpClientContext hcCtx = HttpClientContext.create();
HttpHost h = new HttpHost(path.getHost(), path.getPort(), "http");
HttpHost h = URIUtils.extractHost(path);
CredentialsProvider cp = new BasicCredentialsProvider();
cp.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(credentials.first, credentials.second));
hcCtx.setCredentialsProvider(cp);
@@ -106,7 +106,6 @@
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.mutable.MutableInt;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
@@ -119,6 +118,7 @@
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
@@ -207,10 +207,7 @@ public class TestExecutor {
private static final int MAX_NON_UTF_8_STATEMENT_SIZE = 64 * 1024;
private static final ContentType TEXT_PLAIN_UTF8 = ContentType.create(HttpUtil.ContentType.APPLICATION_JSON, UTF_8);

private final IPollTask plainExecutor = (testCaseCtx, ctx, variableCtx, statement, isDmlRecoveryTest, pb, cUnit,
queryCount, expectedResultFileCtxs, testFile, actualPath, expectedWarnings) -> executeTestFile(testCaseCtx,
ctx, variableCtx, statement, isDmlRecoveryTest, pb, cUnit, queryCount, expectedResultFileCtxs,
testFile, actualPath, expectedWarnings);
private final IPollTask plainExecutor = this::executeTestFile;

public static final String DELIVERY_ASYNC = "async";
public static final String DELIVERY_DEFERRED = "deferred";
@@ -675,7 +672,7 @@ private HttpResponse executeBasicAuthHttpRequest(HttpUriRequest method, Pair<Str
cp.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(credentials.first, credentials.second));
HttpClientContext hcCtx = HttpClientContext.create();
AuthCache ac = new BasicAuthCache();
ac.put(new HttpHost(method.getURI().getHost(), method.getURI().getPort(), "http"), new BasicScheme());
ac.put(URIUtils.extractHost(method.getURI()), new BasicScheme());
hcCtx.setAuthCache(ac);
CloseableHttpClient client = HttpClients.custom().setRetryHandler(StandardHttpRequestRetryHandler.INSTANCE)
.setDefaultCredentialsProvider(cp).build();
@@ -110,6 +110,28 @@ public static String toHostPort(InetSocketAddress address) {
return address != null ? toHostPort(address.getHostString(), address.getPort()) : null;
}

public static String toHostPort(HttpHost httpHost) {
if (httpHost == null) {
return null;
}
int port = httpHost.getPort();
if (port == -1) {
port = "https".equalsIgnoreCase(httpHost.getSchemeName()) ? 443 : 80;
}
return toHostPort(httpHost.getHostName(), port);
}

public static String toHostPort(HttpHost httpHost, int defaultPort) {
if (httpHost == null) {
return null;
}
int port = httpHost.getPort();
if (port == -1) {
port = defaultPort;
}
return toHostPort(httpHost.getHostName(), port);
}

public static InetSocketAddress parseInetSocketAddress(String hostPortString) {
int lastColon = hostPortString.lastIndexOf(':');
String host = decodeIPv6LiteralHost(lastColon < 0 ? hostPortString : hostPortString.substring(0, lastColon));
@@ -18,6 +18,9 @@
*/
package org.apache.hyracks.util;

import java.net.URI;

import org.apache.http.client.utils.URIUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Assert;
@@ -38,5 +41,13 @@ public void testDefaultPort() {
Assert.assertEquals("localhost.localdomain.local:1234",
NetworkUtil.defaultPort("localhost.localdomain.local:1234", 9999));

Assert.assertEquals("[::1]:1234",
NetworkUtil.toHostPort(URIUtils.extractHost(URI.create("http://[::1]:1234"))));
Assert.assertEquals("[::1]:80", NetworkUtil.toHostPort(URIUtils.extractHost(URI.create("http://[::1]"))));
Assert.assertEquals("[::1]:443", NetworkUtil.toHostPort(URIUtils.extractHost(URI.create("https://[::1]"))));
Assert.assertEquals("[::1]:1234",
NetworkUtil.toHostPort(URIUtils.extractHost(URI.create("https://[::1]:1234")), 18091));
Assert.assertEquals("[::1]:8091",
NetworkUtil.toHostPort(URIUtils.extractHost(URI.create("http://[::1]")), 8091));
}
}

0 comments on commit d28f9ad

Please sign in to comment.