New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Copy http headers to ThreadContext strictly #45945
Changes from 7 commits
af85d64
91f4eca
0f5cb21
8bb69ec
6fdb920
97d1da4
a94639d
04ef2c3
545a64d
081959b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,7 @@ | |
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.function.Supplier; | ||
import java.util.function.UnaryOperator; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.elasticsearch.rest.BytesRestResponse.TEXT_CONTENT_TYPE; | ||
import static org.elasticsearch.rest.RestStatus.BAD_REQUEST; | ||
|
@@ -71,10 +72,10 @@ public class RestController implements HttpServerTransport.Dispatcher { | |
private final CircuitBreakerService circuitBreakerService; | ||
|
||
/** Rest headers that are copied to internal requests made during a rest request. */ | ||
private final Set<String> headersToCopy; | ||
private final Set<RestHeaderDefinition> headersToCopy; | ||
private final UsageService usageService; | ||
|
||
public RestController(Set<String> headersToCopy, UnaryOperator<RestHandler> handlerWrapper, | ||
public RestController(Set<RestHeaderDefinition> headersToCopy, UnaryOperator<RestHandler> handlerWrapper, | ||
NodeClient client, CircuitBreakerService circuitBreakerService, UsageService usageService) { | ||
this.headersToCopy = headersToCopy; | ||
this.usageService = usageService; | ||
|
@@ -255,10 +256,18 @@ private void sendContentTypeErrorMessage(@Nullable List<String> contentTypeHeade | |
} | ||
|
||
private void tryAllHandlers(final RestRequest request, final RestChannel channel, final ThreadContext threadContext) throws Exception { | ||
for (String key : headersToCopy) { | ||
String httpHeader = request.header(key); | ||
if (httpHeader != null) { | ||
threadContext.putHeader(key, httpHeader); | ||
for (RestHeaderDefinition restHeader : headersToCopy) { | ||
final String name = restHeader.getName(); | ||
final List<String> headerValues = request.getAllHeaderValues(name); | ||
if (headerValues != null) { | ||
if (restHeader.isMultiValueAllowed() == false && new HashSet<>(headerValues).size() > 1) { | ||
channel.sendResponse( | ||
BytesRestResponse. | ||
createSimpleErrorResponse(channel, BAD_REQUEST, "multiple values for single-valued header [" + name + "].")); | ||
return; | ||
} else { | ||
threadContext.putHeader(name, headerValues.stream().distinct().collect(Collectors.joining(","))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is effectively going to collect allocate a set to collect the header values twice in case multi-value headers are not allowed. Once to collect the header values into a set to see if its size is one, and then again (when the set is size one) to do the trivial join. Since we always need to collect the distinct values (either to check if we should fail the request, or put the header in the context), I think we should refactor this slightly to avoid collecting twice and all the allocations that come with it. |
||
} | ||
} | ||
} | ||
// error_trace cannot be used when we disable detailed errors | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.rest; | ||
|
||
/** | ||
* A definition for an http header that should be copied to the {@link org.elasticsearch.common.util.concurrent.ThreadContext} when | ||
* reading the request on the rest layer. | ||
*/ | ||
public final class RestHeaderDefinition { | ||
private final String name; | ||
/** | ||
* This should be set to true only when the syntax of the value of the Header to copy is defined as a comma separated list of String | ||
* values. | ||
*/ | ||
private final boolean multiValueAllowed; | ||
|
||
public RestHeaderDefinition(String name, boolean multiValueAllowed) { | ||
this.name = name; | ||
this.multiValueAllowed = multiValueAllowed; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public boolean isMultiValueAllowed() { | ||
return multiValueAllowed; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The loop iterator can be
final
.