2018-01-29 17:50:43 +08:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 20:51:48 +08:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2018-01-29 17:50:43 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2018-01-29 17:50:43 +08:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 17:16:50 +08:00
|
|
|
*
|
2018-01-29 17:50:43 +08:00
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "curl_setup.h"
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#include "curl_range.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "strtoofft.h"
|
|
|
|
|
|
|
|
/* Only include this function if one or more of FTP, FILE are enabled. */
|
2018-03-12 03:30:02 +08:00
|
|
|
#if !defined(CURL_DISABLE_FTP) || !defined(CURL_DISABLE_FILE)
|
2018-01-29 17:50:43 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
Check if this is a range download, and if so, set the internal variables
|
|
|
|
properly.
|
|
|
|
*/
|
2021-01-20 16:29:45 +08:00
|
|
|
CURLcode Curl_range(struct Curl_easy *data)
|
2018-01-29 17:50:43 +08:00
|
|
|
{
|
|
|
|
curl_off_t from, to;
|
|
|
|
char *ptr;
|
|
|
|
char *ptr2;
|
|
|
|
|
|
|
|
if(data->state.use_range && data->state.range) {
|
|
|
|
CURLofft from_t;
|
|
|
|
CURLofft to_t;
|
2022-11-17 15:41:44 +08:00
|
|
|
from_t = curlx_strtoofft(data->state.range, &ptr, 10, &from);
|
2018-01-29 17:50:43 +08:00
|
|
|
if(from_t == CURL_OFFT_FLOW)
|
|
|
|
return CURLE_RANGE_ERROR;
|
2022-09-06 05:21:15 +08:00
|
|
|
while(*ptr && (ISBLANK(*ptr) || (*ptr == '-')))
|
2018-01-29 17:50:43 +08:00
|
|
|
ptr++;
|
2022-11-17 15:41:44 +08:00
|
|
|
to_t = curlx_strtoofft(ptr, &ptr2, 10, &to);
|
2018-01-29 17:50:43 +08:00
|
|
|
if(to_t == CURL_OFFT_FLOW)
|
|
|
|
return CURLE_RANGE_ERROR;
|
|
|
|
if((to_t == CURL_OFFT_INVAL) && !from_t) {
|
|
|
|
/* X - */
|
|
|
|
data->state.resume_from = from;
|
2021-07-06 23:05:17 +08:00
|
|
|
DEBUGF(infof(data, "RANGE %" CURL_FORMAT_CURL_OFF_T " to end of file",
|
2018-01-29 17:50:43 +08:00
|
|
|
from));
|
|
|
|
}
|
|
|
|
else if((from_t == CURL_OFFT_INVAL) && !to_t) {
|
|
|
|
/* -Y */
|
|
|
|
data->req.maxdownload = to;
|
|
|
|
data->state.resume_from = -to;
|
2021-07-06 23:05:17 +08:00
|
|
|
DEBUGF(infof(data, "RANGE the last %" CURL_FORMAT_CURL_OFF_T " bytes",
|
2018-01-29 17:50:43 +08:00
|
|
|
to));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* X-Y */
|
2018-01-29 18:12:40 +08:00
|
|
|
curl_off_t totalsize;
|
|
|
|
|
|
|
|
/* Ensure the range is sensible - to should follow from. */
|
|
|
|
if(from > to)
|
|
|
|
return CURLE_RANGE_ERROR;
|
|
|
|
|
|
|
|
totalsize = to - from;
|
2018-01-29 17:50:43 +08:00
|
|
|
if(totalsize == CURL_OFF_T_MAX)
|
|
|
|
return CURLE_RANGE_ERROR;
|
2018-01-29 18:12:40 +08:00
|
|
|
|
2018-01-29 17:50:43 +08:00
|
|
|
data->req.maxdownload = totalsize + 1; /* include last byte */
|
|
|
|
data->state.resume_from = from;
|
|
|
|
DEBUGF(infof(data, "RANGE from %" CURL_FORMAT_CURL_OFF_T
|
2021-07-06 23:05:17 +08:00
|
|
|
" getting %" CURL_FORMAT_CURL_OFF_T " bytes",
|
2018-01-29 17:50:43 +08:00
|
|
|
from, data->req.maxdownload));
|
|
|
|
}
|
|
|
|
DEBUGF(infof(data, "range-download from %" CURL_FORMAT_CURL_OFF_T
|
|
|
|
" to %" CURL_FORMAT_CURL_OFF_T ", totally %"
|
2021-07-06 23:05:17 +08:00
|
|
|
CURL_FORMAT_CURL_OFF_T " bytes",
|
2018-01-29 17:50:43 +08:00
|
|
|
from, to, data->req.maxdownload));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
data->req.maxdownload = -1;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|