1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2018, 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 https://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#include <curl/curl.h>
25#include "curl_range.h"
26#include "sendf.h"
27#include "strtoofft.h"
28
29/* Only include this function if one or more of FTP, FILE are enabled. */
30#if !defined(CURL_DISABLE_FTP) || !defined(CURL_DISABLE_FILE)
31
32 /*
33 Check if this is a range download, and if so, set the internal variables
34 properly.
35 */
36CURLcode Curl_range(struct connectdata *conn)
37{
38 curl_off_t from, to;
39 char *ptr;
40 char *ptr2;
41 struct Curl_easy *data = conn->data;
42
43 if(data->state.use_range && data->state.range) {
44 CURLofft from_t;
45 CURLofft to_t;
46 from_t = curlx_strtoofft(data->state.range, &ptr, 0, &from);
47 if(from_t == CURL_OFFT_FLOW)
48 return CURLE_RANGE_ERROR;
49 while(*ptr && (ISSPACE(*ptr) || (*ptr == '-')))
50 ptr++;
51 to_t = curlx_strtoofft(ptr, &ptr2, 0, &to);
52 if(to_t == CURL_OFFT_FLOW)
53 return CURLE_RANGE_ERROR;
54 if((to_t == CURL_OFFT_INVAL) && !from_t) {
55 /* X - */
56 data->state.resume_from = from;
57 DEBUGF(infof(data, "RANGE %" CURL_FORMAT_CURL_OFF_T " to end of file\n",
58 from));
59 }
60 else if((from_t == CURL_OFFT_INVAL) && !to_t) {
61 /* -Y */
62 data->req.maxdownload = to;
63 data->state.resume_from = -to;
64 DEBUGF(infof(data, "RANGE the last %" CURL_FORMAT_CURL_OFF_T " bytes\n",
65 to));
66 }
67 else {
68 /* X-Y */
69 curl_off_t totalsize;
70
71 /* Ensure the range is sensible - to should follow from. */
72 if(from > to)
73 return CURLE_RANGE_ERROR;
74
75 totalsize = to - from;
76 if(totalsize == CURL_OFF_T_MAX)
77 return CURLE_RANGE_ERROR;
78
79 data->req.maxdownload = totalsize + 1; /* include last byte */
80 data->state.resume_from = from;
81 DEBUGF(infof(data, "RANGE from %" CURL_FORMAT_CURL_OFF_T
82 " getting %" CURL_FORMAT_CURL_OFF_T " bytes\n",
83 from, data->req.maxdownload));
84 }
85 DEBUGF(infof(data, "range-download from %" CURL_FORMAT_CURL_OFF_T
86 " to %" CURL_FORMAT_CURL_OFF_T ", totally %"
87 CURL_FORMAT_CURL_OFF_T " bytes\n",
88 from, to, data->req.maxdownload));
89 }
90 else
91 data->req.maxdownload = -1;
92 return CURLE_OK;
93}
94
95#endif
96