Skip to content

Commit

Permalink
Ensure mutable headers for Jetty WebFlux request
Browse files Browse the repository at this point in the history
In Jetty 10, request headers are immutable, see
jetty/jetty.project#4777, but we need to
remove/hide forwarded headers when they have been used.

See gh-27424
  • Loading branch information
rstoyanchev committed Sep 17, 2021
1 parent cce61c3 commit b732ff3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@
*/
class JettyHeadersAdapter implements MultiValueMap<String, String> {

private final HttpFields headers;
private final HttpFields.Mutable headers;


JettyHeadersAdapter(HttpFields headers) {
JettyHeadersAdapter(HttpFields.Mutable headers) {
this.headers = headers;
}

Expand All @@ -59,10 +59,7 @@ public String getFirst(String key) {

@Override
public void add(String key, @Nullable String value) {
if (!(this.headers instanceof HttpFields.Mutable)) {
throw new IllegalStateException("Immutable headers");
}
((HttpFields.Mutable) this.headers).add(key, value);
this.headers.add(key, value);
}

@Override
Expand All @@ -77,10 +74,7 @@ public void addAll(MultiValueMap<String, String> values) {

@Override
public void set(String key, @Nullable String value) {
if (!(this.headers instanceof HttpFields.Mutable)) {
throw new IllegalStateException("Immutable headers");
}
((HttpFields.Mutable) this.headers).put(key, value);
this.headers.put(key, value);
}

@Override
Expand Down Expand Up @@ -133,23 +127,17 @@ public List<String> get(Object key) {
@Nullable
@Override
public List<String> put(String key, List<String> value) {
if (!(this.headers instanceof HttpFields.Mutable)) {
throw new IllegalStateException("Immutable headers");
}
List<String> oldValues = get(key);
((HttpFields.Mutable) this.headers).put(key, value);
this.headers.put(key, value);
return oldValues;
}

@Nullable
@Override
public List<String> remove(Object key) {
if (!(this.headers instanceof HttpFields.Mutable)) {
throw new IllegalStateException("Immutable headers");
}
if (key instanceof String) {
List<String> oldValues = get(key);
((HttpFields.Mutable) this.headers).remove((String) key);
this.headers.remove((String) key);
return oldValues;
}
return null;
Expand All @@ -162,10 +150,7 @@ public void putAll(Map<? extends String, ? extends List<String>> map) {

@Override
public void clear() {
if (!(this.headers instanceof HttpFields.Mutable)) {
throw new IllegalStateException("Immutable headers");
}
((HttpFields.Mutable) this.headers).clear();
this.headers.clear();
}

@Override
Expand All @@ -181,7 +166,7 @@ public Collection<List<String>> values() {

@Override
public Set<Entry<String, List<String>>> entrySet() {
return new AbstractSet<Entry<String, List<String>>>() {
return new AbstractSet<>() {
@Override
public Iterator<Entry<String, List<String>>> iterator() {
return new EntryIterator();
Expand Down Expand Up @@ -237,11 +222,8 @@ public List<String> getValue() {

@Override
public List<String> setValue(List<String> value) {
if (!(headers instanceof HttpFields.Mutable)) {
throw new IllegalStateException("Immutable headers");
}
List<String> previousValues = headers.getValuesList(this.key);
((HttpFields.Mutable) headers).put(this.key, value);
headers.put(this.key, value);
return previousValues;
}
}
Expand Down Expand Up @@ -285,16 +267,13 @@ public String next() {

@Override
public void remove() {
if (!(headers instanceof HttpFields.Mutable)) {
throw new IllegalStateException("Immutable headers");
}
if (this.currentName == null) {
throw new IllegalStateException("No current Header in iterator");
}
if (!headers.contains(this.currentName)) {
throw new IllegalStateException("Header not present: " + this.currentName);
}
((HttpFields.Mutable) headers).remove(this.currentName);
headers.remove(this.currentName);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private static final class JettyServerHttpRequest extends ServletServerHttpReque

private static MultiValueMap<String, String> createHeaders(HttpServletRequest servletRequest) {
Request request = getRequest(servletRequest);
HttpFields fields = request.getMetaData().getFields();
HttpFields.Mutable fields = HttpFields.build(request.getHttpFields());
return new JettyHeadersAdapter(fields);
}

Expand Down Expand Up @@ -116,7 +116,7 @@ private static final class JettyServerHttpResponse extends ServletServerHttpResp

private static HttpHeaders createHeaders(HttpServletResponse servletResponse) {
Response response = getResponse(servletResponse);
HttpFields fields = response.getHttpFields();
HttpFields.Mutable fields = response.getHttpFields();
return new HttpHeaders(new JettyHeadersAdapter(fields));
}

Expand Down

0 comments on commit b732ff3

Please sign in to comment.