Skip to content

Commit 7ac4465

Browse files
authored
Fix alias HEAD requests
Alias HEAD requests incorrectly return a content-length header of 0. This commit addresses this by removing the special handling for alias HEAD requests, and just relying on the general mechanism that exists for handling HEAD requests in the REST layer. Relates elastic#23094
1 parent 709cc9b commit 7ac4465

File tree

4 files changed

+34
-89
lines changed

4 files changed

+34
-89
lines changed

core/src/main/java/org/elasticsearch/action/ActionModule.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@
234234
import org.elasticsearch.rest.action.admin.cluster.RestRestoreSnapshotAction;
235235
import org.elasticsearch.rest.action.admin.cluster.RestSnapshotsStatusAction;
236236
import org.elasticsearch.rest.action.admin.cluster.RestVerifyRepositoryAction;
237-
import org.elasticsearch.rest.action.admin.indices.RestAliasesExistAction;
238237
import org.elasticsearch.rest.action.admin.indices.RestAnalyzeAction;
239238
import org.elasticsearch.rest.action.admin.indices.RestClearIndicesCacheAction;
240239
import org.elasticsearch.rest.action.admin.indices.RestCloseIndexAction;
@@ -535,7 +534,6 @@ public void initRestHandlers(Supplier<DiscoveryNodes> nodesInCluster) {
535534
registerHandler.accept(new RestIndicesSegmentsAction(settings, restController));
536535
registerHandler.accept(new RestIndicesShardStoresAction(settings, restController));
537536
registerHandler.accept(new RestGetAliasesAction(settings, restController));
538-
registerHandler.accept(new RestAliasesExistAction(settings, restController));
539537
registerHandler.accept(new RestIndexDeleteAliasesAction(settings, restController));
540538
registerHandler.accept(new RestIndexPutAliasAction(settings, restController));
541539
registerHandler.accept(new RestIndicesAliasesAction(settings, restController));

core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestAliasesExistAction.java

Lines changed: 0 additions & 87 deletions
This file was deleted.

core/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetAliasesAction.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.util.stream.Collectors;
4545

4646
import static org.elasticsearch.rest.RestRequest.Method.GET;
47+
import static org.elasticsearch.rest.RestRequest.Method.HEAD;
4748
import static org.elasticsearch.rest.RestStatus.OK;
4849

4950
/**
@@ -54,7 +55,9 @@ public class RestGetAliasesAction extends BaseRestHandler {
5455
public RestGetAliasesAction(final Settings settings, final RestController controller) {
5556
super(settings);
5657
controller.registerHandler(GET, "/_alias/{name}", this);
58+
controller.registerHandler(HEAD, "/_alias/{name}", this);
5759
controller.registerHandler(GET, "/{index}/_alias/{name}", this);
60+
controller.registerHandler(HEAD, "/{index}/_alias/{name}", this);
5861
}
5962

6063
@Override

modules/transport-netty4/src/test/java/org/elasticsearch/rest/Netty4HeadBodyIsEmptyIT.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.apache.http.entity.StringEntity;
2323
import org.elasticsearch.client.Response;
24+
import org.elasticsearch.common.xcontent.XContentBuilder;
2425
import org.elasticsearch.test.rest.ESRestTestCase;
2526
import org.hamcrest.Matcher;
2627

@@ -29,6 +30,7 @@
2930

3031
import static java.util.Collections.emptyMap;
3132
import static java.util.Collections.singletonMap;
33+
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
3234
import static org.hamcrest.Matchers.equalTo;
3335
import static org.hamcrest.Matchers.greaterThan;
3436

@@ -62,10 +64,39 @@ public void testTypeExists() throws IOException {
6264
headTestCase("test/test", singletonMap("pretty", "true"), equalTo(0));
6365
}
6466

67+
public void testAliasExists() throws IOException {
68+
createTestDoc();
69+
try (XContentBuilder builder = jsonBuilder()) {
70+
builder.startObject();
71+
{
72+
builder.startArray("actions");
73+
{
74+
builder.startObject();
75+
{
76+
builder.startObject("add");
77+
{
78+
builder.field("index", "test");
79+
builder.field("alias", "test_alias");
80+
}
81+
builder.endObject();
82+
}
83+
builder.endObject();
84+
}
85+
builder.endArray();
86+
}
87+
builder.endObject();
88+
89+
client().performRequest("POST", "_aliases", emptyMap(), new StringEntity(builder.string()));
90+
headTestCase("/_alias/test_alias", emptyMap(), greaterThan(0));
91+
headTestCase("/test/_alias/test_alias", emptyMap(), greaterThan(0));
92+
}
93+
}
94+
6595
private void headTestCase(String url, Map<String, String> params, Matcher<Integer> matcher) throws IOException {
6696
Response response = client().performRequest("HEAD", url, params);
6797
assertEquals(200, response.getStatusLine().getStatusCode());
6898
assertThat(Integer.valueOf(response.getHeader("Content-Length")), matcher);
6999
assertNull("HEAD requests shouldn't have a response body but " + url + " did", response.getEntity());
70100
}
101+
71102
}

0 commit comments

Comments
 (0)