HTTPS support

This commit is contained in:
Trustin Lee 2010-03-31 02:26:51 +00:00
parent 7c139cf1b7
commit c1a70b3579
2 changed files with 44 additions and 11 deletions

View File

@ -53,15 +53,22 @@ public class HttpClient {
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();
String host = uri.getHost() == null? "localhost" : uri.getHost(); String host = uri.getHost() == null? "localhost" : uri.getHost();
int port = uri.getPort() == -1? 80 : uri.getPort(); int port = uri.getPort();
if (port == -1) {
if (scheme.equalsIgnoreCase("http")) {
port = 80;
} else if (scheme.equalsIgnoreCase("https")) {
port = 443;
}
}
if (!scheme.equals("http")) { if (!scheme.equalsIgnoreCase("http") && !scheme.equalsIgnoreCase("https")) {
// We can actually support HTTPS fairly easily by inserting System.err.println("Only HTTP(S) is supported.");
// an SslHandler to the pipeline - left as an exercise.
System.err.println("Only HTTP is supported.");
return; return;
} }
boolean ssl = scheme.equalsIgnoreCase("https");
// Configure the client. // Configure the client.
ClientBootstrap bootstrap = new ClientBootstrap( ClientBootstrap bootstrap = new ClientBootstrap(
new NioClientSocketChannelFactory( new NioClientSocketChannelFactory(
@ -69,7 +76,7 @@ public class HttpClient {
Executors.newCachedThreadPool())); Executors.newCachedThreadPool()));
// Set up the event pipeline factory. // Set up the event pipeline factory.
bootstrap.setPipelineFactory(new HttpClientPipelineFactory()); bootstrap.setPipelineFactory(new HttpClientPipelineFactory(ssl));
// Start the connection attempt. // Start the connection attempt.
ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
@ -82,16 +89,20 @@ public class HttpClient {
return; return;
} }
// Send the HTTP request. // Prepare the HTTP request.
HttpRequest request = new DefaultHttpRequest( HttpRequest request = new DefaultHttpRequest(
HttpVersion.HTTP_1_1, HttpMethod.GET, uri.toASCIIString()); HttpVersion.HTTP_1_1, HttpMethod.GET, uri.toASCIIString());
request.setHeader(HttpHeaders.Names.HOST, host); request.setHeader(HttpHeaders.Names.HOST, host);
request.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); request.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
request.setHeader(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP); request.setHeader(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
// Set some example cookies.
CookieEncoder httpCookieEncoder = new CookieEncoder(false); CookieEncoder httpCookieEncoder = new CookieEncoder(false);
httpCookieEncoder.addCookie("my-cookie", "foo"); httpCookieEncoder.addCookie("my-cookie", "foo");
httpCookieEncoder.addCookie("another-cookie", "bar"); httpCookieEncoder.addCookie("another-cookie", "bar");
request.setHeader(HttpHeaders.Names.COOKIE, httpCookieEncoder.encode()); request.setHeader(HttpHeaders.Names.COOKIE, httpCookieEncoder.encode());
// Send the HTTP request.
channel.write(request); channel.write(request);
// Wait for the server to close the connection. // Wait for the server to close the connection.

View File

@ -17,11 +17,14 @@ package org.jboss.netty.example.http.snoop;
import static org.jboss.netty.channel.Channels.*; import static org.jboss.netty.channel.Channels.*;
import javax.net.ssl.SSLEngine;
import org.jboss.netty.channel.ChannelPipeline; import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory; import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.example.securechat.SecureChatSslContextFactory;
import org.jboss.netty.handler.codec.http.HttpClientCodec;
import org.jboss.netty.handler.codec.http.HttpContentDecompressor; import org.jboss.netty.handler.codec.http.HttpContentDecompressor;
import org.jboss.netty.handler.codec.http.HttpRequestEncoder; import org.jboss.netty.handler.ssl.SslHandler;
import org.jboss.netty.handler.codec.http.HttpResponseDecoder;
/** /**
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a> * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
@ -31,15 +34,34 @@ import org.jboss.netty.handler.codec.http.HttpResponseDecoder;
* @version $Rev$, $Date$ * @version $Rev$, $Date$
*/ */
public class HttpClientPipelineFactory implements ChannelPipelineFactory { public class HttpClientPipelineFactory implements ChannelPipelineFactory {
private final boolean ssl;
public HttpClientPipelineFactory(boolean ssl) {
this.ssl = ssl;
}
public ChannelPipeline getPipeline() throws Exception { public ChannelPipeline getPipeline() throws Exception {
// Create a default pipeline implementation. // Create a default pipeline implementation.
ChannelPipeline pipeline = pipeline(); ChannelPipeline pipeline = pipeline();
pipeline.addLast("decoder", new HttpResponseDecoder());
// Enable HTTPS if necessary.
if (ssl) {
SSLEngine engine =
SecureChatSslContextFactory.getClientContext().createSSLEngine();
engine.setUseClientMode(true);
pipeline.addLast("ssl", new SslHandler(engine));
}
pipeline.addLast("codec", new HttpClientCodec());
// Remove the following line if you don't want automatic content decompression. // Remove the following line if you don't want automatic content decompression.
pipeline.addLast("inflater", new HttpContentDecompressor()); pipeline.addLast("inflater", new HttpContentDecompressor());
// Uncomment the following line if you don't want to handle HttpChunks. // Uncomment the following line if you don't want to handle HttpChunks.
//pipeline.addLast("aggregator", new HttpChunkAggregator(1048576)); //pipeline.addLast("aggregator", new HttpChunkAggregator(1048576));
pipeline.addLast("encoder", new HttpRequestEncoder());
pipeline.addLast("handler", new HttpResponseHandler()); pipeline.addLast("handler", new HttpResponseHandler());
return pipeline; return pipeline;
} }