Downloads - what to do with open network connection while asking user for filename?

50 views Asked by At

I'm using QtWebKit to write a small browser (why? See the FAQ.). Now when the user clicks on a link the browser can't render, I have to download it, but I get a network reply which is already an open connection.

Now I want to ask the user for a filename before I know where to write the file to, which is where my problem is. I can think of these approaches so far:

  • Not downloading anything until the filename is entered -> The server will probably close the connection after some seconds.
  • Just re-doing the request when the filename is entered -> Could lead to a lot of problems, e.g. with DRM-like "you may download this file N times" sites.
  • Just downloading to RAM -> RAM could fill up rather fast. I have a 250 MBit connection here, so potentially a gigabyte of RAM is filled if I take 30 seconds to enter a filename.
  • Downloading to a temporary file until the user has entered the filename, then copying it over and deleting the temporary file.

The last approach seems to be the cleanest to me, and this seems to be what Firefox/Chrome do as well. However I'm struggling to find documentation or code examples (ideally Qt/PyQt) which do this the right way, as there probably are some corner cases to take care of.

Am I on the right track?

1

There are 1 answers

0
The Compiler On BEST ANSWER

After reading through a lot of comments on related mozilla bugs (69938, 7840, 55690, 129923) I came to the conclusion slowly downloading to RAM is the best thing to do.

I'm doing this by setting a fixed read buffer size on the QNetworkReply, and then with a QTimer slowly reading into another in-RAM buffer.

The (PyQt) implementation can be found here.