I am evaluating CefSharp to build a desktop application which basically only wraps a web application. To access files from the file system I want to intercept http requests of the web app.
I adapted the minimal example and installed a CustomRequestHandler
which overrides GetResourceRequestHandler
as it is done in the docs for request interception.
Now, I would like to answer a HEAD request from the web app (The app needs to read a chunk from the end of the file - so I would need the total size in the first place). But so far I did not find a convenient way to do so.
I tried the following:
protected override IResourceHandler GetResourceHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request)
{
if (request.Method == "HEAD")
{
var handler = ResourceHandler.FromStream(Stream.Null, mimeType: "application/octet-stream");
var total = new System.IO.FileInfo(_filename).Length;
handler.Headers.Set("Content-Length", $"{total}");
return handler;
}
return ResourceHandler.ForErrorMessage("Method not allowed", HttpStatusCode.MethodNotAllowed);
}
In the browser the Content-Length header is set to zero (rightly so for a zero length stream). As for now, I think I need to implement a custom ResourceHandler which allows to set the Content-Length header. But I am wondering if the header is 'corrected' further down the line before sending the actual response. Any hint is appreciated.
Apparently I was missing the obvious. Instead of writing the header I just need to set the property ResponseLength
. The method now looks like this:
protected override IResourceHandler GetResourceHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request)
{
if (request.Method == "HEAD")
{
var handler = ResourceHandler.FromStream(Stream.Null, mimeType: "application/octet-stream");
var total = new System.IO.FileInfo(_filename).Length;
handler.ResponseLength = total;
return handler;
}
return ResourceHandler.ForErrorMessage("Method not allowed", HttpStatusCode.MethodNotAllowed);
}