예제 #1
0
        /// <summary>
        /// Create an instance of FileSystemResourceHandler.
        /// </summary>
        /// <param name="browser">The browser window that originated the request or null if the request did not originate from a browser window (for example, if the request came from CefURLRequest).</param>
        /// <param name="frame">Frame that originated the request or null if the request did not originate from a browser window (for example, if the request came from CefURLRequest).</param>
        /// <param name="schemeName">The scheme name</param>
        /// <param name="request">The request. (will not contain cookie data)</param>
        /// <returns>Return a new FileSystemResourceHandler instance to handle the request or an empty reference to allow default handling of the request.</returns>
        public IResourceHandler Create(IBrowser browser, IFrame frame, string schemeName, IRequest request)
        {
            // Only handle fs scheme.
            if (string.Compare(schemeName, SchemeName, true) != 0)
            {
                return(ResourceHandler.ForErrorMessage($"Invalid scheme [{schemeName}].", HttpStatusCode.BadRequest));
            }

            var uri  = new Uri(request.Url);
            var root = uri.Authority;

            // If the root of the path is a system volume then add the volume separator ":"
            // else it will be consider as just another directory.
            if (_volumes.Any(v => v.StartsWith(root)))
            {
                root = root + Path.VolumeSeparatorChar;
            }
            var filepath = WebUtility.UrlDecode(root + uri.AbsolutePath);

            if (File.Exists(filepath))
            {
                return(ResourceHandler.FromFilePath(filepath, ResourceHandler.GetMimeType(Path.GetExtension(filepath))));
            }

            return(ResourceHandler.ForErrorMessage("File not found.", HttpStatusCode.NotFound));
        }
예제 #2
0
        /// <summary>
        /// If the file requested is within the rootFolder then a IResourceHandler reference to the file requested will be returned
        /// otherwise a 404 ResourceHandler will be returned.
        /// </summary>
        /// <param name="browser">the browser window that originated the
        /// request or null if the request did not originate from a browser window
        /// (for example, if the request came from CefURLRequest).</param>
        /// <param name="frame">frame that originated the request
        /// or null if the request did not originate from a browser window
        /// (for example, if the request came from CefURLRequest).</param>
        /// <param name="schemeName">the scheme name</param>
        /// <param name="request">The request. (will not contain cookie data)</param>
        /// <returns>
        /// A IResourceHandler
        /// </returns>
        IResourceHandler ISchemeHandlerFactory.Create(IBrowser browser, IFrame frame, string schemeName, IRequest request)
        {
            if (this.schemeName != null && !schemeName.Equals(this.schemeName, StringComparison.OrdinalIgnoreCase))
            {
                return(ResourceHandler.ForErrorMessage(string.Format("SchemeName {0} does not match the expected SchemeName of {1}.", schemeName, this.schemeName), HttpStatusCode.NotFound));
            }

            var uri = new Uri(request.Url);

            if (this.hostName != null && !uri.Host.Equals(this.hostName, StringComparison.OrdinalIgnoreCase))
            {
                return(ResourceHandler.ForErrorMessage(string.Format("HostName {0} does not match the expected HostName of {1}.", uri.Host, this.hostName), HttpStatusCode.NotFound));
            }

            //Get the absolute path and remove the leading slash
            var asbolutePath = uri.AbsolutePath.Substring(1);

            if (string.IsNullOrEmpty(asbolutePath))
            {
                asbolutePath = defaultPage;
            }

            var filePath = WebUtility.UrlDecode(Path.GetFullPath(Path.Combine(rootFolder, asbolutePath)));

            //Check the file requested is within the specified path and that the file exists
            if (filePath.StartsWith(rootFolder, StringComparison.OrdinalIgnoreCase) && File.Exists(filePath))
            {
                var fileExtension = Path.GetExtension(filePath);
                var mimeType      = ResourceHandler.GetMimeType(fileExtension);
                var stream        = File.OpenRead(filePath);
                return(ResourceHandler.FromStream(stream, mimeType));
            }

            return(ResourceHandler.ForErrorMessage("File Not Found - " + filePath, HttpStatusCode.NotFound));
        }
        public IResourceHandler Create(IBrowser browser, IFrame frame, string schemeName, IRequest request)
        {
            if (schemeName == "hs")
            {
                var baseDir = Environment.CurrentDirectory + "\\plugins\\ui\\html\\";
                var str     = request.Url.Replace("hs://res/", "").Split('?')[0];


                var filePath = str;

                try
                {
                    var mimetype = "";

                    switch (str.Split('.').Last())
                    {
                    case "html":
                    {
                        mimetype = "text/html";
                        break;
                    }

                    case "css":
                    {
                        mimetype = "text/css";
                        break;
                    }

                    case "js":
                    {
                        mimetype = "text/javascript";
                        break;
                    }

                    default:
                    {
                        mimetype = "application/octet-stream";
                        break;
                    }
                    }

                    //return ResourceHandler.FromFilePath(filePath, mimetype);

                    var bundleName = str.Split('/').First();

                    return(ResourceHandler.FromStream(
                               UIPlugin.PluginManager.GetResourceManager().GetFileStream(bundleName, filePath.Replace(bundleName + "/", "")), mimetype));
                }
                catch (FileNotFoundException e)
                {
                    _logger.Warn(e);
                    return(ResourceHandler.ForErrorMessage("File not found.", HttpStatusCode.NotFound));
                }

                //return ResourceHandler.ForErrorMessage("File not found.", HttpStatusCode.NotFound);
            }

            return(new ResourceHandler());
        }
