diff --git a/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/cors/Netty4CorsConfig.java b/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/cors/Netty4CorsConfig.java index 9c81c07e66314..939d5540ecfdf 100644 --- a/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/cors/Netty4CorsConfig.java +++ b/modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/cors/Netty4CorsConfig.java @@ -76,7 +76,8 @@ public boolean isCorsSupportEnabled() { } /** - * Determines whether a wildcard origin, '*', is supported. + * Determines whether a wildcard origin, '*', is supported. This also means that null origins are + * supported. * * @return {@code boolean} true if any origin is allowed. */ @@ -121,21 +122,21 @@ public boolean isNullOriginAllowed() { } /** - * Determines if cookies are supported for CORS requests. + * Determines if credentials are supported for CORS requests. * - * By default cookies are not included in CORS requests but if isCredentialsAllowed returns - * true cookies will be added to CORS requests. Setting this value to true will set the + * By default credentials are not included in CORS requests but if isCredentialsAllowed returns + * true credentials will be added to CORS requests. Setting this value to true will set the * CORS 'Access-Control-Allow-Credentials' response header to true. * - * Please note that cookie support needs to be enabled on the client side as well. - * The client needs to opt-in to send cookies by calling: + * Please note that credentials support needs to be enabled on the client side as well. + * The client needs to opt-in to send credentials by calling: *
* xhr.withCredentials = true;
*
- * The default value for 'withCredentials' is false in which case no cookies are sent.
- * Setting this to true will included cookies in cross origin requests.
+ * The default value for 'withCredentials' is false in which case no credentials are sent.
+ * Setting this to true will included credentials in cross origin requests.
*
- * @return {@code true} if cookies are supported.
+ * @return {@code true} if credentials are supported.
*/
public boolean isCredentialsAllowed() {
return allowCredentials;
diff --git a/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/HttpReadWriteHandler.java b/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/HttpReadWriteHandler.java
index 681736a311db5..49e560363089b 100644
--- a/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/HttpReadWriteHandler.java
+++ b/plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/HttpReadWriteHandler.java
@@ -36,6 +36,8 @@
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.http.HttpHandlingSettings;
import org.elasticsearch.http.HttpPipelinedRequest;
+import org.elasticsearch.http.nio.cors.NioCorsConfig;
+import org.elasticsearch.http.nio.cors.NioCorsHandler;
import org.elasticsearch.nio.FlushOperation;
import org.elasticsearch.nio.InboundChannelBuffer;
import org.elasticsearch.nio.NioSocketChannel;
@@ -50,6 +52,8 @@
import java.util.List;
import java.util.function.BiConsumer;
+import static org.elasticsearch.http.HttpTransportSettings.SETTING_CORS_ENABLED;
+
public class HttpReadWriteHandler implements ReadWriteHandler {
private final NettyAdaptor adaptor;
@@ -57,14 +61,16 @@ public class HttpReadWriteHandler implements ReadWriteHandler {
private final NioHttpServerTransport transport;
private final HttpHandlingSettings settings;
private final NamedXContentRegistry xContentRegistry;
+ private final NioCorsConfig corsConfig;
private final ThreadContext threadContext;
HttpReadWriteHandler(NioSocketChannel nioChannel, NioHttpServerTransport transport, HttpHandlingSettings settings,
- NamedXContentRegistry xContentRegistry, ThreadContext threadContext) {
+ NamedXContentRegistry xContentRegistry, NioCorsConfig corsConfig, ThreadContext threadContext) {
this.nioChannel = nioChannel;
this.transport = transport;
this.settings = settings;
this.xContentRegistry = xContentRegistry;
+ this.corsConfig = corsConfig;
this.threadContext = threadContext;
List+ * xhr.withCredentials = true; + *+ * The default value for 'withCredentials' is false in which case no credentials are sent. + * Setting this to true will included cookies in cross origin requests. + * + * @return {@code true} if credentials are supported. + */ + public boolean isCredentialsAllowed() { + return allowCredentials; + } + + /** + * Gets the maxAge setting. + * + * When making a preflight request the client has to perform two request with can be inefficient. + * This setting will set the CORS 'Access-Control-Max-Age' response header and enables the + * caching of the preflight response for the specified time. During this time no preflight + * request will be made. + * + * @return {@code long} the time in seconds that a preflight request may be cached. + */ + public long maxAge() { + return maxAge; + } + + /** + * Returns the allowed set of Request Methods. The Http methods that should be returned in the + * CORS 'Access-Control-Request-Method' response header. + * + * @return {@code Set} of {@link HttpMethod}s that represent the allowed Request Methods. + */ + public Set
+ * xhr.withCredentials = true; + *+ * The default value for 'withCredentials' is false in which case no cookies are sent. + * Setting this to true will included cookies in cross origin requests. + * + * @return {@link NioCorsConfigBuilder} to support method chaining. + */ + public NioCorsConfigBuilder allowCredentials() { + allowCredentials = true; + return this; + } + + /** + * When making a preflight request the client has to perform two request with can be inefficient. + * This setting will set the CORS 'Access-Control-Max-Age' response header and enables the + * caching of the preflight response for the specified time. During this time no preflight + * request will be made. + * + * @param max the maximum time, in seconds, that the preflight response may be cached. + * @return {@link NioCorsConfigBuilder} to support method chaining. + */ + public NioCorsConfigBuilder maxAge(final long max) { + maxAge = max; + return this; + } + + /** + * Specifies the allowed set of HTTP Request Methods that should be returned in the + * CORS 'Access-Control-Request-Method' response header. + * + * @param methods the {@link HttpMethod}s that should be allowed. + * @return {@link NioCorsConfigBuilder} to support method chaining. + */ + public NioCorsConfigBuilder allowedRequestMethods(final HttpMethod... methods) { + requestMethods.addAll(Arrays.asList(methods)); + return this; + } + + /** + * Specifies the if headers that should be returned in the CORS 'Access-Control-Allow-Headers' + * response header. + * + * If a client specifies headers on the request, for example by calling: + *
+ * xhr.setRequestHeader('My-Custom-Header', "SomeValue");
+ *
+ * the server will receive the above header name in the 'Access-Control-Request-Headers' of the
+ * preflight request. The server will then decide if it allows this header to be sent for the
+ * real request (remember that a preflight is not the real request but a request asking the server
+ * if it allow a request).
+ *
+ * @param headers the headers to be added to the preflight 'Access-Control-Allow-Headers' response header.
+ * @return {@link NioCorsConfigBuilder} to support method chaining.
+ */
+ public NioCorsConfigBuilder allowedRequestHeaders(final String... headers) {
+ requestHeaders.addAll(Arrays.asList(headers));
+ return this;
+ }
+
+ /**
+ * Returns HTTP response headers that should be added to a CORS preflight response.
+ *
+ * An intermediary like a load balancer might require that a CORS preflight request
+ * have certain headers set. This enables such headers to be added.
+ *
+ * @param name the name of the HTTP header.
+ * @param values the values for the HTTP header.
+ * @return {@link NioCorsConfigBuilder} to support method chaining.
+ */
+ public NioCorsConfigBuilder preflightResponseHeader(final CharSequence name, final Object... values) {
+ if (values.length == 1) {
+ preflightHeaders.put(name, new ConstantValueGenerator(values[0]));
+ } else {
+ preflightResponseHeader(name, Arrays.asList(values));
+ }
+ return this;
+ }
+
+ /**
+ * Returns HTTP response headers that should be added to a CORS preflight response.
+ *
+ * An intermediary like a load balancer might require that a CORS preflight request
+ * have certain headers set. This enables such headers to be added.
+ *
+ * @param name the name of the HTTP header.
+ * @param value the values for the HTTP header.
+ * @param