Skip to content
Merged
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
31 changes: 30 additions & 1 deletion src/main/java/me/itzg/helpers/get/GetCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.PrintWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -14,6 +15,7 @@
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -375,12 +377,26 @@ private void pruneOtherFiles(Set<Path> outputs) throws IOException {

private Path processSingleUri(URI uri, CloseableHttpClient client,
Path modifiedSinceFile, OutputResponseHandler handler) throws URISyntaxException, IOException {
final URI requestUri = uri.getPath().startsWith("//") ?
URI requestUri = uri.getPath().startsWith("//") ?
alterUriPath(uri, uri.getPath().substring(1)) : uri;

final String authHeader;
if (requestUri.getUserInfo() != null) {
authHeader = "Basic " +
Base64.getEncoder().withoutPadding()
.encodeToString(requestUri.getUserInfo().getBytes(StandardCharsets.UTF_8));
requestUri = removeUserInfo(requestUri);
}
else {
authHeader = null;
}

log.debug("Getting uri={}", requestUri);

final HttpGet request = new HttpGet(requestUri);
if (authHeader != null) {
request.addHeader(HttpHeaders.AUTHORIZATION, authHeader);
}
if (acceptContentTypes != null) {
for (String acceptContentType : acceptContentTypes) {
request.addHeader(HttpHeaders.ACCEPT, acceptContentType);
Expand Down Expand Up @@ -431,4 +447,17 @@ private static URI alterUriPath(URI uri, String path) throws URISyntaxException
);
}

private static URI removeUserInfo(URI uri) throws URISyntaxException {
return new URI(
uri.getScheme(),
null,
uri.getHost(),
uri.getPort(),
uri.getPath(),
uri.getQuery(),
uri.getFragment()
);
}


}
21 changes: 21 additions & 0 deletions src/test/java/me/itzg/helpers/get/GetCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import me.itzg.helpers.TestLoggingAppender;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -52,6 +53,26 @@ void outputsDownload() throws MalformedURLException {
assertThat(output.toString()).isEqualTo("Response content");
}

@Test
void usesBasicAuth() throws MalformedURLException, URISyntaxException {
mock.expectRequest("GET","/",
request -> request.withHeader("Authorization", "Basic dXNlcjpwYXNz"),
response()
.withBody("You're in", MediaType.TEXT_PLAIN)
);

final StringWriter output = new StringWriter();
final int status =
new CommandLine(new GetCommand())
.setOut(new PrintWriter(output))
.execute(
mock.buildMockedUrl("/", "user:pass").toString()
);

assertThat(status).isEqualTo(0);
assertThat(output.toString()).isEqualTo("You're in");
}

@Test
void handlesExtraSlashAtStartOfPath() throws MalformedURLException {
mock.expectRequest("GET","/handlesExtraSlashAtStartOfPath.txt",
Expand Down
22 changes: 19 additions & 3 deletions src/test/java/me/itzg/helpers/get/MockServerSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static org.mockserver.model.HttpRequest.request;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import org.mockserver.integration.ClientAndServer;
import org.mockserver.matchers.Times;
Expand All @@ -11,15 +13,28 @@

class MockServerSupport {

private ClientAndServer client;
private final ClientAndServer client;

public MockServerSupport(ClientAndServer client) {

this.client = client;
}

URL buildMockedUrl(String s) throws MalformedURLException {
return new URL("http", "localhost", client.getLocalPort(), s);
URL buildMockedUrl(String path) throws MalformedURLException {
return new URL("http", "localhost", client.getLocalPort(), path);
}

@SuppressWarnings("SameParameterValue")
URL buildMockedUrl(String path, String userInfo) throws URISyntaxException, MalformedURLException {
return new URI(
"http",
userInfo,
"localhost",
client.getLocalPort(),
path,
null,
null
).toURL();
}

@FunctionalInterface
Expand All @@ -31,6 +46,7 @@ void expectRequest(String method, String path, HttpResponse httpResponse) {
expectRequest(method, path, request -> request, httpResponse);
}

@SuppressWarnings("SameParameterValue")
void expectRequest(String method, String path, HttpResponse httpResponse, int responseTimes) {
expectRequest(method, path, request -> request, httpResponse, responseTimes);
}
Expand Down