mirror of
https://github.com/square/okhttp.git
synced 2025-12-08 08:42:13 +03:00
Use HttpUrl in Request.
This commit is contained in:
@@ -654,8 +654,10 @@ public final class MockWebServer {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Adapt the request and response into our Request and Response domain model.
|
// Adapt the request and response into our Request and Response domain model.
|
||||||
|
String scheme = request.getTlsVersion() != null ? "https" : "http";
|
||||||
|
String authority = request.getHeader("Host"); // Has host and port.
|
||||||
final Request fancyRequest = new Request.Builder()
|
final Request fancyRequest = new Request.Builder()
|
||||||
.get().url(request.getPath())
|
.url(scheme + "://" + authority + "/")
|
||||||
.headers(request.getHeaders())
|
.headers(request.getHeaders())
|
||||||
.build();
|
.build();
|
||||||
final Response fancyResponse = new Response.Builder()
|
final Response fancyResponse = new Response.Builder()
|
||||||
|
|||||||
@@ -29,14 +29,14 @@ public class MainTest {
|
|||||||
@Test public void simple() {
|
@Test public void simple() {
|
||||||
Request request = fromArgs("http://example.com").createRequest();
|
Request request = fromArgs("http://example.com").createRequest();
|
||||||
assertEquals("GET", request.method());
|
assertEquals("GET", request.method());
|
||||||
assertEquals("http://example.com", request.urlString());
|
assertEquals("http://example.com/", request.urlString());
|
||||||
assertNull(request.body());
|
assertNull(request.body());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void put() throws IOException {
|
@Test public void put() throws IOException {
|
||||||
Request request = fromArgs("-X", "PUT", "-d", "foo", "http://example.com").createRequest();
|
Request request = fromArgs("-X", "PUT", "-d", "foo", "http://example.com").createRequest();
|
||||||
assertEquals("PUT", request.method());
|
assertEquals("PUT", request.method());
|
||||||
assertEquals("http://example.com", request.urlString());
|
assertEquals("http://example.com/", request.urlString());
|
||||||
assertEquals(3, request.body().contentLength());
|
assertEquals(3, request.body().contentLength());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,7 +44,7 @@ public class MainTest {
|
|||||||
Request request = fromArgs("-d", "foo", "http://example.com").createRequest();
|
Request request = fromArgs("-d", "foo", "http://example.com").createRequest();
|
||||||
RequestBody body = request.body();
|
RequestBody body = request.body();
|
||||||
assertEquals("POST", request.method());
|
assertEquals("POST", request.method());
|
||||||
assertEquals("http://example.com", request.urlString());
|
assertEquals("http://example.com/", request.urlString());
|
||||||
assertEquals("application/x-form-urlencoded; charset=utf-8", body.contentType().toString());
|
assertEquals("application/x-form-urlencoded; charset=utf-8", body.contentType().toString());
|
||||||
assertEquals("foo", bodyAsString(body));
|
assertEquals("foo", bodyAsString(body));
|
||||||
}
|
}
|
||||||
@@ -53,7 +53,7 @@ public class MainTest {
|
|||||||
Request request = fromArgs("-d", "foo", "-X", "PUT", "http://example.com").createRequest();
|
Request request = fromArgs("-d", "foo", "-X", "PUT", "http://example.com").createRequest();
|
||||||
RequestBody body = request.body();
|
RequestBody body = request.body();
|
||||||
assertEquals("PUT", request.method());
|
assertEquals("PUT", request.method());
|
||||||
assertEquals("http://example.com", request.urlString());
|
assertEquals("http://example.com/", request.urlString());
|
||||||
assertEquals("application/x-form-urlencoded; charset=utf-8", body.contentType().toString());
|
assertEquals("application/x-form-urlencoded; charset=utf-8", body.contentType().toString());
|
||||||
assertEquals("foo", bodyAsString(body));
|
assertEquals("foo", bodyAsString(body));
|
||||||
}
|
}
|
||||||
@@ -63,7 +63,7 @@ public class MainTest {
|
|||||||
"http://example.com").createRequest();
|
"http://example.com").createRequest();
|
||||||
RequestBody body = request.body();
|
RequestBody body = request.body();
|
||||||
assertEquals("POST", request.method());
|
assertEquals("POST", request.method());
|
||||||
assertEquals("http://example.com", request.urlString());
|
assertEquals("http://example.com/", request.urlString());
|
||||||
assertEquals("application/json; charset=utf-8", body.contentType().toString());
|
assertEquals("application/json; charset=utf-8", body.contentType().toString());
|
||||||
assertEquals("foo", bodyAsString(body));
|
assertEquals("foo", bodyAsString(body));
|
||||||
}
|
}
|
||||||
@@ -71,7 +71,7 @@ public class MainTest {
|
|||||||
@Test public void referer() {
|
@Test public void referer() {
|
||||||
Request request = fromArgs("-e", "foo", "http://example.com").createRequest();
|
Request request = fromArgs("-e", "foo", "http://example.com").createRequest();
|
||||||
assertEquals("GET", request.method());
|
assertEquals("GET", request.method());
|
||||||
assertEquals("http://example.com", request.urlString());
|
assertEquals("http://example.com/", request.urlString());
|
||||||
assertEquals("foo", request.header("Referer"));
|
assertEquals("foo", request.header("Referer"));
|
||||||
assertNull(request.body());
|
assertNull(request.body());
|
||||||
}
|
}
|
||||||
@@ -79,7 +79,7 @@ public class MainTest {
|
|||||||
@Test public void userAgent() {
|
@Test public void userAgent() {
|
||||||
Request request = fromArgs("-A", "foo", "http://example.com").createRequest();
|
Request request = fromArgs("-A", "foo", "http://example.com").createRequest();
|
||||||
assertEquals("GET", request.method());
|
assertEquals("GET", request.method());
|
||||||
assertEquals("http://example.com", request.urlString());
|
assertEquals("http://example.com/", request.urlString());
|
||||||
assertEquals("foo", request.header("User-Agent"));
|
assertEquals("foo", request.header("User-Agent"));
|
||||||
assertNull(request.body());
|
assertNull(request.body());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,7 +62,6 @@ import okio.GzipSink;
|
|||||||
import okio.Okio;
|
import okio.Okio;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.rules.TemporaryFolder;
|
import org.junit.rules.TemporaryFolder;
|
||||||
@@ -73,7 +72,6 @@ import static com.squareup.okhttp.internal.Internal.logger;
|
|||||||
import static java.net.CookiePolicy.ACCEPT_ORIGINAL_SERVER;
|
import static java.net.CookiePolicy.ACCEPT_ORIGINAL_SERVER;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
import static org.junit.Assert.assertNotSame;
|
import static org.junit.Assert.assertNotSame;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
@@ -127,40 +125,36 @@ public final class CallTest {
|
|||||||
assertNull(recordedRequest.getHeader("Content-Length"));
|
assertNull(recordedRequest.getHeader("Content-Length"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void lazilyEvaluateRequestUrl() throws Exception {
|
@Test public void buildRequestUsingHttpUrl() throws Exception {
|
||||||
server.enqueue(new MockResponse().setBody("abc"));
|
server.enqueue(new MockResponse());
|
||||||
|
|
||||||
Request request1 = new Request.Builder()
|
HttpUrl httpUrl = HttpUrl.get(server.getUrl("/"));
|
||||||
.url("foo://bar?baz")
|
Request request = new Request.Builder()
|
||||||
|
.url(httpUrl)
|
||||||
.build();
|
.build();
|
||||||
Request request2 = request1.newBuilder()
|
assertEquals(httpUrl, request.httpUrl());
|
||||||
.url(server.getUrl("/"))
|
|
||||||
.build();
|
executeSynchronously(request).assertSuccessful();
|
||||||
executeSynchronously(request2)
|
|
||||||
.assertCode(200)
|
|
||||||
.assertSuccessful()
|
|
||||||
.assertBody("abc");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Ignore // TODO(jwilson): fix.
|
|
||||||
@Test public void invalidScheme() throws Exception {
|
@Test public void invalidScheme() throws Exception {
|
||||||
|
Request.Builder requestBuilder = new Request.Builder();
|
||||||
try {
|
try {
|
||||||
Request request = new Request.Builder()
|
requestBuilder.url("ftp://hostname/path");
|
||||||
.url("ftp://hostname/path")
|
|
||||||
.build();
|
|
||||||
executeSynchronously(request);
|
|
||||||
fail();
|
fail();
|
||||||
} catch (IllegalArgumentException expected) {
|
} catch (IllegalArgumentException expected) {
|
||||||
|
assertEquals(expected.getMessage(), "unexpected url: ftp://hostname/path");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void invalidPort() throws Exception {
|
@Test public void invalidPort() throws Exception {
|
||||||
Request request = new Request.Builder()
|
Request.Builder requestBuilder = new Request.Builder();
|
||||||
.url("http://localhost:65536/")
|
try {
|
||||||
.build();
|
requestBuilder.url("http://localhost:65536/");
|
||||||
client.newCall(request).enqueue(callback);
|
fail();
|
||||||
callback.await(request.url())
|
} catch (IllegalArgumentException expected) {
|
||||||
.assertFailure("No route to localhost:65536; port is out of range");
|
assertEquals(expected.getMessage(), "unexpected url: http://localhost:65536/");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void getReturns500() throws Exception {
|
@Test public void getReturns500() throws Exception {
|
||||||
|
|||||||
@@ -15,12 +15,9 @@
|
|||||||
*/
|
*/
|
||||||
package com.squareup.okhttp;
|
package com.squareup.okhttp;
|
||||||
|
|
||||||
import com.squareup.okhttp.internal.Platform;
|
|
||||||
import com.squareup.okhttp.internal.http.HttpMethod;
|
import com.squareup.okhttp.internal.http.HttpMethod;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.MalformedURLException;
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -29,45 +26,44 @@ import java.util.List;
|
|||||||
* is null or itself immutable.
|
* is null or itself immutable.
|
||||||
*/
|
*/
|
||||||
public final class Request {
|
public final class Request {
|
||||||
private final String urlString;
|
private final HttpUrl url;
|
||||||
private final String method;
|
private final String method;
|
||||||
private final Headers headers;
|
private final Headers headers;
|
||||||
private final RequestBody body;
|
private final RequestBody body;
|
||||||
private final Object tag;
|
private final Object tag;
|
||||||
|
|
||||||
private volatile URL url; // Lazily initialized.
|
private volatile URL javaNetUrl; // Lazily initialized.
|
||||||
private volatile URI uri; // Lazily initialized.
|
private volatile URI javaNetUri; // Lazily initialized.
|
||||||
private volatile CacheControl cacheControl; // Lazily initialized.
|
private volatile CacheControl cacheControl; // Lazily initialized.
|
||||||
|
|
||||||
private Request(Builder builder) {
|
private Request(Builder builder) {
|
||||||
this.urlString = builder.urlString;
|
this.url = builder.url;
|
||||||
this.method = builder.method;
|
this.method = builder.method;
|
||||||
this.headers = builder.headers.build();
|
this.headers = builder.headers.build();
|
||||||
this.body = builder.body;
|
this.body = builder.body;
|
||||||
this.tag = builder.tag != null ? builder.tag : this;
|
this.tag = builder.tag != null ? builder.tag : this;
|
||||||
this.url = builder.url;
|
}
|
||||||
|
|
||||||
|
public HttpUrl httpUrl() {
|
||||||
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
public URL url() {
|
public URL url() {
|
||||||
try {
|
URL result = javaNetUrl;
|
||||||
URL result = url;
|
return result != null ? result : (javaNetUrl = url.url());
|
||||||
return result != null ? result : (url = new URL(urlString));
|
|
||||||
} catch (MalformedURLException e) {
|
|
||||||
throw new RuntimeException("Malformed URL: " + urlString, e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public URI uri() throws IOException {
|
public URI uri() throws IOException {
|
||||||
try {
|
try {
|
||||||
URI result = uri;
|
URI result = javaNetUri;
|
||||||
return result != null ? result : (uri = Platform.get().toUriLenient(url()));
|
return result != null ? result : (javaNetUri = url.uri());
|
||||||
} catch (URISyntaxException e) {
|
} catch (IllegalStateException e) {
|
||||||
throw new IOException(e.getMessage());
|
throw new IOException(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String urlString() {
|
public String urlString() {
|
||||||
return urlString;
|
return url.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String method() {
|
public String method() {
|
||||||
@@ -108,22 +104,21 @@ public final class Request {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isHttps() {
|
public boolean isHttps() {
|
||||||
return url().getProtocol().equals("https");
|
return url.isHttps();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public String toString() {
|
@Override public String toString() {
|
||||||
return "Request{method="
|
return "Request{method="
|
||||||
+ method
|
+ method
|
||||||
+ ", url="
|
+ ", url="
|
||||||
+ urlString
|
+ url
|
||||||
+ ", tag="
|
+ ", tag="
|
||||||
+ (tag != this ? tag : null)
|
+ (tag != this ? tag : null)
|
||||||
+ '}';
|
+ '}';
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Builder {
|
public static class Builder {
|
||||||
private String urlString;
|
private HttpUrl url;
|
||||||
private URL url;
|
|
||||||
private String method;
|
private String method;
|
||||||
private Headers.Builder headers;
|
private Headers.Builder headers;
|
||||||
private RequestBody body;
|
private RequestBody body;
|
||||||
@@ -135,7 +130,6 @@ public final class Request {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Builder(Request request) {
|
private Builder(Request request) {
|
||||||
this.urlString = request.urlString;
|
|
||||||
this.url = request.url;
|
this.url = request.url;
|
||||||
this.method = request.method;
|
this.method = request.method;
|
||||||
this.body = request.body;
|
this.body = request.body;
|
||||||
@@ -143,18 +137,24 @@ public final class Request {
|
|||||||
this.headers = request.headers.newBuilder();
|
this.headers = request.headers.newBuilder();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Builder url(HttpUrl url) {
|
||||||
|
if (url == null) throw new IllegalArgumentException("url == null");
|
||||||
|
this.url = url;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public Builder url(String url) {
|
public Builder url(String url) {
|
||||||
if (url == null) throw new IllegalArgumentException("url == null");
|
if (url == null) throw new IllegalArgumentException("url == null");
|
||||||
this.urlString = url;
|
HttpUrl parsed = HttpUrl.parse(url);
|
||||||
this.url = null;
|
if (parsed == null) throw new IllegalArgumentException("unexpected url: " + url);
|
||||||
return this;
|
return url(parsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Builder url(URL url) {
|
public Builder url(URL url) {
|
||||||
if (url == null) throw new IllegalArgumentException("url == null");
|
if (url == null) throw new IllegalArgumentException("url == null");
|
||||||
this.url = url;
|
HttpUrl parsed = HttpUrl.get(url);
|
||||||
this.urlString = url.toString();
|
if (parsed == null) throw new IllegalArgumentException("unexpected url: " + url);
|
||||||
return this;
|
return url(parsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -251,7 +251,7 @@ public final class Request {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Request build() {
|
public Request build() {
|
||||||
if (urlString == null) throw new IllegalStateException("url == null");
|
if (url == null) throw new IllegalStateException("url == null");
|
||||||
return new Request(this);
|
return new Request(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,9 +25,6 @@ import java.lang.reflect.Proxy;
|
|||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
import java.net.URI;
|
|
||||||
import java.net.URISyntaxException;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
@@ -76,10 +73,6 @@ public class Platform {
|
|||||||
public void untagSocket(Socket socket) throws SocketException {
|
public void untagSocket(Socket socket) throws SocketException {
|
||||||
}
|
}
|
||||||
|
|
||||||
public URI toUriLenient(URL url) throws URISyntaxException {
|
|
||||||
return url.toURI(); // this isn't as good as the built-in toUriLenient
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configure TLS extensions on {@code sslSocket} for {@code route}.
|
* Configure TLS extensions on {@code sslSocket} for {@code route}.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import com.squareup.okhttp.Challenge;
|
|||||||
import com.squareup.okhttp.Credentials;
|
import com.squareup.okhttp.Credentials;
|
||||||
import com.squareup.okhttp.Request;
|
import com.squareup.okhttp.Request;
|
||||||
import com.squareup.okhttp.Response;
|
import com.squareup.okhttp.Response;
|
||||||
|
import com.squareup.okhttp.internal.Util;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.Authenticator.RequestorType;
|
import java.net.Authenticator.RequestorType;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
@@ -43,8 +44,9 @@ public final class AuthenticatorAdapter implements Authenticator {
|
|||||||
if (!"Basic".equalsIgnoreCase(challenge.getScheme())) continue;
|
if (!"Basic".equalsIgnoreCase(challenge.getScheme())) continue;
|
||||||
|
|
||||||
PasswordAuthentication auth = java.net.Authenticator.requestPasswordAuthentication(
|
PasswordAuthentication auth = java.net.Authenticator.requestPasswordAuthentication(
|
||||||
url.getHost(), getConnectToInetAddress(proxy, url), url.getPort(), url.getProtocol(),
|
url.getHost(), getConnectToInetAddress(proxy, url), Util.getEffectivePort(url),
|
||||||
challenge.getRealm(), challenge.getScheme(), url, RequestorType.SERVER);
|
url.getProtocol(), challenge.getRealm(), challenge.getScheme(), url,
|
||||||
|
RequestorType.SERVER);
|
||||||
if (auth == null) continue;
|
if (auth == null) continue;
|
||||||
|
|
||||||
String credential = Credentials.basic(auth.getUserName(), new String(auth.getPassword()));
|
String credential = Credentials.basic(auth.getUserName(), new String(auth.getPassword()));
|
||||||
|
|||||||
@@ -178,7 +178,7 @@ public class SocketConnector {
|
|||||||
HttpConnection tunnelConnection = new HttpConnection(connectionPool, connection, socket);
|
HttpConnection tunnelConnection = new HttpConnection(connectionPool, connection, socket);
|
||||||
tunnelConnection.setTimeouts(readTimeout, writeTimeout);
|
tunnelConnection.setTimeouts(readTimeout, writeTimeout);
|
||||||
URL url = tunnelRequest.url();
|
URL url = tunnelRequest.url();
|
||||||
String requestLine = "CONNECT " + url.getHost() + ":" + url.getPort() + " HTTP/1.1";
|
String requestLine = "CONNECT " + url.getHost() + ":" + getEffectivePort(url) + " HTTP/1.1";
|
||||||
while (true) {
|
while (true) {
|
||||||
tunnelConnection.writeRequest(tunnelRequest.headers(), requestLine);
|
tunnelConnection.writeRequest(tunnelRequest.headers(), requestLine);
|
||||||
tunnelConnection.flush();
|
tunnelConnection.flush();
|
||||||
|
|||||||
Reference in New Issue
Block a user