54
54
import static org .elasticsearch .http .HttpTransportSettings .SETTING_CORS_ALLOW_METHODS ;
55
55
import static org .elasticsearch .http .HttpTransportSettings .SETTING_CORS_ALLOW_ORIGIN ;
56
56
import static org .elasticsearch .http .HttpTransportSettings .SETTING_CORS_ENABLED ;
57
+ import static org .elasticsearch .http .HttpTransportSettings .SETTING_CORS_EXPOSE_HEADERS ;
57
58
import static org .elasticsearch .http .HttpTransportSettings .SETTING_CORS_MAX_AGE ;
58
59
59
60
/**
@@ -77,6 +78,7 @@ public class CorsHandler {
77
78
public static final String ACCESS_CONTROL_ALLOW_METHODS = "access-control-allow-methods" ;
78
79
public static final String ACCESS_CONTROL_ALLOW_ORIGIN = "access-control-allow-origin" ;
79
80
public static final String ACCESS_CONTROL_MAX_AGE = "access-control-max-age" ;
81
+ public static final String ACCESS_CONTROL_EXPOSE_HEADERS = "access-control-expose-headers" ;
80
82
81
83
private static final Pattern SCHEME_PATTERN = Pattern .compile ("^https?://" );
82
84
private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter .ofPattern ("EEE, dd MMM yyyy HH:mm:ss O" , Locale .ENGLISH );
@@ -105,6 +107,7 @@ public void setCorsResponseHeaders(final HttpRequest httpRequest, final HttpResp
105
107
}
106
108
if (setOrigin (httpRequest , httpResponse )) {
107
109
setAllowCredentials (httpResponse );
110
+ setExposeHeaders (httpResponse );
108
111
}
109
112
}
110
113
@@ -228,6 +231,12 @@ private void setAllowHeaders(final HttpResponse response) {
228
231
}
229
232
}
230
233
234
+ private void setExposeHeaders (final HttpResponse response ) {
235
+ for (String header : config .accessControlExposeHeaders ) {
236
+ response .addHeader (ACCESS_CONTROL_EXPOSE_HEADERS , header );
237
+ }
238
+ }
239
+
231
240
private void setAllowCredentials (final HttpResponse response ) {
232
241
if (config .isCredentialsAllowed ()) {
233
242
response .addHeader (ACCESS_CONTROL_ALLOW_CREDENTIALS , "true" );
@@ -247,6 +256,7 @@ public static class Config {
247
256
private final boolean credentialsAllowed ;
248
257
private final Set <RestRequest .Method > allowedRequestMethods ;
249
258
private final Set <String > allowedRequestHeaders ;
259
+ private final Set <String > accessControlExposeHeaders ;
250
260
private final long maxAge ;
251
261
252
262
public Config (Builder builder ) {
@@ -257,6 +267,7 @@ public Config(Builder builder) {
257
267
this .credentialsAllowed = builder .allowCredentials ;
258
268
this .allowedRequestMethods = Collections .unmodifiableSet (builder .requestMethods );
259
269
this .allowedRequestHeaders = Collections .unmodifiableSet (builder .requestHeaders );
270
+ this .accessControlExposeHeaders = Collections .unmodifiableSet (builder .accessControlExposeHeaders );
260
271
this .maxAge = builder .maxAge ;
261
272
}
262
273
@@ -314,6 +325,8 @@ public String toString() {
314
325
+ allowedRequestMethods
315
326
+ ", allowedRequestHeaders="
316
327
+ allowedRequestHeaders
328
+ + ", accessControlExposeHeaders="
329
+ + accessControlExposeHeaders
317
330
+ ", maxAge="
318
331
+ maxAge
319
332
+ '}' ;
@@ -329,6 +342,7 @@ private static class Builder {
329
342
long maxAge ;
330
343
private final Set <RestRequest .Method > requestMethods = new HashSet <>();
331
344
private final Set <String > requestHeaders = new HashSet <>();
345
+ private final Set <String > accessControlExposeHeaders = new HashSet <>();
332
346
333
347
private Builder () {
334
348
anyOrigin = true ;
@@ -380,6 +394,11 @@ public Builder allowedRequestHeaders(String[] headers) {
380
394
return this ;
381
395
}
382
396
397
+ public Builder accessControlExposeHeaders (String [] headers ) {
398
+ accessControlExposeHeaders .addAll (Arrays .asList (headers ));
399
+ return this ;
400
+ }
401
+
383
402
public Config build () {
384
403
return new Config (this );
385
404
}
@@ -427,6 +446,7 @@ public static Config buildConfig(Settings settings) {
427
446
Config config = builder .allowedRequestMethods (methods )
428
447
.maxAge (SETTING_CORS_MAX_AGE .get (settings ))
429
448
.allowedRequestHeaders (Strings .tokenizeToStringArray (SETTING_CORS_ALLOW_HEADERS .get (settings ), "," ))
449
+ .accessControlExposeHeaders (Strings .tokenizeToStringArray (SETTING_CORS_EXPOSE_HEADERS .get (settings ), "," ))
430
450
.build ();
431
451
return config ;
432
452
}
0 commit comments