1 /* 2 * Copyright (C) 2014 Square, Inc. 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 java.io.IOException; 19 20 /** 21 * Protocols that OkHttp implements for <a 22 * href="http://tools.ietf.org/html/draft-ietf-tls-applayerprotoneg">ALPN</a> 23 * selection. 24 * 25 * <h3>Protocol vs Scheme</h3> 26 * Despite its name, {@link java.net.URL#getProtocol()} returns the 27 * {@linkplain java.net.URI#getScheme() scheme} (http, https, etc.) of the URL, not 28 * the protocol (http/1.1, spdy/3.1, etc.). OkHttp uses the word <i>protocol</i> 29 * to identify how HTTP messages are framed. 30 */ 31 public enum Protocol { 32 /** 33 * An obsolete plaintext framing that does not use persistent sockets by 34 * default. 35 */ 36 HTTP_1_0("http/1.0"), 37 38 /** 39 * A plaintext framing that includes persistent connections. 40 * 41 * <p>This version of OkHttp implements <a 42 * href="http://www.ietf.org/rfc/rfc2616.txt">RFC 2616</a>, and tracks 43 * revisions to that spec. 44 */ 45 HTTP_1_1("http/1.1"), 46 47 /** 48 * Chromium's binary-framed protocol that includes header compression, 49 * multiplexing multiple requests on the same socket, and server-push. 50 * HTTP/1.1 semantics are layered on SPDY/3. 51 * 52 * <p>This version of OkHttp implements SPDY 3 <a 53 * href="http://dev.chromium.org/spdy/spdy-protocol/spdy-protocol-draft3-1">draft 54 * 3.1</a>. Future releases of OkHttp may use this identifier for a newer draft 55 * of the SPDY spec. 56 */ 57 SPDY_3("spdy/3.1"), 58 59 /** 60 * The IETF's binary-framed protocol that includes header compression, 61 * multiplexing multiple requests on the same socket, and server-push. 62 * HTTP/1.1 semantics are layered on HTTP/2. 63 * 64 * <p>HTTP/2 requires deployments of HTTP/2 that use TLS 1.2 support 65 * {@linkplain com.squareup.okhttp.CipherSuite#TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256} 66 * , present in Java 8+ and Android 5+. Servers that enforce this may send an 67 * exception message including the string {@code INADEQUATE_SECURITY}. 68 */ 69 HTTP_2("h2"); 70 71 private final String protocol; 72 Protocol(String protocol)73 Protocol(String protocol) { 74 this.protocol = protocol; 75 } 76 77 /** 78 * Returns the protocol identified by {@code protocol}. 79 * @throws IOException if {@code protocol} is unknown. 80 */ get(String protocol)81 public static Protocol get(String protocol) throws IOException { 82 // Unroll the loop over values() to save an allocation. 83 if (protocol.equals(HTTP_1_0.protocol)) return HTTP_1_0; 84 if (protocol.equals(HTTP_1_1.protocol)) return HTTP_1_1; 85 if (protocol.equals(HTTP_2.protocol)) return HTTP_2; 86 if (protocol.equals(SPDY_3.protocol)) return SPDY_3; 87 throw new IOException("Unexpected protocol: " + protocol); 88 } 89 90 /** 91 * Returns the string used to identify this protocol for ALPN, like 92 * "http/1.1", "spdy/3.1" or "h2". 93 */ toString()94 @Override public String toString() { 95 return protocol; 96 } 97 } 98