Skip to content

Commit

Permalink
Server WebSocket handshake should set the HttpServerResponse as writt…
Browse files Browse the repository at this point in the history
…en - see #4020 - see #4016
  • Loading branch information
vietj committed Jul 19, 2021
1 parent 2c870a9 commit a2ce185
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
12 changes: 12 additions & 0 deletions src/main/java/io/vertx/core/http/impl/Http1xServerResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,18 @@ private void end(Buffer chunk, PromiseInternal<Void> listener) {
}
}

void completeHandshake() {
if (conn.metrics != null) {
conn.metrics.responseBegin(requestMetric, this);
}
setStatusCode(101);
synchronized (conn) {
headWritten = true;
written = true;
}
conn.responseComplete();
}

@Override
public void close() {
synchronized (conn) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,22 +185,16 @@ private void handleHandshake(int sc) {

private void doHandshake() {
Channel channel = conn.channel();
Object metric;
Http1xServerResponse response = request.response();
try {
handshaker.handshake(channel, request.nettyRequest());
metric = request.metric;
} catch (Exception e) {
response.setStatusCode(BAD_REQUEST.code()).end();
throw e;
} finally {
request = null;
}
response.setStatusCode(101);
if (conn.metrics != null) {
conn.metrics.responseBegin(metric, response);
}
conn.responseComplete();
response.completeHandshake();
status = SWITCHING_PROTOCOLS.code();
subProtocol(handshaker.selectedSubprotocol());
// remove compressor as its not needed anymore once connection was upgraded to websockets
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/io/vertx/core/http/WebSocketTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1956,6 +1956,18 @@ private void testUpgrade(boolean delayed) {
server.requestHandler(request -> {
Runnable runner = () -> {
request.toWebSocket().onComplete(onSuccess(ws -> {
HttpServerResponse response = request.response();
assertTrue(response.ended());
try {
response.putHeader("foo", "bar");
fail();
} catch (IllegalStateException ignore) {
}
try {
response.end();
fail();
} catch (IllegalStateException ignore) {
}
ws.handler(buff -> {
ws.write(Buffer.buffer("helloworld"));
ws.close();
Expand Down

0 comments on commit a2ce185

Please sign in to comment.