Skip to content

Commit

Permalink
Add HttpUrl methods to MWS and update non-HUC usages.
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton committed Jun 21, 2015
1 parent 062435c commit 4c50994
Show file tree
Hide file tree
Showing 17 changed files with 328 additions and 312 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.squareup.okhttp.benchmarks;

import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.internal.SslContextBuilder;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -49,8 +50,8 @@ class ApacheHttpClient extends SynchronousHttpClient {
client = new DefaultHttpClient(connectionManager);
}

@Override public Runnable request(URL url) {
return new ApacheHttpClientRequest(url);
@Override public Runnable request(HttpUrl url) {
return new ApacheHttpClientRequest(url.url());
}

class ApacheHttpClientRequest implements Runnable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
import com.google.caliper.Param;
import com.google.caliper.model.ArbitraryMeasurement;
import com.google.caliper.runner.CaliperMain;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.Protocol;
import com.squareup.okhttp.internal.SslContextBuilder;
import com.squareup.okhttp.mockwebserver.Dispatcher;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
import com.squareup.okhttp.mockwebserver.RecordedRequest;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -101,7 +101,7 @@ public double run() throws Exception {
// Prepare the client & server
httpClient.prepare(this);
MockWebServer server = startServer();
URL url = server.getUrl("/");
HttpUrl url = server.url("/");

int requestCount = 0;
long reportStart = System.nanoTime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
package com.squareup.okhttp.benchmarks;

import java.net.URL;
import com.squareup.okhttp.HttpUrl;

/** An HTTP client to benchmark. */
interface HttpClient {
void prepare(Benchmark benchmark);
void enqueue(URL url) throws Exception;
void enqueue(HttpUrl url) throws Exception;
boolean acceptingJobs();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package com.squareup.okhttp.benchmarks;

import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.internal.SslContextBuilder;
import com.squareup.okhttp.internal.Util;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
Expand All @@ -41,7 +41,6 @@
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.handler.ssl.SslHandler;
import java.net.URL;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.concurrent.TimeUnit;
Expand All @@ -54,7 +53,7 @@ class NettyHttpClient implements HttpClient {

// Guarded by this. Real apps need more capable connection management.
private final Deque<HttpChannel> freeChannels = new ArrayDeque<>();
private final Deque<URL> backlog = new ArrayDeque<>();
private final Deque<HttpUrl> backlog = new ArrayDeque<>();

private int totalChannels = 0;
private int concurrencyLevel;
Expand Down Expand Up @@ -89,7 +88,7 @@ class NettyHttpClient implements HttpClient {
.handler(channelInitializer);
}

@Override public void enqueue(URL url) throws Exception {
@Override public void enqueue(HttpUrl url) throws Exception {
HttpChannel httpChannel = null;
synchronized (this) {
if (!freeChannels.isEmpty()) {
Expand All @@ -102,7 +101,7 @@ class NettyHttpClient implements HttpClient {
}
}
if (httpChannel == null) {
Channel channel = bootstrap.connect(url.getHost(), Util.getEffectivePort(url))
Channel channel = bootstrap.connect(url.host(), url.port())
.sync().channel();
httpChannel = (HttpChannel) channel.pipeline().last();
}
Expand All @@ -119,7 +118,7 @@ private boolean hasFreeChannels() {
}

private void release(HttpChannel httpChannel) {
URL url;
HttpUrl url;
synchronized (this) {
url = backlog.pop();
if (url == null) {
Expand All @@ -143,12 +142,12 @@ public HttpChannel(SocketChannel channel) {
this.channel = channel;
}

private void sendRequest(URL url) {
private void sendRequest(HttpUrl url) {
start = System.nanoTime();
total = 0;
HttpRequest request = new DefaultFullHttpRequest(
HttpVersion.HTTP_1_1, HttpMethod.GET, url.getPath());
request.headers().set(HttpHeaders.Names.HOST, url.getHost());
HttpVersion.HTTP_1_1, HttpMethod.GET, url.encodedPath());
request.headers().set(HttpHeaders.Names.HOST, url.host());
request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
channel.writeAndFlush(request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
*/
package com.squareup.okhttp.benchmarks;

import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.OkUrlFactory;
import com.squareup.okhttp.internal.SslContextBuilder;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
Expand Down Expand Up @@ -50,21 +50,21 @@ class OkHttp extends SynchronousHttpClient {
}
}

@Override public Runnable request(URL url) {
@Override public Runnable request(HttpUrl url) {
return new OkHttpRequest(url);
}

class OkHttpRequest implements Runnable {
private final URL url;
private final HttpUrl url;

public OkHttpRequest(URL url) {
public OkHttpRequest(HttpUrl url) {
this.url = url;
}

public void run() {
long start = System.nanoTime();
try {
HttpURLConnection urlConnection = new OkUrlFactory(client).open(url);
HttpURLConnection urlConnection = new OkUrlFactory(client).open(url.url());
long total = readAllAndClose(urlConnection.getInputStream());
long finish = System.nanoTime();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

import com.squareup.okhttp.Callback;
import com.squareup.okhttp.Dispatcher;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.ResponseBody;
import com.squareup.okhttp.internal.SslContextBuilder;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -83,7 +83,7 @@ class OkHttpAsync implements HttpClient {
};
}

@Override public void enqueue(URL url) throws Exception {
@Override public void enqueue(HttpUrl url) throws Exception {
requestsInFlight.incrementAndGet();
client.newCall(new Request.Builder().tag(System.nanoTime()).url(url).build()).enqueue(callback);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
package com.squareup.okhttp.benchmarks;

import com.squareup.okhttp.HttpUrl;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
Expand All @@ -33,7 +33,7 @@ abstract class SynchronousHttpClient implements HttpClient {
1, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
}

@Override public void enqueue(URL url) {
@Override public void enqueue(HttpUrl url) {
executor.execute(request(url));
}

Expand All @@ -51,5 +51,5 @@ static long readAllAndClose(InputStream in) throws IOException {
return total;
}

abstract Runnable request(URL url);
abstract Runnable request(HttpUrl url);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
package com.squareup.okhttp.benchmarks;

import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.internal.SslContextBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPInputStream;
import javax.net.ssl.HostnameVerifier;
Expand All @@ -46,21 +46,21 @@ class UrlConnection extends SynchronousHttpClient {
}
}

@Override public Runnable request(URL url) {
@Override public Runnable request(HttpUrl url) {
return new UrlConnectionRequest(url);
}

static class UrlConnectionRequest implements Runnable {
private final URL url;
private final HttpUrl url;

public UrlConnectionRequest(URL url) {
public UrlConnectionRequest(HttpUrl url) {
this.url = url;
}

public void run() {
long start = System.nanoTime();
try {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
HttpURLConnection urlConnection = (HttpURLConnection) url.url().openConnection();
InputStream in = urlConnection.getInputStream();
if ("gzip".equals(urlConnection.getHeaderField("Content-Encoding"))) {
in = new GZIPInputStream(in);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.squareup.okhttp.mockwebserver;

import com.squareup.okhttp.Headers;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.Protocol;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
Expand Down Expand Up @@ -153,6 +154,7 @@ public Proxy toProxyAddress() {
* Returns a URL for connecting to this server.
* @param path the request path, such as "/".
*/
@Deprecated
public URL getUrl(String path) {
try {
return sslSocketFactory != null
Expand All @@ -163,6 +165,20 @@ public URL getUrl(String path) {
}
}

/**
* Returns a URL for connecting to this server.
*
* @param path the request path, such as "/".
*/
public HttpUrl url(String path) {
return new HttpUrl.Builder()
.scheme(sslSocketFactory != null ? "https" : "http")
.host(getHostName())
.port(getPort())
.encodedPath(path)
.build();
}

/**
* Returns a cookie domain for this server. This returns the server's
* non-loopback host name if it is known. Otherwise this returns ".local" for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.squareup.okhttp.mockwebserver.rule;

import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
import com.squareup.okhttp.mockwebserver.RecordedRequest;
Expand Down Expand Up @@ -76,10 +77,15 @@ public RecordedRequest takeRequest() throws InterruptedException {
return server.takeRequest();
}

@Deprecated
public URL getUrl(String path) {
return server.getUrl(path);
}

public HttpUrl url(String path) {
return server.url(path);
}

/** For any other functionality, use the {@linkplain MockWebServer} directly. */
public MockWebServer get() {
return server;
Expand Down
Loading

0 comments on commit 4c50994

Please sign in to comment.