12231db3e6bb54447a9b14cf004a6cb03c373651cjwilson/* 22231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * Copyright (C) 2012 The Android Open Source Project 32231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * 42231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * Licensed under the Apache License, Version 2.0 (the "License"); 52231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * you may not use this file except in compliance with the License. 62231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * You may obtain a copy of the License at 72231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * 82231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * http://www.apache.org/licenses/LICENSE-2.0 92231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * 102231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * Unless required by applicable law or agreed to in writing, software 112231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * distributed under the License is distributed on an "AS IS" BASIS, 122231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 132231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * See the License for the specific language governing permissions and 142231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * limitations under the License. 152231db3e6bb54447a9b14cf004a6cb03c373651cjwilson */ 162231db3e6bb54447a9b14cf004a6cb03c373651cjwilsonpackage com.squareup.okhttp; 172231db3e6bb54447a9b14cf004a6cb03c373651cjwilson 183c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.io.IOException; 193c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.net.URL; 202231db3e6bb54447a9b14cf004a6cb03c373651cjwilson 2154cf3446000fdcf88a9e62724f7deb0282e98da1jwilsonimport static com.squareup.okhttp.internal.Util.getDefaultPort; 2254cf3446000fdcf88a9e62724f7deb0282e98da1jwilson 232231db3e6bb54447a9b14cf004a6cb03c373651cjwilson/** 242231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * Routing and authentication information sent to an HTTP proxy to create a 252231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * HTTPS to an origin server. Everything in the tunnel request is sent 262231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * unencrypted to the proxy server. 272231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * 282231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * <p>See <a href="http://www.ietf.org/rfc/rfc2817.txt">RFC 2817, Section 292231db3e6bb54447a9b14cf004a6cb03c373651cjwilson * 5.2</a>. 302231db3e6bb54447a9b14cf004a6cb03c373651cjwilson */ 312231db3e6bb54447a9b14cf004a6cb03c373651cjwilsonpublic final class TunnelRequest { 3254cf3446000fdcf88a9e62724f7deb0282e98da1jwilson final String host; 3354cf3446000fdcf88a9e62724f7deb0282e98da1jwilson final int port; 3454cf3446000fdcf88a9e62724f7deb0282e98da1jwilson final String userAgent; 3554cf3446000fdcf88a9e62724f7deb0282e98da1jwilson final String proxyAuthorization; 362231db3e6bb54447a9b14cf004a6cb03c373651cjwilson 3754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson /** 3854cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * @param host the origin server's hostname. Not null. 3954cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * @param port the origin server's port, like 80 or 443. 4054cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * @param userAgent the client's user-agent. Not null. 4154cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * @param proxyAuthorization proxy authorization, or null if the proxy is 4254cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * used without an authorization header. 4354cf3446000fdcf88a9e62724f7deb0282e98da1jwilson */ 4454cf3446000fdcf88a9e62724f7deb0282e98da1jwilson public TunnelRequest(String host, int port, String userAgent, String proxyAuthorization) { 4554cf3446000fdcf88a9e62724f7deb0282e98da1jwilson if (host == null) throw new NullPointerException("host == null"); 4654cf3446000fdcf88a9e62724f7deb0282e98da1jwilson if (userAgent == null) throw new NullPointerException("userAgent == null"); 4754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson this.host = host; 4854cf3446000fdcf88a9e62724f7deb0282e98da1jwilson this.port = port; 4954cf3446000fdcf88a9e62724f7deb0282e98da1jwilson this.userAgent = userAgent; 5054cf3446000fdcf88a9e62724f7deb0282e98da1jwilson this.proxyAuthorization = proxyAuthorization; 5154cf3446000fdcf88a9e62724f7deb0282e98da1jwilson } 522231db3e6bb54447a9b14cf004a6cb03c373651cjwilson 533c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller String requestLine() { 543c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller return "CONNECT " + host + ":" + port + " HTTP/1.1"; 553c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller } 563c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller 5754cf3446000fdcf88a9e62724f7deb0282e98da1jwilson /** 5854cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * If we're creating a TLS tunnel, send only the minimum set of headers. 5954cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * This avoids sending potentially sensitive data like HTTP cookies to 6054cf3446000fdcf88a9e62724f7deb0282e98da1jwilson * the proxy unencrypted. 6154cf3446000fdcf88a9e62724f7deb0282e98da1jwilson */ 623c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller Request getRequest() throws IOException { 633c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller Request.Builder result = new Request.Builder() 643c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller .url(new URL("https", host, port, "/")); 652231db3e6bb54447a9b14cf004a6cb03c373651cjwilson 6654cf3446000fdcf88a9e62724f7deb0282e98da1jwilson // Always set Host and User-Agent. 673c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller result.header("Host", port == getDefaultPort("https") ? host : (host + ":" + port)); 683c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller result.header("User-Agent", userAgent); 692231db3e6bb54447a9b14cf004a6cb03c373651cjwilson 7054cf3446000fdcf88a9e62724f7deb0282e98da1jwilson // Copy over the Proxy-Authorization header if it exists. 7154cf3446000fdcf88a9e62724f7deb0282e98da1jwilson if (proxyAuthorization != null) { 723c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller result.header("Proxy-Authorization", proxyAuthorization); 732231db3e6bb54447a9b14cf004a6cb03c373651cjwilson } 7454cf3446000fdcf88a9e62724f7deb0282e98da1jwilson 7554cf3446000fdcf88a9e62724f7deb0282e98da1jwilson // Always set the Proxy-Connection to Keep-Alive for the benefit of 7654cf3446000fdcf88a9e62724f7deb0282e98da1jwilson // HTTP/1.0 proxies like Squid. 773c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller result.header("Proxy-Connection", "Keep-Alive"); 783c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller return result.build(); 7954cf3446000fdcf88a9e62724f7deb0282e98da1jwilson } 802231db3e6bb54447a9b14cf004a6cb03c373651cjwilson} 81