use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpRequest in project modules-extra by CubeEngine.
the class HttpRequestHandler method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest message) throws Exception {
InetSocketAddress inetSocketAddress = (InetSocketAddress) ctx.channel().remoteAddress();
this.log.info("{} connected...", inetSocketAddress.getAddress().getHostAddress());
if (!this.server.isAddressAccepted(inetSocketAddress.getAddress())) {
this.log.info("Access denied!");
ctx.channel().close();
}
if (message.getDecoderResult().isFailure()) {
this.error(ctx, RequestStatus.UNKNOWN_ERROR);
this.log.info(message.getDecoderResult().cause(), "The decoder failed on this request...");
return;
}
boolean authorized = this.server.isAuthorized(inetSocketAddress.getAddress());
QueryStringDecoder qsDecoder = new QueryStringDecoder(message.getUri(), this.UTF8, true, 100);
final Parameters params = new Parameters(qsDecoder.parameters(), cm.getProviders());
User authUser = null;
if (!authorized) {
String user = params.get("user", String.class);
String pass = params.get("pass", String.class);
if (user == null || pass == null) {
this.error(ctx, AUTHENTICATION_FAILURE, new ApiRequestException("Could not complete authentication", 200));
return;
}
Optional<User> byName = Sponge.getServiceManager().provide(UserStorageService.class).get().get(user);
if (!byName.isPresent()) {
this.error(ctx, AUTHENTICATION_FAILURE, new ApiRequestException("Could not complete authentication", 200));
return;
}
UUID id = byName.get().getUniqueId();
// TODO make properly async
CompletableFuture<Boolean> cf = am.isPasswordSet(id).thenCompose(isSet -> am.checkPassword(id, pass).thenApply(correctPassword -> !isSet || !correctPassword));
Boolean authFailed = cf.get();
if (authFailed) {
this.error(ctx, AUTHENTICATION_FAILURE, new ApiRequestException("Could not complete authentication", 200));
return;
}
authUser = byName.get();
}
String path = qsDecoder.path().trim();
if (path.length() == 0 || "/".equals(path)) {
this.error(ctx, RequestStatus.ROUTE_NOT_FOUND);
return;
}
path = normalizePath(path);
// is this request intended to initialize a websockets connection?
if (WEBSOCKET_ROUTE.equals(path)) {
WebSocketRequestHandler handler;
if (!(ctx.pipeline().last() instanceof WebSocketRequestHandler)) {
handler = new WebSocketRequestHandler(cm, server, objectMapper, authUser);
ctx.pipeline().addLast("wsEncoder", new TextWebSocketFrameEncoder(objectMapper));
ctx.pipeline().addLast("handler", handler);
} else {
handler = (WebSocketRequestHandler) ctx.pipeline().last();
}
this.log.info("received a websocket request...");
handler.doHandshake(ctx, message);
return;
}
this.handleHttpRequest(ctx, message, path, params, authUser);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpRequest in project modules-extra by CubeEngine.
the class WebSocketRequestHandler method doHandshake.
public void doHandshake(ChannelHandlerContext ctx, FullHttpRequest message) {
WebSocketServerHandshakerFactory handshakerFactory = new WebSocketServerHandshakerFactory("ws://" + message.headers().get(HOST) + "/" + WEBSOCKET_ROUTE, null, false);
this.handshaker = handshakerFactory.newHandshaker(message);
if (handshaker == null) {
this.log.info("client is incompatible!");
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
return;
}
this.log.debug("handshaking now...");
this.handshaker.handshake(ctx.channel(), message).addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
log.debug("Success!");
} else {
log.debug("Failed!");
}
});
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpRequest in project xian by happyyangyuan.
the class ReqReceived method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof FullHttpRequest) {
FullHttpRequest httpRequest = (FullHttpRequest) msg;
String outerMsg = OuterMsgId.get(httpRequest);
if (StringUtil.isEmpty(outerMsg)) {
MsgIdHolder.init();
} else {
MsgIdHolder.set(outerMsg);
LOG.info("xian独立节点传入了msgId=" + outerMsg);
}
String $ip = httpRequest.headers().get("X-Real-IP");
if (StringUtil.isEmpty($ip)) {
InetSocketAddress address = (InetSocketAddress) (ctx.channel().remoteAddress());
$ip = address.getAddress().getHostAddress();
}
LOG.info(String.format("收到来自%s的FullHttpRequest \r\n %s!", $ip, msg));
ctx.fireChannelRead(msg);
} else {
LOG.info("收到chucked http message...直接忽略,等待组装完毕");
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpRequest in project xian by happyyangyuan.
the class RequestDecoderAux method decode.
@Override
protected void decode(ChannelHandlerContext ctx, FullHttpRequest msg, List<Object> out) throws Exception {
LOG.debug(" httpRequest ----> UnitRequest Pojo");
/*if (!HttpMethod.POST.equals(msg.method())) {
throw new BadRequestException(new IllegalArgumentException("拒绝非POST请求!"));
}*/
DecoderResult result = msg.decoderResult();
if (!result.isSuccess()) {
throw new BadRequestException(result.cause());
}
updateLongConnectionStatus(msg, ctx);
Request request = new Request(msg, MsgIdHolder.get());
offerReqQueue(ctx, request);
out.add(request);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpRequest in project scalecube by scalecube.
the class CorsHeadersHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (!(msg instanceof FullHttpRequest)) {
super.channelRead(ctx, msg);
return;
}
FullHttpRequest request = (FullHttpRequest) msg;
if (!(HttpMethod.OPTIONS.equals(request.method()))) {
super.channelRead(ctx, msg);
return;
}
HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
response.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, config.getAccessControlAllowOrigin());
response.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_METHODS, config.getAccessControlAllowMethods());
String accessControlRequestHeaders = request.headers().get(HttpHeaderNames.ACCESS_CONTROL_REQUEST_HEADERS);
if (accessControlRequestHeaders != null) {
response.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, accessControlRequestHeaders);
}
response.headers().add(HttpHeaderNames.ACCESS_CONTROL_MAX_AGE, config.getAccessControlMaxAge());
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, 0);
ctx.writeAndFlush(response);
}
Aggregations