1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at http://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22 
23 #include "curl_setup.h"
24 
25 #include <curl/curl.h>
26 #include "urldata.h"
27 #include "sendf.h"
28 #include "multiif.h"
29 #include "speedcheck.h"
30 
Curl_speedinit(struct SessionHandle * data)31 void Curl_speedinit(struct SessionHandle *data)
32 {
33   memset(&data->state.keeps_speed, 0, sizeof(struct timeval));
34 }
35 
Curl_speedcheck(struct SessionHandle * data,struct timeval now)36 CURLcode Curl_speedcheck(struct SessionHandle *data,
37                          struct timeval now)
38 {
39   if((data->progress.current_speed >= 0) &&
40      data->set.low_speed_time &&
41      (Curl_tvlong(data->state.keeps_speed) != 0) &&
42      (data->progress.current_speed < data->set.low_speed_limit)) {
43     long howlong = Curl_tvdiff(now, data->state.keeps_speed);
44     long nextcheck = (data->set.low_speed_time * 1000) - howlong;
45 
46     /* We are now below the "low speed limit". If we are below it
47        for "low speed time" seconds we consider that enough reason
48        to abort the download. */
49     if(nextcheck <= 0) {
50       /* we have been this slow for long enough, now die */
51       failf(data,
52             "Operation too slow. "
53             "Less than %ld bytes/sec transferred the last %ld seconds",
54             data->set.low_speed_limit,
55             data->set.low_speed_time);
56       return CURLE_OPERATION_TIMEDOUT;
57     }
58     else {
59       /* wait complete low_speed_time */
60       Curl_expire_latest(data, nextcheck);
61     }
62   }
63   else {
64     /* we keep up the required speed all right */
65     data->state.keeps_speed = now;
66 
67     if(data->set.low_speed_limit)
68       /* if there is a low speed limit enabled, we set the expire timer to
69          make this connection's speed get checked again no later than when
70          this time is up */
71       Curl_expire_latest(data, data->set.low_speed_time*1000);
72   }
73   return CURLE_OK;
74 }
75