예제 #4
0
        /// <summary>
        /// Create an instance of FileSystemResourceHandler.
        /// </summary>
        /// <param name="browser">The browser window that originated the request or null if the request did not originate from a browser window (for example, if the request came from CefURLRequest).</param>
        /// <param name="frame">Frame that originated the request or null if the request did not originate from a browser window (for example, if the request came from CefURLRequest).</param>
        /// <param name="schemeName">The scheme name</param>
        /// <param name="request">The request. (will not contain cookie data)</param>
        /// <returns>Return a new FileSystemResourceHandler instance to handle the request or an empty reference to allow default handling of the request.</returns>
        public IResourceHandler Create(IBrowser browser, IFrame frame, string schemeName, IRequest request)
        {
            // Only handle fs scheme.
            if (string.Compare(schemeName, SchemeName, true, CultureInfo.InvariantCulture) != 0)
            {
                return(ResourceHandler.ForErrorMessage($"Invalid scheme [{schemeName}].", HttpStatusCode.BadRequest));
            }

            var uri  = new Uri(request.Url);
            var root = uri.Authority;

            // If the root of the path is a system volume then add the volume separator ":"
            // else it will be consider as just another directory.
            if (_volumes.Any(v => v.StartsWith(root, StringComparison.InvariantCultureIgnoreCase)))
            {
                root = root + Path.VolumeSeparatorChar;
            }
            var filepath = root + Uri.UnescapeDataString(uri.AbsolutePath);

            if (File.Exists(filepath))
            {
                // Read file and then copy to a separate memory stream so the file isn't locked.
                using (var stream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
#pragma warning disable CA2000 // Dispose objects before losing scope
                    // Don't dispose the stream here, ResourceHandler.FromStream autoDisposeStream is set to false by default.
                    // The comment indicates "you will only be able to serve one request" ¯\_(ツ)_/¯
                    var ms = new MemoryStream();
#pragma warning restore CA2000 // Dispose objects before losing scope

                    stream.CopyTo(ms);
                    ms.Seek(0, SeekOrigin.Begin);

                    return(ResourceHandler.FromStream(ms, Cef.GetMimeType(Path.GetExtension(filepath)), false));
                }
            }

            return(ResourceHandler.ForErrorMessage("File not found.", HttpStatusCode.NotFound));
        }
