List of usage examples for io.netty.handler.codec.http HttpHeaderNames SERVER
AsciiString SERVER
To view the source code for io.netty.handler.codec.http HttpHeaderNames SERVER.
Click Source Link
From source file:com.bunjlabs.fuga.network.netty.NettyHttpServerHandler.java
License:Apache License
private void writeResponse(ChannelHandlerContext ctx, Request request, Response response) { HttpResponse httpresponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(response.status())); httpresponse.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED); httpresponse.headers().set(HttpHeaderNames.CONTENT_TYPE, response.contentType()); // Disable cache by default httpresponse.headers().set(HttpHeaderNames.CACHE_CONTROL, "no-cache, no-store, must-revalidate, max-age=0"); httpresponse.headers().set(HttpHeaderNames.PRAGMA, "no-cache"); httpresponse.headers().set(HttpHeaderNames.EXPIRES, "0"); response.headers().entrySet().stream().forEach((e) -> httpresponse.headers().set(e.getKey(), e.getValue())); httpresponse.headers().set(HttpHeaderNames.SERVER, "Fuga Netty Web Server/" + serverVersion); // Set cookies httpresponse.headers().set(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.STRICT.encode(NettyCookieConverter.convertListToNetty(response.cookies()))); if (response.length() >= 0) { httpresponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.length()); }//from w ww . j a va2s . c o m if (HttpUtil.isKeepAlive(httprequest)) { httpresponse.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); } else { httpresponse.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE); } ctx.write(httpresponse); if (response.stream() != null) { ctx.write(new HttpChunkedInput(new ChunkedStream(response.stream()))); } LastHttpContent fs = new DefaultLastHttpContent(); ChannelFuture sendContentFuture = ctx.writeAndFlush(fs); if (!HttpUtil.isKeepAlive(httprequest)) { sendContentFuture.addListener(ChannelFutureListener.CLOSE); } }
From source file:com.github.thinker0.mesos.MesosHealthCheckerServer.java
License:Apache License
/** * Writes a HTTP response./*from w w w.j av a 2s . c o m*/ * * @param ctx The channel context. * @param request The HTTP request. * @param status The HTTP status code. * @param buf The response content buffer. * @param contentType The response content type. * @param contentLength The response content length; */ private void writeResponse(final ChannelHandlerContext ctx, final FullHttpRequest request, final HttpResponseStatus status, final ByteBuf buf, final CharSequence contentType, final int contentLength) { // Decide whether to close the connection or not. final boolean keepAlive = isKeepAlive(request); // Build the response object. final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, buf, false); final ZonedDateTime dateTime = ZonedDateTime.now(); final DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME; final DefaultHttpHeaders headers = (DefaultHttpHeaders) response.headers(); headers.set(HttpHeaderNames.SERVER, SERVER_NAME); headers.set(HttpHeaderNames.DATE, dateTime.format(formatter)); headers.set(HttpHeaderNames.CONTENT_TYPE, contentType); headers.set(HttpHeaderNames.CONTENT_LENGTH, Integer.toString(contentLength)); // Close the non-keep-alive connection after the write operation is done. if (!keepAlive) { ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } else { ctx.writeAndFlush(response, ctx.voidPromise()); } }
From source file:io.syncframework.netty.RequestHandler.java
License:Apache License
private boolean sendResponse(ChannelHandlerContext ctx, Response response) throws Exception { HttpResponseStatus responseStatus = null; switch (response.getCode()) { case INTERNAL_ERROR: responseStatus = HttpResponseStatus.INTERNAL_SERVER_ERROR; break;/* w ww .j a v a 2s . com*/ case NOT_FOUND: responseStatus = HttpResponseStatus.NOT_FOUND; break; case PERMANENT_REDIRECT: responseStatus = HttpResponseStatus.MOVED_PERMANENTLY; break; case TEMPORARY_REDIRECT: responseStatus = HttpResponseStatus.FOUND; break; default: responseStatus = HttpResponseStatus.OK; break; } ByteArrayOutputStream bos = (ByteArrayOutputStream) response.getOutputStream(); ByteBuf buf = copiedBuffer(bos.toByteArray()); // Build the response object. FullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, responseStatus, buf); httpResponse.headers().set(HttpHeaderNames.SERVER, "Sync-AS"); // default content-type header... likely to be overwritten by the Result Content-Type header... httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=" + charset); httpResponse.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE); httpResponse.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, buf.readableBytes()); // // if response has declared specific Headers, then this may or may not override the default headers // declared above. // if (response.getHeaders() != null) { if (log.isTraceEnabled()) log.trace("custom response headers identified... passing to the response"); for (String header : response.getHeaders().keySet()) { if (log.isTraceEnabled()) log.trace("setting response header: {}: {}", header, response.getHeaders().get(header)); httpResponse.headers().set(header, response.getHeaders().get(header)); } } // Write the response. ctx.channel().writeAndFlush(httpResponse).addListener(ChannelFutureListener.CLOSE); reset(); return true; }
From source file:io.vertx.benchmarks.HttpServerHandlerBenchmark.java
License:Open Source License
@Setup public void setup() { vertx = (VertxInternal) Vertx.vertx(); HttpServerOptions options = new HttpServerOptions(); vertxChannel = new EmbeddedChannel( new HttpRequestDecoder(options.getMaxInitialLineLength(), options.getMaxHeaderSize(), options.getMaxChunkSize(), false, options.getDecoderInitialBufferSize()), new HttpResponseEncoder()); vertxChannel.config().setAllocator(new Alloc()); ContextInternal context = new EventLoopContext(vertx, vertxChannel.eventLoop(), null, null, null, new JsonObject(), Thread.currentThread().getContextClassLoader()); Handler<HttpServerRequest> app = request -> { HttpServerResponse response = request.response(); MultiMap headers = response.headers(); headers.add(HEADER_CONTENT_TYPE, RESPONSE_TYPE_PLAIN).add(HEADER_SERVER, SERVER) .add(HEADER_DATE, DATE_STRING).add(HEADER_CONTENT_LENGTH, HELLO_WORLD_LENGTH); response.end(HELLO_WORLD_BUFFER); };/*from w w w.j a v a 2s . c o m*/ HandlerHolder<HttpHandlers> holder = new HandlerHolder<>(context, new HttpHandlers(app, null, null, null)); Http1xServerHandler handler = new Http1xServerHandler(null, new HttpServerOptions(), "localhost", holder, null); vertxChannel.pipeline().addLast("handler", handler); nettyChannel = new EmbeddedChannel( new HttpRequestDecoder(options.getMaxInitialLineLength(), options.getMaxHeaderSize(), options.getMaxChunkSize(), false, options.getDecoderInitialBufferSize()), new HttpResponseEncoder(), new SimpleChannelInboundHandler<HttpRequest>() { private final byte[] STATIC_PLAINTEXT = "Hello, World!".getBytes(CharsetUtil.UTF_8); private final int STATIC_PLAINTEXT_LEN = STATIC_PLAINTEXT.length; private final ByteBuf PLAINTEXT_CONTENT_BUFFER = Unpooled .unreleasableBuffer(Unpooled.directBuffer().writeBytes(STATIC_PLAINTEXT)); private final CharSequence PLAINTEXT_CLHEADER_VALUE = new AsciiString( String.valueOf(STATIC_PLAINTEXT_LEN)); private final CharSequence TYPE_PLAIN = new AsciiString("text/plain"); private final CharSequence SERVER_NAME = new AsciiString("Netty"); private final CharSequence CONTENT_TYPE_ENTITY = HttpHeaderNames.CONTENT_TYPE; private final CharSequence DATE_ENTITY = HttpHeaderNames.DATE; private final CharSequence CONTENT_LENGTH_ENTITY = HttpHeaderNames.CONTENT_LENGTH; private final CharSequence SERVER_ENTITY = HttpHeaderNames.SERVER; private final DateFormat FORMAT = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z"); private final CharSequence date = new AsciiString(FORMAT.format(new Date())); @Override protected void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) throws Exception { writeResponse(ctx, msg, PLAINTEXT_CONTENT_BUFFER.duplicate(), TYPE_PLAIN, PLAINTEXT_CLHEADER_VALUE); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ctx.flush(); } private void writeResponse(ChannelHandlerContext ctx, HttpRequest request, ByteBuf buf, CharSequence contentType, CharSequence contentLength) { // Build the response object. FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf, false); HttpHeaders headers = response.headers(); headers.set(CONTENT_TYPE_ENTITY, contentType); headers.set(SERVER_ENTITY, SERVER_NAME); headers.set(DATE_ENTITY, date); headers.set(CONTENT_LENGTH_ENTITY, contentLength); // Close the non-keep-alive connection after the write operation is done. ctx.write(response, ctx.voidPromise()); } }); nettyChannel.config().setAllocator(new Alloc()); GET = Unpooled.unreleasableBuffer(Unpooled.copiedBuffer(("GET / HTTP/1.1\r\n" + "\r\n").getBytes())); readerIndex = GET.readerIndex(); writeIndex = GET.writerIndex(); }
From source file:net.anyflow.menton.http.HttpRequestRouter.java
License:Apache License
protected static void setDefaultHeaders(FullHttpRequest request, HttpResponse response) { response.headers().add(HttpHeaderNames.SERVER, net.anyflow.lannister.Settings.SELF.getProperty("menton.version")); boolean keepAlive = request.headers().get(HttpHeaderNames.CONNECTION) == HttpHeaderValues.KEEP_ALIVE .toString();/*from www. ja v a2 s . com*/ if (keepAlive) { response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); } if (Settings.SELF.getProperty("menton.httpServer.allowCrossDomain", "false").equalsIgnoreCase("true")) { response.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, "*"); response.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_METHODS, "POST, GET, PUT, DELETE"); response.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, "X-PINGARUNER"); response.headers().add(HttpHeaderNames.ACCESS_CONTROL_MAX_AGE, "1728000"); } response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes()); }
From source file:org.ballerinalang.test.agent.server.WebServer.java
License:Open Source License
/** * Writes a HTTP response./*from ww w . ja v a 2s. c om*/ * * @param ctx The channel context. * @param status The HTTP status code. * @param buf The response content buffer. * @param contentType The response content type. * @param contentLength The response content length; */ private static void writeResponse(ChannelHandlerContext ctx, HttpResponseStatus status, ByteBuf buf, CharSequence contentType, int contentLength) { // Build the response object. final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, buf, false); final ZonedDateTime dateTime = ZonedDateTime.now(); final DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME; final DefaultHttpHeaders headers = (DefaultHttpHeaders) response.headers(); headers.set(HttpHeaderNames.SERVER, SERVER_NAME); headers.set(HttpHeaderNames.DATE, dateTime.format(formatter)); headers.set(HttpHeaderNames.CONTENT_TYPE, contentType); headers.set(HttpHeaderNames.CONTENT_LENGTH, Integer.toString(contentLength)); // Close the non-keep-alive connection after the write operation is done. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
From source file:org.pidome.server.system.network.http.Http2ClientHandler.java
@Override public void writeResponse(ChannelHandlerContext ctx, HttpResponseStatus status, byte[] buf, String fileType, String streamId, boolean cache) { String plainIp = HttpRequestHandler.getPlainIp(ctx.channel().localAddress()); ByteBuf content = ctx.alloc().buffer(buf.length); content.writeBytes(buf);//from ww w.ja v a 2 s . c o m FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, content); HttpUtil.setContentLength(response, response.content().readableBytes()); streamId(response, streamId); response.headers().set(HttpHeaderNames.CONTENT_TYPE, fileType); response.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpRequestHandler.getContentTypeHeader(fileType)); response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, "https://" + plainIp + ((port != 80) ? ":" + port : "")); response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true"); response.headers().set(HttpHeaderNames.SERVER, "PiDome integrated 0.2 HTTP2"); if (cache == true) { DateTime dt = new DateTime(); HttpHeaderDateFormat dateFormat = HttpHeaderDateFormat.get(); response.headers().set(HttpHeaderNames.CACHE_CONTROL, "public, max-age=3153600"); response.headers().set(HttpHeaderNames.EXPIRES, dateFormat.format(dt.plusMonths(12).toDate())); } else { response.headers().set(HttpHeaderNames.CACHE_CONTROL, "no-cache, must-revalidate"); response.headers().set(HttpHeaderNames.EXPIRES, "Sat, 26 Jul 1997 05:00:00 GMT"); } ctx.writeAndFlush(response); }
From source file:org.pidome.server.system.network.http.HttpClientHandler.java
License:Apache License
/** * Writes the response to the output/*from w w w. j a v a2 s . c o m*/ * @param ctx The channel context * @param status The response status * @param buf The buffer containing the data to send. * @param fileType The file type. * @param streamId The Stream id (only used in http2) * @param cache (if cache headers should be send). */ @Override public final void writeResponse(ChannelHandlerContext ctx, HttpResponseStatus status, byte[] buf, String fileType, String streamId, boolean cache) { ByteBuf content = ctx.alloc().buffer(buf.length); content.writeBytes(buf); FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, content); HttpUtil.setContentLength(response, response.content().readableBytes()); // In case of SPDY protocol used. if (spdyId != null) { response.headers().set(SPDY_STREAM_ID, spdyId); response.headers().set(SPDY_STREAM_PRIO, 0); response.headers().set(HttpHeaderNames.SERVER, "PiDome integrated 0.2 SPDY"); } else { response.headers().set(HttpHeaderNames.SERVER, "PiDome integrated 0.2 HTTP1.1"); } response.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpRequestHandler.getContentTypeHeader(fileType)); response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, "http" + ((ssl == true) ? "s" : "") + "://" + plainIp + ((port != 80) ? ":" + port : "")); response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true"); if (cache == true) { DateTime dt = new DateTime(); HttpHeaderDateFormat dateFormat = HttpHeaderDateFormat.get(); response.headers().set(HttpHeaderNames.CACHE_CONTROL, "public, max-age=3153600"); response.headers().set(HttpHeaderNames.EXPIRES, dateFormat.format(dt.plusMonths(12).toDate())); } else { response.headers().set(HttpHeaderNames.CACHE_CONTROL, "no-cache, must-revalidate"); response.headers().set(HttpHeaderNames.EXPIRES, "Sat, 26 Jul 1997 05:00:00 GMT"); } if (keepAlive) { // Add keep alive header as per: // - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); ctx.write(response); } else { // If keep-alive is off, close the connection once the content is fully written. ctx.write(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); } }