Esempio n. 1
0
        public static async void RegisterTestResources(IWebBrowser browser)
        {
            var handler = browser.ResourceRequestHandlerFactory as ResourceRequestHandlerFactory;

            if (handler != null)
            {
                const string renderProcessCrashedBody = "<html><body><h1>Render Process Crashed</h1><p>Your seeing this message as the render process has crashed</p></body></html>";
                handler.RegisterHandler(RenderProcessCrashedUrl, ResourceHandler.GetByteArray(renderProcessCrashedBody, Encoding.UTF8));

                const string responseBody = "<html><body><h1>Success</h1><p>This document is loaded from a System.IO.Stream</p></body></html>";
                handler.RegisterHandler(TestResourceUrl, ResourceHandler.GetByteArray(responseBody, Encoding.UTF8));

                const string unicodeResponseBody = "<html><body>整体满意度</body></html>";
                handler.RegisterHandler(TestUnicodeResourceUrl, ResourceHandler.GetByteArray(unicodeResponseBody, Encoding.UTF8));

                if (string.IsNullOrEmpty(PluginInformation))
                {
                    var pluginBody = new StringBuilder();
                    pluginBody.Append("<html><body><h1>Plugins</h1><table>");
                    pluginBody.Append("<tr>");
                    pluginBody.Append("<th>Name</th>");
                    pluginBody.Append("<th>Description</th>");
                    pluginBody.Append("<th>Version</th>");
                    pluginBody.Append("<th>Path</th>");
                    pluginBody.Append("</tr>");

                    var plugins = await Cef.GetPlugins();

                    if (plugins.Count == 0)
                    {
                        pluginBody.Append("<tr>");
                        pluginBody.Append("<td colspan='4'>Cef.GetPlugins returned an empty list - likely no plugins were loaded on your system</td>");
                        pluginBody.Append("</tr>");
                        pluginBody.Append("<tr>");
                        pluginBody.Append("<td colspan='4'>You may find that NPAPI/PPAPI need to be enabled</td>");
                        pluginBody.Append("</tr>");
                    }
                    else
                    {
                        foreach (var plugin in plugins)
                        {
                            pluginBody.Append("<tr>");
                            pluginBody.Append("<td>" + plugin.Name + "</td>");
                            pluginBody.Append("<td>" + plugin.Description + "</td>");
                            pluginBody.Append("<td>" + plugin.Version + "</td>");
                            pluginBody.Append("<td>" + plugin.Path + "</td>");
                            pluginBody.Append("</tr>");
                        }
                    }

                    pluginBody.Append("</table></body></html>");

                    PluginInformation = pluginBody.ToString();
                }

                handler.RegisterHandler(PluginsTestUrl, ResourceHandler.GetByteArray(PluginInformation, Encoding.UTF8));
            }
        }
Esempio n. 2
0
        public static async void RegisterTestResources(IWebBrowser browser)
        {
            if (browser.ResourceRequestHandlerFactory == null)
            {
                browser.ResourceRequestHandlerFactory = new ResourceRequestHandlerFactory();
            }

            var handler = browser.ResourceRequestHandlerFactory as ResourceRequestHandlerFactory;

            if (handler != null)
            {
                const string renderProcessCrashedBody = "<html><body><h1>Render Process Crashed</h1><p>Your seeing this message as the render process has crashed</p></body></html>";
                handler.RegisterHandler(RenderProcessCrashedUrl, ResourceHandler.GetByteArray(renderProcessCrashedBody, Encoding.UTF8));

                const string responseBody = "<html><body><h1>Success</h1><p>This document is loaded from a System.IO.Stream</p></body></html>";
                handler.RegisterHandler(TestResourceUrl, ResourceHandler.GetByteArray(responseBody, Encoding.UTF8));

                const string unicodeResponseBody = "<html><body>整体满意度</body></html>";
                handler.RegisterHandler(TestUnicodeResourceUrl, ResourceHandler.GetByteArray(unicodeResponseBody, Encoding.UTF8));
            }
        }
