Skip to content

Commit e46edce

Browse files
Use already pre existing reponse header to do websocket handshake (#5324)
1 parent 4ab5f09 commit e46edce

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/main/java/io/vertx/core/http/impl/ServerWebSocketImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import io.netty.channel.Channel;
1515
import io.netty.channel.ChannelHandler;
1616
import io.netty.channel.ChannelPipeline;
17+
import io.netty.handler.codec.http.HttpHeaders;
1718
import io.netty.handler.codec.http.HttpResponseStatus;
1819
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker;
1920
import io.vertx.core.AsyncResult;
@@ -189,7 +190,7 @@ private void doHandshake() {
189190
Channel channel = conn.channel();
190191
Http1xServerResponse response = request.response();
191192
try {
192-
handshaker.handshake(channel, request.nettyRequest());
193+
handshaker.handshake(channel, request.nettyRequest(), (HttpHeaders) response.headers(), channel.newPromise());
193194
} catch (Exception e) {
194195
response.setStatusCode(BAD_REQUEST.code()).end();
195196
throw e;

src/test/java/io/vertx/core/http/WebSocketTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3940,4 +3940,32 @@ public void testClientWebSocketExceptionHandlerIsCalled() {
39403940
await();
39413941
}
39423942

3943+
@Test
3944+
public void testCustomResponseHeadersBeforeUpgrade() {
3945+
String path = "/some/path";
3946+
String message = "here is some text data";
3947+
String headerKey = "custom";
3948+
String headerValue = "value";
3949+
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).requestHandler(req -> {
3950+
req.response().headers().set(headerKey, headerValue);
3951+
req.toWebSocket()
3952+
.onComplete(event -> {
3953+
ServerWebSocket serverWebSocket = event.result();
3954+
serverWebSocket.accept();
3955+
serverWebSocket.writeFinalTextFrame(message);
3956+
});
3957+
});
3958+
server.listen(onSuccess(s -> {
3959+
client = vertx.createWebSocketClient();
3960+
client.connect(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, onSuccess(ws -> {
3961+
assertTrue(ws.headers().contains(headerKey));
3962+
assertEquals(headerValue, ws.headers().get(headerKey));
3963+
ws.handler(buff -> {
3964+
assertEquals(message, buff.toString("UTF-8"));
3965+
testComplete();
3966+
});
3967+
}));
3968+
}));
3969+
await();
3970+
}
39433971
}

0 commit comments

Comments
 (0)