|
3 | 3 | import android.content.Context; |
4 | 4 | import android.content.Intent; |
5 | 5 |
|
6 | | -import android.net.Uri; |
7 | 6 | import android.os.Bundle; |
8 | 7 | import android.preference.PreferenceManager; |
9 | 8 | import android.util.Log; |
|
15 | 14 | import com.burgstaller.okhttp.digest.CachingAuthenticator; |
16 | 15 | import com.burgstaller.okhttp.digest.DigestAuthenticator; |
17 | 16 |
|
| 17 | +import okhttp3.Interceptor; |
| 18 | +import okhttp3.Response; |
| 19 | +import okhttp3.Request; |
18 | 20 | import java.io.FileNotFoundException; |
| 21 | +import java.io.IOException; |
19 | 22 | import java.io.InputStream; |
20 | 23 | import java.io.StringReader; |
21 | 24 | import java.io.UnsupportedEncodingException; |
|
38 | 41 | import javax.net.ssl.TrustManagerFactory; |
39 | 42 | import javax.net.ssl.X509TrustManager; |
40 | 43 |
|
41 | | -import keepass2android.javafilestorage.ConnectionInfo; |
42 | 44 | import keepass2android.javafilestorage.webdav.DecoratedHostnameVerifier; |
43 | 45 | import keepass2android.javafilestorage.webdav.DecoratedTrustManager; |
44 | 46 | import keepass2android.javafilestorage.webdav.PropfindXmlParser; |
45 | 47 | import keepass2android.javafilestorage.webdav.WebDavUtil; |
46 | 48 | import okhttp3.MediaType; |
47 | 49 | import okhttp3.OkHttpClient; |
48 | | -import okhttp3.Request; |
49 | 50 | import okhttp3.RequestBody; |
50 | | -import okhttp3.Response; |
51 | 51 | import okhttp3.internal.tls.OkHostnameVerifier; |
52 | 52 |
|
53 | 53 | public class WebDavStorage extends JavaFileStorageBase { |
54 | 54 |
|
| 55 | + |
| 56 | + public class CleartextBlockInterceptor implements Interceptor { |
| 57 | + private final boolean permitCleartext; |
| 58 | + |
| 59 | + public CleartextBlockInterceptor(Context context) { |
| 60 | + this.permitCleartext = PreferenceManager.getDefaultSharedPreferences(context).getBoolean("permit_cleartext_traffic", false); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Response intercept(Chain chain) throws IOException { |
| 65 | + Request request = chain.request(); |
| 66 | + |
| 67 | + if (!permitCleartext && request.url().scheme().equals("http")) { |
| 68 | + throw new IOException("Cleartext HTTP is disabled by user preference. Go to app settings/File handling if you really want to use HTTP."); |
| 69 | + } |
| 70 | + |
| 71 | + return chain.proceed(request); |
| 72 | + } |
| 73 | + } |
| 74 | + |
55 | 75 | private final ICertificateErrorHandler mCertificateErrorHandler; |
56 | 76 | private Context appContext; |
57 | 77 |
|
58 | | - public WebDavStorage(ICertificateErrorHandler certificateErrorHandler) |
| 78 | + public WebDavStorage(ICertificateErrorHandler certificateErrorHandler, Context appContext) |
59 | 79 | { |
| 80 | + this.appContext = appContext; |
60 | 81 |
|
61 | 82 | mCertificateErrorHandler = certificateErrorHandler; |
62 | 83 | } |
@@ -174,6 +195,7 @@ private OkHttpClient getClient(ConnectionInfo ci) throws NoSuchAlgorithmExceptio |
174 | 195 | builder.readTimeout(25, TimeUnit.SECONDS); |
175 | 196 | builder.writeTimeout(25, TimeUnit.SECONDS); |
176 | 197 | } |
| 198 | + builder.addInterceptor(new CleartextBlockInterceptor(appContext)); |
177 | 199 |
|
178 | 200 | OkHttpClient client = builder.build(); |
179 | 201 |
|
@@ -514,7 +536,6 @@ public void onActivityResult(FileStorageSetupActivity activity, |
514 | 536 |
|
515 | 537 | @Override |
516 | 538 | public void prepareFileUsage(Context appContext, String path) { |
517 | | - this.appContext = appContext; |
518 | 539 |
|
519 | 540 | } |
520 | 541 |
|
|
0 commit comments