use of io.netty.handler.codec.http.FullHttpRequest in project netty by netty.
the class HttpConversionUtil method toFullHttpRequest.
/**
* Create a new object to contain the request data
*
* @param streamId The stream associated with the request
* @param http2Headers The initial set of HTTP/2 headers to create the request with
* @param alloc The {@link ByteBufAllocator} to use to generate the content of the message
* @param validateHttpHeaders <ul>
* <li>{@code true} to validate HTTP headers in the http-codec</li>
* <li>{@code false} not to validate HTTP headers in the http-codec</li>
* </ul>
* @return A new request object which represents headers/data
* @throws Http2Exception see {@link #addHttp2ToHttpHeaders(int, Http2Headers, FullHttpMessage, boolean)}
*/
public static FullHttpRequest toFullHttpRequest(int streamId, Http2Headers http2Headers, ByteBufAllocator alloc, boolean validateHttpHeaders) throws Http2Exception {
// HTTP/2 does not define a way to carry the version identifier that is included in the HTTP/1.1 request line.
final CharSequence method = checkNotNull(http2Headers.method(), "method header cannot be null in conversion to HTTP/1.x");
final CharSequence path = checkNotNull(http2Headers.path(), "path header cannot be null in conversion to HTTP/1.x");
FullHttpRequest msg = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.valueOf(method.toString()), path.toString(), alloc.buffer(), validateHttpHeaders);
try {
addHttp2ToHttpHeaders(streamId, http2Headers, msg, false);
} catch (Http2Exception e) {
msg.release();
throw e;
} catch (Throwable t) {
msg.release();
throw streamError(streamId, PROTOCOL_ERROR, t, "HTTP/2 to HTTP/1.x headers conversion error");
}
return msg;
}
use of io.netty.handler.codec.http.FullHttpRequest in project netty-socketio by mrniko.
the class AuthorizeHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
SchedulerKey key = new SchedulerKey(Type.PING_TIMEOUT, ctx.channel());
disconnectScheduler.cancel(key);
if (msg instanceof FullHttpRequest) {
FullHttpRequest req = (FullHttpRequest) msg;
Channel channel = ctx.channel();
QueryStringDecoder queryDecoder = new QueryStringDecoder(req.uri());
if (!configuration.isAllowCustomRequests() && !queryDecoder.path().startsWith(connectPath)) {
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.BAD_REQUEST);
channel.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE);
req.release();
return;
}
List<String> sid = queryDecoder.parameters().get("sid");
if (queryDecoder.path().equals(connectPath) && sid == null) {
String origin = req.headers().get(HttpHeaderNames.ORIGIN);
if (!authorize(ctx, channel, origin, queryDecoder.parameters(), req)) {
req.release();
return;
}
// forward message to polling or websocket handler to bind channel
}
}
ctx.fireChannelRead(msg);
}
use of io.netty.handler.codec.http.FullHttpRequest in project netty-socketio by mrniko.
the class WrongUrlHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof FullHttpRequest) {
FullHttpRequest req = (FullHttpRequest) msg;
Channel channel = ctx.channel();
QueryStringDecoder queryDecoder = new QueryStringDecoder(req.uri());
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.BAD_REQUEST);
ChannelFuture f = channel.writeAndFlush(res);
f.addListener(ChannelFutureListener.CLOSE);
req.release();
log.warn("Blocked wrong socket.io-context request! url: {}, params: {}, ip: {}", queryDecoder.path(), queryDecoder.parameters(), channel.remoteAddress());
return;
}
super.channelRead(ctx, msg);
}
use of io.netty.handler.codec.http.FullHttpRequest in project netty-socketio by mrniko.
the class PollingTransport method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof FullHttpRequest) {
FullHttpRequest req = (FullHttpRequest) msg;
QueryStringDecoder queryDecoder = new QueryStringDecoder(req.uri());
List<String> transport = queryDecoder.parameters().get("transport");
if (transport != null && NAME.equals(transport.get(0))) {
List<String> sid = queryDecoder.parameters().get("sid");
List<String> j = queryDecoder.parameters().get("j");
List<String> b64 = queryDecoder.parameters().get("b64");
String origin = req.headers().get(HttpHeaderNames.ORIGIN);
ctx.channel().attr(EncoderHandler.ORIGIN).set(origin);
String userAgent = req.headers().get(HttpHeaderNames.USER_AGENT);
ctx.channel().attr(EncoderHandler.USER_AGENT).set(userAgent);
if (j != null && j.get(0) != null) {
Integer index = Integer.valueOf(j.get(0));
ctx.channel().attr(EncoderHandler.JSONP_INDEX).set(index);
}
if (b64 != null && b64.get(0) != null) {
Integer enable = Integer.valueOf(b64.get(0));
ctx.channel().attr(EncoderHandler.B64).set(enable == 1);
}
try {
if (sid != null && sid.get(0) != null) {
final UUID sessionId = UUID.fromString(sid.get(0));
handleMessage(req, sessionId, queryDecoder, ctx);
} else {
// first connection
ClientHead client = ctx.channel().attr(ClientHead.CLIENT).get();
handleMessage(req, client.getSessionId(), queryDecoder, ctx);
}
} finally {
req.release();
}
return;
}
}
ctx.fireChannelRead(msg);
}
use of io.netty.handler.codec.http.FullHttpRequest in project netty-socketio by mrniko.
the class WebSocketTransport method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof CloseWebSocketFrame) {
ctx.channel().close();
ReferenceCountUtil.release(msg);
} else if (msg instanceof BinaryWebSocketFrame || msg instanceof TextWebSocketFrame) {
ByteBufHolder frame = (ByteBufHolder) msg;
ClientHead client = clientsBox.get(ctx.channel());
if (client == null) {
log.debug("Client with was already disconnected. Channel closed!");
ctx.channel().close();
frame.release();
return;
}
ctx.pipeline().fireChannelRead(new PacketsMessage(client, frame.content(), Transport.WEBSOCKET));
frame.release();
} else if (msg instanceof FullHttpRequest) {
FullHttpRequest req = (FullHttpRequest) msg;
QueryStringDecoder queryDecoder = new QueryStringDecoder(req.uri());
String path = queryDecoder.path();
List<String> transport = queryDecoder.parameters().get("transport");
List<String> sid = queryDecoder.parameters().get("sid");
if (transport != null && NAME.equals(transport.get(0))) {
try {
if (!configuration.getTransports().contains(Transport.WEBSOCKET)) {
log.debug("{} transport not supported by configuration.", Transport.WEBSOCKET);
ctx.channel().close();
return;
}
if (sid != null && sid.get(0) != null) {
final UUID sessionId = UUID.fromString(sid.get(0));
handshake(ctx, sessionId, path, req);
} else {
ClientHead client = ctx.channel().attr(ClientHead.CLIENT).get();
// first connection
handshake(ctx, client.getSessionId(), path, req);
}
} finally {
req.release();
}
} else {
ctx.fireChannelRead(msg);
}
} else {
ctx.fireChannelRead(msg);
}
}
Aggregations