* 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
This commit is contained in:
parent
5b8f9b73ec
commit
bfaa647bba
@ -220,6 +220,8 @@ class HttpTunnelingClientSocketChannel extends AbstractChannel
|
|||||||
sslHandshakeFuture.addListener(new ChannelFutureListener() {
|
sslHandshakeFuture.addListener(new ChannelFutureListener() {
|
||||||
public void operationComplete(ChannelFuture f) {
|
public void operationComplete(ChannelFuture f) {
|
||||||
if (f.isSuccess()) {
|
if (f.isSuccess()) {
|
||||||
|
realChannel.write(req);
|
||||||
|
requestHeaderWritten = true;
|
||||||
future.setSuccess();
|
future.setSuccess();
|
||||||
fireChannelConnected(virtualChannel, remoteAddress);
|
fireChannelConnected(virtualChannel, remoteAddress);
|
||||||
} else {
|
} else {
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.jboss.netty.channel.socket.http;
|
package org.jboss.netty.channel.socket.http;
|
||||||
|
|
||||||
|
import java.io.EOFException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PushbackInputStream;
|
import java.io.PushbackInputStream;
|
||||||
import java.net.SocketAddress;
|
import java.net.SocketAddress;
|
||||||
@ -172,7 +173,12 @@ public class HttpTunnelingServlet extends HttpServlet {
|
|||||||
PushbackInputStream in =
|
PushbackInputStream in =
|
||||||
new PushbackInputStream(req.getInputStream());
|
new PushbackInputStream(req.getInputStream());
|
||||||
while (channel.isConnected()) {
|
while (channel.isConnected()) {
|
||||||
ChannelBuffer buffer = read(in);
|
ChannelBuffer buffer;
|
||||||
|
try {
|
||||||
|
buffer = read(in);
|
||||||
|
} catch (EOFException e) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
if (buffer == null) {
|
if (buffer == null) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -29,15 +29,14 @@ import java.net.URI;
|
|||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
import org.jboss.netty.bootstrap.ClientBootstrap;
|
import org.jboss.netty.bootstrap.ClientBootstrap;
|
||||||
import org.jboss.netty.channel.Channel;
|
|
||||||
import org.jboss.netty.channel.ChannelFuture;
|
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.http.HttpTunnelingClientSocketChannelFactory;
|
||||||
import org.jboss.netty.channel.socket.oio.OioClientSocketChannelFactory;
|
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.StringDecoder;
|
||||||
import org.jboss.netty.handler.codec.string.StringEncoder;
|
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
|
* 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]);
|
URI uri = new URI(args[0]);
|
||||||
String scheme = uri.getScheme() == null? "http" : uri.getScheme();
|
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()));
|
HttpTunnelingClientSocketChannelFactory factory = new HttpTunnelingClientSocketChannelFactory(new OioClientSocketChannelFactory(Executors.newCachedThreadPool()));
|
||||||
ClientBootstrap bootstrap = new ClientBootstrap(factory);
|
ClientBootstrap bootstrap = new ClientBootstrap(factory);
|
||||||
bootstrap.getPipeline().addLast("decoder", new StringDecoder());
|
bootstrap.getPipeline().addLast("decoder", new StringDecoder());
|
||||||
bootstrap.getPipeline().addLast("encoder", new StringEncoder());
|
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("serverName", uri.getHost());
|
||||||
bootstrap.setOption("serverPath", uri.getRawPath());
|
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 channelFuture = bootstrap.connect(new InetSocketAddress(uri.getHost(), uri.getPort()));
|
||||||
channelFuture.awaitUninterruptibly();
|
channelFuture.awaitUninterruptibly();
|
||||||
System.out.println("Enter text ('quit' to exit)");
|
System.out.println("Enter text ('quit' to exit)");
|
||||||
@ -102,20 +102,11 @@ public class HttpTunnelingClientExample {
|
|||||||
if (lastWriteFuture != null) {
|
if (lastWriteFuture != null) {
|
||||||
lastWriteFuture.awaitUninterruptibly();
|
lastWriteFuture.awaitUninterruptibly();
|
||||||
}
|
}
|
||||||
|
|
||||||
channelFuture.getChannel().close();
|
channelFuture.getChannel().close();
|
||||||
// Wait until the connection is closed or the connection attempt fails.
|
// Wait until the connection is closed or the connection attempt fails.
|
||||||
channelFuture.getChannel().getCloseFuture().awaitUninterruptibly();
|
channelFuture.getChannel().getCloseFuture().awaitUninterruptibly();
|
||||||
|
|
||||||
factory.releaseExternalResources();
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user