skip to Main Content

I am trying to understand how downloads are sent to the clients browser using reverse engineering of code.

What I have so far discovered is that HTTP headers play a large role particularly in reference to the Content-Disposition tag / line. For example, I have found out that the following code sets up a download for a txt file named TEST1234, and that it is downloaded as an attachment as apposed to an inline.

HTTP/1.x 200 OK
Content-Type: application/octet-stream
Content-Disposition: attachment; filename=" ,"TEST1234.txt

However I was expecting that the code would also hold the files actual contents with in the rest of the http or at least the ajoining HTML code?

What leads me to think this is because some PHP codes are as follows…

<?php
  $filename="download.txt";
  header("Content-disposition: attachment;filename=$filename");
  readfile($filename);
?>

I am not at all familiar with PHP but can make out enough to see that the line readfile($filename); probably reads the file (server end) into a stream and adds the files coding it to either the HTTP header or the adjoining HTML that is posted as response to the client? Upon receiving the response, the clients browser knows that the file should be saved as "download".txt. Assuming that download.txt contained the text Hello world, I would think the sent response would look as follows?

    HTTP/1.x 200 OK
    Content-Type: application/octet-stream
    Content-Disposition: attachment; filename=" ,"download.txt"

    <html>Hello world</html>

2

Answers


  1. Chosen as BEST ANSWER

    I have found the solution...

    I had simply not created a blank line between the HTTP header and the HTML.... or in this case the file contents...

                    .Append("HTTP/1.1 200 OK" & Environment.NewLine)
                    .Append("Content-Type: text/plain" & Environment.NewLine)
                    .Append("Content-Disposition: attachment; filename=" & Chr(34) & "TEST.txt" & Chr(34) & Environment.NewLine)
                    .Append(Environment.NewLine)    <--------------- THIS LINE NOW WORKS
                    .Append("Hello world!")
    

    Although is there an way to encode web freindly/ safe encode the contents which the browser knows how to decode back to its original contents... simular to base64 encoding?


  2. The simple answer is "yes, it does look like that" – assuming that the content of the file is literally <html>Hello world</html>. If it’s just Hello world, then that’s literally what the response will contain – there’s no "adjoining HTML", it’s just that responding with HTML is a common use of HTTP.

    As you’ll see in any introduction to HTTP (e.g. MDN, Wikipedia) an HTTP 1.x response consists of a status line, some headers, and then the response body. (HTTP/2 and HTTP/3 are not text-based protocols, so while all the same elements exist in the response, the way they’re formatted is a bit more complex.)

    So a really simple response would look like this, indicating a successful response, with a plain text body:

    HTTP/1.1 200 OK
    Content-Type: text/plain
    
    Hello, world!
    

    The Content-Disposition line is just an extra header, which the browser uses as a hint for what to do next.

    HTTP/1.1 200 OK
    Content-Type: text/plain
    Content-Disposition: attachment; filename=TEST1234.txt
    
    Hello, world!
    

    Without knowing how you’re currently trying to view the raw HTTP traffic, it’s impossible to say why you’re not seeing the full response. It looks like it’s also mangled the quoting somewhere, unless " ," is actually part of your suggested filename.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search