Home | History | Annotate | Download | only in http
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 package com.squareup.okhttp.internal.http;
     18 
     19 import com.squareup.okhttp.Connection;
     20 import com.squareup.okhttp.OkHttpClient;
     21 import com.squareup.okhttp.TunnelRequest;
     22 import java.io.IOException;
     23 import java.net.CacheResponse;
     24 import java.net.SecureCacheResponse;
     25 import java.net.URL;
     26 import javax.net.ssl.SSLSocket;
     27 
     28 import static com.squareup.okhttp.internal.Util.getEffectivePort;
     29 
     30 public final class HttpsEngine extends HttpEngine {
     31   /**
     32    * Stash of HttpsEngine.connection.socket to implement requests like {@code
     33    * HttpsURLConnection#getCipherSuite} even after the connection has been
     34    * recycled.
     35    */
     36   private SSLSocket sslSocket;
     37 
     38   public HttpsEngine(OkHttpClient client, Policy policy, String method, RawHeaders requestHeaders,
     39       Connection connection, RetryableOutputStream requestBody) throws IOException {
     40     super(client, policy, method, requestHeaders, connection, requestBody);
     41     this.sslSocket = connection != null ? (SSLSocket) connection.getSocket() : null;
     42   }
     43 
     44   @Override protected void connected(Connection connection) {
     45     this.sslSocket = (SSLSocket) connection.getSocket();
     46     super.connected(connection);
     47   }
     48 
     49   @Override protected boolean acceptCacheResponseType(CacheResponse cacheResponse) {
     50     return cacheResponse instanceof SecureCacheResponse;
     51   }
     52 
     53   @Override protected boolean includeAuthorityInRequestLine() {
     54     // Even if there is a proxy, it isn't involved. Always request just the path.
     55     return false;
     56   }
     57 
     58   public SSLSocket getSslSocket() {
     59     return sslSocket;
     60   }
     61 
     62   @Override protected TunnelRequest getTunnelConfig() {
     63     String userAgent = requestHeaders.getUserAgent();
     64     if (userAgent == null) {
     65       userAgent = getDefaultUserAgent();
     66     }
     67 
     68     URL url = policy.getURL();
     69     return new TunnelRequest(url.getHost(), getEffectivePort(url), userAgent,
     70         requestHeaders.getProxyAuthorization());
     71   }
     72 }
     73