use of io.netty.channel.ChannelHandler in project grpc-java by grpc.
the class SdsProtocolNegotiatorsTest method serverSdsHandler_nullTlsContext_expectFallbackProtocolNegotiator.
@Test
public void serverSdsHandler_nullTlsContext_expectFallbackProtocolNegotiator() {
ChannelHandler mockChannelHandler = mock(ChannelHandler.class);
ProtocolNegotiator mockProtocolNegotiator = mock(ProtocolNegotiator.class);
when(mockProtocolNegotiator.newHandler(grpcHandler)).thenReturn(mockChannelHandler);
SdsProtocolNegotiators.HandlerPickerHandler handlerPickerHandler = new SdsProtocolNegotiators.HandlerPickerHandler(grpcHandler, mockProtocolNegotiator);
pipeline.addLast(handlerPickerHandler);
channelHandlerCtx = pipeline.context(handlerPickerHandler);
// should find HandlerPickerHandler
assertThat(channelHandlerCtx).isNotNull();
// kick off protocol negotiation
pipeline.fireUserEventTriggered(InternalProtocolNegotiationEvent.getDefault());
channelHandlerCtx = pipeline.context(handlerPickerHandler);
assertThat(channelHandlerCtx).isNull();
// need this for tasks to execute on eventLoop
channel.runPendingTasks();
Iterator<Map.Entry<String, ChannelHandler>> iterator = pipeline.iterator();
assertThat(iterator.next().getValue()).isSameInstanceAs(mockChannelHandler);
// no more handlers in the pipeline
assertThat(iterator.hasNext()).isFalse();
}
use of io.netty.channel.ChannelHandler in project grpc-java by grpc.
the class SdsProtocolNegotiatorsTest method clientSdsProtocolNegotiatorNewHandler_withTlsContextAttribute.
@Test
public void clientSdsProtocolNegotiatorNewHandler_withTlsContextAttribute() {
UpstreamTlsContext upstreamTlsContext = CommonTlsContextTestsUtil.buildUpstreamTlsContext(CommonTlsContext.newBuilder().build());
ClientSdsProtocolNegotiator pn = new ClientSdsProtocolNegotiator(InternalProtocolNegotiators.plaintext());
GrpcHttp2ConnectionHandler mockHandler = mock(GrpcHttp2ConnectionHandler.class);
ChannelLogger logger = mock(ChannelLogger.class);
doNothing().when(logger).log(any(ChannelLogLevel.class), anyString());
when(mockHandler.getNegotiationLogger()).thenReturn(logger);
TlsContextManager mockTlsContextManager = mock(TlsContextManager.class);
when(mockHandler.getEagAttributes()).thenReturn(Attributes.newBuilder().set(InternalXdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER, new SslContextProviderSupplier(upstreamTlsContext, mockTlsContextManager)).build());
ChannelHandler newHandler = pn.newHandler(mockHandler);
assertThat(newHandler).isNotNull();
assertThat(newHandler).isInstanceOf(ClientSdsHandler.class);
}
use of io.netty.channel.ChannelHandler in project vert.x by eclipse.
the class Http1xUpgradeToH2CHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HttpRequest) {
HttpRequest request = (HttpRequest) msg;
if (request.headers().contains(io.vertx.core.http.HttpHeaders.UPGRADE, Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, true)) {
String connection = request.headers().get(io.vertx.core.http.HttpHeaders.CONNECTION);
int found = 0;
if (connection != null && connection.length() > 0) {
StringBuilder buff = new StringBuilder();
int pos = 0;
int len = connection.length();
while (pos < len) {
char c = connection.charAt(pos++);
if (c != ' ' && c != ',') {
buff.append(Character.toLowerCase(c));
}
if (c == ',' || pos == len) {
if (buff.indexOf("upgrade") == 0 && buff.length() == 7) {
found |= 1;
} else if (buff.indexOf("http2-settings") == 0 && buff.length() == 14) {
found |= 2;
}
buff.setLength(0);
}
}
}
if (found == 3) {
String settingsHeader = request.headers().get(Http2CodecUtil.HTTP_UPGRADE_SETTINGS_HEADER);
if (settingsHeader != null) {
Http2Settings settings = HttpUtils.decodeSettings(settingsHeader);
if (settings != null) {
if (initializer.context.isEventLoopContext()) {
ChannelPipeline pipeline = ctx.pipeline();
DefaultFullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, SWITCHING_PROTOCOLS, Unpooled.EMPTY_BUFFER, false);
res.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE);
res.headers().add(HttpHeaderNames.UPGRADE, Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME);
ctx.writeAndFlush(res);
pipeline.remove("httpEncoder");
if (isCompressionSupported) {
pipeline.remove("deflater");
pipeline.remove("chunkedWriter");
}
if (isDecompressionSupported) {
pipeline.remove("inflater");
}
handler = initializer.buildHttp2ConnectionHandler(initializer.context, initializer.connectionHandler);
pipeline.addLast("handler", handler);
handler.serverUpgrade(ctx, settings, request);
DefaultHttp2Headers headers = new DefaultHttp2Headers();
headers.method(request.method().name());
headers.path(request.uri());
headers.authority(request.headers().get("host"));
headers.scheme("http");
request.headers().remove("http2-settings");
request.headers().remove("host");
request.headers().forEach(header -> headers.set(header.getKey().toLowerCase(), header.getValue()));
ctx.fireChannelRead(new DefaultHttp2HeadersFrame(headers, false));
} else {
HttpServerImpl.log.warn("Cannot perform HTTP/2 upgrade in a worker verticle");
}
}
}
}
if (handler == null) {
DefaultFullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST, Unpooled.EMPTY_BUFFER, false);
res.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
ctx.writeAndFlush(res);
}
} else {
initializer.configureHttp1(ctx.pipeline());
ctx.fireChannelRead(msg);
ctx.pipeline().remove(this);
}
} else {
if (handler != null) {
if (msg instanceof HttpContent) {
HttpContent content = (HttpContent) msg;
ByteBuf buf = VertxHandler.safeBuffer(content.content());
boolean end = msg instanceof LastHttpContent;
ctx.fireChannelRead(new DefaultHttp2DataFrame(buf, end, 0));
if (end) {
ChannelPipeline pipeline = ctx.pipeline();
for (Map.Entry<String, ChannelHandler> handler : pipeline) {
if (handler.getValue() instanceof Http2ConnectionHandler) {
// Continue
} else {
pipeline.remove(handler.getKey());
}
}
initializer.configureHttp2(pipeline);
}
} else {
// We might have left over buffer sent when removing the HTTP decoder that needs to be propagated to the HTTP handler
super.channelRead(ctx, msg);
}
}
}
}
use of io.netty.channel.ChannelHandler in project vert.x by eclipse.
the class WebSocketHandshakeInboundHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof HttpResponse) {
HttpResponse resp = (HttpResponse) msg;
response = new DefaultFullHttpResponse(resp.protocolVersion(), resp.status());
response.headers().add(resp.headers());
}
if (msg instanceof HttpContent) {
HttpContent content = (HttpContent) msg;
try {
if (response != null) {
response.content().writeBytes(content.content());
if (msg instanceof LastHttpContent) {
response.trailingHeaders().add(((LastHttpContent) msg).trailingHeaders());
ChannelPipeline pipeline = chctx.pipeline();
pipeline.remove(WebSocketHandshakeInboundHandler.this);
ChannelHandler handler = pipeline.get(HttpContentDecompressor.class);
if (handler != null) {
// remove decompressor as its not needed anymore once connection was upgraded to WebSocket
ctx.pipeline().remove(handler);
}
Future<HeadersAdaptor> fut = handshakeComplete(response);
wsHandler.handle(fut);
}
}
} finally {
content.release();
}
}
}
use of io.netty.channel.ChannelHandler in project vert.x by eclipse.
the class HttpChannelConnector method wrap.
public Future<HttpClientConnection> wrap(EventLoopContext context, NetSocket so_) {
NetSocketImpl so = (NetSocketImpl) so_;
Object metric = so.metric();
Promise<HttpClientConnection> promise = context.promise();
// Remove all un-necessary handlers
ChannelPipeline pipeline = so.channelHandlerContext().pipeline();
List<ChannelHandler> removedHandlers = new ArrayList<>();
for (Map.Entry<String, ChannelHandler> stringChannelHandlerEntry : pipeline) {
ChannelHandler handler = stringChannelHandlerEntry.getValue();
if (!(handler instanceof SslHandler)) {
removedHandlers.add(handler);
}
}
removedHandlers.forEach(pipeline::remove);
//
Channel ch = so.channelHandlerContext().channel();
if (ssl) {
String protocol = so.applicationLayerProtocol();
if (useAlpn) {
if ("h2".equals(protocol)) {
applyHttp2ConnectionOptions(ch.pipeline());
http2Connected(context, metric, ch, promise);
} else {
applyHttp1xConnectionOptions(ch.pipeline());
HttpVersion fallbackProtocol = "http/1.0".equals(protocol) ? HttpVersion.HTTP_1_0 : HttpVersion.HTTP_1_1;
http1xConnected(fallbackProtocol, server, true, context, metric, ch, promise);
}
} else {
applyHttp1xConnectionOptions(ch.pipeline());
http1xConnected(version, server, true, context, metric, ch, promise);
}
} else {
if (version == HttpVersion.HTTP_2) {
if (this.options.isHttp2ClearTextUpgrade()) {
applyHttp1xConnectionOptions(pipeline);
http1xConnected(version, server, false, context, metric, ch, promise);
} else {
applyHttp2ConnectionOptions(pipeline);
http2Connected(context, metric, ch, promise);
}
} else {
applyHttp1xConnectionOptions(pipeline);
http1xConnected(version, server, false, context, metric, ch, promise);
}
}
return promise.future();
}
Aggregations