List of usage examples for io.netty.handler.codec.http HttpHeaders setContentLength
@Deprecated public static void setContentLength(HttpMessage message, long length)
From source file:io.advantageous.conekt.http.impl.HttpServerImpl.java
License:Open Source License
private void sendError(CharSequence err, HttpResponseStatus status, Channel ch) { FullHttpResponse resp = new DefaultFullHttpResponse(HTTP_1_1, status); if (status.code() == METHOD_NOT_ALLOWED.code()) { // SockJS requires this resp.headers().set(io.advantageous.conekt.http.HttpHeaders.ALLOW, io.advantageous.conekt.http.HttpHeaders.GET); }/*from ww w .j a v a 2s .c o m*/ if (err != null) { resp.content().writeBytes(err.toString().getBytes(CharsetUtil.UTF_8)); HttpHeaders.setContentLength(resp, err.length()); } else { HttpHeaders.setContentLength(resp, 0); } ch.writeAndFlush(resp); }
From source file:io.maelstorm.server.RequestHandler.java
License:Open Source License
private void sendResponse(final ChannelHandlerContext ctx, final FullHttpResponse response, final HttpRequest request) { ReferenceCountUtil.release(request); if (!ctx.channel().isActive()) { return;//from w ww . jav a 2 s. c om } final boolean keepalive = HttpHeaders.isKeepAlive(request); if (keepalive) { HttpHeaders.setContentLength(response, response.content().readableBytes()); response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE); } final ChannelFuture future = ctx.write(response); ctx.flush(); if (!keepalive) { future.addListener(ChannelFutureListener.CLOSE); } }
From source file:io.nebo.container.NettyHttpServletResponse.java
License:Apache License
@Override public void setContentLength(int len) { HttpHeaders.setContentLength(response, len); }
From source file:io.nebo.container.NettyHttpServletResponse.java
License:Apache License
@Override public void setContentLengthLong(long len) { HttpHeaders.setContentLength(response, len); }
From source file:io.reactivex.netty.protocol.http.client.HttpRequestHeaders.java
License:Apache License
public void setContentLength(long length) { HttpHeaders.setContentLength(nettyRequest, length); }
From source file:io.reactivex.netty.protocol.http.server.HttpResponseHeaders.java
License:Apache License
public void setContentLength(long length) { HttpHeaders.setContentLength(nettyResponse, length); }
From source file:io.riox.springxd.sinks.websocket.NettyWebSocketHttpServerHandler.java
License:Apache License
@Override protected boolean additionalHttpRequestHandler(ChannelHandlerContext ctx, FullHttpRequest req) { if ("/".equals(req.getUri())) { ByteBuf content = NettyWebSocketServerIndexPage.getContent(getWebSocketLocation(req)); FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, OK, content); res.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8"); HttpHeaders.setContentLength(res, content.readableBytes()); sendHttpResponse(ctx, req, res); return false; }//from w w w. j av a 2 s .co m if ("/favicon.ico".equals(req.getUri())) { FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND); sendHttpResponse(ctx, req, res); return false; } return true; // continue processing }
From source file:io.riox.springxd.sinks.websocket.NettyWebSocketServerHandler.java
License:Apache License
void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { // Generate an error page if response getStatus code is not OK (200). if (res.getStatus().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf);//from w w w. ja v a 2s. c om buf.release(); HttpHeaders.setContentLength(res, res.content().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.channel().writeAndFlush(res); if (!HttpHeaders.isKeepAlive(req) || res.getStatus().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
From source file:io.scalecube.socketio.pipeline.PipelineUtils.java
License:Apache License
public static HttpResponse createHttpResponse(final String origin, ByteBuf content, boolean json) { FullHttpResponse res = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content); if (json) {/* w w w. ja v a 2s . c o m*/ HttpHeaders.addHeader(res, HttpHeaders.Names.CONTENT_TYPE, "text/javascript; charset=UTF-8"); } else { HttpHeaders.addHeader(res, HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8"); } HttpHeaders.addHeader(res, HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE); if (origin != null) { res.headers().add(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_ORIGIN, origin); res.headers().add(HttpHeaders.Names.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true"); } HttpHeaders.setContentLength(res, content.readableBytes()); return res; }
From source file:io.scalecube.socketio.pipeline.ResourceHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // ?/*ww w . j a v a2s .c om*/ if (msg instanceof HttpRequest) { HttpRequest req = (HttpRequest) msg; QueryStringDecoder queryDecoder = new QueryStringDecoder(req.getUri()); String requestPath = queryDecoder.path(); URL resUrl = resources.get(requestPath); if (resUrl != null) { if (log.isDebugEnabled()) log.debug("Received HTTP resource request: {} {} from channel: {}", req.getMethod(), requestPath, ctx.channel()); URLConnection fileUrl = resUrl.openConnection(); long lastModified = fileUrl.getLastModified(); // check if file has been modified since last request if (isNotModified(req, lastModified)) { sendNotModified(ctx); return; } // create resource input-stream and check existence final InputStream is = fileUrl.getInputStream(); if (is == null) { sendError(ctx, HttpResponseStatus.NOT_FOUND); return; } // create ok response HttpResponse res = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); // set Content-Length header HttpHeaders.setContentLength(res, fileUrl.getContentLengthLong()); // set Content-Type header setContentTypeHeader(res, fileUrl); // set Date, Expires, Cache-Control and Last-Modified headers setDateAndCacheHeaders(res, lastModified); // write initial response header ctx.write(res); // write the content stream ctx.pipeline().addBefore(ctx.name(), "chunked-writer-handler", new ChunkedWriteHandler()); ChannelFuture writeFuture = ctx.writeAndFlush(new ChunkedStream(is, fileUrl.getContentLength())); // add operation complete listener so we can close the channel and the input stream writeFuture.addListener(ChannelFutureListener.CLOSE); ReferenceCountUtil.release(msg); return; } } super.channelRead(ctx, msg); }