Home | History | Annotate | Download | only in okhttp
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * 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,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.squareup.okhttp;
     17 
     18 import com.squareup.okhttp.internal.http.RawHeaders;
     19 
     20 import static com.squareup.okhttp.internal.Util.getDefaultPort;
     21 
     22 /**
     23  * Routing and authentication information sent to an HTTP proxy to create a
     24  * HTTPS to an origin server. Everything in the tunnel request is sent
     25  * unencrypted to the proxy server.
     26  *
     27  * <p>See <a href="http://www.ietf.org/rfc/rfc2817.txt">RFC 2817, Section
     28  * 5.2</a>.
     29  */
     30 public final class TunnelRequest {
     31   final String host;
     32   final int port;
     33   final String userAgent;
     34   final String proxyAuthorization;
     35 
     36   /**
     37    * @param host the origin server's hostname. Not null.
     38    * @param port the origin server's port, like 80 or 443.
     39    * @param userAgent the client's user-agent. Not null.
     40    * @param proxyAuthorization proxy authorization, or null if the proxy is
     41    * used without an authorization header.
     42    */
     43   public TunnelRequest(String host, int port, String userAgent, String proxyAuthorization) {
     44     if (host == null) throw new NullPointerException("host == null");
     45     if (userAgent == null) throw new NullPointerException("userAgent == null");
     46     this.host = host;
     47     this.port = port;
     48     this.userAgent = userAgent;
     49     this.proxyAuthorization = proxyAuthorization;
     50   }
     51 
     52   /**
     53    * If we're creating a TLS tunnel, send only the minimum set of headers.
     54    * This avoids sending potentially sensitive data like HTTP cookies to
     55    * the proxy unencrypted.
     56    */
     57   RawHeaders getRequestHeaders() {
     58     RawHeaders result = new RawHeaders();
     59     result.setRequestLine("CONNECT " + host + ":" + port + " HTTP/1.1");
     60 
     61     // Always set Host and User-Agent.
     62     result.set("Host", port == getDefaultPort("https") ? host : (host + ":" + port));
     63     result.set("User-Agent", userAgent);
     64 
     65     // Copy over the Proxy-Authorization header if it exists.
     66     if (proxyAuthorization != null) {
     67       result.set("Proxy-Authorization", proxyAuthorization);
     68     }
     69 
     70     // Always set the Proxy-Connection to Keep-Alive for the benefit of
     71     // HTTP/1.0 proxies like Squid.
     72     result.set("Proxy-Connection", "Keep-Alive");
     73     return result;
     74   }
     75 }
     76