Skip to content
Merged
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
33 changes: 29 additions & 4 deletions src/main/java/me/itzg/helpers/http/FetchBuilderBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.netty.handler.codec.http.HttpStatusClass;
import io.netty.handler.codec.http.HttpUtil;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.time.ZoneId;
Expand Down Expand Up @@ -47,14 +48,37 @@ public class FetchBuilderBase<SELF extends FetchBuilderBase<SELF>> {
static protected class State {
private final SharedFetch sharedFetch;
private final URI uri;
private final String userInfo;
public String userAgentCommand;
private Set<String> acceptContentTypes;
private final Map<String, String> requestHeaders = new HashMap<>();

State(URI uri, SharedFetch sharedFetch) {
// Netty seems to half-way URL encode paths that have unicode,
// so instead we'll pre-"encode" the URI
this.uri = URI.create(uri.toASCIIString());
final URI encoded = URI.create(uri.toASCIIString());

if (uri.getRawUserInfo() != null) {
this.userInfo = uri.getRawUserInfo();
try {
this.uri = new URI(
encoded.getScheme(),
// just show first letter of username for sanity confirmation
encoded.getRawUserInfo().charAt(0) + "***:***",
encoded.getHost(),
encoded.getPort(),
encoded.getPath(),
encoded.getQuery(),
encoded.getFragment()
);
} catch (URISyntaxException e) {
throw new GenericException("Failed to redact user info", e);
}
}
else {
this.userInfo = null;
this.uri = encoded;
}
this.sharedFetch = sharedFetch;
}
}
Expand Down Expand Up @@ -242,12 +266,13 @@ protected void applyHeaders(io.netty.handler.codec.http.HttpHeaders headers) {
);
}

final String rawUserInfo = state.uri.getRawUserInfo();
if (rawUserInfo != null) {
if (state.userInfo != null) {
headers.set(
AUTHORIZATION.toString(),
"Basic " +
Base64.getEncoder().encodeToString(rawUserInfo.getBytes(StandardCharsets.UTF_8))
Base64.getEncoder().encodeToString(
state.userInfo.getBytes(StandardCharsets.UTF_8)
)
);
}

Expand Down