|
18 | 18 | import java.nio.charset.Charset;
|
19 | 19 | import java.util.Collection;
|
20 | 20 | import java.util.List;
|
| 21 | +import java.util.Map; |
21 | 22 | import java.util.logging.Level;
|
22 | 23 | import java.util.logging.Logger;
|
23 | 24 |
|
24 | 25 | import io.helidon.common.http.Http;
|
25 | 26 | import io.helidon.webserver.ForwardingHandler;
|
26 |
| - |
27 | 27 | import io.netty.buffer.Unpooled;
|
28 | 28 | import io.netty.channel.ChannelFuture;
|
29 | 29 | import io.netty.channel.ChannelFutureListener;
|
|
41 | 41 | import org.glassfish.tyrus.core.TyrusUpgradeResponse;
|
42 | 42 |
|
43 | 43 | class WebSocketUpgradeCodec implements HttpServerUpgradeHandler.UpgradeCodec {
|
44 |
| - |
45 | 44 | private static final Logger LOGGER = Logger.getLogger(WebSocketUpgradeCodec.class.getName());
|
46 | 45 |
|
| 46 | + private static final String SEC_WEBSOCKET_ACCEPT = "Sec-WebSocket-Accept"; |
| 47 | + private static final String SEC_WEBSOCKET_PROTOCOL = "Sec-WebSocket-Protocol"; |
| 48 | + |
47 | 49 | private final WebSocketRouting webSocketRouting;
|
48 | 50 | private String path;
|
49 | 51 | private WebSocketHandler wsHandler;
|
@@ -73,23 +75,26 @@ public boolean prepareUpgradeResponse(ChannelHandlerContext ctx,
|
73 | 75 | TyrusUpgradeResponse upgradeResponse = wsHandler.upgradeResponse();
|
74 | 76 | if (upgradeResponse.getStatus() != Http.Status.SWITCHING_PROTOCOLS_101.code()) {
|
75 | 77 | // prepare headers for failed response
|
76 |
| - upgradeResponse.getHeaders().remove(Http.Header.UPGRADE); |
77 |
| - upgradeResponse.getHeaders().remove(Http.Header.CONNECTION); |
78 |
| - upgradeResponse.getHeaders().remove("sec-websocket-accept"); |
| 78 | + Map<String, List<String>> upgradeHeaders = upgradeResponse.getHeaders(); |
| 79 | + upgradeHeaders.remove(Http.Header.UPGRADE); |
| 80 | + upgradeHeaders.remove(Http.Header.CONNECTION); |
| 81 | + upgradeHeaders.remove(SEC_WEBSOCKET_ACCEPT); |
| 82 | + upgradeHeaders.remove(SEC_WEBSOCKET_PROTOCOL); |
79 | 83 | HttpHeaders headers = new DefaultHttpHeaders();
|
80 |
| - upgradeResponse.getHeaders().forEach(headers::add); |
| 84 | + upgradeHeaders.forEach(headers::add); |
81 | 85 |
|
82 | 86 | // set payload as text/plain with reason phrase
|
83 | 87 | headers.add(Http.Header.CONTENT_TYPE, "text/plain");
|
84 | 88 | String reasonPhrase = upgradeResponse.getReasonPhrase() == null ? ""
|
85 | 89 | : upgradeResponse.getReasonPhrase();
|
86 |
| - HttpResponse r = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, |
| 90 | + HttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, |
87 | 91 | HttpResponseStatus.valueOf(upgradeResponse.getStatus()),
|
88 | 92 | Unpooled.wrappedBuffer(reasonPhrase.getBytes(Charset.defaultCharset())),
|
89 |
| - headers, EmptyHttpHeaders.INSTANCE); |
| 93 | + headers, |
| 94 | + EmptyHttpHeaders.INSTANCE); // trailing headers |
90 | 95 |
|
91 | 96 | // write, flush and later close connection
|
92 |
| - ChannelFuture writeComplete = ctx.writeAndFlush(r); |
| 97 | + ChannelFuture writeComplete = ctx.writeAndFlush(httpResponse); |
93 | 98 | writeComplete.addListener(ChannelFutureListener.CLOSE);
|
94 | 99 | return false;
|
95 | 100 | }
|
|
0 commit comments