2008-11-19 08:22:15 +01:00
|
|
|
/*
|
2012-06-04 22:31:44 +02:00
|
|
|
* Copyright 2012 The Netty Project
|
2009-06-19 19:48:17 +02:00
|
|
|
*
|
2011-12-09 06:18:34 +01:00
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
2008-11-19 08:22:15 +01:00
|
|
|
*
|
2012-06-04 22:31:44 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-11-19 08:22:15 +01:00
|
|
|
*
|
2009-08-28 09:15:49 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
2011-12-09 06:18:34 +01:00
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
2009-08-28 09:15:49 +02:00
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2008-11-19 08:22:15 +01:00
|
|
|
*/
|
2011-12-09 04:38:59 +01:00
|
|
|
package io.netty.example.http.snoop;
|
2008-11-19 08:22:15 +01:00
|
|
|
|
2012-09-12 14:04:41 +02:00
|
|
|
import io.netty.bootstrap.Bootstrap;
|
2019-07-18 10:29:50 +02:00
|
|
|
import io.netty.buffer.Unpooled;
|
2011-12-09 04:38:59 +01:00
|
|
|
import io.netty.channel.Channel;
|
2013-04-03 09:15:33 +02:00
|
|
|
import io.netty.channel.EventLoopGroup;
|
2019-01-23 08:32:05 +01:00
|
|
|
import io.netty.channel.MultithreadEventLoopGroup;
|
|
|
|
import io.netty.channel.nio.NioHandler;
|
2012-06-07 07:52:33 +02:00
|
|
|
import io.netty.channel.socket.nio.NioSocketChannel;
|
2014-02-01 18:32:21 +01:00
|
|
|
import io.netty.handler.codec.http.DefaultFullHttpRequest;
|
2014-10-31 08:48:28 +01:00
|
|
|
import io.netty.handler.codec.http.HttpHeaderNames;
|
|
|
|
import io.netty.handler.codec.http.HttpHeaderValues;
|
2011-12-09 04:38:59 +01:00
|
|
|
import io.netty.handler.codec.http.HttpMethod;
|
2013-01-16 05:22:50 +01:00
|
|
|
import io.netty.handler.codec.http.HttpRequest;
|
2011-12-09 04:38:59 +01:00
|
|
|
import io.netty.handler.codec.http.HttpVersion;
|
2015-04-14 13:57:32 +02:00
|
|
|
import io.netty.handler.codec.http.cookie.ClientCookieEncoder;
|
|
|
|
import io.netty.handler.codec.http.cookie.DefaultCookie;
|
2014-05-17 19:26:01 +02:00
|
|
|
import io.netty.handler.ssl.SslContext;
|
2015-03-26 22:06:11 +01:00
|
|
|
import io.netty.handler.ssl.SslContextBuilder;
|
2014-05-17 19:26:01 +02:00
|
|
|
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
|
2009-03-13 11:12:51 +01:00
|
|
|
|
2012-05-20 07:19:11 +02:00
|
|
|
import java.net.URI;
|
|
|
|
|
2008-11-19 08:22:15 +01:00
|
|
|
/**
|
2009-09-25 04:26:28 +02:00
|
|
|
* A simple HTTP client that prints out the content of the HTTP response to
|
2012-01-14 16:28:25 +01:00
|
|
|
* {@link System#out} to test {@link HttpSnoopServer}.
|
2008-11-19 08:22:15 +01:00
|
|
|
*/
|
2014-05-23 09:46:31 +02:00
|
|
|
public final class HttpSnoopClient {
|
2008-11-26 09:39:32 +01:00
|
|
|
|
2014-05-23 09:46:31 +02:00
|
|
|
static final String URL = System.getProperty("url", "http://127.0.0.1:8080/");
|
2008-11-26 09:39:32 +01:00
|
|
|
|
2014-05-23 09:46:31 +02:00
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
URI uri = new URI(URL);
|
2008-11-26 09:39:32 +01:00
|
|
|
String scheme = uri.getScheme() == null? "http" : uri.getScheme();
|
2014-05-23 09:46:31 +02:00
|
|
|
String host = uri.getHost() == null? "127.0.0.1" : uri.getHost();
|
2010-03-31 04:26:51 +02:00
|
|
|
int port = uri.getPort();
|
|
|
|
if (port == -1) {
|
2012-11-12 00:59:54 +01:00
|
|
|
if ("http".equalsIgnoreCase(scheme)) {
|
2010-03-31 04:26:51 +02:00
|
|
|
port = 80;
|
2012-11-12 00:59:54 +01:00
|
|
|
} else if ("https".equalsIgnoreCase(scheme)) {
|
2010-03-31 04:26:51 +02:00
|
|
|
port = 443;
|
|
|
|
}
|
|
|
|
}
|
2008-11-19 08:22:15 +01:00
|
|
|
|
2012-11-12 00:59:54 +01:00
|
|
|
if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
|
2010-03-31 04:26:51 +02:00
|
|
|
System.err.println("Only HTTP(S) is supported.");
|
2008-11-26 09:39:32 +01:00
|
|
|
return;
|
|
|
|
}
|
2008-11-19 08:22:15 +01:00
|
|
|
|
2014-05-23 09:46:31 +02:00
|
|
|
// Configure SSL context if necessary.
|
|
|
|
final boolean ssl = "https".equalsIgnoreCase(scheme);
|
2014-05-17 19:26:01 +02:00
|
|
|
final SslContext sslCtx;
|
|
|
|
if (ssl) {
|
2015-03-26 22:06:11 +01:00
|
|
|
sslCtx = SslContextBuilder.forClient()
|
|
|
|
.trustManager(InsecureTrustManagerFactory.INSTANCE).build();
|
2014-05-17 19:26:01 +02:00
|
|
|
} else {
|
|
|
|
sslCtx = null;
|
|
|
|
}
|
2010-03-31 04:26:51 +02:00
|
|
|
|
2008-11-19 08:22:15 +01:00
|
|
|
// Configure the client.
|
2019-01-23 08:32:05 +01:00
|
|
|
EventLoopGroup group = new MultithreadEventLoopGroup(NioHandler.newFactory());
|
2012-05-20 07:19:11 +02:00
|
|
|
try {
|
2013-04-03 09:15:33 +02:00
|
|
|
Bootstrap b = new Bootstrap();
|
|
|
|
b.group(group)
|
2012-09-11 09:34:51 +02:00
|
|
|
.channel(NioSocketChannel.class)
|
2014-05-17 19:26:01 +02:00
|
|
|
.handler(new HttpSnoopClientInitializer(sslCtx));
|
2012-05-20 07:19:11 +02:00
|
|
|
|
|
|
|
// Make the connection attempt.
|
2013-01-30 12:06:40 +01:00
|
|
|
Channel ch = b.connect(host, port).sync().channel();
|
2012-05-20 07:19:11 +02:00
|
|
|
|
|
|
|
// Prepare the HTTP request.
|
2014-02-01 18:32:21 +01:00
|
|
|
HttpRequest request = new DefaultFullHttpRequest(
|
2019-07-18 10:29:50 +02:00
|
|
|
HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath(), Unpooled.EMPTY_BUFFER);
|
2014-10-31 08:48:28 +01:00
|
|
|
request.headers().set(HttpHeaderNames.HOST, host);
|
|
|
|
request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
|
|
|
|
request.headers().set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP);
|
2012-05-20 07:19:11 +02:00
|
|
|
|
|
|
|
// Set some example cookies.
|
2013-01-16 05:22:50 +01:00
|
|
|
request.headers().set(
|
2014-10-31 08:48:28 +01:00
|
|
|
HttpHeaderNames.COOKIE,
|
2015-04-14 13:57:32 +02:00
|
|
|
ClientCookieEncoder.STRICT.encode(
|
2012-06-25 22:26:54 +02:00
|
|
|
new DefaultCookie("my-cookie", "foo"),
|
|
|
|
new DefaultCookie("another-cookie", "bar")));
|
2012-05-20 07:19:11 +02:00
|
|
|
|
|
|
|
// Send the HTTP request.
|
2013-07-11 11:25:40 +02:00
|
|
|
ch.writeAndFlush(request);
|
2012-05-20 07:19:11 +02:00
|
|
|
|
|
|
|
// Wait for the server to close the connection.
|
|
|
|
ch.closeFuture().sync();
|
|
|
|
} finally {
|
|
|
|
// Shut down executor threads to exit.
|
2013-04-27 09:39:19 +02:00
|
|
|
group.shutdownGracefully();
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
|
|
|
}
|
2009-08-28 09:15:49 +02:00
|
|
|
}
|