Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ protected void addUserAgentHeader(HttpRequest request) {
request.headers().set(HttpHeaderNames.USER_AGENT, USER_AGENT_VALUE);
}

protected void addAcceptHeaders(HttpRequest request) {
if (request.headers().get(HttpHeaderNames.ACCEPT) == null) {
request.headers().add(HttpHeaderNames.ACCEPT, "*/*");
}
Comment on lines +96 to +98

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The new addAcceptHeaders method changes the behavior from unconditionally setting the Accept header (using set) to conditionally adding it only if it's not already present. While this doesn't alter functionality for the current call sites, it introduces a subtle semantic change for a protected method. To maintain the original, simpler semantics and avoid potential confusion for future developers using this method, it's recommended to use set unconditionally. This makes the method's behavior more explicit: it ensures the Accept header is set to */*.

    request.headers().set(HttpHeaderNames.ACCEPT, "*/*");

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding set vs add: in this context both behave the same due to the surrounding if check, so the choice is largely stylistic. The method conditionally adds a default header, and using add matches the existing add* methods in the request-building chain.

Using set here could also be read as enforcing a value, even though the guard already prevents overwriting. A conditional setAccept(...) would feel a bit odd, whereas an add* method that conditionally calls .add() aligns better with the intent and naming.

That said, I’m happy to switch to set if that’s preferred for consistency or readability.

}

protected String constructPath(URI uri, String hash, boolean isCas) {
StringBuilder builder = new StringBuilder();
builder.append(uri.getPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
addCredentialHeaders(request, cmd.uri());
addExtraRemoteHeaders(request);
addUserAgentHeader(request);
addAcceptHeaders(request);
ctx.writeAndFlush(request)
.addListener(
(f) -> {
Expand All @@ -186,7 +187,6 @@ private HttpRequest buildRequest(String path, String host) {
new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path);
httpRequest.headers().set(HttpHeaderNames.HOST, host);
httpRequest.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
httpRequest.headers().set(HttpHeaderNames.ACCEPT, "*/*");
httpRequest.headers().set(HttpHeaderNames.ACCEPT_ENCODING, ACCEPT_ENCODING);
return httpRequest;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
addCredentialHeaders(request, cmd.uri());
addExtraRemoteHeaders(request);
addUserAgentHeader(request);
addAcceptHeaders(request);
HttpChunkedInput body = buildBody(cmd);
ctx.writeAndFlush(request)
.addListener(
Expand Down Expand Up @@ -136,7 +137,6 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable t) {
private HttpRequest buildRequest(String path, String host, long contentLength) {
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PUT, path);
request.headers().set(HttpHeaderNames.HOST, host);
request.headers().set(HttpHeaderNames.ACCEPT, "*/*");
request.headers().set(HttpHeaderNames.CONTENT_LENGTH, contentLength);
request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
return request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,30 @@ public void extraHeadersAreIncluded() throws Exception {
HttpRequest request = ch.readOutbound();
assertThat(request.headers().get("key1")).isEqualTo("value1");
assertThat(request.headers().get("key2")).isEqualTo("value2");
assertThat(request.headers().get(HttpHeaderNames.ACCEPT)).isEqualTo("*/*");
}

@Test
public void extraHeadersOverridesDefaultAccept() throws Exception {
URI uri = new URI("http://does.not.exist:8080/foo");
ImmutableList<Entry<String, String>> remoteHeaders =
ImmutableList.of(
Maps.immutableEntry("key1", "value1"),
Maps.immutableEntry("key2", "value2"),
Maps.immutableEntry("Accept", "application/octet-stream")
);

EmbeddedChannel ch =
new EmbeddedChannel(new HttpDownloadHandler(/* credentials= */ null, remoteHeaders));
DownloadCommand cmd =
new DownloadCommand(uri, /* casDownload= */ true, DIGEST, new ByteArrayOutputStream());
ChannelPromise writePromise = ch.newPromise();
ch.writeOneOutbound(cmd, writePromise);

HttpRequest request = ch.readOutbound();
assertThat(request.headers().get("key1")).isEqualTo("value1");
assertThat(request.headers().get("key2")).isEqualTo("value2");
assertThat(request.headers().get(HttpHeaderNames.ACCEPT)).isEqualTo("application/octet-stream");
}

@Test
Expand Down