Tee an active download - found a way, but is it supported?

Having problems with building or using the CefSharp .NET binding? Ask your CEF-related questions here. Please ask general usage questions on StackOverflow.

Moderator: amaitland

Tee an active download - found a way, but is it supported?

Postby kiriko » Fri Nov 18, 2022 4:11 pm

Hello,

I am using CEFsharp 107.1.50 and trying to tee an active download, i.e. look at what is being downloaded while the download is running, and stop the download if what I am interested in was found. For the purpose of this discussion, let's say the download is 100 GB and what I am interested in might be in the first 500 or so MB.

AFAIK there is no way to get an instance of System.IO.Stream for an active download, but I have found that if I have a download handler like this:

Code: Select all
protected override void OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
{
   callback.Continue("C:\\TEMP\\test.bin", false);
}


And another program, right now just a separate console application for testing purposes, like this:

Code: Select all
static void Main(string[] args)
{
   try
   {
      using (FileStream fs = new FileStream("C:\\TEMP\\test.BIN", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
      {
         Console.WriteLine("I have the handle");
         Console.ReadLine();
         byte[] tmp = new byte[1024];
         while (true)
         {
            Console.WriteLine(fs.Length);
            fs.Seek(fs.Length - 1024, SeekOrigin.Begin);
            int read = fs.Read(tmp, 0, 1024);
            Console.WriteLine("I read {0:n0} bytes", read);
            for (int i = 0; i < read; i++)
               Console.Write("{0:x2}, ", tmp[i]);
            Console.WriteLine("Press enter.");
            Console.ReadLine();
         }
      }
   } catch (Exception e)
   {
      Console.WriteLine(e.ToString());
      Console.ReadLine();
   }
}
}


I can actually open and read the file while the download is in progress. As expected, the length of the file keeps growing. It seems like CEF doesn't keep a tight lock on it. That's great.

My question: Can I depend on this behavior - being able to open the file that is being downloaded while the Chromium engine is still writing to it - as being officially supported, or is it just luck?



Also I found that this PHP script:
Code: Select all
<?php
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: Binary');
header('Content-disposition: attachment; filename="fuck.exe"');
for(;;){
if (connection_aborted()) die();
echo 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
flush();
usleep(5000);
}


i.e. a download where the server doesn't know the final size, makes downloadItem.TotalBytes return 0. Is this the intended behavior of CEF/CEFsharp? Would it not be more sensible to return something like -1, to mean "I don't know the total bytes"?

PS: To the forum admins, your mail server may be broken. Mails from the forum get marked as phishing attempts.
Code: Select all
Authentication-Results: mailin037.protonmail.ch; dmarc=fail (p=quarantine dis=none) header.from=magpcss.org
kiriko
Newbie
 
Posts: 1
Joined: Fri Nov 18, 2022 2:21 pm

Re: Tee an active download - found a way, but is it supporte

Postby amaitland » Wed Nov 30, 2022 1:52 am

It's possible the behaviour might change, it's implemented in Chromium so it's really upto the Chromium devs.

TotalBytes also comes from Chromium, nothing I can do about the value returned.

It's probably worth checking to see if you can use a response filter to get the returned data. Not something I've tried for downloads though, so no gurantee.

https://github.com/cefsharp/CefSharp/wi ... -filtering
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1290
Joined: Wed Jan 14, 2015 2:35 am


Return to CefSharp Forum

Who is online

Users browsing this forum: No registered users and 12 guests