curl_range.c 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2018, 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.haxx.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 connectdata *conn)
  34. {
  35. curl_off_t from, to;
  36. char *ptr;
  37. char *ptr2;
  38. struct Curl_easy *data = conn->data;
  39. if(data->state.use_range && data->state.range) {
  40. CURLofft from_t;
  41. CURLofft to_t;
  42. from_t = curlx_strtoofft(data->state.range, &ptr, 0, &from);
  43. if(from_t == CURL_OFFT_FLOW)
  44. return CURLE_RANGE_ERROR;
  45. while(*ptr && (ISSPACE(*ptr) || (*ptr == '-')))
  46. ptr++;
  47. to_t = curlx_strtoofft(ptr, &ptr2, 0, &to);
  48. if(to_t == CURL_OFFT_FLOW)
  49. return CURLE_RANGE_ERROR;
  50. if((to_t == CURL_OFFT_INVAL) && !from_t) {
  51. /* X - */
  52. data->state.resume_from = from;
  53. DEBUGF(infof(data, "RANGE %" CURL_FORMAT_CURL_OFF_T " to end of file\n",
  54. from));
  55. }
  56. else if((from_t == CURL_OFFT_INVAL) && !to_t) {
  57. /* -Y */
  58. data->req.maxdownload = to;
  59. data->state.resume_from = -to;
  60. DEBUGF(infof(data, "RANGE the last %" CURL_FORMAT_CURL_OFF_T " bytes\n",
  61. to));
  62. }
  63. else {
  64. /* X-Y */
  65. curl_off_t totalsize;
  66. /* Ensure the range is sensible - to should follow from. */
  67. if(from > to)
  68. return CURLE_RANGE_ERROR;
  69. totalsize = to - from;
  70. if(totalsize == CURL_OFF_T_MAX)
  71. return CURLE_RANGE_ERROR;
  72. data->req.maxdownload = totalsize + 1; /* include last byte */
  73. data->state.resume_from = from;
  74. DEBUGF(infof(data, "RANGE from %" CURL_FORMAT_CURL_OFF_T
  75. " getting %" CURL_FORMAT_CURL_OFF_T " bytes\n",
  76. from, data->req.maxdownload));
  77. }
  78. DEBUGF(infof(data, "range-download from %" CURL_FORMAT_CURL_OFF_T
  79. " to %" CURL_FORMAT_CURL_OFF_T ", totally %"
  80. CURL_FORMAT_CURL_OFF_T " bytes\n",
  81. from, to, data->req.maxdownload));
  82. }
  83. else
  84. data->req.maxdownload = -1;
  85. return CURLE_OK;
  86. }
  87. #endif