1 /*
   2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.net.http;
  27 
  28 import java.io.IOException;
  29 import java.net.InetSocketAddress;
  30 import java.nio.ByteBuffer;
  31 import java.nio.channels.SocketChannel;
  32 import java.security.AccessControlContext;
  33 import java.util.concurrent.CompletableFuture;
  34 
  35 /**
  36  * A plain text socket tunnel through a proxy. Uses "CONNECT" but does not
  37  * encrypt. Used by WebSockets. Subclassed in SSLTunnelConnection for encryption.
  38  */
  39 class PlainTunnelingConnection extends HttpConnection {
  40 
  41     final PlainHttpConnection delegate;
  42     protected final InetSocketAddress proxyAddr;
  43     private volatile boolean connected;
  44     private final AccessControlContext acc;
  45 
  46     @Override
  47     public CompletableFuture<Void> connectAsync() {
  48         return delegate.connectAsync()
  49             .thenCompose((Void v) -> {
  50                 HttpRequestImpl req = new HttpRequestImpl(client, "CONNECT", address);
  51                 Exchange connectExchange = new Exchange(req, acc);
  52                 return connectExchange
  53                     .responseAsyncImpl(delegate)
  54                     .thenCompose((HttpResponse r) -> {
  55                         CompletableFuture<Void> cf = new CompletableFuture<>();
  56                         if (r.statusCode() != 200) {
  57                             cf.completeExceptionally(new IOException("Tunnel failed"));
  58                         } else {
  59                             connected = true;
  60                             cf.complete(null);
  61                         }
  62                         return cf;
  63                     });
  64             });
  65     }
  66 
  67     @Override
  68     public void connect() throws IOException, InterruptedException {
  69         delegate.connect();
  70         HttpRequestImpl req = new HttpRequestImpl(client, "CONNECT", address);
  71         Exchange connectExchange = new Exchange(req, acc);
  72         HttpResponse r = connectExchange.responseImpl(delegate);
  73         if (r.statusCode() != 200) {
  74             throw new IOException("Tunnel failed");
  75         }
  76         connected = true;
  77     }
  78 
  79     @Override
  80     boolean connected() {
  81         return connected;
  82     }
  83 
  84     protected PlainTunnelingConnection(InetSocketAddress addr,
  85                                        InetSocketAddress proxy,
  86                                        HttpClientImpl client,
  87                                        AccessControlContext acc) {
  88         super(addr, client);
  89         this.proxyAddr = proxy;
  90         this.acc = acc;
  91         delegate = new PlainHttpConnection(proxy, client);
  92     }
  93 
  94     @Override
  95     SocketChannel channel() {
  96         return delegate.channel();
  97     }
  98 
  99     @Override
 100     ConnectionPool.CacheKey cacheKey() {
 101         return new ConnectionPool.CacheKey(null, proxyAddr);
 102     }
 103 
 104     @Override
 105     long write(ByteBuffer[] buffers, int start, int number) throws IOException {
 106         return delegate.write(buffers, start, number);
 107     }
 108 
 109     @Override
 110     long write(ByteBuffer buffer) throws IOException {
 111         return delegate.write(buffer);
 112     }
 113 
 114     @Override
 115     public void close() {
 116         delegate.close();
 117         connected = false;
 118     }
 119 
 120     @Override
 121     protected ByteBuffer readImpl(int length) throws IOException {
 122         return delegate.readImpl(length);
 123     }
 124 
 125     @Override
 126     CompletableFuture<Void> whenReceivingResponse() {
 127         return delegate.whenReceivingResponse();
 128     }
 129 
 130     @Override
 131     protected int readImpl(ByteBuffer buffer) throws IOException {
 132         return delegate.readImpl(buffer);
 133     }
 134 
 135     @Override
 136     boolean isSecure() {
 137         return false;
 138     }
 139 
 140     @Override
 141     boolean isProxied() {
 142         return true;
 143     }
 144 }