#1 2019-03-21 07:28:43

edwinsn
Member
Registered: 2010-07-02
Posts: 1,218

Is there a way to abort a http request (the Retrieve function call)?

Assume a record class has a RawUtf8 field which might contain 10MB of string data.
Now the client calls Retrieve() in a background thread to get the record from a remote server.

The question is is there a way to interrupt/abort that http request operation in the middle of the network transfer process? Something like the TIdHttp.OnWork event hanlder, as demonstrated here (https://stackoverflow.com/a/6923250/133516).

Please note I'm not asking about how to terminate my thread, because that thread is waiting for 10MB of data to be downloaded and it cannot respond your 'Terminate' call until the download is completely finished or time out.


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#2 2019-03-21 07:47:02

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,671
Website

Re: Is there a way to abort a http request (the Retrieve function call)?

Not yet.

Offline

#3 2019-03-21 08:06:48

pvn0
Member
From: Slovenia
Registered: 2018-02-12
Posts: 211

Re: Is there a way to abort a http request (the Retrieve function call)?

I haven't tried this but you could just close the HINTERNET Handle from another thread.

https://docs.microsoft.com/en-us/window … losehandle

You would be looking at THttpRequest.InternalCloseRequest implemented in TWinHTTP, TWinINet or TCurlHTTP classes on how to do this.

The documentation warns not to close a synchronized request from another thread but in practice even chromium project was/is using this.
https://chromium.googlesource.com/exter … winhttp.cc

additional info :
https://stackoverflow.com/questions/893 … ed/8939394

Offline

#4 2019-03-21 10:24:06

edwinsn
Member
Registered: 2010-07-02
Posts: 1,218

Re: Is there a way to abort a http request (the Retrieve function call)?

@pvn0,

That's a bunch of helpful info and links you have provided, that gives me the direction for a possible solution for the issue! Thank you very much!


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#5 2019-03-21 12:02:20

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: Is there a way to abort a http request (the Retrieve function call)?

In case you use Linux (pthread) there is no problem at all - most of socket related functions is a cancellation points (see "cancellation points" section),  so in case you terminate thread read/accept/recv and so on will not block termination. This is one on 100500 things which are made there right..

Last edited by mpv (2019-03-21 12:22:09)

Offline

#6 2019-03-21 15:54:50

edwinsn
Member
Registered: 2010-07-02
Posts: 1,218

Re: Is there a way to abort a http request (the Retrieve function call)?

@mpv,

Both the server and client are on Windows, and with the original post I meant cancelling the sockets on the client side.


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

Board footer

Powered by FluxBB