Skip to content

Commit 71dddfd

Browse files
MINOR: Remove Deadcode in NioTransport CORS (#34324)
* Same as #34134 but for nio transport
1 parent da61266 commit 71dddfd

3 files changed

Lines changed: 1 addition & 117 deletions

File tree

plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/cors/NioCorsConfig.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public final class NioCorsConfig {
4848
private final long maxAge;
4949
private final Set<HttpMethod> allowedRequestMethods;
5050
private final Set<String> allowedRequestHeaders;
51-
private final boolean allowNullOrigin;
5251
private final Map<CharSequence, Callable<?>> preflightHeaders;
5352
private final boolean shortCircuit;
5453

@@ -61,7 +60,6 @@ public final class NioCorsConfig {
6160
maxAge = builder.maxAge;
6261
allowedRequestMethods = builder.requestMethods;
6362
allowedRequestHeaders = builder.requestHeaders;
64-
allowNullOrigin = builder.allowNullOrigin;
6563
preflightHeaders = builder.preflightHeaders;
6664
shortCircuit = builder.shortCircuit;
6765
}
@@ -108,19 +106,6 @@ public boolean isOriginAllowed(final String origin) {
108106
return false;
109107
}
110108

111-
/**
112-
* Web browsers may set the 'Origin' request header to 'null' if a resource is loaded
113-
* from the local file system.
114-
*
115-
* If isNullOriginAllowed is true then the server will response with the wildcard for the
116-
* the CORS response header 'Access-Control-Allow-Origin'.
117-
*
118-
* @return {@code true} if a 'null' origin should be supported.
119-
*/
120-
public boolean isNullOriginAllowed() {
121-
return allowNullOrigin;
122-
}
123-
124109
/**
125110
* Determines if credentials are supported for CORS requests.
126111
*

plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/cors/NioCorsConfigBuilder.java

Lines changed: 1 addition & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,6 @@ public static NioCorsConfigBuilder forAnyOrigin() {
4949
return new NioCorsConfigBuilder();
5050
}
5151

52-
/**
53-
* Creates a {@link NioCorsConfigBuilder} instance with the specified origin.
54-
*
55-
* @return {@link NioCorsConfigBuilder} to support method chaining.
56-
*/
57-
public static NioCorsConfigBuilder forOrigin(final String origin) {
58-
if ("*".equals(origin)) {
59-
return new NioCorsConfigBuilder();
60-
}
61-
return new NioCorsConfigBuilder(origin);
62-
}
63-
64-
6552
/**
6653
* Create a {@link NioCorsConfigBuilder} instance with the specified pattern origin.
6754
*
@@ -87,14 +74,12 @@ public static NioCorsConfigBuilder forOrigins(final String... origins) {
8774
Optional<Set<String>> origins;
8875
Optional<Pattern> pattern;
8976
final boolean anyOrigin;
90-
boolean allowNullOrigin;
9177
boolean enabled = true;
9278
boolean allowCredentials;
9379
long maxAge;
9480
final Set<HttpMethod> requestMethods = new HashSet<>();
9581
final Set<String> requestHeaders = new HashSet<>();
9682
final Map<CharSequence, Callable<?>> preflightHeaders = new HashMap<>();
97-
private boolean noPreflightHeaders;
9883
boolean shortCircuit;
9984

10085
/**
@@ -130,18 +115,6 @@ public static NioCorsConfigBuilder forOrigins(final String... origins) {
130115
anyOrigin = false;
131116
}
132117

133-
/**
134-
* Web browsers may set the 'Origin' request header to 'null' if a resource is loaded
135-
* from the local file system. Calling this method will enable a successful CORS response
136-
* with a wildcard for the CORS response header 'Access-Control-Allow-Origin'.
137-
*
138-
* @return {@link NioCorsConfigBuilder} to support method chaining.
139-
*/
140-
NioCorsConfigBuilder allowNullOrigin() {
141-
allowNullOrigin = true;
142-
return this;
143-
}
144-
145118
/**
146119
* Disables CORS support.
147120
*
@@ -219,71 +192,6 @@ public NioCorsConfigBuilder allowedRequestHeaders(final String... headers) {
219192
return this;
220193
}
221194

222-
/**
223-
* Returns HTTP response headers that should be added to a CORS preflight response.
224-
*
225-
* An intermediary like a load balancer might require that a CORS preflight request
226-
* have certain headers set. This enables such headers to be added.
227-
*
228-
* @param name the name of the HTTP header.
229-
* @param values the values for the HTTP header.
230-
* @return {@link NioCorsConfigBuilder} to support method chaining.
231-
*/
232-
public NioCorsConfigBuilder preflightResponseHeader(final CharSequence name, final Object... values) {
233-
if (values.length == 1) {
234-
preflightHeaders.put(name, new ConstantValueGenerator(values[0]));
235-
} else {
236-
preflightResponseHeader(name, Arrays.asList(values));
237-
}
238-
return this;
239-
}
240-
241-
/**
242-
* Returns HTTP response headers that should be added to a CORS preflight response.
243-
*
244-
* An intermediary like a load balancer might require that a CORS preflight request
245-
* have certain headers set. This enables such headers to be added.
246-
*
247-
* @param name the name of the HTTP header.
248-
* @param value the values for the HTTP header.
249-
* @param <T> the type of values that the Iterable contains.
250-
* @return {@link NioCorsConfigBuilder} to support method chaining.
251-
*/
252-
public <T> NioCorsConfigBuilder preflightResponseHeader(final CharSequence name, final Iterable<T> value) {
253-
preflightHeaders.put(name, new ConstantValueGenerator(value));
254-
return this;
255-
}
256-
257-
/**
258-
* Returns HTTP response headers that should be added to a CORS preflight response.
259-
*
260-
* An intermediary like a load balancer might require that a CORS preflight request
261-
* have certain headers set. This enables such headers to be added.
262-
*
263-
* Some values must be dynamically created when the HTTP response is created, for
264-
* example the 'Date' response header. This can be accomplished by using a Callable
265-
* which will have its 'call' method invoked when the HTTP response is created.
266-
*
267-
* @param name the name of the HTTP header.
268-
* @param valueGenerator a Callable which will be invoked at HTTP response creation.
269-
* @param <T> the type of the value that the Callable can return.
270-
* @return {@link NioCorsConfigBuilder} to support method chaining.
271-
*/
272-
public <T> NioCorsConfigBuilder preflightResponseHeader(final CharSequence name, final Callable<T> valueGenerator) {
273-
preflightHeaders.put(name, valueGenerator);
274-
return this;
275-
}
276-
277-
/**
278-
* Specifies that no preflight response headers should be added to a preflight response.
279-
*
280-
* @return {@link NioCorsConfigBuilder} to support method chaining.
281-
*/
282-
public NioCorsConfigBuilder noPreflightResponseHeaders() {
283-
noPreflightHeaders = true;
284-
return this;
285-
}
286-
287195
/**
288196
* Specifies that a CORS request should be rejected if it's invalid before being
289197
* further processing.
@@ -305,7 +213,7 @@ public NioCorsConfigBuilder shortCircuit() {
305213
* @return {@link NioCorsConfig} the configured CorsConfig instance.
306214
*/
307215
public NioCorsConfig build() {
308-
if (preflightHeaders.isEmpty() && !noPreflightHeaders) {
216+
if (preflightHeaders.isEmpty()) {
309217
preflightHeaders.put("date", DateValueGenerator.INSTANCE);
310218
preflightHeaders.put("content-length", new ConstantValueGenerator("0"));
311219
}

plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/cors/NioCorsHandler.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,6 @@ private void setPreflightHeaders(final HttpResponse response) {
167167
private boolean setOrigin(final HttpResponse response) {
168168
final String origin = request.headers().get(HttpHeaderNames.ORIGIN);
169169
if (!Strings.isNullOrEmpty(origin)) {
170-
if ("null".equals(origin) && config.isNullOriginAllowed()) {
171-
setAnyOrigin(response);
172-
return true;
173-
}
174-
175170
if (config.isAnyOriginSupported()) {
176171
if (config.isCredentialsAllowed()) {
177172
echoRequestOrigin(response);
@@ -201,10 +196,6 @@ private boolean validateOrigin() {
201196
return true;
202197
}
203198

204-
if ("null".equals(origin) && config.isNullOriginAllowed()) {
205-
return true;
206-
}
207-
208199
// if the origin is the same as the host of the request, then allow
209200
if (isSameOrigin(origin, request.headers().get(HttpHeaderNames.HOST))) {
210201
return true;

0 commit comments

Comments
 (0)