* Optimized HttpTunnelingServlet.read()

* Do not strip \r\n in HttpTunnelingServlet.read()
This commit is contained in:
Trustin Lee 2009-07-15 01:59:43 +00:00
parent 44110a281e
commit 99261232a0

View File

@ -97,6 +97,12 @@ public class HttpTunnelingServlet extends HttpServlet {
} catch (Exception e) { } catch (Exception e) {
throw new ServletException("Failed to create a channel factory.", e); throw new ServletException("Failed to create a channel factory.", e);
} }
// Stuff for testing purpose
//ServerBootstrap b = new ServerBootstrap(new DefaultLocalServerChannelFactory());
//b.getPipeline().addLast("logger", new LoggingHandler(getClass(), InternalLogLevel.INFO, true));
//b.getPipeline().addLast("handler", new EchoHandler());
//b.bind(remoteAddress);
} }
protected SocketAddress parseEndpoint(String endpoint) throws Exception { protected SocketAddress parseEndpoint(String endpoint) throws Exception {
@ -182,29 +188,25 @@ public class HttpTunnelingServlet extends HttpServlet {
byte[] buf; byte[] buf;
int readBytes; int readBytes;
for (;;) { int bytesToRead = in.available();
int bytesToRead = in.available(); if (bytesToRead > 0) {
if (bytesToRead > 0) { buf = new byte[bytesToRead];
buf = new byte[bytesToRead]; readBytes = in.read(buf);
readBytes = in.read(buf); } else if (bytesToRead == 0) {
break; int b = in.read();
} else if (bytesToRead == 0) { if (b < 0 || in.available() < 0) {
int b = in.read();
if (b < 0 || in.available() < 0) {
return null;
}
if (b == 13) {
// XXX Why do we do this?
in.read();
} else {
in.unread(b);
}
} else {
return null; return null;
} }
in.unread(b);
bytesToRead = in.available();
buf = new byte[bytesToRead];
readBytes = in.read(buf);
} else {
return null;
} }
assert readBytes > 0;
ChannelBuffer buffer; ChannelBuffer buffer;
if (readBytes == buf.length) { if (readBytes == buf.length) {
buffer = ChannelBuffers.wrappedBuffer(buf); buffer = ChannelBuffers.wrappedBuffer(buf);