From 2e9887a3cb7a9a832dcf5e9a2fd212db5b05662c Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Fri, 12 Feb 2016 19:18:00 -0500 Subject: [PATCH] Inline NettyHttpChannel#sendResponse Relates #16725 --- .../http/netty/NettyHttpChannel.java | 80 +++++++++++-------- 1 file changed, 45 insertions(+), 35 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/http/netty/NettyHttpChannel.java b/core/src/main/java/org/elasticsearch/http/netty/NettyHttpChannel.java index b11ca19640b..0ef5fff11bb 100644 --- a/core/src/main/java/org/elasticsearch/http/netty/NettyHttpChannel.java +++ b/core/src/main/java/org/elasticsearch/http/netty/NettyHttpChannel.java @@ -53,10 +53,7 @@ import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.CONNECTION; import static org.jboss.netty.handler.codec.http.HttpHeaders.Values.CLOSE; import static org.jboss.netty.handler.codec.http.HttpHeaders.Values.KEEP_ALIVE; -/** - * - */ -public class NettyHttpChannel extends HttpChannel { +public final class NettyHttpChannel extends HttpChannel { private final NettyHttpServerTransport transport; private final Channel channel; @@ -94,18 +91,11 @@ public class NettyHttpChannel extends HttpChannel { String opaque = nettyRequest.headers().get("X-Opaque-Id"); if (opaque != null) { - resp.headers().add("X-Opaque-Id", opaque); + setHeaderField(resp, "X-Opaque-Id", opaque); } // Add all custom headers - Map> customHeaders = response.getHeaders(); - if (customHeaders != null) { - for (Map.Entry> headerEntry : customHeaders.entrySet()) { - for (String headerValue : headerEntry.getValue()) { - resp.headers().add(headerEntry.getKey(), headerValue); - } - } - } + addCustomHeaders(response, resp); BytesReference content = response.content(); ChannelBuffer buffer; @@ -115,30 +105,11 @@ public class NettyHttpChannel extends HttpChannel { resp.setContent(buffer); // If our response doesn't specify a content-type header, set one - if (!resp.headers().contains(HttpHeaders.Names.CONTENT_TYPE)) { - resp.headers().add(HttpHeaders.Names.CONTENT_TYPE, response.contentType()); - } - + setHeaderField(resp, HttpHeaders.Names.CONTENT_TYPE, response.contentType(), false); // If our response has no content-length, calculate and set one - if (!resp.headers().contains(HttpHeaders.Names.CONTENT_LENGTH)) { - resp.headers().add(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(buffer.readableBytes())); - } + setHeaderField(resp, HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(buffer.readableBytes()), false); - if (transport.resetCookies) { - String cookieString = nettyRequest.headers().get(HttpHeaders.Names.COOKIE); - if (cookieString != null) { - CookieDecoder cookieDecoder = new CookieDecoder(); - Set cookies = cookieDecoder.decode(cookieString); - if (!cookies.isEmpty()) { - // Reset the cookies if necessary. - CookieEncoder cookieEncoder = new CookieEncoder(true); - for (Cookie cookie : cookies) { - cookieEncoder.addCookie(cookie); - } - resp.headers().add(HttpHeaders.Names.SET_COOKIE, cookieEncoder.encode()); - } - } - } + addCookies(resp); ChannelFuture future; @@ -166,6 +137,45 @@ public class NettyHttpChannel extends HttpChannel { } } + private void setHeaderField(HttpResponse resp, String headerField, String value) { + setHeaderField(resp, headerField, value, true); + } + + private void setHeaderField(HttpResponse resp, String headerField, String value, boolean override) { + if (override || !resp.headers().contains(headerField)) { + resp.headers().add(headerField, value); + } + } + + private void addCookies(HttpResponse resp) { + if (transport.resetCookies) { + String cookieString = nettyRequest.headers().get(HttpHeaders.Names.COOKIE); + if (cookieString != null) { + CookieDecoder cookieDecoder = new CookieDecoder(); + Set cookies = cookieDecoder.decode(cookieString); + if (!cookies.isEmpty()) { + // Reset the cookies if necessary. + CookieEncoder cookieEncoder = new CookieEncoder(true); + for (Cookie cookie : cookies) { + cookieEncoder.addCookie(cookie); + } + setHeaderField(resp, HttpHeaders.Names.SET_COOKIE, cookieEncoder.encode()); + } + } + } + } + + private void addCustomHeaders(RestResponse response, HttpResponse resp) { + Map> customHeaders = response.getHeaders(); + if (customHeaders != null) { + for (Map.Entry> headerEntry : customHeaders.entrySet()) { + for (String headerValue : headerEntry.getValue()) { + setHeaderField(resp, headerEntry.getKey(), headerValue); + } + } + } + } + // Determine if the request protocol version is HTTP 1.0 private boolean isHttp10() { return nettyRequest.getProtocolVersion().equals(HttpVersion.HTTP_1_0);