예제 #5
0
            protected override IResourceHandler GetResourceHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request)
            {
                if (request.Url.StartsWith("http://epub.zyfdroid.com/static"))
                {
                    String fileName = request.Url.Replace("http://epub.zyfdroid.com/static", Program.staticPath);
                    if (!File.Exists(fileName))
                    {
                        return(ResourceHandler.ForErrorMessage("404 Not Found", System.Net.HttpStatusCode.NotFound));
                    }
                    return(ResourceHandler.FromFilePath(fileName, Cef.GetMimeType(Path.GetExtension(fileName).Replace(".", "").ToLower())));
                }
                if (request.Url.StartsWith("http://epub.zyfdroid.com/book"))
                {
                    String fileName = request.Url.Replace("http://epub.zyfdroid.com/book", Program.openingPath);
                    if (!File.Exists(fileName))
                    {
                        return(ResourceHandler.ForErrorMessage("404 Not Found", System.Net.HttpStatusCode.NotFound));
                    }
                    return(ResourceHandler.FromFilePath(fileName, Cef.GetMimeType(Path.GetExtension(fileName).Replace(".", "").ToLower())));
                }

                return(base.GetResourceHandler(chromiumWebBrowser, browser, frame, request));
            }
        protected override IResourceHandler GetResourceHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request)
        {
            string url = request.Url.Substring("http://HuiDesktop/".Length);

            if (url.Contains('?'))
            {
                url = url.Substring(0, url.IndexOf('?'));
            }
            if (url.Contains('#'))
            {
                url = url.Substring(0, url.IndexOf('#'));
            }
            if (fileIndex.ContainsKey(url) == false)
            {
                return(ResourceHandler.ForErrorMessage("", System.Net.HttpStatusCode.NotFound));
            }
            var ms = fileIndex[url];

            lock (ms)
            {
                ms.Position = 0;
                return(ResourceHandler.FromStream(ms, FileContentType.GetMimeType(url.Substring(url.LastIndexOf('.')))));
            }
        }
        /// <summary>
        /// If the file requested is within the rootFolder then a IResourceHandler reference to the file requested will be returned
        /// otherwise a 404 ResourceHandler will be returned.
        /// </summary>
        /// <param name="browser">the browser window that originated the
        /// request or null if the request did not originate from a browser window
        /// (for example, if the request came from CefURLRequest).</param>
        /// <param name="frame">frame that originated the request
        /// or null if the request did not originate from a browser window
        /// (for example, if the request came from CefURLRequest).</param>
        /// <param name="schemeName">the scheme name</param>
        /// <param name="request">The request. (will not contain cookie data)</param>
        /// <returns>
        /// A IResourceHandler
        /// </returns>
        protected virtual IResourceHandler Create(IBrowser browser, IFrame frame, string schemeName, IRequest request)
        {
            if (this.schemeName != null && !schemeName.Equals(this.schemeName, StringComparison.OrdinalIgnoreCase))
            {
                return(ResourceHandler.ForErrorMessage(string.Format("SchemeName {0} does not match the expected SchemeName of {1}.", schemeName, this.schemeName), HttpStatusCode.NotFound));
            }

            var uri = new Uri(request.Url);

            if (this.hostName != null && !uri.Host.Equals(this.hostName, StringComparison.OrdinalIgnoreCase))
            {
                return(ResourceHandler.ForErrorMessage(string.Format("HostName {0} does not match the expected HostName of {1}.", uri.Host, this.hostName), HttpStatusCode.NotFound));
            }

            //Get the absolute path and remove the leading slash
            var            asbolutePath = uri.AbsolutePath.Substring(1);
            ResourceLoader loader       = null;

            //Search for ResourceLoader ID #
            int separator = asbolutePath.IndexOf("/");
            int id;

            if ((separator > -1) && int.TryParse(asbolutePath.Substring(0, separator), out id))
            {
                InteractiveCharts.ResourceLoaders.TryGetValue(id, out loader);
                asbolutePath = asbolutePath.Substring(separator + 1); //Adjust uri to only contain the file name now
            }

            // Get file properties
            string fileExtension = Path.GetExtension(asbolutePath);
            var    mimeType      = GetMimeTypeDelegate(fileExtension);
            Stream stream        = null;

            // Check for resources that are loaded by the ResourceLoader
            if (loader != null)
            {
                if (asbolutePath == "config.js")
                {
                    stream = loader.LoadConfig();
                }
                else if (asbolutePath == "data.json")
                {
                    stream = loader.LoadData();
                }
            }

            // If no stream has been loaded yet, attempt to load from an internal assembly resource.
            if (stream == null)
            {
                try {
                    var assembly = Assembly.GetExecutingAssembly();
                    stream = assembly.GetManifestResourceStream("InteractiveCharts.Resources." + asbolutePath.Replace('/', '.'));
                } catch (Exception) {
                    stream = null;
                }
            }

            if (stream != null)
            {
                return(ResourceHandler.FromStream(stream, mimeType));
            }
            else
            {
                return(ResourceHandler.ForErrorMessage("Resource Not Found - " + uri.AbsolutePath, HttpStatusCode.NotFound));
            }
        }