Skip to content

Commit c5990fb

Browse files
Merge remote-tracking branch 'origin/jetty-12.0.x' into jetty-12.1.x
2 parents b5a99e7 + b57b8e6 commit c5990fb

File tree

34 files changed

+856
-180
lines changed

34 files changed

+856
-180
lines changed

jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/HttpFields.java

+15
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,21 @@ default int size()
927927
return size;
928928
}
929929

930+
/**
931+
* <p>Wraps an instance of {@link HttpFields} as a {@link Map}.</p>
932+
* <p>If the provided {@link HttpFields} is an instance of {@link HttpFields.Mutable} then changes to the
933+
* {@link Map} will be reflected in the underlying {@link HttpFields}.
934+
* Otherwise, any modification to the {@link Map} will throw {@link UnsupportedOperationException}.</p>
935+
* @param fields the {@link HttpFields} to convert to a {@link Map}.
936+
* @return an {@link Map} representing the contents of the {@link HttpFields}.
937+
*/
938+
static Map<String, List<String>> asMap(HttpFields fields)
939+
{
940+
return (fields instanceof HttpFields.Mutable mutable)
941+
? new HttpFieldsMap.Mutable(mutable)
942+
: new HttpFieldsMap.Immutable(fields);
943+
}
944+
930945
/**
931946
* @return a sequential stream of the {@link HttpField}s in this instance
932947
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
//
2+
// ========================================================================
3+
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
4+
//
5+
// This program and the accompanying materials are made available under the
6+
// terms of the Eclipse Public License v. 2.0 which is available at
7+
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
8+
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
9+
//
10+
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
11+
// ========================================================================
12+
//
13+
14+
package org.eclipse.jetty.http;
15+
16+
import java.util.AbstractMap;
17+
import java.util.AbstractSet;
18+
import java.util.Iterator;
19+
import java.util.List;
20+
import java.util.Objects;
21+
import java.util.Set;
22+
23+
import org.eclipse.jetty.util.StringUtil;
24+
25+
/**
26+
* <p>A {@link java.util.Map} which is backed by an instance of {@link HttpFields.Mutable}.</p>
27+
* @see HttpFieldsMap.Mutable
28+
* @see HttpFieldsMap.Immutable
29+
*/
30+
abstract class HttpFieldsMap extends AbstractMap<String, List<String>>
31+
{
32+
/**
33+
* <p>A {@link java.util.Map} which is backed by an instance of {@link HttpFields.Mutable}.</p>
34+
* <p>Any changes to the {@link java.util.Map} will be reflected in the underlying instance of {@link HttpFields.Mutable}.</p>
35+
*/
36+
public static class Mutable extends HttpFieldsMap
37+
{
38+
private final HttpFields.Mutable httpFields;
39+
40+
public Mutable(HttpFields.Mutable httpFields)
41+
{
42+
this.httpFields = httpFields;
43+
}
44+
45+
@Override
46+
public List<String> get(Object key)
47+
{
48+
if (key instanceof String s)
49+
return httpFields.getValuesList(s);
50+
return null;
51+
}
52+
53+
@Override
54+
public List<String> put(String key, List<String> value)
55+
{
56+
List<String> oldValue = get(key);
57+
httpFields.put(key, value);
58+
return oldValue;
59+
}
60+
61+
@Override
62+
public List<String> remove(Object key)
63+
{
64+
if (key instanceof String s)
65+
{
66+
List<String> oldValue = get(s);
67+
httpFields.remove(s);
68+
return oldValue;
69+
}
70+
return null;
71+
}
72+
73+
@Override
74+
public Set<Entry<String, List<String>>> entrySet()
75+
{
76+
return new AbstractSet<>()
77+
{
78+
@Override
79+
public Iterator<Entry<String, List<String>>> iterator()
80+
{
81+
return new Iterator<>()
82+
{
83+
private final Iterator<String> iterator = httpFields.getFieldNamesCollection().iterator();
84+
private String name = null;
85+
86+
@Override
87+
public boolean hasNext()
88+
{
89+
return iterator.hasNext();
90+
}
91+
92+
@Override
93+
public Entry<String, List<String>> next()
94+
{
95+
name = iterator.next();
96+
return new HttpFieldsEntry(name);
97+
}
98+
99+
@Override
100+
public void remove()
101+
{
102+
if (name != null)
103+
{
104+
Mutable.this.remove(name);
105+
name = null;
106+
}
107+
}
108+
};
109+
}
110+
111+
@Override
112+
public int size()
113+
{
114+
return httpFields.getFieldNamesCollection().size();
115+
}
116+
};
117+
}
118+
}
119+
120+
/**
121+
* <p>A {@link java.util.Map} which is backed by an instance of {@link HttpFields.Mutable}.</p>
122+
* <p>Any attempt to modify the map will throw {@link UnsupportedOperationException}.</p>
123+
*/
124+
public static class Immutable extends HttpFieldsMap
125+
{
126+
private final HttpFields httpFields;
127+
128+
public Immutable(HttpFields httpFields)
129+
{
130+
this.httpFields = httpFields;
131+
}
132+
133+
@Override
134+
public List<String> get(Object key)
135+
{
136+
if (key instanceof String s)
137+
return httpFields.getValuesList(s);
138+
return null;
139+
}
140+
141+
@Override
142+
public List<String> put(String key, List<String> value)
143+
{
144+
throw new UnsupportedOperationException();
145+
}
146+
147+
@Override
148+
public List<String> remove(Object key)
149+
{
150+
throw new UnsupportedOperationException();
151+
}
152+
153+
@Override
154+
public Set<Entry<String, List<String>>> entrySet()
155+
{
156+
return new AbstractSet<>()
157+
{
158+
@Override
159+
public Iterator<Entry<String, List<String>>> iterator()
160+
{
161+
return new Iterator<>()
162+
{
163+
private final Iterator<String> iterator = httpFields.getFieldNamesCollection().iterator();
164+
165+
@Override
166+
public boolean hasNext()
167+
{
168+
return iterator.hasNext();
169+
}
170+
171+
@Override
172+
public Entry<String, List<String>> next()
173+
{
174+
return new HttpFieldsEntry(iterator.next());
175+
}
176+
177+
@Override
178+
public void remove()
179+
{
180+
throw new UnsupportedOperationException();
181+
}
182+
};
183+
}
184+
185+
@Override
186+
public int size()
187+
{
188+
return httpFields.getFieldNamesCollection().size();
189+
}
190+
};
191+
}
192+
}
193+
194+
private class HttpFieldsEntry implements Entry<String, List<String>>
195+
{
196+
private final String _name;
197+
198+
public HttpFieldsEntry(String name)
199+
{
200+
_name = name;
201+
}
202+
203+
@Override
204+
public String getKey()
205+
{
206+
return _name;
207+
}
208+
209+
@Override
210+
public List<String> getValue()
211+
{
212+
return HttpFieldsMap.this.get(_name);
213+
}
214+
215+
@Override
216+
public List<String> setValue(List<String> value)
217+
{
218+
return HttpFieldsMap.this.put(_name, value);
219+
}
220+
221+
@Override
222+
public boolean equals(Object o)
223+
{
224+
if (this == o)
225+
return true;
226+
if (o instanceof HttpFieldsEntry other)
227+
return StringUtil.asciiEqualsIgnoreCase(_name, other.getKey());
228+
return false;
229+
}
230+
231+
@Override
232+
public int hashCode()
233+
{
234+
return Objects.hash(StringUtil.asciiToLowerCase(_name));
235+
}
236+
}
237+
}

