Пример #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.WebResources.ContainsKey(requestUrl))
                    {
                        browser.SetWebResource(requestUrl, webResource);
                    }
                }


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


            callback.Continue();
            e.SetReturnValue(true);
        }
Пример #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 = string.Format(".{0}", 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(string.Format("[加载]:\t{0}\t->\t{1}", requestUrl, fileName));
            }
            else
            {
                Console.WriteLine(string.Format("[未找到]:\t{0}", requestUrl));
            }

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