1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
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.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * SPDX-License-Identifier: curl
23 ***************************************************************************/
25 #include "curl_setup.h"
26 #include <curl/curl.h>
27 #include "curl_range.h"
29 #include "strtoofft.h"
31 /* Only include this function if one or more of FTP, FILE are enabled. */
32 #if !defined(CURL_DISABLE_FTP) || !defined(CURL_DISABLE_FILE)
35 Check if this is a range download, and if so, set the internal variables
38 CURLcode Curl_range(struct Curl_easy *data)
44 if(data->state.use_range && data->state.range) {
47 from_t = curlx_strtoofft(data->state.range, &ptr, 0, &from);
48 if(from_t == CURL_OFFT_FLOW)
49 return CURLE_RANGE_ERROR;
50 while(*ptr && (ISBLANK(*ptr) || (*ptr == '-')))
52 to_t = curlx_strtoofft(ptr, &ptr2, 0, &to);
53 if(to_t == CURL_OFFT_FLOW)
54 return CURLE_RANGE_ERROR;
55 if((to_t == CURL_OFFT_INVAL) && !from_t) {
57 data->state.resume_from = from;
58 DEBUGF(infof(data, "RANGE %" CURL_FORMAT_CURL_OFF_T " to end of file",
61 else if((from_t == CURL_OFFT_INVAL) && !to_t) {
63 data->req.maxdownload = to;
64 data->state.resume_from = -to;
65 DEBUGF(infof(data, "RANGE the last %" CURL_FORMAT_CURL_OFF_T " bytes",
72 /* Ensure the range is sensible - to should follow from. */
74 return CURLE_RANGE_ERROR;
76 totalsize = to - from;
77 if(totalsize == CURL_OFF_T_MAX)
78 return CURLE_RANGE_ERROR;
80 data->req.maxdownload = totalsize + 1; /* include last byte */
81 data->state.resume_from = from;
82 DEBUGF(infof(data, "RANGE from %" CURL_FORMAT_CURL_OFF_T
83 " getting %" CURL_FORMAT_CURL_OFF_T " bytes",
84 from, data->req.maxdownload));
86 DEBUGF(infof(data, "range-download from %" CURL_FORMAT_CURL_OFF_T
87 " to %" CURL_FORMAT_CURL_OFF_T ", totally %"
88 CURL_FORMAT_CURL_OFF_T " bytes",
89 from, to, data->req.maxdownload));
92 data->req.maxdownload = -1;