mirror of
https://github.com/square/okhttp.git
synced 2026-01-17 08:42:25 +03:00
Merge pull request #2008 from square/jwilson_1117_more_connections
Stop returning HTTP/1.0 from Connection.getProtocol().
This commit is contained in:
@@ -1904,7 +1904,7 @@ public final class CallTest {
|
||||
/** Test which headers are sent unencrypted to the HTTP proxy. */
|
||||
@Test public void proxyConnectOmitsApplicationHeaders() throws Exception {
|
||||
server.useHttps(sslContext.getSocketFactory(), true);
|
||||
server.enqueue( new MockResponse()
|
||||
server.enqueue(new MockResponse()
|
||||
.setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END)
|
||||
.clearHeaders());
|
||||
server.enqueue(new MockResponse()
|
||||
@@ -1942,7 +1942,7 @@ public final class CallTest {
|
||||
server.enqueue(new MockResponse()
|
||||
.setResponseCode(407)
|
||||
.addHeader("Proxy-Authenticate: Basic realm=\"localhost\""));
|
||||
server.enqueue( new MockResponse()
|
||||
server.enqueue(new MockResponse()
|
||||
.setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END)
|
||||
.clearHeaders());
|
||||
server.enqueue(new MockResponse()
|
||||
@@ -1979,7 +1979,7 @@ public final class CallTest {
|
||||
*/
|
||||
@Test public void noProactiveProxyAuthorization() throws Exception {
|
||||
server.useHttps(sslContext.getSocketFactory(), true);
|
||||
server.enqueue( new MockResponse()
|
||||
server.enqueue(new MockResponse()
|
||||
.setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END)
|
||||
.clearHeaders());
|
||||
server.enqueue(new MockResponse()
|
||||
|
||||
@@ -100,8 +100,7 @@ public final class HeadersTest {
|
||||
.addHeader("set-cookie", "Cookie2")
|
||||
.header(":status", "200 OK")
|
||||
.build();
|
||||
List<Header> headerBlock =
|
||||
FramedTransport.writeNameValueBlock(request, Protocol.SPDY_3, "HTTP/1.1");
|
||||
List<Header> headerBlock = FramedTransport.writeNameValueBlock(request, Protocol.SPDY_3);
|
||||
List<Header> expected = headerEntries(
|
||||
":method", "GET",
|
||||
":path", "/",
|
||||
@@ -126,7 +125,7 @@ public final class HeadersTest {
|
||||
":version", "HTTP/1.1",
|
||||
":host", "square.com",
|
||||
":scheme", "http");
|
||||
assertEquals(expected, FramedTransport.writeNameValueBlock(request, Protocol.SPDY_3, "HTTP/1.1"));
|
||||
assertEquals(expected, FramedTransport.writeNameValueBlock(request, Protocol.SPDY_3));
|
||||
}
|
||||
|
||||
@Test public void toNameValueBlockDropsForbiddenHeadersHttp2() {
|
||||
@@ -140,8 +139,7 @@ public final class HeadersTest {
|
||||
":path", "/",
|
||||
":authority", "square.com",
|
||||
":scheme", "http");
|
||||
assertEquals(expected,
|
||||
FramedTransport.writeNameValueBlock(request, Protocol.HTTP_2, "HTTP/1.1"));
|
||||
assertEquals(expected, FramedTransport.writeNameValueBlock(request, Protocol.HTTP_2));
|
||||
}
|
||||
|
||||
@Test public void ofTrims() {
|
||||
|
||||
@@ -78,12 +78,11 @@ public final class Connection {
|
||||
private final Route route;
|
||||
|
||||
private Socket socket;
|
||||
private boolean connected = false;
|
||||
private Handshake handshake;
|
||||
private Protocol protocol;
|
||||
private HttpConnection httpConnection;
|
||||
private FramedConnection framedConnection;
|
||||
private Protocol protocol = Protocol.HTTP_1_1;
|
||||
private long idleStartTimeNs;
|
||||
private Handshake handshake;
|
||||
private int recycleCount;
|
||||
|
||||
/**
|
||||
@@ -152,7 +151,7 @@ public final class Connection {
|
||||
|
||||
void connect(int connectTimeout, int readTimeout, int writeTimeout,
|
||||
List<ConnectionSpec> connectionSpecs, boolean connectionRetryEnabled) throws RouteException {
|
||||
if (connected) throw new IllegalStateException("already connected");
|
||||
if (protocol != null) throw new IllegalStateException("already connected");
|
||||
|
||||
RouteException routeException = null;
|
||||
ConnectionSpecSelector connectionSpecSelector = new ConnectionSpecSelector(connectionSpecs);
|
||||
@@ -165,17 +164,19 @@ public final class Connection {
|
||||
"CLEARTEXT communication not supported: " + connectionSpecs));
|
||||
}
|
||||
|
||||
while (!connected) {
|
||||
while (protocol == null) {
|
||||
try {
|
||||
socket = proxy.type() == Proxy.Type.DIRECT || proxy.type() == Proxy.Type.HTTP
|
||||
? address.getSocketFactory().createSocket()
|
||||
: new Socket(proxy);
|
||||
connectSocket(connectTimeout, readTimeout, writeTimeout,
|
||||
connectionSpecSelector);
|
||||
connected = true; // Success!
|
||||
connectSocket(connectTimeout, readTimeout, writeTimeout, connectionSpecSelector);
|
||||
} catch (IOException e) {
|
||||
Util.closeQuietly(socket);
|
||||
socket = null;
|
||||
handshake = null;
|
||||
protocol = null;
|
||||
httpConnection = null;
|
||||
framedConnection = null;
|
||||
|
||||
if (routeException == null) {
|
||||
routeException = new RouteException(e);
|
||||
@@ -198,6 +199,8 @@ public final class Connection {
|
||||
|
||||
if (route.address.getSslSocketFactory() != null) {
|
||||
connectTls(readTimeout, writeTimeout, connectionSpecSelector);
|
||||
} else {
|
||||
protocol = Protocol.HTTP_1_1;
|
||||
}
|
||||
|
||||
if (protocol == Protocol.SPDY_3 || protocol == Protocol.HTTP_2) {
|
||||
@@ -253,11 +256,11 @@ public final class Connection {
|
||||
String maybeProtocol = connectionSpec.supportsTlsExtensions()
|
||||
? Platform.get().getSelectedProtocol(sslSocket)
|
||||
: null;
|
||||
socket = sslSocket;
|
||||
handshake = unverifiedHandshake;
|
||||
protocol = maybeProtocol != null
|
||||
? Protocol.get(maybeProtocol)
|
||||
: Protocol.HTTP_1_1;
|
||||
handshake = unverifiedHandshake;
|
||||
socket = sslSocket;
|
||||
success = true;
|
||||
} catch (AssertionError e) {
|
||||
if (Util.isAndroidGetsocknameError(e)) throw new IOException(e);
|
||||
@@ -365,7 +368,7 @@ public final class Connection {
|
||||
|
||||
/** Returns true if {@link #connect} has been attempted on this connection. */
|
||||
boolean isConnected() {
|
||||
return connected;
|
||||
return protocol != null;
|
||||
}
|
||||
|
||||
/** Returns the route used by this connection. */
|
||||
@@ -444,25 +447,17 @@ public final class Connection {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the protocol negotiated by this connection, or {@link
|
||||
* Protocol#HTTP_1_1} if no protocol has been negotiated.
|
||||
* Returns the protocol negotiated by this connection, or {@link Protocol#HTTP_1_1} if no protocol
|
||||
* has been negotiated. This method returns {@link Protocol#HTTP_1_1} even if the remote peer is
|
||||
* using {@link Protocol#HTTP_1_0}.
|
||||
*/
|
||||
public Protocol getProtocol() {
|
||||
return protocol;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the protocol negotiated by this connection. Typically this is used
|
||||
* when an HTTP/1.1 request is sent and an HTTP/1.0 response is received.
|
||||
*/
|
||||
void setProtocol(Protocol protocol) {
|
||||
if (protocol == null) throw new IllegalArgumentException("protocol == null");
|
||||
this.protocol = protocol;
|
||||
return protocol != null ? protocol : Protocol.HTTP_1_1;
|
||||
}
|
||||
|
||||
void setTimeouts(int readTimeoutMillis, int writeTimeoutMillis)
|
||||
throws RouteException {
|
||||
if (!connected) throw new IllegalStateException("setTimeouts - not connected");
|
||||
if (protocol == null) throw new IllegalStateException("not connected");
|
||||
|
||||
// Don't set timeouts on shared SPDY connections.
|
||||
if (httpConnection != null) {
|
||||
|
||||
@@ -80,10 +80,6 @@ public class OkHttpClient implements Cloneable {
|
||||
return connection.recycleCount();
|
||||
}
|
||||
|
||||
@Override public void setProtocol(Connection connection, Protocol protocol) {
|
||||
connection.setProtocol(protocol);
|
||||
}
|
||||
|
||||
@Override public void setOwner(Connection connection, HttpEngine httpEngine) {
|
||||
connection.setOwner(httpEngine);
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ import com.squareup.okhttp.ConnectionSpec;
|
||||
import com.squareup.okhttp.Headers;
|
||||
import com.squareup.okhttp.HttpUrl;
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
import com.squareup.okhttp.Protocol;
|
||||
import com.squareup.okhttp.internal.http.HttpEngine;
|
||||
import com.squareup.okhttp.internal.http.RouteException;
|
||||
import com.squareup.okhttp.internal.http.Transport;
|
||||
@@ -59,8 +58,6 @@ public abstract class Internal {
|
||||
|
||||
public abstract int recycleCount(Connection connection);
|
||||
|
||||
public abstract void setProtocol(Connection connection, Protocol protocol);
|
||||
|
||||
public abstract void setOwner(Connection connection, HttpEngine httpEngine);
|
||||
|
||||
public abstract boolean isReadable(Connection pooled);
|
||||
|
||||
@@ -85,9 +85,8 @@ public final class FramedTransport implements Transport {
|
||||
httpEngine.writingRequestHeaders();
|
||||
boolean permitsRequestBody = httpEngine.permitsRequestBody(request);
|
||||
boolean hasResponseBody = true;
|
||||
String version = RequestLine.version(httpEngine.getConnection().getProtocol());
|
||||
stream = framedConnection.newStream(
|
||||
writeNameValueBlock(request, framedConnection.getProtocol(), version), permitsRequestBody,
|
||||
writeNameValueBlock(request, framedConnection.getProtocol()), permitsRequestBody,
|
||||
hasResponseBody);
|
||||
stream.readTimeout().timeout(httpEngine.client.getReadTimeout(), TimeUnit.MILLISECONDS);
|
||||
}
|
||||
@@ -109,15 +108,14 @@ public final class FramedTransport implements Transport {
|
||||
* Names are all lowercase. No names are repeated. If any name has multiple
|
||||
* values, they are concatenated using "\0" as a delimiter.
|
||||
*/
|
||||
public static List<Header> writeNameValueBlock(Request request, Protocol protocol,
|
||||
String version) {
|
||||
public static List<Header> writeNameValueBlock(Request request, Protocol protocol) {
|
||||
Headers headers = request.headers();
|
||||
List<Header> result = new ArrayList<>(headers.size() + 10);
|
||||
result.add(new Header(TARGET_METHOD, request.method()));
|
||||
result.add(new Header(TARGET_PATH, RequestLine.requestPath(request.httpUrl())));
|
||||
String host = Util.hostHeader(request.httpUrl());
|
||||
if (Protocol.SPDY_3 == protocol) {
|
||||
result.add(new Header(VERSION, version));
|
||||
result.add(new Header(VERSION, "HTTP/1.1"));
|
||||
result.add(new Header(TARGET_HOST, host));
|
||||
} else if (Protocol.HTTP_2 == protocol) {
|
||||
result.add(new Header(TARGET_AUTHORITY, host)); // Optional in HTTP/2
|
||||
|
||||
@@ -694,8 +694,7 @@ public final class HttpEngine {
|
||||
result.header("Host", Util.hostHeader(request.httpUrl()));
|
||||
}
|
||||
|
||||
if ((connection == null || connection.getProtocol() != Protocol.HTTP_1_0)
|
||||
&& request.header("Connection") == null) {
|
||||
if (request.header("Connection") == null) {
|
||||
result.header("Connection", "Keep-Alive");
|
||||
}
|
||||
|
||||
@@ -920,7 +919,6 @@ public final class HttpEngine {
|
||||
.build();
|
||||
}
|
||||
|
||||
Internal.instance.setProtocol(connection, networkResponse.protocol());
|
||||
return networkResponse;
|
||||
}
|
||||
|
||||
|
||||
@@ -70,9 +70,8 @@ public final class HttpTransport implements Transport {
|
||||
*/
|
||||
public void writeRequestHeaders(Request request) throws IOException {
|
||||
httpEngine.writingRequestHeaders();
|
||||
String requestLine = RequestLine.get(request,
|
||||
httpEngine.getConnection().getRoute().getProxy().type(),
|
||||
httpEngine.getConnection().getProtocol());
|
||||
String requestLine = RequestLine.get(
|
||||
request, httpEngine.getConnection().getRoute().getProxy().type());
|
||||
httpConnection.writeRequest(request.headers(), requestLine);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.squareup.okhttp.internal.http;
|
||||
|
||||
import com.squareup.okhttp.HttpUrl;
|
||||
import com.squareup.okhttp.Protocol;
|
||||
import com.squareup.okhttp.Request;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.Proxy;
|
||||
@@ -15,7 +14,7 @@ public final class RequestLine {
|
||||
* to the application by {@link HttpURLConnection#getHeaderFields}, so it
|
||||
* needs to be set even if the transport is SPDY.
|
||||
*/
|
||||
static String get(Request request, Proxy.Type proxyType, Protocol protocol) {
|
||||
static String get(Request request, Proxy.Type proxyType) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
result.append(request.method());
|
||||
result.append(' ');
|
||||
@@ -26,8 +25,7 @@ public final class RequestLine {
|
||||
result.append(requestPath(request.httpUrl()));
|
||||
}
|
||||
|
||||
result.append(' ');
|
||||
result.append(version(protocol));
|
||||
result.append(" HTTP/1.1");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
@@ -49,8 +47,4 @@ public final class RequestLine {
|
||||
String query = url.encodedQuery();
|
||||
return query != null ? (path + '?' + query) : path;
|
||||
}
|
||||
|
||||
public static String version(Protocol protocol) {
|
||||
return protocol == Protocol.HTTP_1_0 ? "HTTP/1.0" : "HTTP/1.1";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user