Skip to content

Commit

Permalink
Allow HTTP connections for upload (fix #1137)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniele Ricci <[email protected]>
  • Loading branch information
daniele-athome committed Feb 17, 2018
1 parent 1c537d8 commit da4d38b
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions app/src/main/java/org/kontalk/upload/HTPPFileUploadConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
Expand Down Expand Up @@ -52,7 +53,7 @@ public class HTPPFileUploadConnection implements UploadConnection {
private final Context mContext;
private final String mUrl;

private HttpsURLConnection currentRequest;
private HttpURLConnection currentRequest;

private final static int CONNECT_TIMEOUT = 15000;
private final static int READ_TIMEOUT = 40000;
Expand Down Expand Up @@ -94,7 +95,7 @@ public String upload(Uri uri, long length, String mime, boolean encrypt, String[
return null;
}
catch (Exception e) {
throw innerException("upload error", e);
throw new IOException("upload error", e);
}
finally {
currentRequest.disconnect();
Expand All @@ -110,22 +111,19 @@ public String upload(Uri uri, long length, String mime, boolean encrypt, String[
}
}

private IOException innerException(String detail, Throwable cause) {
return new IOException(detail, cause);
}

@SuppressLint("AllowAllHostnameVerifier")
@SuppressWarnings("deprecation")
private void setupClient(HttpsURLConnection conn, long length, String mime, boolean acceptAnyCertificate)
private void setupClient(HttpURLConnection conn, long length, String mime, boolean acceptAnyCertificate)
throws CertificateException, UnrecoverableKeyException,
NoSuchAlgorithmException, KeyStoreException,
KeyManagementException, NoSuchProviderException,
IOException {

conn.setSSLSocketFactory(ClientHTTPConnection.setupSSLSocketFactory(mContext,
null, null, acceptAnyCertificate));
if (acceptAnyCertificate)
conn.setHostnameVerifier(new AllowAllHostnameVerifier());
if (conn instanceof HttpsURLConnection) {
((HttpsURLConnection) conn).setSSLSocketFactory(ClientHTTPConnection.setupSSLSocketFactory(mContext,
null, null, acceptAnyCertificate));
if (acceptAnyCertificate)
((HttpsURLConnection) conn).setHostnameVerifier(new AllowAllHostnameVerifier());
}
conn.setRequestProperty("Content-Type", mime != null ? mime
: "application/octet-stream");
// bug caused by Lighttpd
Expand All @@ -146,11 +144,11 @@ private void setupClient(HttpsURLConnection conn, long length, String mime, bool
}

/** A message posting method. */
private HttpsURLConnection prepareMessage(long length, String mime, boolean acceptAnyCertificate)
private HttpURLConnection prepareMessage(long length, String mime, boolean acceptAnyCertificate)
throws IOException {

// create uri
HttpsURLConnection conn = (HttpsURLConnection) new URL(mUrl).openConnection();
HttpURLConnection conn = (HttpURLConnection) new URL(mUrl).openConnection();
try {
setupClient(conn, length, mime, acceptAnyCertificate);
}
Expand Down

0 comments on commit da4d38b

Please sign in to comment.