mirror of
https://github.com/square/okhttp.git
synced 2026-01-18 20:40:58 +03:00
Merge pull request #719 from square/jwilson_0418_protocol_tostring
Hide Protocol's internal byte string.
This commit is contained in:
@@ -70,7 +70,7 @@ public final class SpdyServer implements IncomingStreamHandler {
|
||||
System.out.println("UNSUPPORTED");
|
||||
}
|
||||
@Override public List<String> protocols() {
|
||||
return Arrays.asList(Protocol.SPDY_3.name.utf8());
|
||||
return Arrays.asList(Protocol.SPDY_3.toString());
|
||||
}
|
||||
@Override public void protocolSelected(String protocol) {
|
||||
System.out.println("PROTOCOL SELECTED: " + protocol);
|
||||
|
||||
@@ -333,8 +333,10 @@ public final class MockWebServer {
|
||||
sslSocket.startHandshake();
|
||||
|
||||
if (npnEnabled) {
|
||||
ByteString selectedProtocol = Platform.get().getNpnSelectedProtocol(sslSocket);
|
||||
protocol = Protocol.find(selectedProtocol);
|
||||
ByteString protocolBytes = Platform.get().getNpnSelectedProtocol(sslSocket);
|
||||
protocol = protocolBytes != null
|
||||
? Protocol.find(protocolBytes.utf8())
|
||||
: Protocol.HTTP_11;
|
||||
}
|
||||
openClientSockets.remove(raw);
|
||||
} else {
|
||||
@@ -634,7 +636,7 @@ public final class MockWebServer {
|
||||
writeResponse(stream, response);
|
||||
if (logger.isLoggable(Level.INFO)) {
|
||||
logger.info("Received request: " + request + " and responded: " + response
|
||||
+ " protocol is " + protocol.name.utf8());
|
||||
+ " protocol is " + protocol.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,9 +15,7 @@
|
||||
*/
|
||||
package com.squareup.okhttp.curl;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.squareup.okhttp.ConnectionPool;
|
||||
import com.squareup.okhttp.Headers;
|
||||
import com.squareup.okhttp.MediaType;
|
||||
@@ -34,7 +32,6 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
@@ -73,12 +70,7 @@ public class Main extends HelpOption implements Runnable {
|
||||
}
|
||||
|
||||
private static String protocols() {
|
||||
return Joiner.on(", ").join(Lists.transform(Arrays.asList(Protocol.values()),
|
||||
new Function<Protocol, String>() {
|
||||
@Override public String apply(Protocol protocol) {
|
||||
return protocol.name.utf8();
|
||||
}
|
||||
}));
|
||||
return Joiner.on(", ").join(Protocol.values());
|
||||
}
|
||||
|
||||
@Option(name = { "-X", "--request" }, description = "Specify request command to use")
|
||||
|
||||
@@ -43,9 +43,9 @@ public final class HeadersTest {
|
||||
assertEquals("HTTP/1.1 200 OK", response.statusLine());
|
||||
assertEquals("no-cache, no-store", headers.get("cache-control"));
|
||||
assertEquals("Cookie2", headers.get("set-cookie"));
|
||||
assertEquals(Protocol.SPDY_3.name.utf8(), headers.get(OkHeaders.SELECTED_PROTOCOL));
|
||||
assertEquals(Protocol.SPDY_3.toString(), headers.get(OkHeaders.SELECTED_PROTOCOL));
|
||||
assertEquals(OkHeaders.SELECTED_PROTOCOL, headers.name(0));
|
||||
assertEquals(Protocol.SPDY_3.name.utf8(), headers.value(0));
|
||||
assertEquals(Protocol.SPDY_3.toString(), headers.value(0));
|
||||
assertEquals("cache-control", headers.name(1));
|
||||
assertEquals("no-cache, no-store", headers.value(1));
|
||||
assertEquals("set-cookie", headers.name(2));
|
||||
@@ -67,7 +67,7 @@ public final class HeadersTest {
|
||||
Headers headers = response.headers();
|
||||
assertEquals(1, headers.size());
|
||||
assertEquals(OkHeaders.SELECTED_PROTOCOL, headers.name(0));
|
||||
assertEquals(Protocol.SPDY_3.name.utf8(), headers.value(0));
|
||||
assertEquals(Protocol.SPDY_3.toString(), headers.value(0));
|
||||
}
|
||||
|
||||
@Test public void readNameValueBlockDropsForbiddenHeadersHttp2() throws IOException {
|
||||
@@ -81,7 +81,7 @@ public final class HeadersTest {
|
||||
Headers headers = response.headers();
|
||||
assertEquals(1, headers.size());
|
||||
assertEquals(OkHeaders.SELECTED_PROTOCOL, headers.name(0));
|
||||
assertEquals(Protocol.HTTP_2.name.utf8(), headers.value(0));
|
||||
assertEquals(Protocol.HTTP_2.toString(), headers.value(0));
|
||||
}
|
||||
|
||||
@Test public void toNameValueBlock() {
|
||||
|
||||
@@ -2723,7 +2723,7 @@ public final class URLConnectionTest {
|
||||
client.setProtocols(Arrays.asList(Protocol.HTTP_11, protocol));
|
||||
connection = client.open(server.getUrl("/"));
|
||||
List<String> protocolValues = connection.getHeaderFields().get(SELECTED_PROTOCOL);
|
||||
assertEquals(Arrays.asList(protocol.name.utf8()), protocolValues);
|
||||
assertEquals(Arrays.asList(protocol.toString()), protocolValues);
|
||||
assertContent("A", connection);
|
||||
}
|
||||
|
||||
|
||||
@@ -207,7 +207,7 @@ public final class Connection implements Closeable {
|
||||
ByteString maybeProtocol;
|
||||
Protocol selectedProtocol = Protocol.HTTP_11;
|
||||
if (useNpn && (maybeProtocol = platform.getNpnSelectedProtocol(sslSocket)) != null) {
|
||||
selectedProtocol = Protocol.find(maybeProtocol); // Throws IOE on unknown.
|
||||
selectedProtocol = Protocol.find(maybeProtocol.utf8()); // Throws IOE on unknown.
|
||||
}
|
||||
|
||||
if (selectedProtocol.spdyVariant) {
|
||||
|
||||
@@ -39,7 +39,6 @@ import javax.net.SocketFactory;
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
import okio.ByteString;
|
||||
|
||||
/**
|
||||
* Configures and creates HTTP connections. Most applications can use a single
|
||||
@@ -320,8 +319,7 @@ public final class OkHttpClient implements URLStreamHandlerFactory, Cloneable {
|
||||
List<Protocol> protocols = new ArrayList<Protocol>(transports.size());
|
||||
for (int i = 0, size = transports.size(); i < size; i++) {
|
||||
try {
|
||||
Protocol protocol = Protocol.find(ByteString.encodeUtf8(transports.get(i)));
|
||||
protocols.add(protocol);
|
||||
protocols.add(Protocol.find(transports.get(i)));
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
@@ -378,7 +376,7 @@ public final class OkHttpClient implements URLStreamHandlerFactory, Cloneable {
|
||||
public List<String> getTransports() {
|
||||
List<String> transports = new ArrayList<String>(protocols.size());
|
||||
for (int i = 0, size = protocols.size(); i < size; i++) {
|
||||
transports.add(protocols.get(i).name.utf8());
|
||||
transports.add(protocols.get(i).toString());
|
||||
}
|
||||
return transports;
|
||||
}
|
||||
|
||||
@@ -19,18 +19,17 @@ import com.squareup.okhttp.internal.Util;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import okio.ByteString;
|
||||
|
||||
/**
|
||||
* Contains protocols that OkHttp supports
|
||||
* <a href="http://tools.ietf.org/html/draft-agl-tls-nextprotoneg-04">NPN</a> or
|
||||
* <a href="http://tools.ietf.org/html/draft-ietf-tls-applayerprotoneg">ALPN</a> selection.
|
||||
* Protocols that OkHttp implements for <a
|
||||
* href="http://tools.ietf.org/html/draft-agl-tls-nextprotoneg-04">NPN</a> and
|
||||
* <a href="http://tools.ietf.org/html/draft-ietf-tls-applayerprotoneg">ALPN</a>.
|
||||
*
|
||||
* <h3>Protocol vs Scheme</h3>
|
||||
* Despite its name, {@link java.net.URL#getProtocol()} returns the
|
||||
* {@link java.net.URI#getScheme() scheme} (http, https, etc.) of the URL, not
|
||||
* the protocol (http/1.1, spdy/3.1, etc.). OkHttp uses the word protocol to
|
||||
* indicate how HTTP messages are framed.
|
||||
* the protocol (http/1.1, spdy/3.1, etc.). OkHttp uses the word <i>protocol</i>
|
||||
* to identify how HTTP messages are framed.
|
||||
*/
|
||||
public enum Protocol {
|
||||
HTTP_2("h2-10", true),
|
||||
@@ -45,7 +44,7 @@ public enum Protocol {
|
||||
Util.immutableList(Arrays.asList(HTTP_2, HTTP_11));
|
||||
|
||||
/** Identifier string used in NPN or ALPN selection. */
|
||||
public final ByteString name;
|
||||
private final String protocol;
|
||||
|
||||
/**
|
||||
* When true the protocol is binary framed and derived from SPDY.
|
||||
@@ -54,21 +53,28 @@ public enum Protocol {
|
||||
*/
|
||||
public final boolean spdyVariant;
|
||||
|
||||
Protocol(String name, boolean spdyVariant) {
|
||||
this.name = ByteString.encodeUtf8(name);
|
||||
Protocol(String protocol, boolean spdyVariant) {
|
||||
this.protocol = protocol;
|
||||
this.spdyVariant = spdyVariant;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the protocol matching {@code input} or {@link #HTTP_11} is on
|
||||
* {@code null}. Throws an {@link IOException} when {@code input} doesn't
|
||||
* match the {@link #name} of a supported protocol.
|
||||
* Returns the protocol identified by {@code protocol}.
|
||||
* @throws IOException if {@code protocol} is unknown.
|
||||
*/
|
||||
public static Protocol find(ByteString input) throws IOException {
|
||||
if (input == null) return HTTP_11;
|
||||
for (Protocol protocol : values()) {
|
||||
if (protocol.name.equals(input)) return protocol;
|
||||
}
|
||||
throw new IOException("Unexpected protocol: " + input.utf8());
|
||||
public static Protocol find(String protocol) throws IOException {
|
||||
// Unroll the loop over values() to save an allocation.
|
||||
if (protocol.equals(HTTP_11.protocol)) return HTTP_11;
|
||||
if (protocol.equals(HTTP_2.protocol)) return HTTP_2;
|
||||
if (protocol.equals(SPDY_3.protocol)) return SPDY_3;
|
||||
throw new IOException("Unexpected protocol: " + protocol);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the string used to identify this protocol for ALPN and NPN, like
|
||||
* "http/1.1", "spdy/3.1" or "h2-10".
|
||||
*/
|
||||
@Override public String toString() {
|
||||
return protocol;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ import java.util.logging.Logger;
|
||||
import java.util.zip.Deflater;
|
||||
import java.util.zip.DeflaterOutputStream;
|
||||
import javax.net.ssl.SSLSocket;
|
||||
import okio.Buffer;
|
||||
import okio.ByteString;
|
||||
|
||||
/**
|
||||
@@ -303,7 +304,7 @@ public class Platform {
|
||||
try {
|
||||
List<String> names = new ArrayList<String>(npnProtocols.size());
|
||||
for (int i = 0, size = npnProtocols.size(); i < size; i++) {
|
||||
names.add(npnProtocols.get(i).name.utf8());
|
||||
names.add(npnProtocols.get(i).toString());
|
||||
}
|
||||
Object provider = Proxy.newProxyInstance(Platform.class.getClassLoader(),
|
||||
new Class[] { clientProviderClass, serverProviderClass }, new JettyNpnProvider(names));
|
||||
@@ -386,24 +387,15 @@ public class Platform {
|
||||
}
|
||||
|
||||
/**
|
||||
* Concatenation of 8-bit, length prefixed protocol names.
|
||||
*
|
||||
* Returns the concatenation of 8-bit, length prefixed protocol names.
|
||||
* http://tools.ietf.org/html/draft-agl-tls-nextprotoneg-04#page-4
|
||||
*/
|
||||
static byte[] concatLengthPrefixed(List<Protocol> protocols) {
|
||||
int size = 0;
|
||||
Buffer result = new Buffer();
|
||||
for (Protocol protocol : protocols) {
|
||||
size += protocol.name.size() + 1; // add a byte for 8-bit length prefix.
|
||||
result.writeByte(protocol.toString().length());
|
||||
result.writeUtf8(protocol.toString());
|
||||
}
|
||||
byte[] result = new byte[size];
|
||||
int pos = 0;
|
||||
for (Protocol protocol : protocols) {
|
||||
int nameSize = protocol.name.size();
|
||||
result[pos++] = (byte) nameSize;
|
||||
// toByteArray allocates an array, but this is only called on new connections.
|
||||
System.arraycopy(protocol.name.toByteArray(), 0, result, pos, nameSize);
|
||||
pos += nameSize;
|
||||
}
|
||||
return result;
|
||||
return result.readByteArray(result.size());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,7 +189,7 @@ public final class HttpConnection {
|
||||
|
||||
Response.Builder responseBuilder = new Response.Builder()
|
||||
.statusLine(statusLine)
|
||||
.header(OkHeaders.SELECTED_PROTOCOL, Protocol.HTTP_11.name.utf8());
|
||||
.header(OkHeaders.SELECTED_PROTOCOL, Protocol.HTTP_11.toString());
|
||||
|
||||
Headers.Builder headersBuilder = new Headers.Builder();
|
||||
readHeaders(headersBuilder);
|
||||
|
||||
@@ -46,7 +46,6 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import okio.BufferedSink;
|
||||
import okio.ByteString;
|
||||
import okio.Sink;
|
||||
|
||||
import static com.squareup.okhttp.internal.Util.getEffectivePort;
|
||||
@@ -568,7 +567,7 @@ public class HttpURLConnectionImpl extends HttpURLConnection {
|
||||
}
|
||||
for (String protocol : protocolsString.split(",", -1)) {
|
||||
try {
|
||||
protocolsList.add(Protocol.find(ByteString.encodeUtf8(protocol)));
|
||||
protocolsList.add(Protocol.find(protocol));
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
|
||||
@@ -179,7 +179,7 @@ public final class SpdyTransport implements Transport {
|
||||
String version = "HTTP/1.1"; // :version present only in spdy/3.
|
||||
|
||||
Headers.Builder headersBuilder = new Headers.Builder();
|
||||
headersBuilder.set(OkHeaders.SELECTED_PROTOCOL, protocol.name.utf8());
|
||||
headersBuilder.set(OkHeaders.SELECTED_PROTOCOL, protocol.toString());
|
||||
for (int i = 0; i < headerBlock.size(); i++) {
|
||||
ByteString name = headerBlock.get(i).name;
|
||||
String values = headerBlock.get(i).value.utf8();
|
||||
|
||||
Reference in New Issue
Block a user