Skip to content

Commit

Permalink
Use HttpUrl in Request.
Browse files Browse the repository at this point in the history
  • Loading branch information
squarejesse committed May 12, 2015
1 parent 8161318 commit 2b236fd
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -654,8 +654,10 @@ private void handleWebSocketUpgrade(Socket socket, BufferedSource source, Buffer
};

// 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()
.get().url(request.getPath())
.url(scheme + "://" + authority + "/")
.headers(request.getHeaders())
.build();
final Response fancyResponse = new Response.Builder()
Expand Down
14 changes: 7 additions & 7 deletions okcurl/src/test/java/com/squareup/okhttp/curl/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ public class MainTest {
@Test public void simple() {
Request request = fromArgs("http://example.com").createRequest();
assertEquals("GET", request.method());
assertEquals("http://example.com", request.urlString());
assertEquals("http://example.com/", request.urlString());
assertNull(request.body());
}

@Test public void put() throws IOException {
Request request = fromArgs("-X", "PUT", "-d", "foo", "http://example.com").createRequest();
assertEquals("PUT", request.method());
assertEquals("http://example.com", request.urlString());
assertEquals("http://example.com/", request.urlString());
assertEquals(3, request.body().contentLength());
}

@Test public void dataPost() {
Request request = fromArgs("-d", "foo", "http://example.com").createRequest();
RequestBody body = request.body();
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("foo", bodyAsString(body));
}
Expand All @@ -53,7 +53,7 @@ public class MainTest {
Request request = fromArgs("-d", "foo", "-X", "PUT", "http://example.com").createRequest();
RequestBody body = request.body();
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("foo", bodyAsString(body));
}
Expand All @@ -63,23 +63,23 @@ public class MainTest {
"http://example.com").createRequest();
RequestBody body = request.body();
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("foo", bodyAsString(body));
}

@Test public void referer() {
Request request = fromArgs("-e", "foo", "http://example.com").createRequest();
assertEquals("GET", request.method());
assertEquals("http://example.com", request.urlString());
assertEquals("http://example.com/", request.urlString());
assertEquals("foo", request.header("Referer"));
assertNull(request.body());
}

@Test public void userAgent() {
Request request = fromArgs("-A", "foo", "http://example.com").createRequest();
assertEquals("GET", request.method());
assertEquals("http://example.com", request.urlString());
assertEquals("http://example.com/", request.urlString());
assertEquals("foo", request.header("User-Agent"));
assertNull(request.body());
}
Expand Down
42 changes: 18 additions & 24 deletions okhttp-tests/src/test/java/com/squareup/okhttp/CallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
import okio.Okio;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand All @@ -73,7 +72,6 @@
import static java.net.CookiePolicy.ACCEPT_ORIGINAL_SERVER;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -127,40 +125,36 @@ public final class CallTest {
assertNull(recordedRequest.getHeader("Content-Length"));
}

