Add gzip to outgoing requests. According to my benchmarks it has a very small effect.

pull/14/head
Tim Su 14 years ago
parent e0f1b14d04
commit 772e90fa56

@ -5,10 +5,16 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.zip.GZIPInputStream;
import org.apache.http.Header; import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.HttpVersion; import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
@ -19,12 +25,14 @@ import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.HttpEntityWrapper;
import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams; import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams; import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams; import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HttpContext;
import android.util.Log; import android.util.Log;
@ -49,15 +57,36 @@ public class HttpRestClient implements RestClient {
private WeakReference<HttpClient> httpClient = null; private WeakReference<HttpClient> httpClient = null;
protected boolean debug = false; protected boolean debug = false;
private int timeout = TIMEOUT_MILLIS; private int timeout = TIMEOUT_MILLIS;
@SuppressWarnings("nls")
public HttpRestClient() { public HttpRestClient() {
DependencyInjectionService.getInstance().inject(this); DependencyInjectionService.getInstance().inject(this);
System.err.println("NEW REST CLIENT");
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, timeout);
HttpConnectionParams.setSoTimeout(params, timeout);
params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
cm = new ThreadSafeClientConnManager(params, schemeRegistry);
} }
public HttpRestClient(int timeout) { public HttpRestClient(int timeout) {
super(); super();
this.timeout = timeout; this.timeout = timeout;
HttpConnectionParams.setConnectionTimeout(params, timeout);
HttpConnectionParams.setSoTimeout(params, timeout);
} }
private static String convertStreamToString(InputStream is) { private static String convertStreamToString(InputStream is) {
@ -88,28 +117,74 @@ public class HttpRestClient implements RestClient {
return sb.toString(); return sb.toString();
} }
private HttpParams params;
private ThreadSafeClientConnManager cm;
@SuppressWarnings("nls") @SuppressWarnings("nls")
private synchronized HttpClient getClient() { private synchronized HttpClient getClient() {
if (httpClient == null || httpClient.get() == null) { if (httpClient == null || httpClient.get() == null) {
SchemeRegistry schemeRegistry = new SchemeRegistry(); DefaultHttpClient client = new DefaultHttpClient(cm, params);
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, timeout);
HttpConnectionParams.setSoTimeout(params, timeout);
params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
HttpClient client = new DefaultHttpClient(cm, params);
httpClient = new WeakReference<HttpClient>(client); httpClient = new WeakReference<HttpClient>(client);
actsAsGzippable(client);
return client; return client;
} else {
return httpClient.get();
} }
return httpClient.get();
}
@SuppressWarnings("nls")
protected void actsAsGzippable(DefaultHttpClient client) {
client.addRequestInterceptor(new HttpRequestInterceptor() {
public void process(
final HttpRequest request,
final HttpContext context) throws HttpException, IOException {
if (!request.containsHeader("Accept-Encoding"))
request.addHeader("Accept-Encoding", "gzip");
}
});
client.addResponseInterceptor(new HttpResponseInterceptor() {
public void process(
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
HttpEntity entity = response.getEntity();
Header ceheader = entity.getContentEncoding();
if (ceheader != null) {
HeaderElement[] codecs = ceheader.getElements();
for (int i = 0; i < codecs.length; i++) {
if (codecs[i].getName().equalsIgnoreCase("gzip")) {
response.setEntity(
new GzipDecompressingEntity(response.getEntity()));
return;
}
}
}
}
});
}
private static class GzipDecompressingEntity extends HttpEntityWrapper {
public GzipDecompressingEntity(final HttpEntity entity) {
super(entity);
}
@Override
public InputStream getContent()
throws IOException, IllegalStateException {
// the wrapped entity's getContent() decides about repeatability
InputStream wrappedin = wrappedEntity.getContent();
return new GZIPInputStream(wrappedin);
}
@Override
public long getContentLength() {
// length of ungzipped content is not known
return -1;
}
} }
private String processHttpResponse(HttpResponse response) throws IOException { private String processHttpResponse(HttpResponse response) throws IOException {

Loading…
Cancel
Save