Esempio n. 1
0
        public override WebResourceResponse ShouldInterceptRequest(WebView view, IWebResourceRequest request)
        {
            try
            {
                Logger.Info($"Requested Url: {request.Url.ToString()}");
                var urlToFetch    = request.Url.ToString().Replace(Constants.BufferText, string.Empty);
                var isHttpRequest = request.Url.Scheme == "https";
                if (isHttpRequest && !urlToFetch.Contains("favicon"))
                {
                    var requestHeaders = request.RequestHeaders;

                    if (requestHeaders.ContainsKey("Range"))
                    {
                        var options = new WebFetchOptions
                        {
                            Range = RequestHelpers.RangeStringToArray(requestHeaders["Range"])
                        };

                        var task         = WebFetchService.FetchResourceAsync(urlToFetch, options);
                        var safeResponse = task.WaitAndUnwrapException();

                        var stream   = new MemoryStream(safeResponse.Data);
                        var response = new WebResourceResponse(safeResponse.MimeType, "UTF-8", stream);
                        response.SetStatusCodeAndReasonPhrase(206, "Partial Content");
                        response.ResponseHeaders = new Dictionary <string, string>
                        {
                            { "Accept-Ranges", "bytes" },
                            { "content-type", safeResponse.MimeType },
                            { "Content-Range", safeResponse.Headers["Content-Range"] },
                            { "Content-Length", safeResponse.Headers["Content-Length"] },
                        };
                        return(response);
                    }
                    else
                    {
                        var safeResponse = WebFetchService.FetchResourceAsync(urlToFetch).Result;
                        var stream       = new MemoryStream(safeResponse.Data);
                        var response     = new WebResourceResponse(safeResponse.MimeType, "UTF-8", stream);
                        return(response);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);

                if (ex.InnerException != null)
                {
                    using (var stream = new MemoryStream())
                    {
                        var urlToFetch = request.Url.ToString().Replace(Constants.BufferText, string.Empty);
                        var mimeType   = MimeTypeMap.Singleton.GetMimeTypeFromExtension(urlToFetch) ?? "text/html";
                        var response   = new WebResourceResponse(mimeType, "UTF-8", stream);
                        response.SetStatusCodeAndReasonPhrase(404, "Not Found");
                        return(response);
                    }
                }
            }
            return(base.ShouldInterceptRequest(view, request));
        }
Esempio n. 2
0
        /// <summary>
        /// Intercept request.
        /// </summary>
        /// <returns>The intercept request.</returns>
        /// <param name="view">View.</param>
        /// <param name="request">Request.</param>
        public override WebResourceResponse ShouldInterceptRequest(Android.Webkit.WebView view, IWebResourceRequest request)
        {
            string url  = request.Url.ToString();
            string ext  = MimeTypeMap.GetFileExtensionFromUrl(url);
            string mime = MimeTypeMap.Singleton.GetMimeTypeFromExtension(ext);

            if (url.ToLower().Contains("logoff"))
            {
                Xamarin.Forms.Device.BeginInvokeOnMainThread(App.Instance.Logout);
            }
            else if (request.Url.ToString().Contains("geofenceAutoCreate"))
            {
            }
            else if (mime != null)
            {
                HttpURLConnection conn = (HttpURLConnection) new URL(url).OpenConnection();
                foreach (var i in request.RequestHeaders)
                {
                    conn.SetRequestProperty(i.Key, i.Value);
                }
                var webResourceResponse = new WebResourceResponse(mime, "UTF-8", conn.InputStream);
                return(webResourceResponse);
            }

            BrowserPage.CheckWebSession();
            return(base.ShouldInterceptRequest(view, request));
        }
            public override WebResourceResponse ShouldInterceptRequest(WebView view, string url)
            {
                var postData = GetNavigationData(url);

                if (postData != null)
                {
                    if (postData.PostData != null)
                    {
                        try
                        {
                            var request = HttpWebRequest.Create(url);
                            //request.ContentType = "text/plain"; //TODO: What should we set here?
                            request.Method = "POST";

                            if (postData.AdditionalHeaders != null)
                            {
                                foreach (var additionalHeader in postData.AdditionalHeaders)
                                {
                                    request.Headers.Add(additionalHeader.Key, additionalHeader.Value);
                                }
                            }

                            using (var reqestStream = request.GetRequestStream())
                            {
                                reqestStream.Write(postData.PostData, 0, postData.PostData.Length);
                                reqestStream.Flush();
                            }

                            //using ( //TODO: Don't we need using directive here?
                            var response = request.GetResponse() as HttpWebResponse;
                            //)
                            {
                                if (response.StatusCode != HttpStatusCode.OK)
                                {
                                    //TODO: do we need to rise NavigationFailed event here?
                                }

                                var res = new WebResourceResponse(response.ContentType, response.ContentEncoding, response.GetResponseStream());
                                return(res);
                            }
                        }
                        catch (InvalidOperationException ioex)
                        {
                            var e = new NavigationFailedEventArgs(
                                new Uri(url),
                                ioex // Do we need to set this exception? WP8 WebView always set it to null
                                );
                            _Parent.OnNavigationFailed(e);

                            if (e.Handled)
                            {
                                return(GetCancelWebResourceResponse());
                            }
                        }
                    }
                }

                return(null); //base.ShouldInterceptRequest(view, url);
            }
Esempio n. 4
0
        private void CommonInit()
        {
            _responseHeaders = new Dictionary <string, string>();
            _data            = new MemoryStream();
            _response        = new WebResourceResponse("text/plain", "UTF8", _data);

            //Adding a default value
            UpdateReasonAndStatusCode();
        }
Esempio n. 5
0
        public override void OnReceivedHttpError(Android.Webkit.WebView view, IWebResourceRequest request,
                                                 WebResourceResponse errorResponse)
        {
            if (Reference == null || !Reference.TryGetTarget(out FormsWebViewRenderer renderer))
            {
                return;
            }
            if (renderer.Element == null)
            {
                return;
            }

            renderer.Element.HandleNavigationError(errorResponse.StatusCode);
            renderer.Element.HandleNavigationCompleted(request.Url.ToString());
            renderer.Element.Navigating = false;
        }
Esempio n. 6
0
        private bool TryIntercept(HybridWebViewRenderer renderer, string url, out WebResourceResponse response)
        {
            try
            {
                var res = renderer.Element.ShouldInterceptRequest(url);
                if (res != null)
                {
                    if (string.IsNullOrEmpty(res.MimeType))
                    {
                        var ext = url.Split('.').LastOrDefault();
                        if (!string.IsNullOrEmpty(ext))
                        {
                            res.MimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension(ext);
                        }
                    }
                    response = new WebResourceResponse(res.MimeType, res.Encoding, res.Contents);

                    try
                    {
                        if (response.ResponseHeaders == null)
                        {
                            response.ResponseHeaders = new ConcurrentDictionary <string, string>();
                        }
                        response.ResponseHeaders.Add("Cache-Control", "no-cache, no-store, must-revalidate");
                        response.ResponseHeaders.Add("Pragma", "no-cache");
                        response.ResponseHeaders.Add("Expires", "0");
                    }catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine("Older Android; cache headers couldn't be set.");
                    }

                    return(true);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }

            response = null;
            return(false);
        }
Esempio n. 7
0
        public override WebResourceResponse ShouldInterceptRequest(WebView view, IWebResourceRequest request)
        {
            try
            {
                OnPageEventShouldInterceptRequest?.Invoke(view, request);
                if (Settings.UseCachResourceLoad)
                {
                    WebResourceResponse respond = LoadFromAssetCach(request.Url);
                    if (respond != null)
                    {
                        return(respond);
                    }
                }

                return(base.ShouldInterceptRequest(view, request));
            }
            catch (Exception e)
            {
                Crashes.TrackError(e);
                return(base.ShouldInterceptRequest(view, request));
            }
        }
Esempio n. 8
0
 public override WebResourceResponse ShouldInterceptRequest(WebView view, string url)
 {
     IsInWebClientFrame = true;
     try {
         WebResourceResponse result = null;
         if (BaseClient != null)
         {
             result = BaseClient.ShouldInterceptRequest(view, url);
         }
         if (result == null)
         {
             // See if any of our cached resources match..
             var cached = Cache.GetCached(url);
             if (cached != null)
             {
                 result = new WebResourceResponse(cached.MimeType, "UTF-8", cached.DataSource());
             }
         }
         return(result ?? base.ShouldInterceptRequest(view, url));
     } finally {
         IsInWebClientFrame = false;
     }
 }
Esempio n. 9
0
        public override void OnReceivedHttpError(Android.Webkit.WebView view, IWebResourceRequest request, WebResourceResponse errorResponse)
        {
            base.OnReceivedHttpError(view, request, errorResponse);
            var pageInstance = ((AnnexioWebAppPage)App.Current.MainPage);

            //Set firstLoad to true to enable loader again
            firstLoad = true;

            //Switch the view to display the error (message override optional)
            if (pageInstance.IsHeaderRequired(request.Url.ToString()))
            {
                ((AnnexioWebAppPage)App.Current.MainPage).DisplayErrorMessage(errorResponse.StatusCode == (int)HttpStatusCode.RequestTimeout
                                                                              ? "Request to server timed out"
                                                                                                                                                                       : null);
            }
        }
Esempio n. 10
0
 public override void OnReceivedHttpError(Android.Webkit.WebView view, IWebResourceRequest request, WebResourceResponse errorResponse)
 {
     base.OnReceivedHttpError(view, request, errorResponse);
     _taskCompletionSource.SetResult(new ToFileResult(true, errorResponse.ReasonPhrase));
 }
Esempio n. 11
0
 public override void OnReceivedHttpError(WebView view, IWebResourceRequest request, WebResourceResponse errorResponse)
 {
     try
     {
         base.OnReceivedHttpError(view, request, errorResponse);
         OnPageEventReceivedHttpError?.Invoke(view, request, errorResponse);
     }
     catch (Exception e)
     {
         Crashes.TrackError(e);
     }
 }
Esempio n. 12
0
 public override void OnReceivedHttpError(Android.Webkit.WebView view, IWebResourceRequest request, WebResourceResponse errorResponse)
 {
     Element.InvokeEvent(WebViewEventType.NavigationError, new NavigationErrorDelegate(Element, errorResponse.StatusCode));
 }
Esempio n. 13
0
 public override void OnReceivedHttpError(Android.Webkit.WebView view, IWebResourceRequest request, WebResourceResponse errorResponse)
 {
     base.OnReceivedHttpError(view, request, errorResponse);
     pDFToHtml.Status = PDFEnum.Failed;
 }
Esempio n. 14
0
            public override WebResourceResponse ShouldInterceptRequest(WebView view, string url)
            {
                var postData = GetNavigationData(url);

                if (postData != null)
                {
                    if (postData.PostData != null)
                    {
                    try
                    {
                        var request = HttpWebRequest.Create(url);
                        //request.ContentType = "text/plain"; //TODO: What should we set here?
                        request.Method = "POST";

                        if (postData.AdditionalHeaders != null)
                        {
                            foreach (var additionalHeader in postData.AdditionalHeaders)
                            {
                                request.Headers.Add(additionalHeader.Key, additionalHeader.Value);
                            }
                        }

                        using (var reqestStream = request.GetRequestStream())
                        {
                            reqestStream.Write(postData.PostData, 0, postData.PostData.Length);
                            reqestStream.Flush();
                        }

                        //using ( //TODO: Don't we need using directive here?
                        var response = request.GetResponse() as HttpWebResponse;
                        //)
                        {
                            if (response.StatusCode != HttpStatusCode.OK)
                            {
                                //TODO: do we need to rise NavigationFailed event here?
                            }

                            var res = new WebResourceResponse(response.ContentType, response.ContentEncoding, response.GetResponseStream());
                            return res;
                        }
                    }
                    catch (InvalidOperationException ioex)
                    {
                        var e = new NavigationFailedEventArgs(
                            new Uri(url),
                            ioex // Do we need to set this exception? WP8 WebView always set it to null
                            );
                        _Parent.OnNavigationFailed(e);

                        if (e.Handled)
                        {
                            return GetCancelWebResourceResponse();
                        }
                    }
                }
                }

                return null; //base.ShouldInterceptRequest(view, url);
            }
Esempio n. 15
0
 public override void OnReceivedHttpError(Android.Webkit.WebView view, IWebResourceRequest request, WebResourceResponse errorResponse)
 {
     base.OnReceivedHttpError(view, request, errorResponse);
 }
Esempio n. 16
0
 public override void OnReceivedHttpError(WebView view, IWebResourceRequest request, WebResourceResponse errorResponse)
 {
     if (request.IsForMainFrame)
     {
         wvp.ShowErrorPage(errorResponse.ReasonPhrase, new Uri(request.Url.ToString()));
     }
 }
            public override void OnReceivedHttpError(WebView view, IWebResourceRequest request, WebResourceResponse errorResponse)
            {
                var error = "HTTP error " + errorResponse.StatusCode + " " + errorResponse.ReasonPhrase;

                base.OnReceivedHttpError(view, request, errorResponse);
                System.Diagnostics.Debug.WriteLine("OnReceivedHttpError [" + error + "] for request " + request.Url.ToString());
                if (request.Url.ToString() == Renderer?.HomeUrl)
                {
                    Renderer?.RequestLoadFailed(request.Url.ToString(), error);
                }
            }
Esempio n. 18
0
 public override void OnReceivedHttpError(Android.Webkit.WebView view, IWebResourceRequest request, WebResourceResponse errorResponse)
 {
     base.OnReceivedHttpError(view, request, errorResponse);
     CustomWebView.LoadFailed(this, new EventArgs());
 }
Esempio n. 19
0
        public override void OnReceivedHttpError(Android.Webkit.WebView view, IWebResourceRequest request, WebResourceResponse errorResponse)
        {
            if (Reference == null || !Reference.TryGetTarget(out FormsWebViewRenderer renderer))
            {
                return;
            }
            if (renderer.Element == null || (view as WebViewEx).Disposed)
            {
                return;
            }

            if (!request.IsForMainFrame || request.Url.ToString() != renderer.Control.Url.ToString())
            {
                base.OnReceivedHttpError(view, request, errorResponse);
                return;
            }

            renderer.Element.HandleNavigationError(errorResponse.StatusCode);
            renderer.Element.HandleNavigationCompleted(request.Url.ToString());
            renderer.Element.Navigating = false;
        }
 public override void OnReceivedHttpError(WebView view, IWebResourceRequest request, WebResourceResponse errorResponse)
 {
     base.OnReceivedHttpError(view, request, errorResponse);
     ErroTeste?.Invoke(this, 404);
 }
 public override void OnReceivedHttpError(Android.Webkit.WebView view, IWebResourceRequest request, WebResourceResponse errorResponse)
 {
     base.OnReceivedHttpError(view, request, errorResponse);
     Android.Util.Log.Error(logTag, $"OnReceivedError: {errorResponse.ReasonPhrase}, Url:{request.Url.EncodedSchemeSpecificPart}");
 }