From 13ac29382f9087d410a96f7ca943b16d39a10afb Mon Sep 17 00:00:00 2001
From: Daniel Stenberg <daniel@haxx.se>
Date: Tue, 2 Mar 2010 13:26:23 +0000
Subject: [PATCH] - Made the pingpong timeout code properly deal with the
 response timeout AND   the global timeout if set. Also, as was reported in
 the bug report #2956437   by Ryan Chan, the time stamp to use as basis for
 the per command timeout was   not set properly in the DONE phase for FTP (and
 not for SMTP) so I fixed   that just now. This was a regression compared to
 7.19.7 due to the   conversion of FTP code over to the generic pingpong
 concepts.

  http://curl.haxx.se/bug/view.cgi?id=2956437
---
 CHANGES        | 10 ++++++++++
 RELEASE-NOTES  |  4 +++-
 lib/ftp.c      |  1 +
 lib/pingpong.c | 33 +++++++++++++++++++--------------
 lib/smtp.c     |  6 +++++-
 5 files changed, 38 insertions(+), 16 deletions(-)

diff --git a/CHANGES b/CHANGES
index 3448d16b70..effef4095e 100644
--- a/CHANGES
+++ b/CHANGES
@@ -6,6 +6,16 @@
 
                                   Changelog
 
+Daniel Stenberg (2 Mar 2010)
+- Made the pingpong timeout code properly deal with the response timeout AND
+  the global timeout if set. Also, as was reported in the bug report #2956437
+  by Ryan Chan, the time stamp to use as basis for the per command timeout was
+  not set properly in the DONE phase for FTP (and not for SMTP) so I fixed
+  that just now. This was a regression compared to 7.19.7 due to the
+  conversion of FTP code over to the generic pingpong concepts.
+
+  http://curl.haxx.se/bug/view.cgi?id=2956437
+
 Daniel Stenberg (1 Mar 2010)
 - Ben Greear provided an update for TFTP that fixes upload.
 
diff --git a/RELEASE-NOTES b/RELEASE-NOTES
index b6e2b4ea30..f47f6944f5 100644
--- a/RELEASE-NOTES
+++ b/RELEASE-NOTES
@@ -23,6 +23,7 @@ This release includes the following bugfixes:
  o off-by-one in the chunked encoding trailer parser
  o superfluous blocking for OpenSSL-based SSL connects and multi interface
  o TFTP upload
+ o FTP timeouts after file transferred completely
 
 This release includes the following known bugs:
 
@@ -32,6 +33,7 @@ This release would not have looked like this without help, code, reports and
 advice from friends like these:
 
  Steven M. Schweda, Yang Tse, Jack Zhang, Tom Donovan, Martin Hager,
- Daniel Fandrich, Patrick Monnerat, Pat Ray, Wesley Miaw, Ben Greear
+ Daniel Fandrich, Patrick Monnerat, Pat Ray, Wesley Miaw, Ben Greear,
+ Ryan Chan
 
         Thanks! (and sorry if I forgot to mention someone)
diff --git a/lib/ftp.c b/lib/ftp.c
index 85419009eb..552ecf8ee7 100644
--- a/lib/ftp.c
+++ b/lib/ftp.c
@@ -3071,6 +3071,7 @@ static CURLcode ftp_done(struct connectdata *conn, CURLcode status,
     long old_time = pp->response_time;
 
     pp->response_time = 60*1000; /* give it only a minute for now */
+    pp->response = Curl_tvnow(); /* timeout relative now */
 
     result = Curl_GetFTPResponse(&nread, conn, &ftpcode);
 
diff --git a/lib/pingpong.c b/lib/pingpong.c
index 6f27f57612..072e56207c 100644
--- a/lib/pingpong.c
+++ b/lib/pingpong.c
@@ -50,23 +50,28 @@ long Curl_pp_state_timeout(struct pingpong *pp)
   struct connectdata *conn = pp->conn;
   struct SessionHandle *data=conn->data;
   long timeout_ms; /* in milliseconds */
+  long timeout2_ms; /* in milliseconds */
+  long response_time= (data->set.server_response_timeout)?
+    data->set.server_response_timeout: pp->response_time;
 
-  if(data->set.server_response_timeout )
-    /* if CURLOPT_SERVER_RESPONSE_TIMEOUT is set, use that to determine
-       remaining time.  Also, use pp->response because SERVER_RESPONSE_TIMEOUT
-       is supposed to govern the response for any given server response, not
-       for the time from connect to the given server response. */
-    timeout_ms = data->set.server_response_timeout - /* timeout time */
-      Curl_tvdiff(Curl_tvnow(), pp->response); /* spent time */
-  else if(data->set.timeout)
+  /* if CURLOPT_SERVER_RESPONSE_TIMEOUT is set, use that to determine
+     remaining time, or use pp->response because SERVER_RESPONSE_TIMEOUT is
+     supposed to govern the response for any given server response, not for
+     the time from connect to the given server response. */
+
+  /* Without a requested timeout, we only wait 'response_time' seconds for the
+     full response to arrive before we bail out */
+  timeout_ms = response_time -
+    Curl_tvdiff(Curl_tvnow(), pp->response); /* spent time */
+
+  if(data->set.timeout) {
     /* if timeout is requested, find out how much remaining time we have */
-    timeout_ms = data->set.timeout - /* timeout time */
+    timeout2_ms = data->set.timeout - /* timeout time */
       Curl_tvdiff(Curl_tvnow(), conn->now); /* spent time */
-  else
-    /* Without a requested timeout, we only wait 'response_time' seconds for
-       the full response to arrive before we bail out */
-    timeout_ms = pp->response_time -
-      Curl_tvdiff(Curl_tvnow(), pp->response); /* spent time */
+
+    /* pick the lowest number */
+    timeout_ms = CURLMIN(timeout_ms, timeout2_ms);
+  }
 
   return timeout_ms;
 }
diff --git a/lib/smtp.c b/lib/smtp.c
index 2587934cf8..f7757f057d 100644
--- a/lib/smtp.c
+++ b/lib/smtp.c
@@ -637,7 +637,7 @@ static CURLcode smtp_init(struct connectdata *conn)
  * a part of the easy interface, it will always be TRUE.
  */
 static CURLcode smtp_connect(struct connectdata *conn,
-                                 bool *done) /* see description above */
+                             bool *done) /* see description above */
 {
   CURLcode result;
   struct smtp_conn *smtpc = &conn->proto.smtpc;
@@ -784,6 +784,10 @@ static CURLcode smtp_done(struct connectdata *conn, CURLcode status,
 
 
   if(status == CURLE_OK) {
+    struct smtp_conn *smtpc = &conn->proto.smtpc;
+    struct pingpong *pp= &smtpc->pp;
+    pp->response = Curl_tvnow(); /* timeout relative now */
+
     state(conn, SMTP_POSTDATA);
     /* run the state-machine