curl_range.c 3.1 KB

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