List of usage examples for io.netty.handler.codec.http DefaultFullHttpResponse DefaultFullHttpResponse
public DefaultFullHttpResponse(HttpVersion version, HttpResponseStatus status)
From source file:HelloWorldHttp1Handler.java
License:Apache License
@Override public void messageReceived(ChannelHandlerContext ctx, HttpRequest req) throws Exception { if (HttpHeaderUtil.is100ContinueExpected(req)) { ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE)); }// w w w . j a v a2 s . co m boolean keepAlive = HttpHeaderUtil.isKeepAlive(req); ByteBuf content = ctx.alloc().buffer(); content.writeBytes(HelloWorldHttp2Handler.RESPONSE_BYTES.duplicate()); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); response.headers().set(CONTENT_LENGTH, response.content().readableBytes()); if (!keepAlive) { ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } else { response.headers().set(CONNECTION, Values.KEEP_ALIVE); ctx.writeAndFlush(response); } }
From source file:adalightserver.http.HttpServer.java
License:Apache License
private static void sendErrorResponse(ChannelHandlerContext ctx, FullHttpRequest req, HttpResponseStatus status) {/* w w w.j a v a 2 s.c o m*/ sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, status)); }
From source file:adalightserver.http.HttpServer.java
License:Apache License
@SuppressWarnings("unused") private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, String response) { FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, OK); ByteBuf buf = Unpooled.copiedBuffer(response, CharsetUtil.US_ASCII); res.content().writeBytes(buf);//from w w w. j a va 2 s. com buf.release(); res.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8"); HttpHeaders.setContentLength(res, res.content().readableBytes()); sendHttpResponse(ctx, req, res); }
From source file:adalightserver.http.HttpServer.java
License:Apache License
private void handleStaticFileRequest(ChannelHandlerContext ctx, FullHttpRequest req, String path) throws Exception { if (req.getMethod() != HttpMethod.GET) { sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN)); return;// w ww . j av a 2s. co m } if (path.equals("/")) path = "/index.html"; final String spath = sanitizeUri(path); if (spath == null) { sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN)); return; } File file = new File(spath); if (file.isHidden() || !file.exists()) { sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN)); return; } if (file.isDirectory() || !file.isFile() || !file.canRead()) { sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN)); return; } RandomAccessFile raf = null; byte[] content; long fileLength; try { raf = new RandomAccessFile(file, "r"); fileLength = raf.length(); if (fileLength > 4 * 1024 * 1024) { sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND)); return; } content = new byte[(int) fileLength]; raf.read(content); } catch (Exception e) { sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND)); return; } finally { if (raf != null) raf.close(); } FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, OK); res.content().writeBytes(content); MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap(); res.headers().set(HttpHeaders.Names.CONTENT_TYPE, mimeTypesMap.getContentType(file.getPath())); res.headers().set(HttpHeaders.Names.CONTENT_LENGTH, fileLength); sendHttpResponse(ctx, req, res); }
From source file:app.WebSocketServerHandler.java
License:Apache License
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) { // Handle a bad request. if (!req.decoderResult().isSuccess()) { sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST)); return;/*from w w w . j ava 2 s . co m*/ } // Allow only GET methods. if (req.method() != GET) { sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN)); return; } // Handshake WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketLocation(req), null, true, 5 * 1024 * 1024); handshaker = wsFactory.newHandshaker(req); if (handshaker == null) { WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); } else { handshaker.handshake(ctx.channel(), req); channels.add(ctx.channel()); } }
From source file:appium.android.server.http.ServerHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (!(msg instanceof FullHttpRequest)) { return;//from www .j a va 2s. c o m } FullHttpRequest request = (FullHttpRequest) msg; FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK); response.headers().add("Connection", "close"); HttpRequest httpRequest = new NettyHttpRequest(request); HttpResponse httpResponse = new NettyHttpResponse(response); for (HttpServlet handler : httpHandlers) { handler.handleHttpRequest(httpRequest, httpResponse); if (httpResponse.isClosed()) { break; } } if (!httpResponse.isClosed()) { httpResponse.setStatus(404); httpResponse.end(); } ctx.write(response).addListener(ChannelFutureListener.CLOSE); super.channelRead(ctx, msg); }
From source file:baseFrame.netty.atest.HttpHelloWorldServerHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) { if (msg instanceof HttpRequest) { HttpRequest request = (HttpRequest) msg; if (HttpHeaders.is100ContinueExpected(request)) { ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE)); }//from w w w . j av a2 s. co m boolean keepAlive = HttpHeaders.isKeepAlive(request); // Encode the cookie. String cookieString = request.headers().get(Names.COOKIE); if (cookieString != null) { Set<Cookie> cookies = CookieDecoder.decode(cookieString); if (!cookies.isEmpty()) { // Reset the cookies if necessary. for (Cookie cookie : cookies) { response.headers().add(Names.SET_COOKIE, ServerCookieEncoder.encode(cookie)); } } } else { // Browser sent no cookie. Add some. /* response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.encode("key1", "value1")); response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.encode("key2", "value2"));*/ } if (HttpHeaders.is100ContinueExpected(request)) { send100Continue(ctx); } responseContent.append("VERSION: ").append(request.getProtocolVersion()).append("\r\n"); //responseContent.append("HOSTNAME: ").append(request.headers().).append("\r\n"); responseContent.append("REQUEST_URI: ").append(request.getUri()).append("\r\n\r\n"); HttpHeaders headers = request.headers(); if (!headers.isEmpty()) { for (Entry<String, String> h : headers) { CharSequence key = h.getKey(); CharSequence value = h.getValue(); responseContent.append("HEADER: ").append(key).append(" = ").append(value).append("\r\n"); } responseContent.append("\r\n"); } QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.getUri()); Map<String, List<String>> params = queryStringDecoder.parameters(); if (!params.isEmpty()) { for (Entry<String, List<String>> p : params.entrySet()) { String key = p.getKey(); List<String> vals = p.getValue(); for (String val : vals) { responseContent.append("PARAM: ").append(key).append(" = ").append(val).append("\r\n"); } } responseContent.append("\r\n"); } response = setResponse(response, responseContent); if (!keepAlive) { ctx.write(response).addListener(ChannelFutureListener.CLOSE); } else { response.headers().set(Names.CONNECTION, Values.KEEP_ALIVE); ctx.write(response); } } if (msg instanceof HttpContent) { HttpContent content = (HttpContent) msg; if (msg instanceof LastHttpContent) { } } }
From source file:baseFrame.netty.atest.HttpHelloWorldServerHandler.java
License:Apache License
private static void send100Continue(ChannelHandlerContext ctx) { FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, CONTINUE); ctx.write(response);/* www . ja va2s .co m*/ }
From source file:bean.lee.demo.netty.learn.http.file.HttpStaticFileServerHandler.java
License:Apache License
private static void sendListing(ChannelHandlerContext ctx, File dir) { FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK); response.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8"); StringBuilder buf = new StringBuilder(); String dirPath = dir.getPath(); buf.append("<!DOCTYPE html>\r\n"); buf.append("<html><head><title>"); buf.append("Listing of: "); buf.append(dirPath);//from w w w.java2 s . com buf.append("</title></head><body>\r\n"); buf.append("<h3>Listing of: "); buf.append(dirPath); buf.append("</h3>\r\n"); buf.append("<ul>"); buf.append("<li><a href=\"../\">..</a></li>\r\n"); for (File f : dir.listFiles()) { if (f.isHidden() || !f.canRead()) { continue; } String name = f.getName(); if (!ALLOWED_FILE_NAME.matcher(name).matches()) { continue; } buf.append("<li><a href=\""); buf.append(name); buf.append("\">"); buf.append(name); buf.append("</a></li>\r\n"); } buf.append("</ul></body></html>\r\n"); ByteBuf buffer = Unpooled.copiedBuffer(buf, CharsetUtil.UTF_8); response.content().writeBytes(buffer); buffer.release(); // Close the connection as soon as the error message is sent. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
From source file:bean.lee.demo.netty.learn.http.file.HttpStaticFileServerHandler.java
License:Apache License
private static void sendRedirect(ChannelHandlerContext ctx, String newUri) { FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, FOUND); response.headers().set(LOCATION, newUri); // Close the connection as soon as the error message is sent. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }