0

I am using TIdHTTPProxyServer and now I want to terminate connection when it is success to connect to the target HTTP server but receive no response for a long time(i.g. 3 mins) Currently I find no related property or event about it. And even if the client terminate the connection before the proxy server receive the response from the HTTP server. OnException Event will not be fired until the proxy server receive the response. (That is, if the proxy server still receive no response from HTTP Server, I even do not know the client has already terminate the connection...)

Any help will be appreciated.

Thanks!

Willy

Willy
  • 1,828
  • 16
  • 41
  • 1
    possible duplicate of [Is there a way to set response timeout for Indy Tidhttp gets?](http://stackoverflow.com/questions/3314878/is-there-a-way-to-set-response-timeout-for-indy-tidhttp-gets) – Rob Kennedy Jun 29 '12 at 03:56
  • Thanks for reply. As I am using TIdHTTPProxyServer, does it mean I have to write my own class extended to TIdHTTPProxyServer and override its functions ? – Willy Jun 29 '12 at 04:14
  • I don't think so. Is that what the other answers say? I thought the answer was to disconnect the connection after too much time had passed. If it takes a subclass for that, then so be it. – Rob Kennedy Jun 29 '12 at 04:25
  • Yeah... The problem is I can not find a way to access the connection between the HTTP Server and the Proxy Server in IdHTTPProxyServer. – Willy Jun 29 '12 at 04:47
  • You are right. I will try to do it. – Willy Jun 29 '12 at 12:01
  • I have posted an answer showing how to access the connection between the proxy and the HTTP server. – Remy Lebeau Jun 29 '12 at 16:23

1 Answers1

1

Indy uses infinite timeouts by default. To do what you are asking for, you need to set the ReadTimeout property of the outbound connection to the target server. You can access that connection via the TIdHTTPProxyServerContext.OutboundClient property. Use the OnHTTPBeforeCommand event, which is triggered just before the OutboundClient connects to the target server, eg:

#include "IdTCPClient.hpp"

void __fastcall TForm1::IdHTTPProxyServer1HTTPBeforeCommand(TIdHTTPProxyServerContext *AContext)
{
    static_cast<TIdTCPClient*>(AContext->OutboundClient)->ReadTimeout = ...;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thanks to reply. I have tried to insert a line AContext->OutboundClient->Socket->ReadTimeout = 15000; in OnHTTPBeforeCommand event. The problem I met is that OutboundClient object has not been created yet in this event so it occurs access violation error – Willy Jul 02 '12 at 04:14
  • 1
    The `OutboundClient` has been created before the event is fired, but its `IOHandler` has not been assigned yet (and thus its `Socket` property). That is why I type-cast the `OutboundClient` to a `TIdTCPClient` first, as it has its own `ReadTimeout` property that gets copied to the `IOHandler` when it finally does get assigned. – Remy Lebeau Jul 02 '12 at 20:32
  • Wow! it is awesome! I did not realize it is TIdTCPClient not TIdTCPConnection and I need to include IdTCPClient.hpp additionally. Thank you so much! – Willy Jul 03 '12 at 02:34
  • Sorry, didn't realize you were using C++. I updated my example accordingly. – Remy Lebeau Jul 03 '12 at 19:46