|
|
@@ -43,6 +43,7 @@ import org.elasticsearch.common.util.MockBigArrays;
|
|
|
import org.elasticsearch.common.util.MockPageCacheRecycler;
|
|
|
import org.elasticsearch.common.util.concurrent.ThreadContext;
|
|
|
import org.elasticsearch.http.BindHttpException;
|
|
|
+import org.elasticsearch.http.CorsHandler;
|
|
|
import org.elasticsearch.http.HttpServerTransport;
|
|
|
import org.elasticsearch.http.HttpTransportSettings;
|
|
|
import org.elasticsearch.http.NullDispatcher;
|
|
|
@@ -66,6 +67,8 @@ import java.util.concurrent.CountDownLatch;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
|
|
+import static org.elasticsearch.http.HttpTransportSettings.SETTING_CORS_ALLOW_ORIGIN;
|
|
|
+import static org.elasticsearch.http.HttpTransportSettings.SETTING_CORS_ENABLED;
|
|
|
import static org.elasticsearch.rest.RestStatus.BAD_REQUEST;
|
|
|
import static org.elasticsearch.rest.RestStatus.OK;
|
|
|
import static org.hamcrest.Matchers.containsString;
|
|
|
@@ -159,13 +162,13 @@ public class NioHttpServerTransportTests extends ESTestCase {
|
|
|
request.headers().set(HttpHeaderNames.EXPECT, expectation);
|
|
|
HttpUtil.setContentLength(request, contentLength);
|
|
|
|
|
|
- final FullHttpResponse response = client.post(remoteAddress.address(), request);
|
|
|
+ final FullHttpResponse response = client.send(remoteAddress.address(), request);
|
|
|
try {
|
|
|
assertThat(response.status(), equalTo(expectedStatus));
|
|
|
if (expectedStatus.equals(HttpResponseStatus.CONTINUE)) {
|
|
|
final FullHttpRequest continuationRequest =
|
|
|
new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", Unpooled.EMPTY_BUFFER);
|
|
|
- final FullHttpResponse continuationResponse = client.post(remoteAddress.address(), continuationRequest);
|
|
|
+ final FullHttpResponse continuationResponse = client.send(remoteAddress.address(), continuationRequest);
|
|
|
try {
|
|
|
assertThat(continuationResponse.status(), is(HttpResponseStatus.OK));
|
|
|
assertThat(
|
|
|
@@ -196,6 +199,65 @@ public class NioHttpServerTransportTests extends ESTestCase {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public void testCorsRequest() throws InterruptedException {
|
|
|
+ final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void dispatchRequest(final RestRequest request, final RestChannel channel, final ThreadContext threadContext) {
|
|
|
+ throw new AssertionError();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void dispatchBadRequest(final RestRequest request,
|
|
|
+ final RestChannel channel,
|
|
|
+ final ThreadContext threadContext,
|
|
|
+ final Throwable cause) {
|
|
|
+ throw new AssertionError();
|
|
|
+ }
|
|
|
+
|
|
|
+ };
|
|
|
+
|
|
|
+ final Settings settings = Settings.builder()
|
|
|
+ .put(SETTING_CORS_ENABLED.getKey(), true)
|
|
|
+ .put(SETTING_CORS_ALLOW_ORIGIN.getKey(), "elastic.co").build();
|
|
|
+
|
|
|
+ try (NioHttpServerTransport transport = new NioHttpServerTransport(settings, networkService, bigArrays, pageRecycler,
|
|
|
+ threadPool, xContentRegistry(), dispatcher, new NioGroupFactory(settings, logger))) {
|
|
|
+ transport.start();
|
|
|
+ final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses());
|
|
|
+
|
|
|
+ // Test pre-flight request
|
|
|
+ try (NioHttpClient client = new NioHttpClient()) {
|
|
|
+ final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.OPTIONS, "/");
|
|
|
+ request.headers().add(CorsHandler.ORIGIN, "elastic.co");
|
|
|
+ request.headers().add(CorsHandler.ACCESS_CONTROL_REQUEST_METHOD, "POST");
|
|
|
+
|
|
|
+ final FullHttpResponse response = client.send(remoteAddress.address(), request);
|
|
|
+ try {
|
|
|
+ assertThat(response.status(), equalTo(HttpResponseStatus.OK));
|
|
|
+ assertThat(response.headers().get(CorsHandler.ACCESS_CONTROL_ALLOW_ORIGIN), equalTo("elastic.co"));
|
|
|
+ assertThat(response.headers().get(CorsHandler.VARY), equalTo(CorsHandler.ORIGIN));
|
|
|
+ assertTrue(response.headers().contains(CorsHandler.DATE));
|
|
|
+ } finally {
|
|
|
+ response.release();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Test short-circuited request
|
|
|
+ try (NioHttpClient client = new NioHttpClient()) {
|
|
|
+ final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
|
|
|
+ request.headers().add(CorsHandler.ORIGIN, "elastic2.co");
|
|
|
+
|
|
|
+ final FullHttpResponse response = client.send(remoteAddress.address(), request);
|
|
|
+ try {
|
|
|
+ assertThat(response.status(), equalTo(HttpResponseStatus.FORBIDDEN));
|
|
|
+ } finally {
|
|
|
+ response.release();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public void testBadRequest() throws InterruptedException {
|
|
|
final AtomicReference<Throwable> causeReference = new AtomicReference<>();
|
|
|
final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() {
|
|
|
@@ -241,7 +303,7 @@ public class NioHttpServerTransportTests extends ESTestCase {
|
|
|
final String url = "/" + new String(new byte[maxInitialLineLength], Charset.forName("UTF-8"));
|
|
|
final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, url);
|
|
|
|
|
|
- final FullHttpResponse response = client.post(remoteAddress.address(), request);
|
|
|
+ final FullHttpResponse response = client.send(remoteAddress.address(), request);
|
|
|
try {
|
|
|
assertThat(response.status(), equalTo(HttpResponseStatus.BAD_REQUEST));
|
|
|
assertThat(
|