diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpFields.java b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpFields.java index 28df6791d203..e436ded5d348 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpFields.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpFields.java @@ -90,7 +90,7 @@ public HttpFields(HttpFields fields) } /** - *

Computes a single field for the given HTTP header name and for existing fields with the same name.

+ *

Computes a single field for the given HttpHeader and for existing fields with the same header.

* *

The compute function receives the field name and a list of fields with the same name * so that their values can be used to compute the value of the field that is returned @@ -161,17 +161,34 @@ public HttpFields(HttpFields fields) * httpFields.computeField("Connection", (name, fields) -> null); * * + * @param header the HTTP header + * @param computeFn the compute function + */ + public void computeField(HttpHeader header, BiFunction, HttpField> computeFn) + { + computeField(header, computeFn, (f, h) -> f.getHeader() == h); + } + + /** + *

Computes a single field for the given HTTP header name and for existing fields with the same name.

+ * * @param name the HTTP header name * @param computeFn the compute function + * @see #computeField(HttpHeader, BiFunction) */ public void computeField(String name, BiFunction, HttpField> computeFn) + { + computeField(name, computeFn, HttpField::is); + } + + private void computeField(T header, BiFunction, HttpField> computeFn, BiFunction matcher) { // Look for first occurrence int first = -1; for (int i = 0; i < _size; i++) { HttpField f = _fields[i]; - if (f.is(name)) + if (matcher.apply(f, header)) { first = i; break; @@ -181,7 +198,7 @@ public void computeField(String name, BiFunction, HttpFi // If the header is not found, add a new one; if (first < 0) { - HttpField newField = computeFn.apply(name, null); + HttpField newField = computeFn.apply(header, null); if (newField != null) add(newField); return; @@ -192,7 +209,7 @@ public void computeField(String name, BiFunction, HttpFi for (int i = first + 1; i < _size; i++) { HttpField f = _fields[i]; - if (f.is(name)) + if (matcher.apply(f, header)) { if (found == null) { @@ -211,7 +228,7 @@ public void computeField(String name, BiFunction, HttpFi else found = Collections.unmodifiableList(found); - HttpField newField = computeFn.apply(name, found); + HttpField newField = computeFn.apply(header, found); if (newField == null) remove(first); else diff --git a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpFieldsTest.java b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpFieldsTest.java index bb9eb90826d0..2e1978d125e2 100644 --- a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpFieldsTest.java +++ b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpFieldsTest.java @@ -868,7 +868,7 @@ public void testStream() } @Test - public void testCompute() + public void testComputeField() { HttpFields header = new HttpFields(); assertThat(header.size(), is(0)); diff --git a/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AbstractProxyServlet.java b/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AbstractProxyServlet.java index 154c754b2038..7ea2264b9909 100644 --- a/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AbstractProxyServlet.java +++ b/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AbstractProxyServlet.java @@ -554,10 +554,10 @@ protected void addViaHeader(HttpServletRequest clientRequest, Request proxyReque // Retain only the version if the protocol is HTTP. String protocolPart = parts.length == 2 && "HTTP".equalsIgnoreCase(parts[0]) ? parts[1] : protocol; String viaHeaderValue = protocolPart + " " + getViaHost(); - proxyRequest.getHeaders().computeField(HttpHeader.VIA.asString(), (name, viaFields) -> + proxyRequest.getHeaders().computeField(HttpHeader.VIA, (header, viaFields) -> { if (viaFields == null || viaFields.isEmpty()) - return new HttpField(name, viaHeaderValue); + return new HttpField(header, viaHeaderValue); String separator = ", "; String newValue = viaFields.stream() .flatMap(field -> Stream.of(field.getValues())) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipHandler.java index ad32f35bb01b..72b987a7ed9f 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipHandler.java @@ -662,7 +662,7 @@ public void handle(String target, Request baseRequest, HttpServletRequest reques baseRequest.getHttpInput().addInterceptor(new GzipHttpInputInterceptor(baseRequest.getHttpChannel().getByteBufferPool(), _inflateBufferSize)); - baseRequest.getHttpFields().computeField(HttpHeader.CONTENT_LENGTH.asString(), (name, fields) -> + baseRequest.getHttpFields().computeField(HttpHeader.CONTENT_LENGTH, (header, fields) -> { if (fields == null) return null;