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));
}
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);
}
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);
}
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;
}
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);
}
Aggregations