HTTP: Disable automatic cookie parsing and resetting, allow to enable it, closes #1177.

This commit is contained in:
Shay Banon 2011-07-28 22:02:39 +03:00
parent b104c19e7a
commit b07d9d56b4
4 changed files with 27 additions and 13 deletions

View File

@ -258,6 +258,7 @@ public class HttpDownloadHelper {
if (connection instanceof HttpURLConnection) { if (connection instanceof HttpURLConnection) {
((HttpURLConnection) connection).setInstanceFollowRedirects(false); ((HttpURLConnection) connection).setInstanceFollowRedirects(false);
((HttpURLConnection) connection).setUseCaches(true); ((HttpURLConnection) connection).setUseCaches(true);
((HttpURLConnection) connection).setConnectTimeout(5000);
} }
// connect to the remote site (may take some time) // connect to the remote site (may take some time)
connection.connect(); connection.connect();

View File

@ -19,7 +19,11 @@
package org.elasticsearch.http.netty; package org.elasticsearch.http.netty;
import org.elasticsearch.common.netty.channel.*; import org.elasticsearch.common.netty.channel.ChannelHandler;
import org.elasticsearch.common.netty.channel.ChannelHandlerContext;
import org.elasticsearch.common.netty.channel.ExceptionEvent;
import org.elasticsearch.common.netty.channel.MessageEvent;
import org.elasticsearch.common.netty.channel.SimpleChannelUpstreamHandler;
import org.elasticsearch.common.netty.handler.codec.http.HttpRequest; import org.elasticsearch.common.netty.handler.codec.http.HttpRequest;
@ -37,7 +41,7 @@ public class HttpRequestHandler extends SimpleChannelUpstreamHandler {
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { @Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
HttpRequest request = (HttpRequest) e.getMessage(); HttpRequest request = (HttpRequest) e.getMessage();
serverTransport.dispatchRequest(new NettyHttpRequest(request), new NettyHttpChannel(e.getChannel(), request)); serverTransport.dispatchRequest(new NettyHttpRequest(request), new NettyHttpChannel(serverTransport, e.getChannel(), request));
super.messageReceived(ctx, e); super.messageReceived(ctx, e);
} }

View File

@ -48,10 +48,12 @@ import java.util.Set;
* @author kimchy (shay.banon) * @author kimchy (shay.banon)
*/ */
public class NettyHttpChannel implements HttpChannel { public class NettyHttpChannel implements HttpChannel {
private final NettyHttpServerTransport transport;
private final Channel channel; private final Channel channel;
private final org.elasticsearch.common.netty.handler.codec.http.HttpRequest request; private final org.elasticsearch.common.netty.handler.codec.http.HttpRequest request;
public NettyHttpChannel(Channel channel, org.elasticsearch.common.netty.handler.codec.http.HttpRequest request) { public NettyHttpChannel(NettyHttpServerTransport transport, Channel channel, org.elasticsearch.common.netty.handler.codec.http.HttpRequest request) {
this.transport = transport;
this.channel = channel; this.channel = channel;
this.request = request; this.request = request;
} }
@ -126,6 +128,7 @@ public class NettyHttpChannel implements HttpChannel {
resp.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(buf.readableBytes())); resp.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(buf.readableBytes()));
if (transport.resetCookies) {
String cookieString = request.getHeader(HttpHeaders.Names.COOKIE); String cookieString = request.getHeader(HttpHeaders.Names.COOKIE);
if (cookieString != null) { if (cookieString != null) {
CookieDecoder cookieDecoder = new CookieDecoder(); CookieDecoder cookieDecoder = new CookieDecoder();
@ -139,6 +142,7 @@ public class NettyHttpChannel implements HttpChannel {
resp.addHeader(HttpHeaders.Names.SET_COOKIE, cookieEncoder.encode()); resp.addHeader(HttpHeaders.Names.SET_COOKIE, cookieEncoder.encode());
} }
} }
}
// Write the response. // Write the response.
ChannelFuture future = channel.write(resp); ChannelFuture future = channel.write(resp);

View File

@ -95,6 +95,8 @@ public class NettyHttpServerTransport extends AbstractLifecycleComponent<HttpSer
private final int compressionLevel; private final int compressionLevel;
final boolean resetCookies;
private final String port; private final String port;
private final String bindHost; private final String bindHost;
@ -128,6 +130,9 @@ public class NettyHttpServerTransport extends AbstractLifecycleComponent<HttpSer
this.maxChunkSize = componentSettings.getAsBytesSize("max_chunk_size", settings.getAsBytesSize("http.max_chunk_size", new ByteSizeValue(8, ByteSizeUnit.KB))); this.maxChunkSize = componentSettings.getAsBytesSize("max_chunk_size", settings.getAsBytesSize("http.max_chunk_size", new ByteSizeValue(8, ByteSizeUnit.KB)));
this.maxHeaderSize = componentSettings.getAsBytesSize("max_header_size", settings.getAsBytesSize("http.max_header_size", new ByteSizeValue(8, ByteSizeUnit.KB))); this.maxHeaderSize = componentSettings.getAsBytesSize("max_header_size", settings.getAsBytesSize("http.max_header_size", new ByteSizeValue(8, ByteSizeUnit.KB)));
this.maxInitialLineLength = componentSettings.getAsBytesSize("max_initial_line_length", settings.getAsBytesSize("http.max_initial_line_length", new ByteSizeValue(4, ByteSizeUnit.KB))); this.maxInitialLineLength = componentSettings.getAsBytesSize("max_initial_line_length", settings.getAsBytesSize("http.max_initial_line_length", new ByteSizeValue(4, ByteSizeUnit.KB)));
// don't reset cookies by default, since I don't think we really need to, and parsing of cookies with netty is slow
// and requires a large stack allocation because of the use of regex
this.resetCookies = componentSettings.getAsBoolean("reset_cookies", settings.getAsBoolean("http.reset_cookies", false));
this.workerCount = componentSettings.getAsInt("worker_count", Runtime.getRuntime().availableProcessors() * 2); this.workerCount = componentSettings.getAsInt("worker_count", Runtime.getRuntime().availableProcessors() * 2);
this.blockingServer = settings.getAsBoolean("http.blocking_server", settings.getAsBoolean(TCP_BLOCKING_SERVER, settings.getAsBoolean(TCP_BLOCKING, false))); this.blockingServer = settings.getAsBoolean("http.blocking_server", settings.getAsBoolean(TCP_BLOCKING_SERVER, settings.getAsBoolean(TCP_BLOCKING, false)));
this.port = componentSettings.get("port", settings.get("http.port", "9200-9300")); this.port = componentSettings.get("port", settings.get("http.port", "9200-9300"));