22
33import android .os .Build ;
44import android .os .Handler ;
5+ import android .text .TextUtils ;
56import android .util .Log ;
67import android .webkit .WebResourceResponse ;
78
89import androidx .annotation .Nullable ;
910
10- import com .pichillilorenzo .flutter_inappwebview .webview .in_app_webview .InAppWebView ;
11- import com .pichillilorenzo .flutter_inappwebview .plugin_scripts_js .JavaScriptBridgeJS ;
1211import com .pichillilorenzo .flutter_inappwebview .Util ;
12+ import com .pichillilorenzo .flutter_inappwebview .plugin_scripts_js .JavaScriptBridgeJS ;
13+ import com .pichillilorenzo .flutter_inappwebview .types .WebResourceRequestExt ;
14+ import com .pichillilorenzo .flutter_inappwebview .webview .in_app_webview .InAppWebView ;
1315
1416import java .io .ByteArrayInputStream ;
1517import java .io .InputStream ;
18+ import java .net .HttpURLConnection ;
1619import java .net .MalformedURLException ;
1720import java .net .URI ;
1821import java .net .URISyntaxException ;
1922import java .net .URL ;
2023import java .util .ArrayList ;
24+ import java .util .HashMap ;
2125import java .util .List ;
26+ import java .util .Map ;
2227import java .util .concurrent .CopyOnWriteArrayList ;
2328import java .util .concurrent .CountDownLatch ;
2429import java .util .regex .Matcher ;
2530
2631import javax .net .ssl .SSLHandshakeException ;
2732
28- import okhttp3 .Request ;
29- import okhttp3 .Response ;
30-
3133public class ContentBlockerHandler {
3234 protected static final String LOG_TAG = "ContentBlockerHandler" ;
3335
@@ -48,10 +50,14 @@ public void setRuleList(List<ContentBlocker> newRuleList) {
4850 }
4951
5052 @ Nullable
51- public WebResourceResponse checkUrl (final InAppWebView webView , String url , ContentBlockerTriggerResourceType responseResourceType ) throws URISyntaxException , InterruptedException , MalformedURLException {
53+ public WebResourceResponse checkUrl (final InAppWebView webView , WebResourceRequestExt request ,
54+ ContentBlockerTriggerResourceType responseResourceType )
55+ throws URISyntaxException , InterruptedException , MalformedURLException {
5256 if (webView .customSettings .contentBlockers == null )
5357 return null ;
5458
59+ String url = request .getUrl ();
60+
5561 URI u ;
5662 try {
5763 u = new URI (url );
@@ -182,36 +188,85 @@ public void run() {
182188 if (scheme .equals ("http" ) && (port == -1 || port == 80 )) {
183189 String urlHttps = url .replace ("http://" , "https://" );
184190
185- Request mRequest = new Request .Builder ().url (urlHttps ).build ();
186- Response response = null ;
187-
188- try {
189- response = Util .getBasicOkHttpClient ().newCall (mRequest ).execute ();
190- byte [] dataBytes = response .body ().bytes ();
191- InputStream dataStream = new ByteArrayInputStream (dataBytes );
192-
193- String [] contentTypeSplitted = response .header ("content-type" , "text/plain" ).split (";" );
194-
195- String contentType = contentTypeSplitted [0 ].trim ();
196- String encoding = (contentTypeSplitted .length > 1 && contentTypeSplitted [1 ].contains ("charset=" ))
197- ? contentTypeSplitted [1 ].replace ("charset=" , "" ).trim ()
198- : "utf-8" ;
199-
200- response .body ().close ();
201- response .close ();
202-
203- return new WebResourceResponse (contentType , encoding , dataStream );
204-
205- } catch (Exception e ) {
206- if (response != null ) {
207- response .body ().close ();
208- response .close ();
209- }
210- if (!(e instanceof SSLHandshakeException )) {
211- e .printStackTrace ();
212- Log .e (LOG_TAG , e .getMessage ());
191+ HttpURLConnection urlConnection = Util .makeHttpRequest (urlHttps , request .getMethod (), request .getHeaders ());
192+ if (urlConnection != null ) {
193+ try {
194+ byte [] dataBytes = Util .readAllBytes (urlConnection .getInputStream ());
195+ if (dataBytes == null ) {
196+ return null ;
197+ }
198+ InputStream dataStream = new ByteArrayInputStream (dataBytes );
199+
200+ String encoding = urlConnection .getContentEncoding ();
201+ String contentType = urlConnection .getContentType ();
202+ if (contentType == null ) {
203+ contentType = "text/plain" ;
204+ } else {
205+ String [] contentTypeSplitted = contentType .split (";" );
206+ contentType = contentTypeSplitted [0 ].trim ();
207+ if (encoding == null ) {
208+ encoding = (contentTypeSplitted .length > 1 && contentTypeSplitted [1 ].contains ("charset=" ))
209+ ? contentTypeSplitted [1 ].replace ("charset=" , "" ).trim ()
210+ : "utf-8" ;
211+ }
212+ }
213+
214+ String reasonPhrase = urlConnection .getResponseMessage ();
215+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP && reasonPhrase != null ) {
216+ Map <String , String > responseHeaders = new HashMap <>();
217+ for (Map .Entry <String , List <String >> responseHeader : urlConnection .getHeaderFields ().entrySet ()) {
218+ responseHeaders .put (responseHeader .getKey (), TextUtils .join ("," , responseHeader .getValue ()));
219+ }
220+ return new WebResourceResponse (contentType ,
221+ encoding ,
222+ urlConnection .getResponseCode (),
223+ reasonPhrase ,
224+ responseHeaders ,
225+ dataStream );
226+ } else {
227+ return new WebResourceResponse (contentType ,
228+ encoding ,
229+ dataStream );
230+ }
231+ } catch (Exception e ) {
232+ if (!(e instanceof SSLHandshakeException )) {
233+ e .printStackTrace ();
234+ }
235+ } finally {
236+ urlConnection .disconnect ();
213237 }
214238 }
239+
240+ // Request mRequest = new Request.Builder().url(urlHttps).build();
241+ // Response response = null;
242+ //
243+ // try {
244+ // response = Util.getBasicOkHttpClient().newCall(mRequest).execute();
245+ // byte[] dataBytes = response.body().bytes();
246+ // InputStream dataStream = new ByteArrayInputStream(dataBytes);
247+ //
248+ // String[] contentTypeSplitted = response.header("content-type", "text/plain").split(";");
249+ //
250+ // String contentType = contentTypeSplitted[0].trim();
251+ // String encoding = (contentTypeSplitted.length > 1 && contentTypeSplitted[1].contains("charset="))
252+ // ? contentTypeSplitted[1].replace("charset=", "").trim()
253+ // : "utf-8";
254+ //
255+ // response.body().close();
256+ // response.close();
257+ //
258+ // return new WebResourceResponse(contentType, encoding, dataStream);
259+ //
260+ // } catch (Exception e) {
261+ // if (response != null) {
262+ // response.body().close();
263+ // response.close();
264+ // }
265+ // if (!(e instanceof SSLHandshakeException)) {
266+ // e.printStackTrace();
267+ // Log.e(LOG_TAG, e.getMessage());
268+ // }
269+ // }
215270 }
216271 break ;
217272 }
@@ -221,48 +276,36 @@ public void run() {
221276 }
222277
223278 @ Nullable
224- public WebResourceResponse checkUrl (final InAppWebView webView , String url ) throws URISyntaxException , InterruptedException , MalformedURLException {
225- ContentBlockerTriggerResourceType responseResourceType = getResourceTypeFromUrl (url );
226- return checkUrl (webView , url , responseResourceType );
279+ public WebResourceResponse checkUrl (final InAppWebView webView , WebResourceRequestExt request ) throws URISyntaxException , InterruptedException , MalformedURLException {
280+ ContentBlockerTriggerResourceType responseResourceType = getResourceTypeFromUrl (request );
281+ return checkUrl (webView , request , responseResourceType );
227282 }
228283
229284 @ Nullable
230- public WebResourceResponse checkUrl (final InAppWebView webView , String url , String contentType ) throws URISyntaxException , InterruptedException , MalformedURLException {
285+ public WebResourceResponse checkUrl (final InAppWebView webView , WebResourceRequestExt request , String contentType ) throws URISyntaxException , InterruptedException , MalformedURLException {
231286 ContentBlockerTriggerResourceType responseResourceType = getResourceTypeFromContentType (contentType );
232- return checkUrl (webView , url , responseResourceType );
287+ return checkUrl (webView , request , responseResourceType );
233288 }
234289
235- public ContentBlockerTriggerResourceType getResourceTypeFromUrl (String url ) {
290+ public ContentBlockerTriggerResourceType getResourceTypeFromUrl (WebResourceRequestExt request ) {
236291 ContentBlockerTriggerResourceType responseResourceType = ContentBlockerTriggerResourceType .RAW ;
292+ String url = request .getUrl ();
237293
238294 if (url .startsWith ("http://" ) || url .startsWith ("https://" )) {
239295 // make an HTTP "HEAD" request to the server for that URL. This will not return the full content of the URL.
240- Request mRequest = new Request .Builder ().url (url ).head ().build ();
241- Response response = null ;
242- try {
243- response = Util .getBasicOkHttpClient ().newCall (mRequest ).execute ();
244-
245- if (response .header ("content-type" ) != null ) {
246- String [] contentTypeSplitted = response .header ("content-type" ).split (";" );
247-
248- String contentType = contentTypeSplitted [0 ].trim ();
249- String encoding = (contentTypeSplitted .length > 1 && contentTypeSplitted [1 ].contains ("charset=" ))
250- ? contentTypeSplitted [1 ].replace ("charset=" , "" ).trim ()
251- : "utf-8" ;
252-
253- response .body ().close ();
254- response .close ();
255- responseResourceType = getResourceTypeFromContentType (contentType );
256- }
257-
258- } catch (Exception e ) {
259- if (response != null ) {
260- response .body ().close ();
261- response .close ();
262- }
263- if (!(e instanceof SSLHandshakeException )) {
296+ HttpURLConnection urlConnection = Util .makeHttpRequest (url , "HEAD" , request .getHeaders ());
297+ if (urlConnection != null ) {
298+ try {
299+ String contentType = urlConnection .getContentType ();
300+ if (contentType != null ) {
301+ String [] contentTypeSplitted = contentType .split (";" );
302+ contentType = contentTypeSplitted [0 ].trim ();
303+ responseResourceType = getResourceTypeFromContentType (contentType );
304+ }
305+ } catch (Exception e ) {
264306 e .printStackTrace ();
265- Log .e (LOG_TAG , e .getMessage ());
307+ } finally {
308+ urlConnection .disconnect ();
266309 }
267310 }
268311 }
0 commit comments