This repository has been archived by the owner on Feb 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle blogs under subfolders + comprehensive unit tests for various …
…kinds of blog URLs. Fixes #161.
- Loading branch information
1 parent
e2fd415
commit 4a33dc0
Showing
10 changed files
with
391 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
app/src/debug/java/me/vickychijwani/spectre/network/UnsafeHttpClientFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package me.vickychijwani.spectre.network; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.support.annotation.Nullable; | ||
|
||
import java.io.File; | ||
import java.security.cert.CertificateException; | ||
import java.security.cert.X509Certificate; | ||
|
||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.SSLSocketFactory; | ||
import javax.net.ssl.TrustManager; | ||
import javax.net.ssl.X509TrustManager; | ||
|
||
import okhttp3.OkHttpClient; | ||
|
||
public class UnsafeHttpClientFactory extends ProductionHttpClientFactory { | ||
|
||
private final X509TrustManager mGullibleTrustManager; | ||
|
||
public UnsafeHttpClientFactory() { | ||
mGullibleTrustManager = new GullibleX509TrustManager(); | ||
} | ||
|
||
@Override | ||
public OkHttpClient create(@Nullable File cacheDir) { | ||
return super.create(cacheDir).newBuilder() | ||
// trust all SSL certs, for TESTING ONLY! | ||
.hostnameVerifier((hostname, session) -> true) | ||
.sslSocketFactory(getUnsafeSslSocketFactory(), mGullibleTrustManager) | ||
.build(); | ||
} | ||
|
||
private SSLSocketFactory getUnsafeSslSocketFactory() { | ||
try { | ||
// Create a trust manager that does not validate certificate chains | ||
final TrustManager[] trustAllCerts = new TrustManager[] { mGullibleTrustManager }; | ||
// Install the all-trusting trust manager | ||
final SSLContext sslContext = SSLContext.getInstance("SSL"); | ||
sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); | ||
// Create an ssl socket factory with our all-trusting manager | ||
return sslContext.getSocketFactory(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static class GullibleX509TrustManager implements X509TrustManager { | ||
|
||
@SuppressLint("TrustAllX509TrustManager") | ||
@Override | ||
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) | ||
throws CertificateException {} | ||
|
||
@SuppressLint("TrustAllX509TrustManager") | ||
@Override | ||
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) | ||
throws CertificateException {} | ||
|
||
@Override | ||
public java.security.cert.X509Certificate[] getAcceptedIssuers() { | ||
return new X509Certificate[0]; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
app/src/main/java/me/vickychijwani/spectre/network/HttpClientFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package me.vickychijwani.spectre.network; | ||
|
||
import android.support.annotation.Nullable; | ||
|
||
import java.io.File; | ||
|
||
import okhttp3.OkHttpClient; | ||
|
||
public interface HttpClientFactory { | ||
|
||
OkHttpClient create(@Nullable File cacheDir); | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
app/src/main/java/me/vickychijwani/spectre/network/ProductionHttpClientFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package me.vickychijwani.spectre.network; | ||
|
||
import android.os.Build; | ||
import android.os.StatFs; | ||
import android.support.annotation.Nullable; | ||
|
||
import java.io.File; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import okhttp3.Cache; | ||
import okhttp3.OkHttpClient; | ||
|
||
public class ProductionHttpClientFactory implements HttpClientFactory { | ||
|
||
private static final int MIN_DISK_CACHE_SIZE = 5 * 1024 * 1024; // in bytes | ||
private static final int MAX_DISK_CACHE_SIZE = 50 * 1024 * 1024; // in bytes | ||
private static final int CONNECTION_TIMEOUT = 10 * 1000; // in milliseconds | ||
|
||
/** | ||
* | ||
* @param cacheDir - directory for the HTTP cache, disabled if null | ||
* @return an HTTP client intended for production use | ||
*/ | ||
@Override | ||
public OkHttpClient create(@Nullable File cacheDir) { | ||
OkHttpClient.Builder builder = new OkHttpClient.Builder(); | ||
if (cacheDir != null) { | ||
long size = calculateDiskCacheSize(cacheDir); | ||
builder.cache(new Cache(cacheDir, size)); | ||
} | ||
return builder.connectTimeout(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS) | ||
.readTimeout(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS) | ||
.writeTimeout(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS) | ||
.build(); | ||
} | ||
|
||
private static long calculateDiskCacheSize(File dir) { | ||
long size = MIN_DISK_CACHE_SIZE; | ||
try { | ||
StatFs statFs = new StatFs(dir.getAbsolutePath()); | ||
long available; | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { | ||
available = statFs.getBlockCountLong() * statFs.getBlockSizeLong(); | ||
} else { | ||
// checked at runtime | ||
//noinspection deprecation | ||
available = statFs.getBlockCount() * statFs.getBlockSize(); | ||
} | ||
// Target 2% of the total space. | ||
size = available / 50; | ||
} catch (IllegalArgumentException ignored) { | ||
} | ||
// Bound inside min/max size for disk cache. | ||
return Math.max(Math.min(size, MAX_DISK_CACHE_SIZE), MIN_DISK_CACHE_SIZE); | ||
} | ||
|
||
} |
Oops, something went wrong.