Esempio n. 3
0
        public IResourceHandler Create(IBrowser browser, IFrame frame, string schemeName, IRequest request)
        {
            //Note:
            // - Avoid doing lots of processing in this method as it will affect performance.

            string url = request.Url;

            Tools.Logger.Log("Requested uri: " + url, Tools.Logger.LogLevel.debug);
            url = url.ToLower();

            Uri uri = null;

            Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out uri);

            // handle utf8 exchanges
            foreach (var utf8ex in _utf8_exchanges)
            {
                if (utf8ex.Item1.Match(url))
                {
                    var rh = new ResourceHandler(stream: new MemoryStream(ResourceHandler.GetByteArray(utf8ex.Item2, Encoding.UTF8, true)), autoDisposeStream: true);
                    // necessary to enable cross scheme fetches
                    if (!rh.Headers.AllKeys.Contains("Access-Control-Allow-Origin"))
                    {
                        rh.Headers.Add("Access-Control-Allow-Origin", "*");
                    }
                    return(rh);
                }
            }

            // handle js manipulations
            foreach (var js_rewrite in _js_Rewrites)
            {
                if (js_rewrite.Item1.Match(url))
                {
                    string rewriteScript = "";
                    rewriteScript += "var url = '" + url + "';";
                    rewriteScript += "var method = '" + request.Method.ToLower() + "';\n";
                    rewriteScript += js_rewrite.Item2;

                    var    jsInstance = Tools.JSEngine.Instance;
                    string newText    = jsInstance.ExecuteResult(rewriteScript, " for parameter 'exchange-response-utf8_script': ");
                    var    rh         = new ResourceHandler(stream: new MemoryStream(ResourceHandler.GetByteArray(newText, Encoding.UTF8, true)), autoDisposeStream: true);
                    // necessary to enable cross scheme fetches
                    if (!rh.Headers.AllKeys.Contains("Access-Control-Allow-Origin"))
                    {
                        rh.Headers.Add("Access-Control-Allow-Origin", "*");
                    }

                    return(rh);
                }
            }

            // handle internal ressources / files
            if (uri != null && uri.Scheme.ToLower() == "scchrom")
            {
                MemoryStream content  = null;
                string       mimeType = "text/html";

                if (uri.Host.ToLower() == "internal")
                {
                    if (Tools.Arguments.GetArgument("allow-internal_resource", "true") == "false")
                    {
                        Tools.Logger.Log("Refused request due to allow-internal_resource being false, url: " + uri.ToString(), Tools.Logger.LogLevel.info);
                        return(new ResourceHandler());
                    }

                    string resourceName = uri.LocalPath.Replace("/", "");
                    var    res          = getRessource(resourceName);
                    if (res != null)
                    {
                        mimeType = res.Item1;
                        content  = res.Item2;
                    }
                    else
                    {
                        Tools.Logger.Log("Failed to load resource for uri " + uri.ToString(), Tools.Logger.LogLevel.error);
                        return(new ResourceHandler());
                    }
                }

                if (uri.Host.ToLower() == "file")
                {
                    if (Tools.Arguments.GetArgument("allow-file_resource", "false") != "true")
                    {
                        Tools.Logger.Log("Refused request due to allow-file_resource not being true, url: " + uri.ToString(), Tools.Logger.LogLevel.info);
                        return(new ResourceHandler());
                    }

                    var res = getFile(uri.LocalPath);
                    if (res != null)
                    {
                        mimeType = res.Item1;
                        content  = res.Item2;
                    }
                    else
                    {
                        Tools.Logger.Log("Failed to load resource for uri " + uri.ToString(), Tools.Logger.LogLevel.error);
                        return(new ResourceHandler());
                    }
                }

                if (content == null)
                {
                    return(new ResourceHandler());
                }


                var rh = new ResourceHandler(mimeType, content, true);
                // necessary to enable cross scheme fetches
                if (!rh.Headers.AllKeys.Contains("Access-Control-Allow-Origin"))
                {
                    rh.Headers.Add("Access-Control-Allow-Origin", "*");
                }
                return(rh);
            }


            // check whitelist is set
            if (_whitelistedUrls != null && _whitelistedUrls.Count > 0)
            {
                bool isWhitelisted = false;

                foreach (var filter in _whitelistedUrls)
                {
                    if (filter.Match(url))
                    {
                        isWhitelisted = true;
                        break;
                    }
                }

                if (!isWhitelisted)
                {
                    Tools.Logger.Log("REJECTED uri: " + url);
                    return(new ResourceHandler());
                }
            }

            // just handle the request the default way
            return(null);
        }