Skip to content

Commit

Permalink
Remove session on 4xx response from WebSocket handshake
Browse files Browse the repository at this point in the history
Closes gh-25608
  • Loading branch information
rstoyanchev committed Sep 7, 2020
1 parent 94c91c9 commit d616c66
Showing 1 changed file with 12 additions and 1 deletion.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,6 +34,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -270,6 +271,7 @@ else if (transportType.supportsCors()) {
}

SockJsSession session = this.sessions.get(sessionId);
boolean isNewSession = false;
if (session == null) {
if (transportHandler instanceof SockJsSessionFactory) {
Map<String, Object> attributes = new HashMap<>();
Expand All @@ -278,6 +280,7 @@ else if (transportType.supportsCors()) {
}
SockJsSessionFactory sessionFactory = (SockJsSessionFactory) transportHandler;
session = createSockJsSession(sessionId, sessionFactory, handler, attributes);
isNewSession = true;
}
else {
response.setStatusCode(HttpStatus.NOT_FOUND);
Expand Down Expand Up @@ -311,6 +314,14 @@ else if (transportType.supportsCors()) {
}

transportHandler.handleRequest(request, response, handler, session);

if (isNewSession && (response instanceof ServletServerHttpResponse)) {
int status = ((ServletServerHttpResponse) response).getServletResponse().getStatus();
if (HttpStatus.valueOf(status).is4xxClientError()) {
this.sessions.remove(sessionId);
}
}

chain.applyAfterHandshake(request, response, null);
}
catch (SockJsException ex) {
Expand Down

0 comments on commit d616c66

Please sign in to comment.