7 void CURL_File::error_check(CURLcode code)
15 void CURL_File::perform_range(int64_t start, int64_t size)
const
18 std::ostringstream range;
19 range << start <<
'-' << start + size - 1;
21 error_check(curl_easy_setopt(curl, CURLOPT_RANGE, range.str().c_str()));
22 error_check(curl_easy_perform(curl));
25 error_check(curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code));
27 if (code != 0 && code != 206)
29 std::ostringstream error_message;
30 error_message <<
"unexpected response code: " << code;
31 throw Exception(error_message.str());
36 size_t CURL_File::pread_Callback_Data::copy
43 const size_t copy_size = std::min(size - offset, real_size);
44 std::copy_n(contents, copy_size, buffer + offset);
50 size_t CURL_File::pread_callback
59 const size_t real_size = size * nmemb;
60 pread_Callback_Data &callback_data = *(pread_Callback_Data *)p;
61 return callback_data.copy(
reinterpret_cast<char *
>(contents), real_size);
65 size_t CURL_File::pread(
char *buffer,
size_t size, int64_t offset)
const
68 pread_Callback_Data callback_data{buffer, size, 0};
70 error_check(curl_easy_setopt(curl, CURLOPT_WRITEDATA, &callback_data));
71 error_check(curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, pread_callback));
73 perform_range(offset, int64_t(size));
75 return callback_data.offset;
79 size_t CURL_File::copy_callback
88 const size_t real_size = size * nmemb;
89 Buffered_File &destination = *((Buffered_File *)p);
90 destination.sequential_write((
const char *)contents, real_size);
95 void CURL_File::copy_to
98 Buffered_File &destination,
103 destination.set_position(start);
105 error_check(curl_easy_setopt(curl, CURLOPT_WRITEDATA, &destination));
106 error_check(curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, copy_callback));
108 perform_range(start, size);
116 throw Exception(
"Could not initialize CURL");
123 curl_easy_cleanup(
curl);
CURL_File(const char *url, bool verbose)
@ read_existing
fails if does not exist