use of io.netty.handler.codec.http.HttpMessage in project netty by netty.
the class Http2ServerInitializer method configureClearText.
/**
* Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0
*/
private void configureClearText(SocketChannel ch) {
final ChannelPipeline p = ch.pipeline();
final HttpServerCodec sourceCodec = new HttpServerCodec();
final HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory);
final CleartextHttp2ServerUpgradeHandler cleartextHttp2ServerUpgradeHandler = new CleartextHttp2ServerUpgradeHandler(sourceCodec, upgradeHandler, new HelloWorldHttp2HandlerBuilder().build());
p.addLast(cleartextHttp2ServerUpgradeHandler);
p.addLast(new SimpleChannelInboundHandler<HttpMessage>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception {
// If this handler is hit then no upgrade has been attempted and the client is just talking HTTP.
System.err.println("Directly talking: " + msg.protocolVersion() + " (no upgrade was attempted)");
ChannelPipeline pipeline = ctx.pipeline();
ChannelHandlerContext thisCtx = pipeline.context(this);
pipeline.addAfter(thisCtx.name(), null, new HelloWorldHttp1Handler("Direct. No Upgrade Attempted."));
pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength));
ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
}
});
p.addLast(new UserEventLogger());
}
use of io.netty.handler.codec.http.HttpMessage in project netty by netty.
the class Http2StreamFrameToHttpObjectCodec method encode.
/**
* Encode from an {@link HttpObject} to an {@link Http2StreamFrame}. This method will
* be called for each written message that can be handled by this encoder.
*
* NOTE: 100-Continue responses that are NOT {@link FullHttpResponse} will be rejected.
*
* @param ctx the {@link ChannelHandlerContext} which this handler belongs to
* @param obj the {@link HttpObject} message to encode
* @param out the {@link List} into which the encoded msg should be added
* needs to do some kind of aggregation
* @throws Exception is thrown if an error occurs
*/
@Override
protected void encode(ChannelHandlerContext ctx, HttpObject obj, List<Object> out) throws Exception {
// Http2HeadersFrame should not be marked as endStream=true
if (obj instanceof HttpResponse) {
final HttpResponse res = (HttpResponse) obj;
if (res.status().equals(HttpResponseStatus.CONTINUE)) {
if (res instanceof FullHttpResponse) {
final Http2Headers headers = toHttp2Headers(ctx, res);
out.add(new DefaultHttp2HeadersFrame(headers, false));
return;
} else {
throw new EncoderException(HttpResponseStatus.CONTINUE + " must be a FullHttpResponse");
}
}
}
if (obj instanceof HttpMessage) {
Http2Headers headers = toHttp2Headers(ctx, (HttpMessage) obj);
boolean noMoreFrames = false;
if (obj instanceof FullHttpMessage) {
FullHttpMessage full = (FullHttpMessage) obj;
noMoreFrames = !full.content().isReadable() && full.trailingHeaders().isEmpty();
}
out.add(new DefaultHttp2HeadersFrame(headers, noMoreFrames));
}
if (obj instanceof LastHttpContent) {
LastHttpContent last = (LastHttpContent) obj;
encodeLastContent(last, out);
} else if (obj instanceof HttpContent) {
HttpContent cont = (HttpContent) obj;
out.add(new DefaultHttp2DataFrame(cont.content().retain(), false));
}
}
use of io.netty.handler.codec.http.HttpMessage in project jersey by jersey.
the class JerseyServerInitializer method configureClearText.
/**
* Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.
*/
private void configureClearText(SocketChannel ch) {
final ChannelPipeline p = ch.pipeline();
final HttpServerCodec sourceCodec = new HttpServerCodec();
p.addLast(sourceCodec);
p.addLast(new HttpServerUpgradeHandler(sourceCodec, new HttpServerUpgradeHandler.UpgradeCodecFactory() {
@Override
public HttpServerUpgradeHandler.UpgradeCodec newUpgradeCodec(CharSequence protocol) {
if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) {
return new Http2ServerUpgradeCodec(new Http2Codec(true, new JerseyHttp2ServerHandler(baseUri, container)));
} else {
return null;
}
}
}));
p.addLast(new SimpleChannelInboundHandler<HttpMessage>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception {
// If this handler is hit then no upgrade has been attempted and the client is just talking HTTP.
// "Directly talking: " + msg.protocolVersion() + " (no upgrade was attempted)");
ChannelPipeline pipeline = ctx.pipeline();
ChannelHandlerContext thisCtx = pipeline.context(this);
pipeline.addAfter(thisCtx.name(), null, new JerseyServerHandler(baseUri, container));
pipeline.replace(this, null, new ChunkedWriteHandler());
ctx.fireChannelRead(msg);
}
});
}
use of io.netty.handler.codec.http.HttpMessage in project riposte by Nike-Inc.
the class RequestStateCleanerHandlerTest method channelRead_does_nothing_if_msg_is_not_HttpRequest_or_LastHttpContent.
@Test
public void channelRead_does_nothing_if_msg_is_not_HttpRequest_or_LastHttpContent() throws Exception {
// given
HttpMessage ignoredMsgMock = mock(HttpMessage.class);
// when
handler.channelRead(ctxMock, ignoredMsgMock);
// then
// the normal continuation behavior from the super class.
verify(ctxMock).fireChannelRead(ignoredMsgMock);
// nothing else should have happened related to the ctx.
verifyNoMoreInteractions(ctxMock);
verifyZeroInteractions(stateMock);
verifyZeroInteractions(metricsListenerMock);
}
use of io.netty.handler.codec.http.HttpMessage in project janusgraph by JanusGraph.
the class SaslAndHMACAuthenticationHandlerTest method testHttpChannelReadWhenAuthenticatorHasNotBeenAdded.
@Test
public void testHttpChannelReadWhenAuthenticatorHasNotBeenAdded() throws Exception {
final HMACAuthenticator hmacAuth = createMock(HMACAuthenticator.class);
final SaslAndHMACAuthenticator authenticator = createMock(SaslAndHMACAuthenticator.class);
final ChannelHandlerContext ctx = createMock(ChannelHandlerContext.class);
final ChannelPipeline pipeline = createMock(ChannelPipeline.class);
final HttpMessage msg = createMock(HttpMessage.class);
final HttpHeaders headers = createMock(HttpHeaders.class);
expect(authenticator.getHMACAuthenticator()).andReturn(hmacAuth);
expect(authenticator.getSimpleAuthenticator()).andReturn(createMock(JanusGraphSimpleAuthenticator.class));
expect(ctx.pipeline()).andReturn(pipeline).times(2);
expect(pipeline.get("hmac_authenticator")).andReturn(null);
expect(pipeline.addAfter(eq(PIPELINE_AUTHENTICATOR), eq("hmac_authenticator"), isA(ChannelHandler.class))).andReturn(null);
expect(msg.headers()).andReturn(headers).times(2);
expect(headers.get(isA(String.class))).andReturn(null).times(2);
expect(ctx.fireChannelRead(eq(msg))).andReturn(ctx);
replayAll();
final SaslAndHMACAuthenticationHandler handler = new SaslAndHMACAuthenticationHandler(authenticator, null);
handler.channelRead(ctx, msg);
}
Aggregations