Search in sources :

Example 26 with DefaultHttpResponse

use of io.netty.handler.codec.http.DefaultHttpResponse in project hadoop by apache.

the class WebHdfsHandler method onAppend.

private void onAppend(ChannelHandlerContext ctx) throws IOException {
    writeContinueHeader(ctx);
    final String nnId = params.namenodeId();
    final int bufferSize = params.bufferSize();
    DFSClient dfsClient = newDfsClient(nnId, conf);
    OutputStream out = dfsClient.append(path, bufferSize, EnumSet.of(CreateFlag.APPEND), null, null);
    resp = new DefaultHttpResponse(HTTP_1_1, OK);
    resp.headers().set(CONTENT_LENGTH, 0);
    ctx.pipeline().replace(this, HdfsWriter.class.getSimpleName(), new HdfsWriter(dfsClient, out, resp));
}
Also used : DFSClient(org.apache.hadoop.hdfs.DFSClient) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) OutputStream(java.io.OutputStream)

Example 27 with DefaultHttpResponse

use of io.netty.handler.codec.http.DefaultHttpResponse in project hadoop by apache.

the class FSImageHandler method channelRead0.

@Override
public void channelRead0(ChannelHandlerContext ctx, HttpRequest request) throws Exception {
    if (request.getMethod() != HttpMethod.GET) {
        DefaultHttpResponse resp = new DefaultHttpResponse(HTTP_1_1, METHOD_NOT_ALLOWED);
        resp.headers().set(CONNECTION, CLOSE);
        ctx.write(resp).addListener(ChannelFutureListener.CLOSE);
        return;
    }
    QueryStringDecoder decoder = new QueryStringDecoder(request.getUri());
    final String op = getOp(decoder);
    final String content;
    String path = getPath(decoder);
    switch(op) {
        case "GETFILESTATUS":
            content = image.getFileStatus(path);
            break;
        case "LISTSTATUS":
            content = image.listStatus(path);
            break;
        case "GETACLSTATUS":
            content = image.getAclStatus(path);
            break;
        case "GETXATTRS":
            List<String> names = getXattrNames(decoder);
            String encoder = getEncoder(decoder);
            content = image.getXAttrs(path, names, encoder);
            break;
        case "LISTXATTRS":
            content = image.listXAttrs(path);
            break;
        case "GETCONTENTSUMMARY":
            content = image.getContentSummary(path);
            break;
        default:
            throw new IllegalArgumentException("Invalid value for webhdfs parameter" + " \"op\"");
    }
    LOG.info("op=" + op + " target=" + path);
    DefaultFullHttpResponse resp = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(content.getBytes(Charsets.UTF_8)));
    resp.headers().set(CONTENT_TYPE, APPLICATION_JSON_UTF8);
    resp.headers().set(CONTENT_LENGTH, resp.content().readableBytes());
    resp.headers().set(CONNECTION, CLOSE);
    ctx.write(resp).addListener(ChannelFutureListener.CLOSE);
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse)

Example 28 with DefaultHttpResponse

use of io.netty.handler.codec.http.DefaultHttpResponse 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);
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) Channel(io.netty.channel.Channel) SchedulerKey(com.corundumstudio.socketio.scheduler.SchedulerKey) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse)

Example 29 with DefaultHttpResponse

use of io.netty.handler.codec.http.DefaultHttpResponse in project netty-socketio by mrniko.

the class AuthorizeHandler method authorize.

private boolean authorize(ChannelHandlerContext ctx, Channel channel, String origin, Map<String, List<String>> params, FullHttpRequest req) throws IOException {
    Map<String, List<String>> headers = new HashMap<String, List<String>>(req.headers().names().size());
    for (String name : req.headers().names()) {
        List<String> values = req.headers().getAll(name);
        headers.put(name, values);
    }
    HandshakeData data = new HandshakeData(req.headers(), params, (InetSocketAddress) channel.remoteAddress(), req.uri(), origin != null && !origin.equalsIgnoreCase("null"));
    boolean result = false;
    try {
        result = configuration.getAuthorizationListener().isAuthorized(data);
    } catch (Exception e) {
        log.error("Authorization error", e);
    }
    if (!result) {
        HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.UNAUTHORIZED);
        channel.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE);
        log.debug("Handshake unauthorized, query params: {} headers: {}", params, headers);
        return false;
    }
    UUID sessionId = this.generateOrGetSessionIdFromRequest(req.headers());
    List<String> transportValue = params.get("transport");
    if (transportValue == null) {
        log.error("Got no transports for request {}", req.uri());
        HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.UNAUTHORIZED);
        channel.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE);
        return false;
    }
    Transport transport = Transport.byName(transportValue.get(0));
    if (!configuration.getTransports().contains(transport)) {
        Map<String, Object> errorData = new HashMap<String, Object>();
        errorData.put("code", 0);
        errorData.put("message", "Transport unknown");
        channel.attr(EncoderHandler.ORIGIN).set(origin);
        channel.writeAndFlush(new HttpErrorMessage(errorData));
        return false;
    }
    ClientHead client = new ClientHead(sessionId, ackManager, disconnectable, storeFactory, data, clientsBox, transport, disconnectScheduler, configuration);
    channel.attr(ClientHead.CLIENT).set(client);
    clientsBox.addClient(client);
    String[] transports = {};
    if (configuration.getTransports().contains(Transport.WEBSOCKET)) {
        transports = new String[] { "websocket" };
    }
    AuthPacket authPacket = new AuthPacket(sessionId, transports, configuration.getPingInterval(), configuration.getPingTimeout());
    Packet packet = new Packet(PacketType.OPEN);
    packet.setData(authPacket);
    client.send(packet);
    client.schedulePingTimeout();
    log.debug("Handshake authorized for sessionId: {}, query params: {} headers: {}", sessionId, params, headers);
    return true;
}
Also used : Packet(com.corundumstudio.socketio.protocol.Packet) AuthPacket(com.corundumstudio.socketio.protocol.AuthPacket) HashMap(java.util.HashMap) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) IOException(java.io.IOException) HttpErrorMessage(com.corundumstudio.socketio.messages.HttpErrorMessage) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) AuthPacket(com.corundumstudio.socketio.protocol.AuthPacket) HandshakeData(com.corundumstudio.socketio.HandshakeData) List(java.util.List) UUID(java.util.UUID) Transport(com.corundumstudio.socketio.Transport)

Example 30 with DefaultHttpResponse

use of io.netty.handler.codec.http.DefaultHttpResponse 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);
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) ChannelFuture(io.netty.channel.ChannelFuture) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) Channel(io.netty.channel.Channel) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse)

Aggregations

DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)32 HttpResponse (io.netty.handler.codec.http.HttpResponse)21 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)10 ChannelFuture (io.netty.channel.ChannelFuture)8 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)6 HttpChunkedInput (io.netty.handler.codec.http.HttpChunkedInput)5 HttpRequest (io.netty.handler.codec.http.HttpRequest)5 ByteBuf (io.netty.buffer.ByteBuf)4 Channel (io.netty.channel.Channel)4 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 HttpContent (io.netty.handler.codec.http.HttpContent)4 DefaultFileRegion (io.netty.channel.DefaultFileRegion)3 QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)3 ChunkedFile (io.netty.handler.stream.ChunkedFile)3 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 RandomAccessFile (java.io.RandomAccessFile)3 Bootstrap (io.netty.bootstrap.Bootstrap)2 ChannelPipeline (io.netty.channel.ChannelPipeline)2 ChannelProgressiveFuture (io.netty.channel.ChannelProgressiveFuture)2