[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [jetty-users] Easiest way to cleanup headers before sending on connector level
|
Hi,
I think the problem is in HttpChannel#sendResponse(). This method calls
onResponseBegin() too late, it should call this before
_response.newResponseMetaData() which makes an immutable snapshot of the
HttpFields.
Should I open a bug report or PR?
> I have a Jetty server that was upgraded from 9.4 to 10.0. There is one
> requirement to do some "header maintenance": Before ethe response is sent
to
> client, I want all headers created by several addHeaders in a bunch of
> servlets, filters and other handlers to be cleaned up. In my case, this is
> deduplication and merging of "vary" and "cache-control" headers to assist
> NGINX in front so it only gets one header and not multiple.
>
> In Jetty 9.4, I had the following code, added as bean to the connector:
>
> private static HttpField mergeHttpFields(String name, List<HttpField>
> fields) {
> return (fields == null) ? null : new HttpField(name, fields.stream()
> .map(HttpField::getValues)
> .flatMap(Stream::of)
> .distinct()
> .collect(COMMA_JOINER));
> }
>
> private static final HttpChannel.Listener HEADER_MERGER = new
> HttpChannel.Listener() {
> @Override
> public void onResponseBegin(Request request) {
> final HttpFields headers = request.getResponse().getHttpFields();
> headers.computeField("Vary", Bootstrap::mergeHttpFields);
> headers.computeField("Cache-Control", Bootstrap::mergeHttpFields);
> }
> };
>
> // This is how its added:
> connector.addBean(HEADER_MERGER);
>
>
> In 10.0 this no longer compiled, but was easy to fix by using the
> HttpFields.Mutable interface instead of HttpFields. But actually the
headers
> were no longer merged! Debuggig through the code, I figured out that my
> header merger was actually called before the response was submitted and
> after it ran, the HttpFields were correctly merged. But on the wire they
did
> not appear, the original headers were sent.
>
> What changed? The Mutable vs. Immutable is the problem: Before the
> onResponseBegin() handler is called, the HttpChannel has already made a
> Immutable snapshot of the headers, so all changes to the Mutable ones are
> lost.
>
> My question now: How to "clean up headers" the easy way with Jetty 10? I
> know there might be ways to implement Interceptors, but this makes it
really
> unreadable and hard to maintain. Does anybody has a better idea how to
tell
> Jetty to modify the headers before sending them? I found no other Listener
> that could achieve this. Is there something like beforeCommit?
>
> Uwe
>
> -----
> Uwe Schindler
> uschindler@xxxxxxxxxx
> ASF Member, Member of PMC and Committer of Apache Lucene and Apache
> Solr
> Bremen, Germany
> https://lucene.apache.org/
> https://solr.apache.org/
>
>
> _______________________________________________
> jetty-users mailing list
> jetty-users@xxxxxxxxxxx
> To unsubscribe from this list, visit
> https://www.eclipse.org/mailman/listinfo/jetty-users