Note that there are some explanatory texts on larger screens.

plurals
  1. POC: simple HTTP server. Read a local file and download it from browser
    text
    copied!<p>I'm working on a simple server that have to work with a browser. When I give it some command, it has to reply me with some html code so to reply my answer. For example I can see the list of the files of a folder etc. I can access my server using localhost:port/somecommand Now I'm working on donwloading a file from the local hard-disk. What I want to do is enter an url like localhost:port/download/filepath and make the browser download it. When I create the reply I put all things html need to understand that there is a file to download, and infact I have the classical pop-up window that ask me to download the file, but what I receive is bigger than the original file located to the hard-disk, and infact the file is corrupted. Here the code:</p> <p>This is the html I send back</p> <pre><code>HTTP/1.0 200 OK Date: Tue Apr 10 16:23:55 2012 Content-Type: application/octet-stream Content-Disposition: attachment; filename=mypic.jpg Content-Length: 2574359 //empty line here </code></pre> <p>(I followed <a href="http://www.ietf.org/rfc/rfc2183.txt" rel="nofollow"> The Content-Disposition Header Field text</a> )</p> <p>then I read from file and I send back html first and what I read from file then:</p> <pre><code>int file=open(name,O_RDONLY); void * data=malloc(buf.st_size+1); //buf.st_size is the correct file size size_t readed=read(file,data,buf.st_size); send(sd_current, html, DIRSIZE, 0); //html is a string containing what you I showed you send(sd_current, data, readed); </code></pre> <p>This result the file I can download using localhost:port/download/filepath to be bigger than original then corrupted, but I can't get rid of why. Can someone help me?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload