@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (!(msg instanceof FullHttpRequest)) { return; } FullHttpRequest req = (FullHttpRequest) msg; StaticFile staticFile = getStaticPath(req.uri()); if (staticFile != null) { try { serveStatic(ctx, req, staticFile); } finally { ReferenceCountUtil.release(req); } return; } ctx.fireChannelRead(req); }
/** * When file timestamp is the same as what the browser is sending up, send a "304 Not Modified" * * @param ctx * Context */ private static void sendNotModified(ChannelHandlerContext ctx) { FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, NOT_MODIFIED); setDateHeader(response); // Close the connection as soon as the error message is sent. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
private void serveStatic(ChannelHandlerContext ctx, FullHttpRequest request, StaticFile staticFile) throws Exception { if (!request.decoderResult().isSuccess()) { sendError(ctx, BAD_REQUEST); return; String uri = request.uri(); if (isNotSecure(uri)) { sendError(ctx, NOT_FOUND); return; sendError(ctx, NOT_FOUND); return; long fileLastModifiedSeconds = file.lastModified() / 1000; if (ifModifiedSinceDateSeconds == fileLastModifiedSeconds) { sendNotModified(ctx); return; raf = new RandomAccessFile(file, "r"); } catch (FileNotFoundException ignore) { sendError(ctx, NOT_FOUND); return; setDateAndCacheHeaders(response, file); if (HttpUtil.isKeepAlive(request)) { response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
private void initAdminPipeline(ChannelHandlerContext ctx) { if (!ipFilterHandler.accept(ctx)) { ctx.close(); return; } var pipeline = ctx.pipeline(); pipeline.addLast(new UploadHandler(holder.props.jarPath, "/upload", "/static/ota")) .addLast(new OTAHandler(holder, rootPath + "/ota/start", "/static/ota")) .addLast(adminAuthHandler) .addLast(authCookieHandler) .addLast(cookieBasedUrlReWriterHandler); pipeline.remove(StaticFileHandler.class); pipeline.addLast(new StaticFileHandler(holder.props, new NoCacheStaticFile("/static"))) .addLast(otaLogic) .addLast(usersLogic) .addLast(statsLogic) .addLast(configsLogic) .addLast(hardwareStatsLogic) .addLast(httpAPILogic) .addLast(noMatchHandler) .remove(this); if (log.isTraceEnabled()) { log.trace("Initialized admin pipeline. {}", ctx.pipeline().names()); } }
private void initHttpPipeline(ChannelHandlerContext ctx) { ctx.pipeline() .addLast(letsEncryptHandler) .addLast("HttpChunkedWrite", new ChunkedWriteHandler()) .addLast("HttpUrlMapper", new UrlReWriterHandler("/favicon.ico", "/static/favicon.ico")) .addLast("HttpStaticFile", new StaticFileHandler(holder.props, new StaticFile("/static"), new StaticFileEdsWith(FileUtils.CSV_DIR, ".gz"), new StaticFileEdsWith(FileUtils.CSV_DIR, ".zip"))) .addLast(resetPasswordLogic) .addLast(httpAPILogic) .addLast(noMatchHandler) .remove(this); if (log.isTraceEnabled()) { log.trace("Initialized http pipeline. {}", ctx.pipeline().names()); } }