View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.example.http.snoop;
17  
18  import io.netty.bootstrap.Bootstrap;
19  import io.netty.channel.Channel;
20  import io.netty.channel.EventLoopGroup;
21  import io.netty.channel.nio.NioEventLoopGroup;
22  import io.netty.channel.socket.nio.NioSocketChannel;
23  import io.netty.handler.codec.http.DefaultFullHttpRequest;
24  import io.netty.handler.codec.http.HttpHeaderNames;
25  import io.netty.handler.codec.http.HttpHeaderValues;
26  import io.netty.handler.codec.http.HttpMethod;
27  import io.netty.handler.codec.http.HttpRequest;
28  import io.netty.handler.codec.http.HttpVersion;
29  import io.netty.handler.codec.http.cookie.ClientCookieEncoder;
30  import io.netty.handler.codec.http.cookie.DefaultCookie;
31  import io.netty.handler.ssl.SslContext;
32  import io.netty.handler.ssl.SslContextBuilder;
33  import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
34  
35  import java.net.URI;
36  
37  /**
38   * A simple HTTP client that prints out the content of the HTTP response to
39   * {@link System#out} to test {@link HttpSnoopServer}.
40   */
41  public final class HttpSnoopClient {
42  
43      static final String URL = System.getProperty("url", "http://127.0.0.1:8080/");
44  
45      public static void main(String[] args) throws Exception {
46          URI uri = new URI(URL);
47          String scheme = uri.getScheme() == null? "http" : uri.getScheme();
48          String host = uri.getHost() == null? "127.0.0.1" : uri.getHost();
49          int port = uri.getPort();
50          if (port == -1) {
51              if ("http".equalsIgnoreCase(scheme)) {
52                  port = 80;
53              } else if ("https".equalsIgnoreCase(scheme)) {
54                  port = 443;
55              }
56          }
57  
58          if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
59              System.err.println("Only HTTP(S) is supported.");
60              return;
61          }
62  
63          // Configure SSL context if necessary.
64          final boolean ssl = "https".equalsIgnoreCase(scheme);
65          final SslContext sslCtx;
66          if (ssl) {
67              sslCtx = SslContextBuilder.forClient()
68                  .trustManager(InsecureTrustManagerFactory.INSTANCE).build();
69          } else {
70              sslCtx = null;
71          }
72  
73          // Configure the client.
74          EventLoopGroup group = new NioEventLoopGroup();
75          try {
76              Bootstrap b = new Bootstrap();
77              b.group(group)
78               .channel(NioSocketChannel.class)
79               .handler(new HttpSnoopClientInitializer(sslCtx));
80  
81              // Make the connection attempt.
82              Channel ch = b.connect(host, port).sync().channel();
83  
84              // Prepare the HTTP request.
85              HttpRequest request = new DefaultFullHttpRequest(
86                      HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath());
87              request.headers().set(HttpHeaderNames.HOST, host);
88              request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
89              request.headers().set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP);
90  
91              // Set some example cookies.
92              request.headers().set(
93                      HttpHeaderNames.COOKIE,
94                      ClientCookieEncoder.STRICT.encode(
95                              new DefaultCookie("my-cookie", "foo"),
96                              new DefaultCookie("another-cookie", "bar")));
97  
98              // Send the HTTP request.
99              ch.writeAndFlush(request);
100 
101             // Wait for the server to close the connection.
102             ch.closeFuture().sync();
103         } finally {
104             // Shut down executor threads to exit.
105             group.shutdownGracefully();
106         }
107     }
108 }