use of io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory in project vert.x by eclipse.
the class HttpServerImpl method createHandshaker.
WebSocketServerHandshaker createHandshaker(Channel ch, HttpRequest request) {
// As a fun part, Firefox 6.0.2 supports Websockets protocol '7'. But,
// it doesn't send a normal 'Connection: Upgrade' header. Instead it
// sends: 'Connection: keep-alive, Upgrade'. Brilliant.
String connectionHeader = request.headers().get(io.vertx.core.http.HttpHeaders.CONNECTION);
if (connectionHeader == null || !connectionHeader.toLowerCase().contains("upgrade")) {
sendError("\"Connection\" must be \"Upgrade\".", BAD_REQUEST, ch);
return null;
}
if (request.getMethod() != HttpMethod.GET) {
sendError(null, METHOD_NOT_ALLOWED, ch);
return null;
}
try {
WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(getWebSocketLocation(ch.pipeline(), request), subProtocols, false, options.getMaxWebsocketFrameSize(), options.isAcceptUnmaskedFrames());
WebSocketServerHandshaker shake = factory.newHandshaker(request);
if (shake == null) {
log.error("Unrecognised websockets handshake");
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ch);
}
return shake;
} catch (Exception e) {
throw new VertxException(e);
}
}
use of io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory in project activemq-artemis by apache.
the class WebSocketServerHandler method handleHttpRequest.
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
// Allow only GET methods.
if (req.method() != GET) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN));
return;
}
// Handshake
String supportedProtocolsCSV = StringUtil.joinStringList(supportedProtocols, ",");
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(this.getWebSocketLocation(req), supportedProtocolsCSV, false, maxFramePayloadLength);
this.httpRequest = req;
this.handshaker = wsFactory.newHandshaker(req);
if (this.handshaker == null) {
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
} else {
ChannelFuture handshake = this.handshaker.handshake(ctx.channel(), req);
handshake.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
// we need to insert an encoder that takes the underlying ChannelBuffer of a StompFrame.toActiveMQBuffer and
// wrap it in a binary web socket frame before letting the wsencoder send it on the wire
future.channel().pipeline().addAfter("wsencoder", "binary-websocket-encoder", BINARY_WEBSOCKET_ENCODER);
} else {
// Handshake failed, fire an exceptionCaught event
future.channel().pipeline().fireExceptionCaught(future.cause());
}
}
});
}
}
use of io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory in project netty by netty.
the class WebSocketServerHandler method handleHttpRequest.
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) {
// Handle a bad request.
if (!req.decoderResult().isSuccess()) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(req.protocolVersion(), BAD_REQUEST, ctx.alloc().buffer(0)));
return;
}
// Allow only GET methods.
if (!GET.equals(req.method())) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(req.protocolVersion(), FORBIDDEN, ctx.alloc().buffer(0)));
return;
}
// Send the demo page and favicon.ico
if ("/".equals(req.uri())) {
ByteBuf content = WebSocketServerBenchmarkPage.getContent(getWebSocketLocation(req));
FullHttpResponse res = new DefaultFullHttpResponse(req.protocolVersion(), OK, content);
res.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8");
HttpUtil.setContentLength(res, content.readableBytes());
sendHttpResponse(ctx, req, res);
return;
}
if ("/favicon.ico".equals(req.uri())) {
FullHttpResponse res = new DefaultFullHttpResponse(req.protocolVersion(), NOT_FOUND, ctx.alloc().buffer(0));
sendHttpResponse(ctx, req, res);
return;
}
// Handshake
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketLocation(req), null, true, 5 * 1024 * 1024);
handshaker = wsFactory.newHandshaker(req);
if (handshaker == null) {
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
} else {
handshaker.handshake(ctx.channel(), req);
}
}
use of io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory in project moco by dreamhead.
the class ActualWebSocketServer method connectRequest.
public void connectRequest(final ChannelHandlerContext ctx, final FullHttpRequest request) {
QueryStringDecoder decoder = new QueryStringDecoder(request.uri());
final String actual = decoder.path();
if (!uri.equals(actual)) {
ctx.writeAndFlush(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST));
return;
}
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(this.uri, null, false);
WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(request);
Channel channel = ctx.channel();
if (handshaker == null) {
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(channel);
return;
}
handshaker.handshake(channel, request);
connect(channel);
sendConnected(channel);
}
Aggregations