コード例 #1
0
        protected override bool ProcessRequest(CefRequest request, CefSchemeHandlerCallback callback)
        {
            var urlString = request.GetURL();

            string errorMessage = null;
            int errorStatus = 0;
            string errorStatusText = null;

            try
            {
                var uri = new Uri(urlString);
                var path = uri.Host + uri.AbsolutePath; // ignore host

                var asm = typeof(ClientSchemeHandler).Assembly;
                var resPrefix = "CefGlue.Client.Resources.";

                // convert path to resource name
                var parts = path.Split('/');
                for (var i = 0; i < parts.Length-1; i++)
                {
                    var filename = Path.GetFileNameWithoutExtension(parts[i]);
                    var extension = Path.GetExtension(parts[i]);

                    parts[i] = filename.Replace(".", "._").Replace('-', '_') + extension;
                }

                var resName = resPrefix + string.Join(".", parts);
                this.stream = asm.GetManifestResourceStream(resName);

                if (this.stream != null)
                {
                    // found
                    this.responseLength = -1;
                    this.status = 200;
                    this.statusText = "OK";
                    this.mimeType = GetMimeTypeFromUriSuffix(path);
                    callback.HeadersAvailable();
                    return true;
                }
            }
            catch (Exception ex)
            {
                errorStatus = 500;
                errorStatusText = "Internal Error";
                errorMessage = "<!doctype html><html><body><h1>Internal Error!</h1><pre>" + ex.ToString() + "</pre></body></html>";
            }

            // not found or error while processing request
            errorMessage = errorMessage ?? "<!doctype html><html><body><h1>Not Found!</h1><p>The requested url [" + urlString + "] not found!</p></body></html>";
            var bytes = Encoding.UTF8.GetBytes(errorMessage);
            this.stream = new MemoryStream(bytes, false);

            this.responseLength = -1;
            this.status = errorStatus != 0 ? errorStatus : 404;
            this.statusText = errorStatusText ?? "Not Found";
            this.mimeType = "text/html";
            callback.HeadersAvailable();
            return true;
        }