curl_range.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, 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 "curlx/strparse.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. if(data->state.use_range && data->state.range) {
  38. curl_off_t from, to;
  39. bool first_num = TRUE;
  40. const char *p = data->state.range;
  41. if(curlx_str_number(&p, &from, CURL_OFF_T_MAX))
  42. first_num = FALSE;
  43. if(curlx_str_single(&p, '-'))
  44. /* no leading dash or after the first number is an error */
  45. return CURLE_RANGE_ERROR;
  46. if(curlx_str_number(&p, &to, CURL_OFF_T_MAX)) {
  47. /* no second number */
  48. /* X - */
  49. data->state.resume_from = from;
  50. DEBUGF(infof(data, "RANGE %" FMT_OFF_T " to end of file", from));
  51. }
  52. else if(!first_num) {
  53. /* -Y */
  54. if(!to)
  55. /* "-0" is just wrong */
  56. return CURLE_RANGE_ERROR;
  57. data->req.maxdownload = to;
  58. data->state.resume_from = -to;
  59. DEBUGF(infof(data, "RANGE the last %" FMT_OFF_T " bytes", to));
  60. }
  61. else {
  62. /* X-Y */
  63. curl_off_t totalsize;
  64. /* Ensure the range is sensible - to should follow from. */
  65. if(from > to)
  66. return CURLE_RANGE_ERROR;
  67. totalsize = to - from;
  68. if(totalsize == CURL_OFF_T_MAX)
  69. return CURLE_RANGE_ERROR;
  70. data->req.maxdownload = totalsize + 1; /* include last byte */
  71. data->state.resume_from = from;
  72. DEBUGF(infof(data, "RANGE from %" FMT_OFF_T
  73. " getting %" FMT_OFF_T " bytes",
  74. from, data->req.maxdownload));
  75. }
  76. DEBUGF(infof(data, "range-download from %" FMT_OFF_T
  77. " to %" FMT_OFF_T ", totally %" FMT_OFF_T " bytes",
  78. from, to, data->req.maxdownload));
  79. }
  80. else
  81. data->req.maxdownload = -1;
  82. return CURLE_OK;
  83. }
  84. #endif