in reply to Re: Perl output is not inducing file download as expected
in thread Perl output is not inducing file download as expected

I have a question. You have 4 print statements, and you turn on buffering. What happens when buffering is turned off is you send the header, some time goes by, and then you send the body. If you combined the 4 print statements into just one line, then buffering could be turned off or on, and it wouldn't make a difference. Am I wrong on this?

binmode STDOUT;
print "Content-type: application/x-download\n",
"Content-disposition: attachment; filename=$file\n",
"Content-length: $len\n\n", @document;

  • Comment on Re^2: Perl output is not inducing file download as expected

Replies are listed 'Best First'.
Re^3: Perl output is not inducing file download as expected
by Polyglot (Chaplain) on Sep 24, 2023 at 11:16 UTC
    The truth is, I became frustrated by the amount of confusion on this issue. I found not a single decent commentary online for how to print the headers manually, and the packages that printed them appeared to be printing more than I had specified when I checked the browser console--and I had doubts as to what else they might be doing under the surface. This is what led me to attempt to print them manually, so I would have full control over what got put into the header.

    But the internet seemed to provide multiple instructions on what the headers required: specifically, how each line of the header should be terminated. I tried one thing after another--I'm sure I must have tried at least 40 or more various configurations before I found anything that worked--and, of course, much of that time it was unrelated to the headers anyhow, but I didn't know that yet (the hot print handle was messing things up, or the fork: {} that I was attempting to use may not have helped).

    For example: some sites said that each line should end in "\r\n"--whereas I had been using just "\n". Was this something that the CGI package was "fixing" for me automatically? Did I need to add this manually? Another point of question was whether or not each line of the header should end in a comma, and whether or not this included the last line of the header, too. Perhaps the comma was just required by the CGI package, and not by the client. I searched in vain online for http header syntax. I found sites that claimed to say something about it, alright, but they focused on the headers themselves, not whether or not they should be case-sensitive, or how their lines should end, or anything else that I needed to know.

    In the end, I found an answer online with two lines, printed as I posted in my solution except that the second one had the double newline, that worked! I then added only the third line--the Content-length. Seeing that it worked satisfied me. Its simplicity pleased me. I am certain it could still be improved, and originally I did have all of it in a multi-line quote to be printed at once--but that was back when things were not working. Once I got it all working, I tended to let it be as it was! So that's how there got to be multiple print lines. I do not, however, presume that it must be this way, nor that it would not be superior to combine them. But it works; as-is. And with that I am, for now, content.

    Blessings,

    ~Polyglot~

      I searched in vain online for http header syntax.

      See the Wikipedia page for HTTP, you'll find a list of RFCs that give the exact specifications.

Re^3: Perl output is not inducing file download as expected
by Polyglot (Chaplain) on Sep 24, 2023 at 11:24 UTC
    Regarding the buffering, my understanding is that it is only to prevent having to slurp the entire file into memory (which I may be doing anyhow at this point, but it is not necessary to do so). For a large file, that buffering would come in handy so as not to need to consume so much RAM in the process. So I don't see any real connection between the buffering and the http headers. Now, this is merely my interpretation--someone here may be able to enlighten me as to the true purpose of the buffering.

    Blessings,

    ~Polyglot~

      Regarding the buffering, my understanding is that it is only to prevent having to slurp the entire file into memory

      Yes, that's correct. And since the network protocol stack is unlikely to be able to stuff a large file into a single network packet anyway, it makes no sense to read the entire file into RAM at once.