Skip to content

Commit

Permalink
WIP Use deprecated OkApacheClient in lieue of deprecated HttpClient
Browse files Browse the repository at this point in the history
This is an intermediate step, and an experiment on the way to supporting TLS1.2 on Android API<=21

I can't override the trust manager in OkApacheClient though, and it's deprecated anyway, so we need to move
to the full OkHttp Request/Response implementation, and alter the OkHttpClient builder invocation like so:

square/okhttp#2372 (comment)
  • Loading branch information
mikehardy committed Dec 12, 2019
1 parent 186e99f commit 864c661
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 241 deletions.
2 changes: 1 addition & 1 deletion AnkiDroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ android {
buildConfigField "String", "ACRA_URL", '"https://ankidroid.org/acra/report"'
}
}
useLibrary 'org.apache.http.legacy'

testOptions {
animationsDisabled true
Expand Down Expand Up @@ -143,6 +142,7 @@ dependencies {
implementation 'net.mikehardy:google-analytics-java7:2.0.10'
//noinspection GradleDependency
implementation 'com.squareup.okhttp3:okhttp:3.12.6'
implementation 'com.squareup.okhttp3:okhttp-apache:3.12.6'
implementation 'com.arcao:slf4j-timber:3.1'

implementation 'com.jakewharton.timber:timber:4.7.1'
Expand Down
47 changes: 17 additions & 30 deletions AnkiDroid/src/main/java/com/ichi2/libanki/sync/HttpSyncer.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
import com.ichi2.async.Connection;
import com.ichi2.libanki.Consts;
import com.ichi2.libanki.Utils;
import com.ichi2.utils.VersionUtils;

import org.apache.commons.httpclient.contrib.ssl.EasySSLSocketFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.AbstractHttpEntity;
import org.json.JSONException;
import org.json.JSONObject;

Expand Down Expand Up @@ -95,7 +97,7 @@ public HttpSyncer(String hkey, Connection con) {
}


public void assertOk(org.apache.http.HttpResponse resp) throws UnknownHttpResponseException {
public void assertOk(HttpResponse resp) throws UnknownHttpResponseException {
// Throw RuntimeException if HTTP error
if (resp == null) {
throw new UnknownHttpResponseException("Null HttpResponse", -2);
Expand All @@ -108,27 +110,27 @@ public void assertOk(org.apache.http.HttpResponse resp) throws UnknownHttpRespon
}


public org.apache.http.HttpResponse req(String method) throws UnknownHttpResponseException {
public HttpResponse req(String method) throws UnknownHttpResponseException {
return req(method, null);
}


public org.apache.http.HttpResponse req(String method, InputStream fobj) throws UnknownHttpResponseException {
public HttpResponse req(String method, InputStream fobj) throws UnknownHttpResponseException {
return req(method, fobj, 6);
}


public org.apache.http.HttpResponse req(String method, int comp, InputStream fobj) throws UnknownHttpResponseException {
public HttpResponse req(String method, int comp, InputStream fobj) throws UnknownHttpResponseException {
return req(method, fobj, comp);
}


public org.apache.http.HttpResponse req(String method, InputStream fobj, int comp) throws UnknownHttpResponseException {
public HttpResponse req(String method, InputStream fobj, int comp) throws UnknownHttpResponseException {
return req(method, fobj, comp, null);
}


private org.apache.http.HttpResponse req(String method, InputStream fobj, int comp, JSONObject registerData) throws UnknownHttpResponseException {
private HttpResponse req(String method, InputStream fobj, int comp, JSONObject registerData) throws UnknownHttpResponseException {
File tmpFileBuffer = null;
try {
String bdry = "--" + BOUNDARY;
Expand Down Expand Up @@ -185,31 +187,16 @@ private org.apache.http.HttpResponse req(String method, InputStream fobj, int co
} else {
url = syncURL() + method;
}
org.apache.http.client.methods.HttpPost httpPost = new org.apache.http.client.methods.HttpPost(url);
org.apache.http.HttpEntity entity = new ProgressByteEntity(tmpFileBuffer);
HttpPost httpPost = new HttpPost(url);
HttpEntity entity = new ProgressByteEntity(tmpFileBuffer);

// body
httpPost.setEntity(entity);
httpPost.setHeader("Content-type", "multipart/form-data; boundary=" + BOUNDARY);

// HttpParams
org.apache.http.params.HttpParams params = new org.apache.http.params.BasicHttpParams();
params.setParameter(org.apache.http.conn.params.ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
params.setParameter(org.apache.http.conn.params.ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new org.apache.http.conn.params.ConnPerRouteBean(30));
params.setParameter(org.apache.http.params.CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
params.setParameter(org.apache.http.params.CoreProtocolPNames.USER_AGENT, "AnkiDroid-" + VersionUtils.getPkgVersionName());
org.apache.http.params.HttpProtocolParams.setVersion(params, org.apache.http.HttpVersion.HTTP_1_1);
org.apache.http.params.HttpConnectionParams.setSoTimeout(params, Connection.CONN_TIMEOUT);

// Registry
org.apache.http.conn.scheme.SchemeRegistry registry = new org.apache.http.conn.scheme.SchemeRegistry();
registry.register(new org.apache.http.conn.scheme.Scheme("http", org.apache.http.conn.scheme.PlainSocketFactory.getSocketFactory(), 80));
registry.register(new org.apache.http.conn.scheme.Scheme("https", new EasySSLSocketFactory(), 443));
org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager cm = new org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager(params, registry);

try {
org.apache.http.client.HttpClient httpClient = new org.apache.http.impl.client.DefaultHttpClient(cm, params);
org.apache.http.HttpResponse httpResponse = httpClient.execute(httpPost);
okhttp3.apache.OkApacheClient httpClient = new okhttp3.apache.OkApacheClient();
HttpResponse httpResponse = httpClient.execute(httpPost);
// we assume badAuthRaises flag from Anki Desktop always False
// so just throw new RuntimeException if response code not 200 or 403
assertOk(httpResponse);
Expand Down Expand Up @@ -295,7 +282,7 @@ private void publishProgress() {
}


public org.apache.http.HttpResponse hostKey(String arg1, String arg2) throws UnknownHttpResponseException {
public HttpResponse hostKey(String arg1, String arg2) throws UnknownHttpResponseException {
return null;
}

Expand Down Expand Up @@ -324,7 +311,7 @@ public void abort() throws UnknownHttpResponseException {
}


public org.apache.http.HttpResponse meta() throws UnknownHttpResponseException {
public HttpResponse meta() throws UnknownHttpResponseException {
return null;
}

Expand All @@ -348,7 +335,7 @@ public void applyChunk(JSONObject sech) throws UnknownHttpResponseException {
// do nothing
}

public class ProgressByteEntity extends org.apache.http.entity.AbstractHttpEntity {
public class ProgressByteEntity extends AbstractHttpEntity {

private InputStream mInputStream;
private long mLength;
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit 864c661

Please sign in to comment.