jetty-core/jetty-websocket/jetty-websocket-jetty-client/src/main/java/org/eclipse/jetty/websocket/client/internal/DelegatedJettyClientUpgradeRequest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.stream.Collectors;
2323

2424
import org.eclipse.jetty.http.HttpField;
25+
import org.eclipse.jetty.http.HttpFields;
2526
import org.eclipse.jetty.http.HttpHeader;
2627
import org.eclipse.jetty.http.HttpScheme;
2728
import org.eclipse.jetty.io.EndPoint;
@@ -78,7 +79,7 @@ public List<String> getHeaders(String name)
7879
@Override
7980
public Map<String, List<String>> getHeaders()
8081
{
81-
return null;
82+
return HttpFields.asMap(delegate.getHeaders());
8283
}
8384

8485
@Override

jetty-core/jetty-websocket/jetty-websocket-jetty-client/src/main/java/org/eclipse/jetty/websocket/client/internal/DelegatedJettyClientUpgradeResponse.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313

1414
package org.eclipse.jetty.websocket.client.internal;
1515

16-
import java.util.ArrayList;
1716
import java.util.Collections;
1817
import java.util.List;
1918
import java.util.Map;
2019
import java.util.Set;
2120
import java.util.stream.Collectors;
2221

2322
import org.eclipse.jetty.client.Response;
23+
import org.eclipse.jetty.http.HttpFields;
2424
import org.eclipse.jetty.http.HttpHeader;
2525
import org.eclipse.jetty.websocket.api.ExtensionConfig;
2626
import org.eclipse.jetty.websocket.api.UpgradeResponse;
@@ -65,9 +65,7 @@ public List<String> getHeaders(String name)
6565
@Override
6666
public Map<String, List<String>> getHeaders()
6767
{
68-
Map<String, List<String>> headers = getHeaderNames().stream()
69-
.collect(Collectors.toMap((name) -> name, (name) -> new ArrayList<>(getHeaders(name))));
70-
return Collections.unmodifiableMap(headers);
68+
return HttpFields.asMap(delegate.getHeaders());
7169
}
7270

