|
@@ -54,6 +54,7 @@ import static org.elasticsearch.http.HttpTransportSettings.SETTING_CORS_ALLOW_HE
|
|
|
import static org.elasticsearch.http.HttpTransportSettings.SETTING_CORS_ALLOW_METHODS;
|
|
|
import static org.elasticsearch.http.HttpTransportSettings.SETTING_CORS_ALLOW_ORIGIN;
|
|
|
import static org.elasticsearch.http.HttpTransportSettings.SETTING_CORS_ENABLED;
|
|
|
+import static org.elasticsearch.http.HttpTransportSettings.SETTING_CORS_EXPOSE_HEADERS;
|
|
|
import static org.elasticsearch.http.HttpTransportSettings.SETTING_CORS_MAX_AGE;
|
|
|
|
|
|
/**
|
|
@@ -77,6 +78,7 @@ public class CorsHandler {
|
|
|
public static final String ACCESS_CONTROL_ALLOW_METHODS = "access-control-allow-methods";
|
|
|
public static final String ACCESS_CONTROL_ALLOW_ORIGIN = "access-control-allow-origin";
|
|
|
public static final String ACCESS_CONTROL_MAX_AGE = "access-control-max-age";
|
|
|
+ public static final String ACCESS_CONTROL_EXPOSE_HEADERS = "access-control-expose-headers";
|
|
|
|
|
|
private static final Pattern SCHEME_PATTERN = Pattern.compile("^https?://");
|
|
|
private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss O", Locale.ENGLISH);
|
|
@@ -105,6 +107,7 @@ public class CorsHandler {
|
|
|
}
|
|
|
if (setOrigin(httpRequest, httpResponse)) {
|
|
|
setAllowCredentials(httpResponse);
|
|
|
+ setExposeHeaders(httpResponse);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -228,6 +231,12 @@ public class CorsHandler {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void setExposeHeaders(final HttpResponse response) {
|
|
|
+ for (String header : config.accessControlExposeHeaders) {
|
|
|
+ response.addHeader(ACCESS_CONTROL_EXPOSE_HEADERS, header);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private void setAllowCredentials(final HttpResponse response) {
|
|
|
if (config.isCredentialsAllowed()) {
|
|
|
response.addHeader(ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
|
|
@@ -247,6 +256,7 @@ public class CorsHandler {
|
|
|
private final boolean credentialsAllowed;
|
|
|
private final Set<RestRequest.Method> allowedRequestMethods;
|
|
|
private final Set<String> allowedRequestHeaders;
|
|
|
+ private final Set<String> accessControlExposeHeaders;
|
|
|
private final long maxAge;
|
|
|
|
|
|
public Config(Builder builder) {
|
|
@@ -257,6 +267,7 @@ public class CorsHandler {
|
|
|
this.credentialsAllowed = builder.allowCredentials;
|
|
|
this.allowedRequestMethods = Collections.unmodifiableSet(builder.requestMethods);
|
|
|
this.allowedRequestHeaders = Collections.unmodifiableSet(builder.requestHeaders);
|
|
|
+ this.accessControlExposeHeaders = Collections.unmodifiableSet(builder.accessControlExposeHeaders);
|
|
|
this.maxAge = builder.maxAge;
|
|
|
}
|
|
|
|
|
@@ -314,6 +325,8 @@ public class CorsHandler {
|
|
|
+ allowedRequestMethods
|
|
|
+ ", allowedRequestHeaders="
|
|
|
+ allowedRequestHeaders
|
|
|
+ + ", accessControlExposeHeaders="
|
|
|
+ + accessControlExposeHeaders
|
|
|
+ ", maxAge="
|
|
|
+ maxAge
|
|
|
+ '}';
|
|
@@ -329,6 +342,7 @@ public class CorsHandler {
|
|
|
long maxAge;
|
|
|
private final Set<RestRequest.Method> requestMethods = new HashSet<>();
|
|
|
private final Set<String> requestHeaders = new HashSet<>();
|
|
|
+ private final Set<String> accessControlExposeHeaders = new HashSet<>();
|
|
|
|
|
|
private Builder() {
|
|
|
anyOrigin = true;
|
|
@@ -380,6 +394,11 @@ public class CorsHandler {
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+ public Builder accessControlExposeHeaders(String[] headers) {
|
|
|
+ accessControlExposeHeaders.addAll(Arrays.asList(headers));
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
public Config build() {
|
|
|
return new Config(this);
|
|
|
}
|
|
@@ -427,6 +446,7 @@ public class CorsHandler {
|
|
|
Config config = builder.allowedRequestMethods(methods)
|
|
|
.maxAge(SETTING_CORS_MAX_AGE.get(settings))
|
|
|
.allowedRequestHeaders(Strings.tokenizeToStringArray(SETTING_CORS_ALLOW_HEADERS.get(settings), ","))
|
|
|
+ .accessControlExposeHeaders(Strings.tokenizeToStringArray(SETTING_CORS_EXPOSE_HEADERS.get(settings), ","))
|
|
|
.build();
|
|
|
return config;
|
|
|
}
|