Dan
Dan

Reputation: 3884

CefSharp1: Custom HTTP SchemeHandler with fallback to default

I'm writing an application where I need to override both the HTTP and HTTPS schemes with my own handler. However, I need to let any requests that are not handled fallback to the default handler and be processed normally. I thought I could just return "false" from ProcessRequestAsync but that doesn't work. Any insight would be appreciated. I'd rather not have to build my own "fallback" as it seems that it would be problematic.

I'm currently using 1.25.7 of CefSharp, however I did upgrade to version 37 and got the same results.

Here's the code to my SchemeHandler:

public bool ProcessRequestAsync(IRequest request, SchemeHandlerResponse response, OnRequestCompletedHandler requestCompletedCallback)
{
    Uri uri = new Uri(request.Url);

    foreach (IUriInterceptor interceptor in _interceptors)
    {

        if (interceptor.canHandle(uri))
        {
            interceptor.handleRequest(request, response);
            requestCompletedCallback.Invoke();
            return true;
        }
    }

    return false;
}

IUriInterceptor is my own class. I made it to better organize my custom handlers.

Upvotes: 0

Views: 1465

Answers (1)

Yoshi
Yoshi

Reputation: 3405

This isn't currently supported by CefSharp.

The underlying CEF library does allow it in CefSchemeHandlerFactory.Create():

Return a new resource handler instance to handle the request or an empty reference to allow default handling of the request.

http://magpcss.org/ceforum/apidocs3/projects/(default)/CefSchemeHandlerFactory.html

However the C++ implementation in CefSharp always expects a handler to be returned, and so you can never tell CEF to allow default handling:

CefRefPtr<CefResourceHandler> SchemeHandlerFactoryWrapper::Create(
    CefRefPtr<CefBrowser> browser,
    CefRefPtr<CefFrame> frame,
    const CefString& scheme_name,
    CefRefPtr<CefRequest> request)
{
    ISchemeHandler^ handler = _factory->Create();
    CefRefPtr<SchemeHandlerWrapper> wrapper = new SchemeHandlerWrapper(handler);
    return static_cast<CefRefPtr<CefResourceHandler>>(wrapper);
}

https://github.com/cefsharp/CefSharp/blob/00b25f5e3fac20b94b3022019fdeefbac6f9e271/CefSharp.Core/SchemeHandlerFactoryWrapper.cpp#L19-L21

The source code of CefSharp would need to be changed for this to work. They are very happy to guide contributors and to accept contributions to the project (remember, it's open source and free!).

Another alternative is to use the CefGlue project, which is a minimal wrapper around CEF that uses pinvoke, so it probably works out of the box there. However, integrating CefGlue into your WinForms or WPF app may be more difficult than CefSharp.

Upvotes: 1

Related Questions