7371
@Override

jetty-core/jetty-websocket/jetty-websocket-jetty-server/src/main/java/org/eclipse/jetty/websocket/server/ServerWebSocketContainer.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -359,13 +359,12 @@ private org.eclipse.jetty.websocket.core.server.WebSocketCreator newWebSocketCre
359359
{
360360
try
361361
{
362-
Object webSocket = creator.createWebSocket(new ServerUpgradeRequestDelegate(rq), new ServerUpgradeResponseDelegate(rq, rs), cb);
363-
if (webSocket == null)
364-
cb.succeeded();
365-
return webSocket;
362+
return creator.createWebSocket(new ServerUpgradeRequestDelegate(rq), new ServerUpgradeResponseDelegate(rq, rs), cb);
366363
}
367364
catch (Throwable x)
368365
{
366+
if (LOG.isDebugEnabled())
367+
LOG.debug("Could not create WebSocket endpoint", x);
369368
cb.failed(x);
370369
return null;
371370
}

jetty-core/jetty-websocket/jetty-websocket-jetty-server/src/main/java/org/eclipse/jetty/websocket/server/internal/UpgradeRequestDelegate.java

+1-9
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.Map;
2222
import java.util.stream.Collectors;
2323

24-
import org.eclipse.jetty.http.HttpField;
2524
import org.eclipse.jetty.http.HttpFields;
2625
import org.eclipse.jetty.http.HttpHeader;
2726
import org.eclipse.jetty.http.HttpScheme;
@@ -73,14 +72,7 @@ public int getHeaderInt(String name)
7372
@Override
7473
public Map<String, List<String>> getHeaders()
7574
{
76-
Map<String, List<String>> result = new LinkedHashMap<>();
77-
HttpFields headers = request.getHeaders();
78-
for (HttpField header : headers)
79-
{
80-
String name = header.getName();
81-
result.put(name, headers.getValuesList(name));
82-
}
83-
return result;
75+
return HttpFields.asMap(request.getHeaders());
8476
}
8577

8678
@Override

jetty-core/jetty-websocket/jetty-websocket-jetty-server/src/main/java/org/eclipse/jetty/websocket/server/internal/UpgradeResponseDelegate.java

+1-10
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@
1313

1414
package org.eclipse.jetty.websocket.server.internal;
1515

16-
import java.util.LinkedHashMap;
1716
import java.util.List;
1817
import java.util.Map;
1918
import java.util.Set;
2019
import java.util.stream.Collectors;
2120

22-
import org.eclipse.jetty.http.HttpField;
2321
import org.eclipse.jetty.http.HttpFields;
2422
import org.eclipse.jetty.websocket.api.ExtensionConfig;
2523
import org.eclipse.jetty.websocket.api.UpgradeResponse;
@@ -64,14 +62,7 @@ public Set<String> getHeaderNames()
6462
@Override
6563
public Map<String, List<String>> getHeaders()
6664
{
67-
Map<String, List<String>> result = new LinkedHashMap<>();
68-
HttpFields.Mutable headers = response.getHeaders();
69-
for (HttpField header : headers)
70-
{
71-
String name = header.getName();
72-
result.put(name, headers.getValuesList(name));
73-
}
74-
return result;
65+
return HttpFields.asMap(response.getHeaders());
7566
}
7667

7768
@Override

0 commit comments

Comments
 (0)