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;
        }
 /// <summary>
 /// Create a new CefWebUrlRequest object.
 /// </summary>
 public static CefWebUrlRequest Create(CefRequest request, CefWebUrlRequestClient client)
 {
     return CefWebUrlRequest.From(
         NativeMethods.cef_web_urlrequest_create(
             request.GetNativePointerAndAddRef(),
             client.GetNativePointerAndAddRef()
             )
         );
 }
 /// <summary>
 /// Return a new scheme handler instance to handle the request.
 /// |browser| will be the browser window that initiated the request.
 /// If the request was initiated using the CefWebURLRequest API |browser| will be NULL.
 /// </summary>
 protected abstract CefSchemeHandler Create(CefBrowser browser, string schemeName, CefRequest request);
Example #4
0
 /// <summary>
 /// Begin processing the request.
 ///
 /// To handle the request return true and call HeadersAvailable() once the response header information is
 /// available (HeadersAvailable() can also be called from inside this
 /// method if header information is available immediately).
 ///
 /// To cancel the request return false.
 /// </summary>
 protected abstract bool ProcessRequest(CefRequest request, CefSchemeHandlerCallback callback);
 /// <summary>
 /// Return a new scheme handler instance to handle the request.
 /// |browser| will be the browser window that initiated the request.
 /// If the request was initiated using the CefWebURLRequest API |browser| will be NULL.
 /// </summary>
 protected abstract CefSchemeHandler Create(CefBrowser browser, string schemeName, CefRequest request);
Example #6
0
 /// <summary>
 /// Notifies the client that the request has been redirected and provides a chance to change the request parameters.
 /// </summary>
 protected virtual void OnRedirect(CefWebUrlRequest requester, CefRequest request, CefResponse response)
 {
 }
Example #7
0
 /// <summary>
 /// Create a new CefRequest object.
 /// </summary>
 public static CefRequest Create()
 {
     return(CefRequest.From(
                NativeMethods.cef_request_create()
                ));
 }
Example #8
0
 /// <summary>
 /// Load the request represented by the |request| object.
 /// </summary>
 public void LoadRequest(CefRequest request)
 {
     cef_frame_t.invoke_load_request(this.ptr, request.GetNativePointerAndAddRef());
 }
Example #9
0
 /// <summary>
 /// Load the request represented by the |request| object.
 /// </summary>
 public void LoadRequest(CefRequest request)
 {
     cef_frame_t.invoke_load_request(this.ptr, request.GetNativePointerAndAddRef());
 }
Example #10
0
 /// <summary>
 /// Called on the UI thread before browser navigation.
 /// Return true to cancel the navigation
 /// or false to allow the navigation to proceed.
 /// </summary>
 protected virtual bool OnBeforeBrowse(CefBrowser browser, CefFrame frame, CefRequest request, CefHandlerNavType navType, bool isRedirect)
 {
     return(false);
 }
Example #11
0
 /// <summary>
 /// Called on the IO thread before a resource is loaded.
 /// To allow the resource to load normally return false.
 /// To redirect the resource to a new url populate the |redirectUrl| value and return false.
 /// To specify data for the resource return a CefStream object in |resourceStream|,
 /// use the |response| object to set mime type, HTTP status code and optional header values, and return false.
 /// To cancel loading of the resource return true.
 /// Any modifications to |request| will be observed.
 /// If the URL in |request| is changed and |redirectUrl| is also set, the URL in |request| will be used.
 /// </summary>
 protected virtual bool OnBeforeResourceLoad(CefBrowser browser, CefRequest request, out string redirectUrl, out CefStreamReader resourceStream, CefResponse response, int loadFlags)
 {
     redirectUrl    = null;
     resourceStream = null;
     return(false);
 }
 /// <summary>
 /// Begin processing the request.
 /// 
 /// To handle the request return true and call HeadersAvailable() once the response header information is
 /// available (HeadersAvailable() can also be called from inside this
 /// method if header information is available immediately).
 /// 
 /// To cancel the request return false.
 /// </summary>
 protected abstract bool ProcessRequest(CefRequest request, CefSchemeHandlerCallback callback);
 protected override CefSchemeHandler Create(CefBrowser browser, string schemeName, CefRequest request)
 {
     return new ClientSchemeHandler();
 }
 /// <summary>
 /// Called on the IO thread before a resource is loaded.
 /// To allow the resource to load normally return false.
 /// To redirect the resource to a new url populate the |redirectUrl| value and return false.
 /// To specify data for the resource return a CefStream object in |resourceStream|,
 /// use the |response| object to set mime type, HTTP status code and optional header values, and return false.
 /// To cancel loading of the resource return true.
 /// Any modifications to |request| will be observed.
 /// If the URL in |request| is changed and |redirectUrl| is also set, the URL in |request| will be used.
 /// </summary>
 protected virtual bool OnBeforeResourceLoad(CefBrowser browser, CefRequest request, out string redirectUrl, out CefStreamReader resourceStream, CefResponse response, int loadFlags)
 {
     redirectUrl = null;
     resourceStream = null;
     return false;
 }
 /// <summary>
 /// Called on the UI thread before browser navigation.
 /// Return true to cancel the navigation 
 /// or false to allow the navigation to proceed.
 /// </summary>
 protected virtual bool OnBeforeBrowse(CefBrowser browser, CefFrame frame, CefRequest request, CefHandlerNavType navType, bool isRedirect)
 {
     return false;
 }