From bfaa647bba22ef82196580be614aaa02f8ebc3d0 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Wed, 15 Jul 2009 02:41:04 +0000 Subject: [PATCH] * Fixed a bug in HttpTunnelingClientSocketChannel where the HTTP request is not sent if the scheme is HTTPS * Fixed a bug where HttpTunnelingServlet does not catch EOFException * Added HTTPS support to the HttpTunnelingClientExample --- .../HttpTunnelingClientSocketChannel.java | 2 + .../socket/http/HttpTunnelingServlet.java | 8 +++- .../tunnel/HttpTunnelingClientExample.java | 37 +++++++------------ 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/jboss/netty/channel/socket/http/HttpTunnelingClientSocketChannel.java b/src/main/java/org/jboss/netty/channel/socket/http/HttpTunnelingClientSocketChannel.java index 30d6ca3848..068d0b70be 100644 --- a/src/main/java/org/jboss/netty/channel/socket/http/HttpTunnelingClientSocketChannel.java +++ b/src/main/java/org/jboss/netty/channel/socket/http/HttpTunnelingClientSocketChannel.java @@ -220,6 +220,8 @@ class HttpTunnelingClientSocketChannel extends AbstractChannel sslHandshakeFuture.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture f) { if (f.isSuccess()) { + realChannel.write(req); + requestHeaderWritten = true; future.setSuccess(); fireChannelConnected(virtualChannel, remoteAddress); } else { diff --git a/src/main/java/org/jboss/netty/channel/socket/http/HttpTunnelingServlet.java b/src/main/java/org/jboss/netty/channel/socket/http/HttpTunnelingServlet.java index 3b720d879a..534c8bb7ff 100644 --- a/src/main/java/org/jboss/netty/channel/socket/http/HttpTunnelingServlet.java +++ b/src/main/java/org/jboss/netty/channel/socket/http/HttpTunnelingServlet.java @@ -22,6 +22,7 @@ */ package org.jboss.netty.channel.socket.http; +import java.io.EOFException; import java.io.IOException; import java.io.PushbackInputStream; import java.net.SocketAddress; @@ -172,7 +173,12 @@ public class HttpTunnelingServlet extends HttpServlet { PushbackInputStream in = new PushbackInputStream(req.getInputStream()); while (channel.isConnected()) { - ChannelBuffer buffer = read(in); + ChannelBuffer buffer; + try { + buffer = read(in); + } catch (EOFException e) { + break; + } if (buffer == null) { break; } diff --git a/src/main/java/org/jboss/netty/example/http/tunnel/HttpTunnelingClientExample.java b/src/main/java/org/jboss/netty/example/http/tunnel/HttpTunnelingClientExample.java index f98b137bfe..7f559812e2 100644 --- a/src/main/java/org/jboss/netty/example/http/tunnel/HttpTunnelingClientExample.java +++ b/src/main/java/org/jboss/netty/example/http/tunnel/HttpTunnelingClientExample.java @@ -29,15 +29,14 @@ import java.net.URI; import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ClientBootstrap; -import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelFuture; -import org.jboss.netty.channel.ChannelHandlerContext; -import org.jboss.netty.channel.ChannelPipelineCoverage; import org.jboss.netty.channel.socket.http.HttpTunnelingClientSocketChannelFactory; import org.jboss.netty.channel.socket.oio.OioClientSocketChannelFactory; -import org.jboss.netty.handler.codec.oneone.OneToOneDecoder; +import org.jboss.netty.example.securechat.SecureChatSslContextFactory; import org.jboss.netty.handler.codec.string.StringDecoder; import org.jboss.netty.handler.codec.string.StringEncoder; +import org.jboss.netty.handler.logging.LoggingHandler; +import org.jboss.netty.logging.InternalLogLevel; /** * An HTTP tunneled version of the telnet client example. Please refer to the @@ -65,23 +64,24 @@ public class HttpTunnelingClientExample { URI uri = new URI(args[0]); String scheme = uri.getScheme() == null? "http" : uri.getScheme(); - if (!scheme.equals("http")) { - // We can actually support HTTPS fairly easily by setting - // "sslContext" option in the bootstrap, as explained in - // {@link HttpTunnelingSocketChannelConfig}. - System.err.println("Only HTTP is supported."); - return; - } - HttpTunnelingClientSocketChannelFactory factory = new HttpTunnelingClientSocketChannelFactory(new OioClientSocketChannelFactory(Executors.newCachedThreadPool())); ClientBootstrap bootstrap = new ClientBootstrap(factory); bootstrap.getPipeline().addLast("decoder", new StringDecoder()); bootstrap.getPipeline().addLast("encoder", new StringEncoder()); - bootstrap.getPipeline().addLast("handler", new PrintHandler()); + bootstrap.getPipeline().addLast("handler", new LoggingHandler(InternalLogLevel.INFO)); bootstrap.setOption("serverName", uri.getHost()); bootstrap.setOption("serverPath", uri.getRawPath()); + // Configure SSL if necessary + if (scheme.equals("https")) { + bootstrap.setOption("sslContext", SecureChatSslContextFactory.getClientContext()); + } else if (!scheme.equals("http")) { + // Only HTTP and HTTPS are supported. + System.err.println("Only HTTP(S) is supported."); + return; + } + ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress(uri.getHost(), uri.getPort())); channelFuture.awaitUninterruptibly(); System.out.println("Enter text ('quit' to exit)"); @@ -102,20 +102,11 @@ public class HttpTunnelingClientExample { if (lastWriteFuture != null) { lastWriteFuture.awaitUninterruptibly(); } + channelFuture.getChannel().close(); // Wait until the connection is closed or the connection attempt fails. channelFuture.getChannel().getCloseFuture().awaitUninterruptibly(); factory.releaseExternalResources(); } - - @ChannelPipelineCoverage("all") - static class PrintHandler extends OneToOneDecoder { - @Override - protected Object decode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { - String message = (String) msg; - System.out.println("received message back '" + message + "'"); - return message; - } - } }