use of io.netty.handler.codec.http.FullHttpRequest in project netty by netty.
the class Http2ServerDowngrader method decode.
@Override
protected void decode(ChannelHandlerContext ctx, Http2StreamFrame frame, List<Object> out) throws Exception {
if (frame instanceof Http2HeadersFrame) {
// not really the id
int id = 0;
Http2HeadersFrame headersFrame = (Http2HeadersFrame) frame;
Http2Headers headers = headersFrame.headers();
if (headersFrame.isEndStream()) {
if (headers.method() == null) {
LastHttpContent last = new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER, validateHeaders);
HttpConversionUtil.addHttp2ToHttpHeaders(id, headers, last.trailingHeaders(), HttpVersion.HTTP_1_1, true, true);
out.add(last);
} else {
FullHttpRequest full = HttpConversionUtil.toFullHttpRequest(id, headers, ctx.alloc(), validateHeaders);
out.add(full);
}
} else {
HttpRequest req = HttpConversionUtil.toHttpRequest(id, headersFrame.headers(), validateHeaders);
if (!HttpUtil.isContentLengthSet(req)) {
req.headers().add(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
}
out.add(req);
}
} else if (frame instanceof Http2DataFrame) {
Http2DataFrame dataFrame = (Http2DataFrame) frame;
if (dataFrame.isEndStream()) {
out.add(new DefaultLastHttpContent(dataFrame.content(), validateHeaders));
} else {
out.add(new DefaultHttpContent(dataFrame.content()));
}
}
ReferenceCountUtil.retain(frame);
}
use of io.netty.handler.codec.http.FullHttpRequest in project ratpack by ratpack.
the class WebSocketEngine method connect.
@SuppressWarnings("deprecation")
public static <T> void connect(final Context context, String path, int maxLength, final WebSocketHandler<T> handler) {
PublicAddress publicAddress = context.get(PublicAddress.class);
URI address = publicAddress.get(context);
URI httpPath = address.resolve(path);
URI wsPath;
try {
wsPath = new URI("ws", httpPath.getUserInfo(), httpPath.getHost(), httpPath.getPort(), httpPath.getPath(), httpPath.getQuery(), httpPath.getFragment());
} catch (URISyntaxException e) {
throw uncheck(e);
}
WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(wsPath.toString(), null, false, maxLength);
Request request = context.getRequest();
HttpMethod method = valueOf(request.getMethod().getName());
FullHttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, request.getUri());
nettyRequest.headers().add(SEC_WEBSOCKET_VERSION, request.getHeaders().get(SEC_WEBSOCKET_VERSION));
nettyRequest.headers().add(SEC_WEBSOCKET_KEY, request.getHeaders().get(SEC_WEBSOCKET_KEY));
final WebSocketServerHandshaker handshaker = factory.newHandshaker(nettyRequest);
final DirectChannelAccess directChannelAccess = context.getDirectChannelAccess();
final Channel channel = directChannelAccess.getChannel();
if (!channel.config().isAutoRead()) {
channel.config().setAutoRead(true);
}
handshaker.handshake(channel, nettyRequest).addListener(new HandshakeFutureListener<>(context, handshaker, handler));
}
use of io.netty.handler.codec.http.FullHttpRequest in project camel by apache.
the class NettyHttpConverter method convertToHttpRequest.
/**
* A fallback converter that allows us to easily call Java beans and use the raw Netty {@link HttpRequest} as parameter types.
*/
@FallbackConverter
public static Object convertToHttpRequest(Class<?> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
// if we want to covert to HttpRequest
if (value != null && HttpRequest.class.isAssignableFrom(type)) {
// okay we may need to cheat a bit when we want to grab the HttpRequest as its stored on the NettyHttpMessage
// so if the message instance is a NettyHttpMessage and its body is the value, then we can grab the
// HttpRequest from the NettyHttpMessage
NettyHttpMessage msg;
if (exchange.hasOut()) {
msg = exchange.getOut(NettyHttpMessage.class);
} else {
msg = exchange.getIn(NettyHttpMessage.class);
}
if (msg != null && msg.getBody() == value) {
// ensure the http request content is reset so we can read all the content out-of-the-box
FullHttpRequest request = msg.getHttpRequest();
request.content().resetReaderIndex();
return request;
}
}
return null;
}
use of io.netty.handler.codec.http.FullHttpRequest in project camel by apache.
the class NettyHttpEndpoint method createExchange.
@Override
public Exchange createExchange(ChannelHandlerContext ctx, Object message) throws Exception {
Exchange exchange = createExchange();
FullHttpRequest request = (FullHttpRequest) message;
Message in = getNettyHttpBinding().toCamelMessage(request, exchange, getConfiguration());
exchange.setIn(in);
// setup the common message headers
updateMessageHeader(in, ctx);
// honor the character encoding
String contentType = in.getHeader(Exchange.CONTENT_TYPE, String.class);
String charset = NettyHttpHelper.getCharsetFromContentType(contentType);
if (charset != null) {
exchange.setProperty(Exchange.CHARSET_NAME, charset);
in.setHeader(Exchange.HTTP_CHARACTER_ENCODING, charset);
}
return exchange;
}
use of io.netty.handler.codec.http.FullHttpRequest in project rest.li by linkedin.
the class TestRAPClientCodec method testRequestEncoder.
@Test(dataProvider = "restRequest")
public void testRequestEncoder(String uri, RestRequest request) {
final EmbeddedChannel ch = new EmbeddedChannel(new RAPClientCodec());
ch.writeOutbound(request);
FullHttpRequest nettyRequest = (FullHttpRequest) ch.readOutbound();
Assert.assertEquals(nettyRequest.uri(), uri);
Assert.assertEquals(nettyRequest.method(), HttpMethod.valueOf(request.getMethod()));
Assert.assertEquals(nettyRequest.content().toString(CHARSET), request.getEntity().asString(CHARSET));
Assert.assertEquals(nettyRequest.headers().get(HttpHeaderNames.HOST), HOST);
assertList(nettyRequest.headers().getAll(HttpConstants.REQUEST_COOKIE_HEADER_NAME), request.getCookies());
for (String name : request.getHeaders().keySet()) {
Assert.assertEquals(nettyRequest.headers().get(name), request.getHeader(name));
}
ch.finish();
}
Aggregations