2
0

curl_range.c 3.2 KB

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