Skip to content

Commit 5626ba6

Browse files
committed
Port fix for Atmosphere#2241 from vaadin/atmosphere#2241
1 parent 7a6fb6c commit 5626ba6

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

modules/cpr/src/main/java/org/atmosphere/container/JSR356Endpoint.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public class JSR356Endpoint extends Endpoint {
6565
private final AtmosphereFramework framework;
6666
private WebSocket webSocket;
6767
private final int webSocketWriteTimeout;
68-
private HandshakeRequest handshakeRequest;
68+
private HttpSession handshakeSession;
69+
private Map<String, List<String>> handshakeHeaders;
6970

7071
public JSR356Endpoint(AtmosphereFramework framework, WebSocketProcessor webSocketProcessor) {
7172
this.framework = framework;
@@ -98,7 +99,12 @@ public JSR356Endpoint(AtmosphereFramework framework, WebSocketProcessor webSocke
9899
}
99100

100101
public JSR356Endpoint handshakeRequest(HandshakeRequest handshakeRequest) {
101-
this.handshakeRequest = handshakeRequest;
102+
this.handshakeSession = (HttpSession) handshakeRequest.getHttpSession();
103+
this.handshakeHeaders = new HashMap<>();
104+
for (Map.Entry<String, List<String>> e : handshakeRequest.getHeaders()
105+
.entrySet()) {
106+
handshakeHeaders.put(e.getKey(), e.getValue());
107+
}
102108
return this;
103109
}
104110

@@ -120,7 +126,7 @@ public void onOpen(Session session, final EndpointConfig endpointConfig) {
120126

121127
Map<String, String> headers = new HashMap<String, String>();
122128
// TODO: We don't support multi map header, which cause => https://github.com/Atmosphere/atmosphere/issues/1945
123-
for (Map.Entry<String, List<String>> e : handshakeRequest.getHeaders().entrySet()) {
129+
for (Map.Entry<String, List<String>> e : handshakeHeaders.entrySet()) {
124130
headers.put(e.getKey(), !e.getValue().isEmpty() ? e.getValue().get(0) : "");
125131
}
126132

@@ -182,25 +188,25 @@ public void onOpen(Session session, final EndpointConfig endpointConfig) {
182188
// https://java.net/jira/browse/WEBSOCKET_SPEC-228
183189
if ((!requestURL.startsWith("http://")) || (!requestURL.startsWith("https://"))) {
184190
if (requestURL.startsWith("/")) {
185-
List<String> l = handshakeRequest.getHeaders().get("origin");
191+
List<String> l = handshakeHeaders.get("origin");
186192
if (l == null) {
187193
// https://issues.jboss.org/browse/UNDERTOW-252
188-
l = handshakeRequest.getHeaders().get("Origin");
194+
l = handshakeHeaders.get("Origin");
189195
}
190196
String origin = null;
191197
if (l != null && !l.isEmpty()) {
192198
origin = l.get(0);
193-
}
194-
195-
// There is a weird use case when 'Origin' header may contain
199+
}
200+
201+
// There is a weird use case when 'Origin' header may contain
196202
// 'null', not as value but as a string. In this case the requestURL
197203
// become something like 'null/path/to/resource'.
198204
if (origin == null || origin.equalsIgnoreCase("null")) {
199205
// Broken WebSocket Spec
200206
logger.trace("Unable to retrieve the `origin` header for websocket {}", session);
201207
origin = new StringBuilder("http").append(session.isSecure() ? "s" : "").append("://0.0.0.0:80").toString();
202208
}
203-
209+
204210
requestURL = new StringBuilder(origin).append(requestURL).toString();
205211
} else if (requestURL.startsWith("ws://")) {
206212
requestURL = requestURL.replace("ws://", "http://");
@@ -209,20 +215,20 @@ public void onOpen(Session session, final EndpointConfig endpointConfig) {
209215
}
210216
}
211217

212-
List<String> cookieHeaders = handshakeRequest.getHeaders().get("Cookie");
218+
List<String> cookieHeaders = handshakeHeaders.get("Cookie");
213219
Set<Cookie> cookies = null;
214220
if (cookieHeaders != null) {
215221
cookies = new HashSet<Cookie>();
216222
for (String cookieHeader : cookieHeaders)
217-
cookies.addAll(CookieUtil.ServerCookieDecoder.STRICT.decode(cookieHeader));
223+
cookies.addAll(CookieUtil.ServerCookieDecoder.STRICT.decode(cookieHeader));
218224
}
219-
225+
220226
request = new AtmosphereRequestImpl.Builder()
221227
.requestURI(uri.getPath())
222228
.requestURL(requestURL)
223229
.headers(headers)
224230
.cookies(cookies)
225-
.session((HttpSession) handshakeRequest.getHttpSession())
231+
.session(handshakeSession)
226232
.servletPath(servletPath)
227233
.contextPath(framework.getServletContext().getContextPath())
228234
.pathInfo(pathInfo)
@@ -280,7 +286,7 @@ public void onMessage(ByteBuffer bb) {
280286
onClose(session, new CloseReason(CloseReason.CloseCodes.GOING_AWAY, "Session closed already"));
281287
}
282288
} catch (Throwable e) {
283-
if (session.isOpen()){
289+
if (session.isOpen()) {
284290
logger.error("", e);
285291
} else {
286292
logger.trace("Session closed during onOpen", e);

0 commit comments

Comments
 (0)