5353
5454import javax .net .ssl .SSLException ;
5555
56- import io .sentry .android .okhttp .SentryOkHttpInterceptor ;
5756import okhttp3 .Cache ;
5857import okhttp3 .Dns ;
5958import okhttp3 .HttpUrl ;
@@ -79,6 +78,7 @@ public enum Http {
7978 private static final boolean hasWebView ;
8079 private static String needCaptchaAndroidacyHost ;
8180 private static boolean doh ;
81+ private static boolean urlFactoryInstalled ;
8282
8383 static {
8484 MainApplication mainApplication = MainApplication .getINSTANCE ();
@@ -90,8 +90,7 @@ public enum Http {
9090 System .err .flush ();
9191 try {
9292 Os .kill (Os .getpid (), 9 );
93- } catch (
94- ErrnoException e ) {
93+ } catch (ErrnoException e ) {
9594 System .exit (9 );
9695 }
9796 throw error ;
@@ -101,8 +100,7 @@ public enum Http {
101100 cookieManager = CookieManager .getInstance ();
102101 cookieManager .setAcceptCookie (true );
103102 cookieManager .flush (); // Make sure the instance work
104- } catch (
105- Exception t ) {
103+ } catch (Exception t ) {
106104 cookieManager = null ;
107105 Timber .e (t , "No WebView support!" );
108106 // show a toast
@@ -148,9 +146,7 @@ public enum Http {
148146 WebkitCookieManagerProxy cookieJar = new WebkitCookieManagerProxy ();
149147 httpclientBuilder .cookieJar (cookieJar );
150148 dns = new DnsOverHttps .Builder ().client (httpclientBuilder .build ()).url (Objects .requireNonNull (HttpUrl .parse ("https://cloudflare-dns.com/dns-query" ))).bootstrapDnsHosts (cloudflareBootstrap ).resolvePrivateAddresses (true ).build ();
151- } catch (
152- UnknownHostException |
153- RuntimeException e ) {
149+ } catch (UnknownHostException | RuntimeException e ) {
154150 Timber .e (e , "Failed to init DoH" );
155151 }
156152 // User-Agent format was agreed on telegram
@@ -186,10 +182,6 @@ public enum Http {
186182 request .header ("Sec-CH-UA-Bitness" , Build .SUPPORTED_64_BIT_ABIS .length > 0 ? "64" : "32" );
187183 return chain .proceed (request .build ());
188184 });
189- // add sentry interceptor
190- if (MainApplication .isCrashReportingEnabled ()) {
191- httpclientBuilder .addInterceptor (new SentryOkHttpInterceptor ());
192- }
193185
194186 // for debug builds, add a logging interceptor
195187 // this spams the logcat, so it's disabled by default and hidden behind a build config flag
@@ -204,7 +196,7 @@ public enum Http {
204196 // init cronet
205197 try {
206198 // Load the cronet library
207- CronetEngine .Builder builder = new CronetEngine .Builder (mainApplication );
199+ CronetEngine .Builder builder = new CronetEngine .Builder (mainApplication . getApplicationContext () );
208200 builder .enableBrotli (true );
209201 builder .enableHttp2 (true );
210202 builder .enableQuic (true );
@@ -218,7 +210,6 @@ public enum Http {
218210 }
219211 builder .setStoragePath (mainApplication .getCacheDir ().getAbsolutePath () + "/cronet" );
220212 builder .enableHttpCache (CronetEngine .Builder .HTTP_CACHE_DISK_NO_HTTP , 10 * 1024 * 1024 );
221- builder .enableNetworkQualityEstimator (true );
222213 // Add quic hint
223214 builder .addQuicHint ("github.com" , 443 , 443 );
224215 builder .addQuicHint ("githubusercontent.com" , 443 , 443 );
@@ -227,8 +218,7 @@ public enum Http {
227218 builder .addQuicHint ("sentry.io" , 443 , 443 );
228219 CronetEngine engine = builder .build ();
229220 httpclientBuilder .addInterceptor (CronetInterceptor .newBuilder (engine ).build ());
230- } catch (
231- Exception e ) {
221+ } catch (Exception e ) {
232222 Timber .e (e , "Failed to init cronet" );
233223 // Gracefully fallback to okhttp
234224 }
@@ -249,8 +239,6 @@ public enum Http {
249239 doh = MainApplication .isDohEnabled ();
250240 }
251241
252- private static boolean urlFactoryInstalled ;
253-
254242 private static OkHttpClient .Builder followRedirects (OkHttpClient .Builder builder , boolean followRedirects ) {
255243 return builder .followRedirects (followRedirects ).followSslRedirects (followRedirects );
256244 }
@@ -316,8 +304,7 @@ public static byte[] doHttpGet(String url, boolean allowCache) throws IOExceptio
316304 // Use cache api if used cached response
317305 if (response .code () == 304 ) {
318306 response = response .cacheResponse ();
319- if (response != null )
320- responseBody = response .body ();
307+ if (response != null ) responseBody = response .body ();
321308 }
322309 if (BuildConfig .DEBUG_HTTP ) {
323310 Timber .d ("doHttpGet: returning " + responseBody .contentLength () + " bytes" );
@@ -347,8 +334,7 @@ private static Object doHttpPostRaw(String url, String data, boolean allowCache)
347334 // Use cache api if used cached response
348335 if (response .code () == 304 ) {
349336 response = response .cacheResponse ();
350- if (response != null )
351- responseBody = response .body ();
337+ if (response != null ) responseBody = response .body ();
352338 }
353339 return responseBody .bytes ();
354340 }
@@ -377,8 +363,7 @@ public static byte[] doHttpGet(String url, ProgressListener progressListener) th
377363 progressListener .onUpdate (0 , (int ) (target / divider ), false );
378364 while (true ) {
379365 int read = inputStream .read (buff );
380- if (read == -1 )
381- break ;
366+ if (read == -1 ) break ;
382367 byteArrayOutputStream .write (buff , 0 , read );
383368 downloaded += read ;
384369 currentUpdate = System .currentTimeMillis ();
@@ -411,7 +396,7 @@ public static boolean hasWebView() {
411396 return hasWebView ;
412397 }
413398
414- public static void ensureCacheDirs () {
399+ public static void ensureCacheDirs () {
415400 try {
416401 FileUtils .forceMkdir (new File ((MainApplication .getINSTANCE ().getDataDir () + "/cache/WebView/Default/HTTP Cache/Code Cache/wasm" ).replaceAll ("//" , "/" )));
417402 FileUtils .forceMkdir (new File ((MainApplication .getINSTANCE ().getDataDir () + "/cache/WebView/Default/HTTP Cache/Code Cache/js" ).replaceAll ("//" , "/" )));
@@ -487,22 +472,19 @@ public FallBackDNS(Context context, Dns parent, String... fallbacks) {
487472
488473 @ NonNull
489474 private static String toString (@ NonNull List <InetAddress > inetAddresses ) {
490- if (inetAddresses .isEmpty ())
491- return "" ;
475+ if (inetAddresses .isEmpty ()) return "" ;
492476 Iterator <InetAddress > inetAddressIterator = inetAddresses .iterator ();
493477 StringBuilder stringBuilder = new StringBuilder ();
494478 while (true ) {
495479 stringBuilder .append (inetAddressIterator .next ().getHostAddress ());
496- if (!inetAddressIterator .hasNext ())
497- return stringBuilder .toString ();
480+ if (!inetAddressIterator .hasNext ()) return stringBuilder .toString ();
498481 stringBuilder .append ("|" );
499482 }
500483 }
501484
502485 @ NonNull
503486 private static List <InetAddress > fromString (@ NonNull String string ) throws UnknownHostException {
504- if (string .isEmpty ())
505- return Collections .emptyList ();
487+ if (string .isEmpty ()) return Collections .emptyList ();
506488 String [] strings = string .split ("\\ |" );
507489 ArrayList <InetAddress > inetAddresses = new ArrayList <>(strings .length );
508490 for (String address : strings ) {
@@ -518,24 +500,20 @@ public List<InetAddress> lookup(@NonNull String s) throws UnknownHostException {
518500 List <InetAddress > addresses ;
519501 synchronized (this .fallbackCache ) {
520502 addresses = this .fallbackCache .get (s );
521- if (addresses != null )
522- return addresses ;
503+ if (addresses != null ) return addresses ;
523504 try {
524505 addresses = this .parent .lookup (s );
525506 if (addresses .isEmpty () || addresses .get (0 ).isLoopbackAddress ())
526507 throw new UnknownHostException (s );
527508 this .fallbackCache .put (s , addresses );
528509 this .sharedPreferences .edit ().putString (s .replace ('.' , '_' ), toString (addresses )).apply ();
529- } catch (
530- UnknownHostException e ) {
510+ } catch (UnknownHostException e ) {
531511 String key = this .sharedPreferences .getString (s .replace ('.' , '_' ), "" );
532- if (key .isEmpty ())
533- throw e ;
512+ if (key .isEmpty ()) throw e ;
534513 try {
535514 addresses = fromString (key );
536515 this .fallbackCache .put (s , addresses );
537- } catch (
538- UnknownHostException e2 ) {
516+ } catch (UnknownHostException e2 ) {
539517 this .sharedPreferences .edit ().remove (s .replace ('.' , '_' )).apply ();
540518 throw e ;
541519 }
0 commit comments