Joedb 10.2.1
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
CURL_File.cpp
Go to the documentation of this file.
4
5#include <cstring>
6
7namespace joedb
8{
9 ////////////////////////////////////////////////////////////////////////////
10 void CURL_File::error_check(CURLcode code)
11 ////////////////////////////////////////////////////////////////////////////
12 {
13 if (code != CURLE_OK)
14 throw Exception(std::string("CURL_File: ") + curl_easy_strerror(code));
15 }
16
17 ////////////////////////////////////////////////////////////////////////////
18 void CURL_File::perform_range(int64_t start, int64_t size) const
19 ////////////////////////////////////////////////////////////////////////////
20 {
21 const std::string range =
22 std::to_string(start) + '-' + std::to_string(start + size - 1);
23
24 error_check(curl_easy_setopt(curl, CURLOPT_RANGE, range.c_str()));
25 error_check(curl_easy_perform(curl));
26
27 long code = 0;
28 error_check(curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code));
29
30 if (code != 0 && code != 206)
31 throw Exception("CURL_FIle: unexpected response code: " + std::to_string(code));
32 }
33
34 ////////////////////////////////////////////////////////////////////////////
35 size_t CURL_File::pread_Callback_Data::copy
36 ////////////////////////////////////////////////////////////////////////////
37 (
38 char *contents,
39 size_t real_size
40 )
41 {
42 const size_t copy_size = std::min(size - offset, real_size);
43 std::memcpy(buffer + offset, contents, copy_size);
44 offset += copy_size;
45 return copy_size;
46 }
47
48 ////////////////////////////////////////////////////////////////////////////
49 size_t CURL_File::pread_callback
50 ////////////////////////////////////////////////////////////////////////////
51 (
52 void *contents,
53 size_t size,
54 size_t nmemb,
55 void *p
56 )
57 {
58 const size_t real_size = size * nmemb;
59 pread_Callback_Data &callback_data = *(pread_Callback_Data *)p;
60 return callback_data.copy(reinterpret_cast<char *>(contents), real_size);
61 }
62
63 ////////////////////////////////////////////////////////////////////////////
64 size_t CURL_File::pread(char *buffer, size_t size, int64_t offset) const
65 ////////////////////////////////////////////////////////////////////////////
66 {
67 pread_Callback_Data callback_data{buffer, size, 0};
68
69 error_check(curl_easy_setopt(curl, CURLOPT_WRITEDATA, &callback_data));
70 error_check(curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, pread_callback));
71
72 perform_range(offset, int64_t(size));
73
74 return callback_data.offset;
75 }
76
77 ////////////////////////////////////////////////////////////////////////////
78 size_t CURL_File::copy_callback
79 ////////////////////////////////////////////////////////////////////////////
80 (
81 void * const contents,
82 const size_t size,
83 const size_t nmemb,
84 void * const p
85 )
86 {
87 const size_t real_size = size * nmemb;
88 File_Iterator &file_iterator = *((File_Iterator *)p);
89 file_iterator.write((const char *)contents, real_size);
90 return real_size;
91 }
92
93 ////////////////////////////////////////////////////////////////////////////
94 void CURL_File::copy_to
95 ////////////////////////////////////////////////////////////////////////////
96 (
97 Abstract_File &destination,
98 const int64_t start,
99 const int64_t size
100 ) const
101 {
102 File_Iterator file_iterator(destination);
103 file_iterator.seek(start);
104 error_check(curl_easy_setopt(curl, CURLOPT_WRITEDATA, &file_iterator));
105 error_check(curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, copy_callback));
106 perform_range(start, size);
107 }
108
109 ////////////////////////////////////////////////////////////////////////////
110 CURL_Easy::CURL_Easy(): curl(curl_easy_init())
111 ////////////////////////////////////////////////////////////////////////////
112 {
113 if (curl == nullptr)
114 throw Exception("Could not initialize CURL");
115 }
116
117 ////////////////////////////////////////////////////////////////////////////
119 ////////////////////////////////////////////////////////////////////////////
120 {
121 curl_easy_cleanup(curl);
122 }
123
124 ////////////////////////////////////////////////////////////////////////////
125 CURL_File::CURL_File(const char *url, bool verbose):
126 ////////////////////////////////////////////////////////////////////////////
128 {
129 error_check(curl_easy_setopt(curl, CURLOPT_VERBOSE, verbose));
130 error_check(curl_easy_setopt(curl, CURLOPT_URL, url));
131 error_check(curl_easy_setopt(curl, CURLOPT_USERAGENT, "joedb"));
132 error_check(curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L));
133 }
134}
CURL *const curl
Definition CURL_File.h:14
CURL_File(const char *url, bool verbose)
Open_Mode
Definition Open_Mode.h:8
@ read_existing
fails if does not exist