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