functions-framework-java icon indicating copy to clipboard operation
functions-framework-java copied to clipboard

HttpRequest.getFirstHeader("foo") performs case-sensitive header look ups

Open alexbrand opened this issue 3 years ago • 1 comments

We were digging into Cloud Functions and OpenTelemetry and ran into an issue with getting the traceparent header to ensure tracing context is propagated. We noticed that the problem was that Cloud Functions sets the header name to Traceparent, but OpenTelemetry looks for a header named traceparent.

Ideally, this should not matter, given that headers should be treated without case sensitivity, per RFC 7320 Section 3.2. However, com.google.cloud.functions.HttpRequest.getFirstHeader seems to care about casing.

Would it make sense to change the implementation to treat headers without case sensitivity? At the moment, we are planning to use a workaround (See gist for an example)

alexbrand avatar Apr 19 '21 21:04 alexbrand

This does seem wrong. I think here we should change the code from this:

        .collect(toMap(Map.Entry::getKey, Map.Entry::getValue));

to this:

        .collect(toMap(
            Map.Entry::getKey, Map.Entry::getValue,
            (x, y) -> x,
            () -> new TreeMap<>(String.CASE_INSENSITIVE_COMPARATOR)));

The assumption is that the underlying servlet implementation isn't going to give us two keys that differ only in case, so we don't need a real merge function.

There would be a similar change in HttpResponseImpl.

eamonnmcmanus avatar Apr 19 '21 22:04 eamonnmcmanus

This should be fixed in https://github.com/GoogleCloudPlatform/functions-framework-java/pull/178. You can find release trigger in https://github.com/GoogleCloudPlatform/functions-framework-java/pull/179 . Feel free to reopen if issue continues.

kenneth-rosario avatar Mar 24 '23 21:03 kenneth-rosario