-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add a class for handling headers in a case-insensitive way #9031
Conversation
This can then be used by aws, gcp, azure and elsewhere
This for example should allow us to remove diff --git a/function-aws-api-proxy/src/main/java/io/micronaut/function/aws/proxy/MicronautAwsProxyRequest.java b/function-aws-api-proxy/src/main/java/io/micronaut/function/aws/proxy/MicronautAwsProxyRequest.java
index 85eeb9e5d..d3bf75b06 100644
--- a/function-aws-api-proxy/src/main/java/io/micronaut/function/aws/proxy/MicronautAwsProxyRequest.java
+++ b/function-aws-api-proxy/src/main/java/io/micronaut/function/aws/proxy/MicronautAwsProxyRequest.java
@@ -35,6 +35,7 @@ import io.micronaut.core.convert.value.MutableConvertibleValuesMap;
import io.micronaut.core.type.Argument;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.core.util.StringUtils;
+import io.micronaut.http.CaseInsensitiveMutableHttpHeaders;
import io.micronaut.http.HttpHeaders;
import io.micronaut.http.HttpMethod;
import io.micronaut.http.HttpParameters;
@@ -188,7 +189,6 @@ public class MicronautAwsProxyRequest<T> implements HttpRequest<T> {
getHeaders().getAll(HttpHeaders.COOKIE).forEach(cookieValue -> {
List<HeaderValue> parsedHeaders = parseHeaderValue(cookieValue, ";", ",");
-
parsedHeaders.stream()
.filter(e -> e.getKey() != null)
.map(e -> new SimpleCookie(SecurityUtils.crlf(e.getKey()), SecurityUtils.crlf(e.getValue())))
@@ -504,83 +504,30 @@ public class MicronautAwsProxyRequest<T> implements HttpRequest<T> {
/**
* Implementation of {@link HttpHeaders} for AWS.
*/
- private static class AwsHeaders implements HttpHeaders {
-
- private final Map<String, List<String>> headers;
+ private static class AwsHeaders extends CaseInsensitiveMutableHttpHeaders {
public AwsHeaders(@Nullable Headers multivalueHeaders, @Nullable SingleValueHeaders singleValueHeaders) {
+ super(buildHeaders(multivalueHeaders, singleValueHeaders), ConversionService.SHARED);
+ }
+
+ private static Map<String, List<String>> buildHeaders(Headers multivalueHeaders, SingleValueHeaders singleValueHeaders) {
if (multivalueHeaders == null && singleValueHeaders == null) {
- headers = Collections.emptyMap();
+ return Collections.emptyMap();
} else {
- headers = new HashMap<>();
+ Map<String, List<String>> headers = new HashMap<>();
if (multivalueHeaders != null) {
- for (String name : multivalueHeaders.keySet()) {
- String headerName = HttpHeaderUtils.normalizeHttpHeaderCase(name);
- headers.computeIfAbsent(headerName, s -> new ArrayList<>());
- headers.get(headerName).addAll(multivalueHeaders.get(headerName));
+ for (Map.Entry<String, List<String>> entry : multivalueHeaders.entrySet()) {
+ headers.computeIfAbsent(entry.getKey(), s -> new ArrayList<>()).addAll(entry.getValue());
}
}
if (CollectionUtils.isNotEmpty(singleValueHeaders)) {
- for (String name : singleValueHeaders.keySet()) {
- String headerName = HttpHeaderUtils.normalizeHttpHeaderCase(name);
- headers.computeIfAbsent(headerName, s -> new ArrayList<>());
- headers.get(headerName).add(singleValueHeaders.get(headerName));
+ for (Map.Entry<String, String> entry : singleValueHeaders.entrySet()) {
+ headers.computeIfAbsent(entry.getKey(), s -> new ArrayList<>()).add(entry.getValue());
}
}
+ return headers;
}
}
-
- @Override
- public List<String> getAll(CharSequence name) {
- Optional<String> headerName = findKey(name.toString());
- if (!headerName.isPresent()) {
- return Collections.emptyList();
- }
- List<String> values = headers.get(headerName.get());
- if (values == null) {
- return Collections.emptyList();
- }
- return values;
- }
-
- @Nullable
- @Override
- public String get(CharSequence name) {
- List<String> values = getAll(name);
- if (CollectionUtils.isEmpty(values)) {
- return null;
- }
- return values.get(0);
- }
-
- @Override
- public Set<String> names() {
- return headers.keySet();
- }
-
- @Override
- public Collection<List<String>> values() {
- return headers.values();
- }
-
- @Override
- public <T> Optional<T> get(CharSequence name, ArgumentConversionContext<T> conversionContext) {
- final String v = get(name);
- if (v != null) {
- return ConversionService.SHARED.convert(v, conversionContext);
- }
- return Optional.empty();
- }
-
- @NonNull
- private Optional<String> findKey(@NonNull String name) {
- for (String headerName : headers.keySet()) {
- if (headerName.equalsIgnoreCase(name)) {
- return Optional.of(headerName);
- }
- }
- return Optional.empty();
- }
}
/** |
@Override | ||
public MutableHttpHeaders add(CharSequence header, CharSequence value) { | ||
validate(header, value); | ||
backing.computeIfAbsent(header.toString(), s -> new ArrayList<>()).add(value.toString()); |
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.
Might want to size this list with some sensible default. Repeated headers of the same name are rare and normally there are not many of them
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.
I chose 2 as a default 🤔 Maybe 1 is better?
http/src/main/java/io/micronaut/http/CaseInsensitiveMutableHttpHeaders.java
Outdated
Show resolved
Hide resolved
http/src/main/java/io/micronaut/http/CaseInsensitiveMutableHttpHeaders.java
Show resolved
Hide resolved
http/src/main/java/io/micronaut/http/CaseInsensitiveMutableHttpHeaders.java
Outdated
Show resolved
Hide resolved
http/src/main/java/io/micronaut/http/CaseInsensitiveMutableHttpHeaders.java
Show resolved
Hide resolved
http/src/main/java/io/micronaut/http/CaseInsensitiveMutableHttpHeaders.java
Show resolved
Hide resolved
if (CollectionUtils.isEmpty(values)) { | ||
return Collections.emptyList(); | ||
} | ||
return values; |
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.
wrap in unmodifiable list because returning emptyList()
above is immutable
http/src/main/java/io/micronaut/http/CaseInsensitiveMutableHttpHeaders.java
Outdated
Show resolved
Hide resolved
http/src/main/java/io/micronaut/http/CaseInsensitiveMutableHttpHeaders.java
Outdated
Show resolved
Hide resolved
Kudos, SonarCloud Quality Gate passed! |
This can then be used by aws, gcp, azure and elsewhere