Esempio n. 1
0
        private void EmbeddedResourceHandler_ProcessRequest(object sender, Chromium.Event.CfxProcessRequestEventArgs e)
        {
            readResponseStreamOffset = 0;
            var request  = e.Request;
            var callback = e.Callback;

            var uri = new Uri(request.Url);

            requestUrl = request.Url;

            var fileName = string.Format("{0}{1}", uri.Authority, uri.AbsolutePath);

            requestFile = uri.AbsolutePath;

            var ass          = resourceAssembly;
            var resourcePath = string.Format("{0}.{1}", ass.GetName().Name, fileName.Replace('/', '.'));
            var resourceName = ass.GetManifestResourceNames().SingleOrDefault(p => p.Equals(resourcePath, StringComparison.CurrentCultureIgnoreCase));

            if (!string.IsNullOrEmpty(resourceName) && ass.GetManifestResourceInfo(resourceName) != null)
            {
                using (var reader = new System.IO.BinaryReader(ass.GetManifestResourceStream(resourceName)))
                {
                    var buff = reader.ReadBytes((int)reader.BaseStream.Length);

                    webResource = new WebResource(buff, MimeHelper.GetMimeType(System.IO.Path.GetExtension(fileName)));

                    reader.Close();

                    if (!browser.WebResource.ContainsKey(requestUrl))
                    {
                        browser.SetWebResource(requestUrl, webResource);
                    }
                }


                Console.WriteLine($"[加载]:\t{requestUrl}");
            }
            else
            {
                Console.WriteLine($"[未找到]:\t{requestUrl}");
            }


            callback.Continue();
            e.SetReturnValue(true);
        }
Esempio n. 2
0
        private void LocalResourceHandler_ProcessRequest(object sender, Chromium.Event.CfxProcessRequestEventArgs e)
        {
            readResponseStreamOffset = 0;
            var request  = e.Request;
            var callback = e.Callback;

            var uri = new Uri(request.Url);

            requestUrl = request.Url;
            var localPath = uri.LocalPath;

            if (localPath.StartsWith("/"))
            {
                localPath = $".{localPath}";
            }

            var fileName = System.IO.Path.GetFullPath(localPath);


            requestFile = request.Url;


            if (System.IO.File.Exists(fileName))
            {
                using (var stream = System.IO.File.OpenRead(fileName))
                    using (var reader = new System.IO.BinaryReader(stream))
                    {
                        var buff = reader.ReadBytes((int)reader.BaseStream.Length);
                        webResource = new WebResource(buff, MimeHelper.GetMimeType(System.IO.Path.GetExtension(fileName)));

                        reader.Close();
                        stream.Close();
                    }

                Console.WriteLine($"[加载]:\t{requestUrl}\t->\t{fileName}");
            }
            else
            {
                Console.WriteLine($"[未找到]:\t{requestUrl}");
            }

            callback.Continue();
            e.SetReturnValue(true);
        }