parent
a447c06efc
commit
2e9887a3cb
|
@ -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.CLOSE;
|
||||||
import static org.jboss.netty.handler.codec.http.HttpHeaders.Values.KEEP_ALIVE;
|
import static org.jboss.netty.handler.codec.http.HttpHeaders.Values.KEEP_ALIVE;
|
||||||
|
|
||||||
/**
|
public final class NettyHttpChannel extends HttpChannel {
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class NettyHttpChannel extends HttpChannel {
|
|
||||||
|
|
||||||
private final NettyHttpServerTransport transport;
|
private final NettyHttpServerTransport transport;
|
||||||
private final Channel channel;
|
private final Channel channel;
|
||||||
|
@ -94,18 +91,11 @@ public class NettyHttpChannel extends HttpChannel {
|
||||||
|
|
||||||
String opaque = nettyRequest.headers().get("X-Opaque-Id");
|
String opaque = nettyRequest.headers().get("X-Opaque-Id");
|
||||||
if (opaque != null) {
|
if (opaque != null) {
|
||||||
resp.headers().add("X-Opaque-Id", opaque);
|
setHeaderField(resp, "X-Opaque-Id", opaque);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add all custom headers
|
// Add all custom headers
|
||||||
Map<String, List<String>> customHeaders = response.getHeaders();
|
addCustomHeaders(response, resp);
|
||||||
if (customHeaders != null) {
|
|
||||||
for (Map.Entry<String, List<String>> headerEntry : customHeaders.entrySet()) {
|
|
||||||
for (String headerValue : headerEntry.getValue()) {
|
|
||||||
resp.headers().add(headerEntry.getKey(), headerValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
BytesReference content = response.content();
|
BytesReference content = response.content();
|
||||||
ChannelBuffer buffer;
|
ChannelBuffer buffer;
|
||||||
|
@ -115,30 +105,11 @@ public class NettyHttpChannel extends HttpChannel {
|
||||||
resp.setContent(buffer);
|
resp.setContent(buffer);
|
||||||
|
|
||||||
// If our response doesn't specify a content-type header, set one
|
// If our response doesn't specify a content-type header, set one
|
||||||
if (!resp.headers().contains(HttpHeaders.Names.CONTENT_TYPE)) {
|
setHeaderField(resp, HttpHeaders.Names.CONTENT_TYPE, response.contentType(), false);
|
||||||
resp.headers().add(HttpHeaders.Names.CONTENT_TYPE, response.contentType());
|
|
||||||
}
|
|
||||||
|
|
||||||
// If our response has no content-length, calculate and set one
|
// If our response has no content-length, calculate and set one
|
||||||
if (!resp.headers().contains(HttpHeaders.Names.CONTENT_LENGTH)) {
|
setHeaderField(resp, HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(buffer.readableBytes()), false);
|
||||||
resp.headers().add(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(buffer.readableBytes()));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (transport.resetCookies) {
|
addCookies(resp);
|
||||||
String cookieString = nettyRequest.headers().get(HttpHeaders.Names.COOKIE);
|
|
||||||
if (cookieString != null) {
|
|
||||||
CookieDecoder cookieDecoder = new CookieDecoder();
|
|
||||||
Set<Cookie> 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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ChannelFuture future;
|
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<Cookie> 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<String, List<String>> customHeaders = response.getHeaders();
|
||||||
|
if (customHeaders != null) {
|
||||||
|
for (Map.Entry<String, List<String>> headerEntry : customHeaders.entrySet()) {
|
||||||
|
for (String headerValue : headerEntry.getValue()) {
|
||||||
|
setHeaderField(resp, headerEntry.getKey(), headerValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Determine if the request protocol version is HTTP 1.0
|
// Determine if the request protocol version is HTTP 1.0
|
||||||
private boolean isHttp10() {
|
private boolean isHttp10() {
|
||||||
return nettyRequest.getProtocolVersion().equals(HttpVersion.HTTP_1_0);
|
return nettyRequest.getProtocolVersion().equals(HttpVersion.HTTP_1_0);
|
||||||
|
|
Loading…
Reference in New Issue