synapse httpserver send data and re-direct lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- epergola · 2018-10-05 22:02 Hi I have a small synapse http server. It uses TTCPBlockSocket.sendbuffer (as in the example in the synapse directory) to send outputdata. The outputdata is created this way: st := TFileStream.Create(filename, fmOpenRead or fmShareDenyWrite); headers.add('--' + IntToHex(Random(MaxInt), + '_Synapse_boundary'); headers.add('content-disposition: form-data; name="' + filename + '";' + ' filename="' + filename +'"'); headers.add('Content-Type: Application/octet-string'); outputdata.CopyFrom(st, 0); And it will download the file "filename". This part works fine. Now, I would like to also attach an html file to outputdata wich should show up on the client (browser) with a thank you message. I have the html file on disk. I can't find out how to make this work. When I simply attach the file preceded by 'Content-Type: text/plain', it gets attached but not loaded as a apage in the browser. Anybody could give me a hint? ---------------------------------------------------------------------- sash · 2018-10-06 09:31 You need to display download page (with your thank-text and automatic / manual (anchor) download) instead. To do so you need to (in a simplest way) handle request uri (to distinguish between actual file or download page html). ---------------------------------------------------------------------- epergola · 2018-10-06 17:47 That "distinguish" is exaclty what I do not know how to implement. I have a login page which the user fills. The HTTp server (uses a TTCPBlockSocket, the bind() funcion and the ProcessHttpRequest function as in the synapse example) sends a response. The response is a report. This part works WELL (the browser downloads the file sent by the server). Now I would like to send DATA+an html sequence so that after (or while) downloads the browser opens up the html content. I do not know how to ATTACH the html to the data part to make show in the browser. I do what is show in the attachment, but what it does is that the html content is physically send at the end of the data but is not displayed in the browser. If I send the html part only (no data) it shows. ---------------------------------------------------------------------- sash · 2018-10-06 20:38 QuoteThat "distinguish" is exaclty what I do not know how to implement. You need routing: server should check request headers - uri http://wiki.freepascal.org/Networking#Webserver_example Code: Pascal [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // ......... //read request line s := ASocket.RecvString(timeout); method := fetch(s, ' '); uri := fetch(s, ' '); protocol := fetch(s, ' '); // ......... simplest routing case uri of 'donwload' : SendMyFile(); 'donwload-page' : ShowDownloadPage(); end;QuoteNow I would like to send DATA+an html sequence so that after (or while) downloads the browser opens up the html contentYou cannot send TWO entities (distinguishable by browser) in a single request-response transaction. That's why I said you need to display download page as (html). And from this page (by clicking link(anchor) or automatically) new request (actual download) will be initiated. Here's sample page where download will start automatically in 2 sec after page loaded in browser. Code: HTML5 [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
And please, learn how HTTP works (request, response, headers, content type, method, response codes).