Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2014-07

CGI Response.ContentStream client break

8 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Leonardo M. Ramé

Hi, I have a CGI program that serves some files using this code in an action:

AResponse.ContentStream := TFileStream.Create(lImagePath, fmOpenRead);

The problem I'm facing is that some times the client breaks the connection to the server, aparently leaving the file open. If the client tries to download the file again, a code 500 is returned.

Is there a way to detect the connection break and destroy the stream gracefully?.

Regards,

Reply by email

silvioprog
to Leonardo M. Ramé

2014-07-03 16:51 GMT-03:00 Leonardo M. Ram? :

Show quoted text 12 lines
Hi, I have a CGI program that serves some files using this code in an action: AResponse.ContentStream := TFileStream.Create(lImagePath, fmOpenRead); The problem I'm facing is that some times the client breaks the connection to the server, aparently leaving the file open. If the client tries to download the file again, a code 500 is returned. Is there a way to detect the connection break and destroy the stream gracefully?.

You can use the FreeContentStream property and free your stream manually, e.g.:

var
  f: TFileStream;
begin
  f := TFileStream.Create(...);
  try
    HttpResponse.FreeContentStream := False;
    HttpResponse.ContentType := 'application/octet-stream';
    HttpResponse.ContentStream := f;
    HttpResponse.SendContent;
  finally
    f.Free;
  end;
end;
Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140703/73b271bc/attachment.html>

Reply by email

Leonardo M. Ramé
to silvioprog
silvioprog wrote 32 lines
2014-07-03 16:51 GMT-03:00 Leonardo M. Ram? : > Hi, I have a CGI program that serves some files using this code in an > action: > > AResponse.ContentStream := TFileStream.Create(lImagePath, fmOpenRead); > > The problem I'm facing is that some times the client breaks the > connection to the server, aparently leaving the file open. If the client > tries to download the file again, a code 500 is returned. > > Is there a way to detect the connection break and destroy the stream > gracefully?. > You can use the FreeContentStream property and free your stream manually, e.g.: var f: TFileStream; begin f := TFileStream.Create(...); try HttpResponse.FreeContentStream := False; HttpResponse.ContentType := 'application/octet-stream'; HttpResponse.ContentStream := f; HttpResponse.SendContent; finally f.Free; end; end;

Thanks Silvio, it seems to work.

Reply by email

Leonardo M. Ramé
to Leonardo M. Ramé
Leonardo M. Ram? wrote 36 lines
On 2014-07-03 17:20:53 -0300, silvioprog wrote: > 2014-07-03 16:51 GMT-03:00 Leonardo M. Ram? : > > > Hi, I have a CGI program that serves some files using this code in an > > action: > > > > AResponse.ContentStream := TFileStream.Create(lImagePath, fmOpenRead); > > > > The problem I'm facing is that some times the client breaks the > > connection to the server, aparently leaving the file open. If the client > > tries to download the file again, a code 500 is returned. > > > > Is there a way to detect the connection break and destroy the stream > > gracefully?. > > > > You can use the FreeContentStream property and free your stream manually, > e.g.: > > var > f: TFileStream; > begin > f := TFileStream.Create(...); > try > HttpResponse.FreeContentStream := False; > HttpResponse.ContentType := 'application/octet-stream'; > HttpResponse.ContentStream := f; > HttpResponse.SendContent; > finally > f.Free; > end; > end; > Thanks Silvio, it seems to work.

Mmm, no, too fast.

Maybe the problem is on the client side. I'm using Synapse library as client, and I'm issuing THttpSend's Abort method to stop downloading. Is the cgi notified when the client does an Abort call?.

Reply by email

silvioprog
to Leonardo M. Ramé

2014-07-03 17:33 GMT-03:00 Leonardo M. Ram? :
[...]

Mmm, no, too fast. Maybe the problem is on the client side. I'm using Synapse library as client, and I'm issuing THttpSend's Abort method to stop downloading.

You can change your 'broker' to a embedded HTTP server, and put a break point and debug both (client/server).

Is the cgi notified when the client does an Abort call?.

Hm... AFAIK, no.

Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140703/cfd90eb6/attachment.html>

Reply by email

Michael Van Canneyt
to silvioprog
silvioprog wrote 14 lines
2014-07-03 17:33 GMT-03:00 Leonardo M. Ram? : [...] Mmm, no, too fast. Maybe the problem is on the client side. I'm using Synapse library as client, and I'm issuing THttpSend's Abort method to stop downloading. You can change your 'broker' to a embedded HTTP server, and put a break point and debug both (client/server). ? Is?the cgi notified when the client does an Abort call?. Hm...?AFAIK, no.

Indeed. The CGI program is just terminated.

Normally the file stream is then closed by the OS, so this should not have any side effects. (unless the OS is buggy).

If you do observe side effects, try opening the file without share locks. using flags (fmOpenRead or fmShareDenyNone), I think.

Michael.

Reply by email

Leonardo M. Ramé
to Michael Van Canneyt
Michael Van Canneyt wrote 28 lines
On Thu, 3 Jul 2014, silvioprog wrote: >2014-07-03 17:33 GMT-03:00 Leonardo M. Ram? : >[...] > Mmm, no, too fast. > > Maybe the problem is on the client side. I'm using Synapse library as > client, and I'm issuing THttpSend's Abort method to stop downloading. > > >You can change your 'broker' to a embedded HTTP server, and put a break point and debug both (client/server). >? > Is?the cgi notified when the client does an Abort call?. > > >Hm...?AFAIK, no. Indeed. The CGI program is just terminated. Normally the file stream is then closed by the OS, so this should not have any side effects. (unless the OS is buggy). If you do observe side effects, try opening the file without share locks. using flags (fmOpenRead or fmShareDenyNone), I think. Michael.

Thanks Michael, using (fmOpenRead or fmShareDenyNone) params did the trick!.

Regards,

Reply by email

Michael Van Canneyt
to Leonardo M. Ramé
Leonardo M. Ram? wrote 32 lines
On 2014-07-04 08:22:30 +0200, Michael Van Canneyt wrote: > > > On Thu, 3 Jul 2014, silvioprog wrote: > >> 2014-07-03 17:33 GMT-03:00 Leonardo M. Ram? : >> [...] >> Mmm, no, too fast. >> >> Maybe the problem is on the client side. I'm using Synapse library as >> client, and I'm issuing THttpSend's Abort method to stop downloading. >> >> >> You can change your 'broker' to a embedded HTTP server, and put a break point and debug both (client/server). >> ? >> Is?the cgi notified when the client does an Abort call?. >> >> >> Hm...?AFAIK, no. > > Indeed. The CGI program is just terminated. > > Normally the file stream is then closed by the OS, so this should not have any side effects. > (unless the OS is buggy). > > If you do observe side effects, try opening the file without share locks. > using flags (fmOpenRead or fmShareDenyNone), I think. > > Michael. Thanks Michael, using (fmOpenRead or fmShareDenyNone) params did the trick!.

On what platform did you get this ?

Michael

Reply by email