Recent

Author Topic: synapse httpserver send data and re-direct  (Read 3688 times)

epergola

  • Full Member
  • ***
  • Posts: 157
synapse httpserver send data and re-direct
« on: October 06, 2018, 12:02:45 am »
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), 8) + '_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

  • Sr. Member
  • ****
  • Posts: 366
Re: synapse httpserver send data and re-direct
« Reply #1 on: October 06, 2018, 11:31:45 am »
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).
Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

epergola

  • Full Member
  • ***
  • Posts: 157
Re: synapse httpserver send data and re-direct
« Reply #2 on: October 06, 2018, 07:47:48 pm »
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

  • Sr. Member
  • ****
  • Posts: 366
Re: synapse httpserver send data and re-direct
« Reply #3 on: October 06, 2018, 10:38:29 pm »
Quote
That "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][+][-]
  1.   // .........
  2.   //read request line
  3.   s := ASocket.RecvString(timeout);
  4.   method := fetch(s, ' ');
  5.   uri := fetch(s, ' ');
  6.   protocol := fetch(s, ' ');
  7.   // ......... simplest routing
  8.   case uri of
  9.   'donwload' : SendMyFile();
  10.   'donwload-page' : ShowDownloadPage();
  11.   end;
Quote
Now I would like to send DATA+an html sequence so that after (or while) downloads the browser opens up the html content
You 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][+][-]
  1. <html lang="en">
  2.   <meta charset="utf-8">
  3.   <title>Thank you</title>
  4.   <!--link rel="stylesheet" href="css/styles.css"-->
  5.  
  6. </head>
  7.  
  8. <iframe id="download_iframe" style="display:none;"></iframe>
  9.  
  10. <body onload="autoDownload()">
  11.   <script>
  12.     var myDownload = '/download';
  13.    
  14.     function autoDownload()
  15.     {
  16.       setTimeout(function(){
  17.         //alert('Loaded');
  18.         document.getElementById('download_iframe').src = myDownload;
  19.         }, 2000
  20.       );
  21.     }
  22.   </script>
  23.  
  24.   <h1>Congrats, here's your download</h1>
  25.  
  26.   <p>
  27.   Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
  28.   </p>
  29.  
  30.  
  31. </body>
  32. </html>
  33.  


And please, learn how HTTP works (request, response, headers, content type, method, response codes).
Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

 

TinyPortal © 2005-2018