@Test public void lazilyEvaluateRequestUrl() throws Exception {
server.enqueue(new MockResponse().setBody("abc"));
@Test public void buildRequestUsingHttpUrl() throws Exception {
server.enqueue(new MockResponse());

Request request1 = new Request.Builder()
.url("foo://bar?baz")
.build();
Request request2 = request1.newBuilder()
.url(server.getUrl("/"))
HttpUrl httpUrl = HttpUrl.get(server.getUrl("/"));
Request request = new Request.Builder()
.url(httpUrl)
.build();
executeSynchronously(request2)
.assertCode(200)
.assertSuccessful()
.assertBody("abc");
assertEquals(httpUrl, request.httpUrl());

executeSynchronously(request).assertSuccessful();
}

@Ignore // TODO(jwilson): fix.
@Test public void invalidScheme() throws Exception {
Request.Builder requestBuilder = new Request.Builder();
try {
Request request = new Request.Builder()
.url("ftp://hostname/path")
.build();
executeSynchronously(request);
requestBuilder.url("ftp://hostname/path");
fail();
} catch (IllegalArgumentException expected) {
assertEquals(expected.getMessage(), "unexpected url: ftp://hostname/path");
}
}

@Test public void invalidPort() throws Exception {
Request request = new Request.Builder()
.url("http://localhost:65536/")
.build();
client.newCall(request).enqueue(callback);
callback.await(request.url())
.assertFailure("No route to localhost:65536; port is out of range");
Request.Builder requestBuilder = new Request.Builder();
try {
requestBuilder.url("http://localhost:65536/");
fail();
} catch (IllegalArgumentException expected) {
assertEquals(expected.getMessage(), "unexpected url: http://localhost:65536/");
}
}

@Test public void getReturns500() throws Exception {
Expand Down
60 changes: 30 additions & 30 deletions okhttp/src/main/java/com/squareup/okhttp/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@
*/
package com.squareup.okhttp;

import com.squareup.okhttp.internal.Platform;
import com.squareup.okhttp.internal.http.HttpMethod;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;

Expand All @@ -29,45 +26,44 @@
* is null or itself immutable.
*/
public final class Request {
private final String urlString;
private final HttpUrl url;
private final String method;
private final Headers headers;
private final RequestBody body;
private final Object tag;

private volatile URL url; // Lazily initialized.
private volatile URI uri; // Lazily initialized.
private volatile URL javaNetUrl; // Lazily initialized.
private volatile URI javaNetUri; // Lazily initialized.
private volatile CacheControl cacheControl; // Lazily initialized.

private Request(Builder builder) {
this.urlString = builder.urlString;
this.url = builder.url;
this.method = builder.method;
this.headers = builder.headers.build();
this.body = builder.body;
this.tag = builder.tag != null ? builder.tag : this;
this.url = builder.url;
}

public HttpUrl httpUrl() {
return url;
}

public URL url() {
try {
URL result = url;
return result != null ? result : (url = new URL(urlString));
} catch (MalformedURLException e) {
throw new RuntimeException("Malformed URL: " + urlString, e);
}
URL result = javaNetUrl;
return result != null ? result : (javaNetUrl = url.url());
}

public URI uri() throws IOException {
try {
URI result = uri;
return result != null ? result : (uri = Platform.get().toUriLenient(url()));
} catch (URISyntaxException e) {
URI result = javaNetUri;
return result != null ? result : (javaNetUri = url.uri());
} catch (IllegalStateException e) {
throw new IOException(e.getMessage());
}
}

public String urlString() {
return urlString;
return url.toString();
}

public String method() {
Expand Down Expand Up @@ -108,22 +104,21 @@ public CacheControl cacheControl() {
}

public boolean isHttps() {
return url().getProtocol().equals("https");
return url.isHttps();
}

@Override public String toString() {
return "Request{method="
+ method
+ ", url="
+ urlString
+ url
+ ", tag="
+ (tag != this ? tag : null)
+ '}';
}

public static class Builder {
private String urlString;
private URL url;
private HttpUrl url;
private String method;
private Headers.Builder headers;
private RequestBody body;
Expand All @@ -135,26 +130,31 @@ public Builder() {
}

private Builder(Request request) {
this.urlString = request.urlString;
this.url = request.url;
this.method = request.method;
this.body = request.body;
this.tag = request.tag;
this.headers = request.headers.newBuilder();
}

public Builder url(String url) {
public Builder url(HttpUrl url) {
if (url == null) throw new IllegalArgumentException("url == null");
this.urlString = url;
this.url = null;
this.url = url;
return this;
}

public Builder url(String url) {
if (url == null) throw new IllegalArgumentException("url == null");
HttpUrl parsed = HttpUrl.parse(url);
if (parsed == null) throw new IllegalArgumentException("unexpected url: " + url);
return url(parsed);
}

public Builder url(URL url) {
if (url == null) throw new IllegalArgumentException("url == null");
this.url = url;
this.urlString = url.toString();
return this;
HttpUrl parsed = HttpUrl.get(url);
if (parsed == null) throw new IllegalArgumentException("unexpected url: " + url);
return url(parsed);
}

/**
Expand Down Expand Up @@ -251,7 +251,7 @@ public Builder tag(Object tag) {
}

public Request build() {
if (urlString == null) throw new IllegalStateException("url == null");
if (url == null) throw new IllegalStateException("url == null");
return new Request(this);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
Expand Down Expand Up @@ -76,10 +73,6 @@ public void tagSocket(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}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.squareup.okhttp.Credentials;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.internal.Util;
import java.io.IOException;
import java.net.Authenticator.RequestorType;
import java.net.InetAddress;
Expand All @@ -43,8 +44,9 @@ public final class AuthenticatorAdapter implements Authenticator {
if (!"Basic".equalsIgnoreCase(challenge.getScheme())) continue;

PasswordAuthentication auth = java.net.Authenticator.requestPasswordAuthentication(
url.getHost(), getConnectToInetAddress(proxy, url), url.getPort(), url.getProtocol(),
challenge.getRealm(), challenge.getScheme(), url, RequestorType.SERVER);
url.getHost(), getConnectToInetAddress(proxy, url), Util.getEffectivePort(url),
url.getProtocol(), challenge.getRealm(), challenge.getScheme(), url,
RequestorType.SERVER);
if (auth == null) continue;

String credential = Credentials.basic(auth.getUserName(), new String(auth.getPassword()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private void createTunnel(int readTimeout, int writeTimeout, Request request, Ro
HttpConnection tunnelConnection = new HttpConnection(connectionPool, connection, socket);
tunnelConnection.setTimeouts(readTimeout, writeTimeout);
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) {
tunnelConnection.writeRequest(tunnelRequest.headers(), requestLine);
tunnelConnection.flush();
Expand Down

0 comments on commit 2b236fd

Please sign in to comment.