use of io.micronaut.http.netty.NettyHttpHeaders in project micronaut-core by micronaut-projects.
the class NettyServerWebSocketUpgradeHandler method handleHandshake.
/**
* Do the handshaking for WebSocket request.
*
* @param ctx The channel handler context
* @param req The request
* @param webSocketBean The web socket bean
* @param response The response
* @return The channel future
*/
protected ChannelFuture handleHandshake(ChannelHandlerContext ctx, NettyHttpRequest req, WebSocketBean<?> webSocketBean, MutableHttpResponse<?> response) {
int maxFramePayloadLength = webSocketBean.messageMethod().map(m -> m.intValue(OnMessage.class, "maxPayloadLength").orElse(65536)).orElse(65536);
String subprotocols = webSocketBean.getBeanDefinition().stringValue(ServerWebSocket.class, "subprotocols").filter(s -> !StringUtils.isEmpty(s)).orElse(null);
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketURL(ctx, req), subprotocols, true, maxFramePayloadLength);
handshaker = wsFactory.newHandshaker(req.getNativeRequest());
MutableHttpHeaders headers = response.getHeaders();
io.netty.handler.codec.http.HttpHeaders nettyHeaders;
if (headers instanceof NettyHttpHeaders) {
nettyHeaders = ((NettyHttpHeaders) headers).getNettyHeaders();
} else {
nettyHeaders = new DefaultHttpHeaders();
for (Map.Entry<String, List<String>> entry : headers) {
nettyHeaders.add(entry.getKey(), entry.getValue());
}
}
Channel channel = ctx.channel();
if (handshaker == null) {
return WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(channel);
} else {
return handshaker.handshake(channel, req.getNativeRequest(), nettyHeaders, channel.newPromise());
}
}
use of io.micronaut.http.netty.NettyHttpHeaders in project micronaut-core by micronaut-projects.
the class DefaultHttpClient method connectWebSocket.
private <T> Flux<T> connectWebSocket(URI uri, MutableHttpRequest<?> request, Class<T> clientEndpointType, WebSocketBean<T> webSocketBean) {
Bootstrap bootstrap = this.bootstrap.clone();
if (webSocketBean == null) {
webSocketBean = webSocketRegistry.getWebSocket(clientEndpointType);
}
WebSocketBean<T> finalWebSocketBean = webSocketBean;
return Flux.create(emitter -> {
SslContext sslContext = buildSslContext(uri);
WebSocketVersion protocolVersion = finalWebSocketBean.getBeanDefinition().enumValue(ClientWebSocket.class, "version", WebSocketVersion.class).orElse(WebSocketVersion.V13);
int maxFramePayloadLength = finalWebSocketBean.messageMethod().map(m -> m.intValue(OnMessage.class, "maxPayloadLength").orElse(65536)).orElse(65536);
String subprotocol = finalWebSocketBean.getBeanDefinition().stringValue(ClientWebSocket.class, "subprotocol").orElse(StringUtils.EMPTY_STRING);
RequestKey requestKey;
try {
requestKey = new RequestKey(uri);
} catch (HttpClientException e) {
emitter.error(e);
return;
}
bootstrap.remoteAddress(requestKey.getHost(), requestKey.getPort());
initBootstrapForProxy(bootstrap, sslContext != null, requestKey.getHost(), requestKey.getPort());
bootstrap.handler(new HttpClientInitializer(sslContext, requestKey.getHost(), requestKey.getPort(), false, false, false, null) {
@Override
protected void addFinalHandler(ChannelPipeline pipeline) {
pipeline.remove(ChannelPipelineCustomizer.HANDLER_HTTP_DECODER);
ReadTimeoutHandler readTimeoutHandler = pipeline.get(ReadTimeoutHandler.class);
if (readTimeoutHandler != null) {
pipeline.remove(readTimeoutHandler);
}
Optional<Duration> readIdleTime = configuration.getReadIdleTimeout();
if (readIdleTime.isPresent()) {
Duration duration = readIdleTime.get();
if (!duration.isNegative()) {
pipeline.addLast(ChannelPipelineCustomizer.HANDLER_IDLE_STATE, new IdleStateHandler(duration.toMillis(), duration.toMillis(), duration.toMillis(), TimeUnit.MILLISECONDS));
}
}
final NettyWebSocketClientHandler webSocketHandler;
try {
String scheme = (sslContext == null) ? "ws" : "wss";
URI webSocketURL = UriBuilder.of(uri).scheme(scheme).host(host).port(port).build();
MutableHttpHeaders headers = request.getHeaders();
HttpHeaders customHeaders = EmptyHttpHeaders.INSTANCE;
if (headers instanceof NettyHttpHeaders) {
customHeaders = ((NettyHttpHeaders) headers).getNettyHeaders();
}
if (StringUtils.isNotEmpty(subprotocol)) {
customHeaders.add("Sec-WebSocket-Protocol", subprotocol);
}
webSocketHandler = new NettyWebSocketClientHandler<>(request, finalWebSocketBean, WebSocketClientHandshakerFactory.newHandshaker(webSocketURL, protocolVersion, subprotocol, true, customHeaders, maxFramePayloadLength), requestBinderRegistry, mediaTypeCodecRegistry, emitter);
pipeline.addLast(WebSocketClientCompressionHandler.INSTANCE);
pipeline.addLast(ChannelPipelineCustomizer.HANDLER_MICRONAUT_WEBSOCKET_CLIENT, webSocketHandler);
} catch (Throwable e) {
emitter.error(new WebSocketSessionException("Error opening WebSocket client session: " + e.getMessage(), e));
}
}
});
addInstrumentedListener(bootstrap.connect(), future -> {
if (!future.isSuccess()) {
emitter.error(future.cause());
}
});
}, FluxSink.OverflowStrategy.ERROR);
}
Aggregations