Skip to content
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

Merged
merged 11 commits into from Mar 31, 2023

Conversation

timyates
Copy link
Member

This can then be used by aws, gcp, azure and elsewhere

This can then be used by aws, gcp, azure and elsewhere
@timyates timyates added this to the 3.9.0 milestone Mar 30, 2023
@timyates timyates requested a review from sdelamo March 30, 2023 19:12
@timyates timyates self-assigned this Mar 30, 2023
@timyates
Copy link
Member Author

This for example should allow us to remove HttpHeaderUtils from function-aws-api-proxy, and simplify MicronautAwsProxyRequest to

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());
Copy link
Contributor

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

Copy link
Member Author

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?

if (CollectionUtils.isEmpty(values)) {
return Collections.emptyList();
}
return values;
Copy link
Contributor

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

@sonarcloud
Copy link

sonarcloud bot commented Mar 31, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

92.1% 92.1% Coverage
0.0% 0.0% Duplication

@sdelamo sdelamo merged commit 6094617 into 3.9.x Mar 31, 2023
11 checks passed
@sdelamo sdelamo deleted the case-insensitive-headers branch March 31, 2023 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants