List of usage examples for io.netty.handler.codec.http HttpHeaders setContentLength
@Deprecated public static void setContentLength(HttpMessage message, long length)
From source file:snow.http.server.StaticHttpHandler.java
License:Open Source License
private void sendFile(final ChannelHandlerContext ctx, final FullHttpRequest request, final File file) throws Throwable { final RandomAccessFile raf = new RandomAccessFile(file, "r"); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK); long modified = file.lastModified(); Date since = HttpHelper.parseDate(getHeader(request, IF_MODIFIED_SINCE)); if (since != null && since.getTime() >= modified) { HttpHelper.sendStatus(ctx, NOT_MODIFIED); } else {/*from w ww. ja v a 2s. c om*/ final long length = raf.length(); HttpHelper.setDate(response); HttpHelper.setLastModified(response, modified); HttpHeaders.setContentLength(response, length); HttpHeaders.setHeader(response, CONTENT_TYPE, mimeType(file.toPath())); setHeader(response, CACHE_CONTROL, cache); // boolean isKeep = isKeepAlive(request); // if (isKeep) { // setHeader(response, CONNECTION, KEEP_ALIVE); // } ctx.write(response); // ChannelFuture writeFuture = ctx.writeAndFlush(new ChunkedFile(raf, 0, length, 8192)); // Write the content. ChannelFuture sendFileFuture; if (useSendFile) { sendFileFuture = ctx.write(new DefaultFileRegion(raf.getChannel(), 0, length)); } else { sendFileFuture = ctx.write(new ChunkedFile(raf, 0, length, 8192)); } // Write the end marker //ChannelFuture lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); // if (!isKeep) { sendFileFuture.addListener(CLOSE); // } } }
From source file:timely.netty.http.HttpStaticFileServerHandler.java
License:Apache License
@Override public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception { if (!request.getDecoderResult().isSuccess()) { sendError(ctx, BAD_REQUEST);//from www . jav a2 s . co m return; } if (request.getMethod() != GET) { sendError(ctx, METHOD_NOT_ALLOWED); return; } String uri = request.getUri(); if (uri.startsWith("/favicon.ico")) { uri = uri.replaceFirst("\\/", "/webapp/"); } if (!uri.startsWith("/webapp")) { sendError(ctx, FORBIDDEN); return; } final String path = sanitizeUri(uri); if (path == null) { sendError(ctx, FORBIDDEN); return; } LOG.trace("Looking for requested file at: " + path); File file = new File(path); if (file.isHidden() || !file.exists()) { sendError(ctx, NOT_FOUND); return; } if (file.isDirectory()) { if (uri.endsWith("/")) { sendListing(ctx, file); } else { sendRedirect(ctx, uri + '/'); } return; } if (!file.isFile()) { sendError(ctx, FORBIDDEN); return; } // Cache Validation String ifModifiedSince = request.headers().get(IF_MODIFIED_SINCE); if (ifModifiedSince != null && !ifModifiedSince.isEmpty()) { SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US); Date ifModifiedSinceDate = dateFormatter.parse(ifModifiedSince); // Only compare up to the second because the datetime format we send // to the client // does not have milliseconds long ifModifiedSinceDateSeconds = ifModifiedSinceDate.getTime() / 1000; long fileLastModifiedSeconds = file.lastModified() / 1000; if (ifModifiedSinceDateSeconds == fileLastModifiedSeconds) { sendNotModified(ctx); return; } } RandomAccessFile raf; try { raf = new RandomAccessFile(file, "r"); } catch (FileNotFoundException ignore) { sendError(ctx, NOT_FOUND); return; } long fileLength = raf.length(); HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); HttpHeaders.setContentLength(response, fileLength); setContentTypeHeader(response, file); setDateAndCacheHeaders(response, file); HttpHeaders.setTransferEncodingChunked(response); if (HttpHeaders.isKeepAlive(request)) { response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE); } // Write the initial line and the header. sendResponse(ctx, response); LOG.trace("Returning {}", response); // Write the content. sendResponse(ctx, new HttpChunkedInput(new ChunkedFile(raf, 0, fileLength, 8192))); }