-
Notifications
You must be signed in to change notification settings - Fork 11
/
netutils.re2c.cpp
231 lines (203 loc) · 7.17 KB
/
netutils.re2c.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "netutils.hpp"
#include <numeric>
static std::string tos(const char* b, const char* e)
{
return (b && e && b < e) ? std::string(b, e) : std::string();
}
uri::uri(const std::string& u)
{
const char* YYCURSOR = u.c_str();
const char* YYMARKER = YYCURSOR;
const char *scheme_b = nullptr, *scheme_e = nullptr;
const char *authority_b = nullptr, *authority_e = nullptr;
const char *path_b = nullptr, *path_e = nullptr;
const char *query_b = nullptr, *query_e = nullptr;
const char *fragment_b = nullptr, *fragment_e = nullptr;
/*!stags:re2c format = "const char * @@;"; */
/*!stags:re2c format = "@@ = nullptr;"; */
/*!re2c
re2c:flags:tags = 1;
re2c:yyfill:enable = 0;
re2c:define:YYCTYPE = char;
re2c:flags:bit-vectors = 1;
* {return;}
( @scheme_b [^:/?#\x00]+ @scheme_e ":")?
("//" @authority_b [^/?#\x00]* @authority_e )?
(@path_b [^?#\x00]* @path_e )
("?" @query_b [^#\x00]* @query_e )?
("#" @fragment_b [^\x00]* @fragment_e )? {
m_scheme = tos(scheme_b, scheme_e);
m_authority = tos(authority_b, authority_e);
m_path = tos(path_b, path_e);
m_query = tos(query_b, query_e);
m_fragment = tos(fragment_b, fragment_e);
return;
}
*/
}
std::string uri::merge(const std::string& path) const
{
// If the base URI has a defined authority component and an empty
// path, then return a string consisting of "/" concatenated with the
// reference's path
if (!m_authority.empty() && m_path.empty()) {
return "/" + path;
}
// return a string consisting of the reference's path component
// appended to all but the last segment of the base URI's path (i.e.,
// excluding any characters after the right-most "/" in the base URI
// path, or excluding the entire base URI path if it does not contain
// any "/" characters).
auto end = m_path.find_last_of("/");
if (std::string::npos != end) {
return m_path.substr(0, end + 1) + path;
}
return path;
}
static std::string remove_dot_segments(const std::string& path)
{
// While the input buffer is not empty, loop as follows:
const char* YYCURSOR = path.c_str();
const char* YYMARKER = YYCURSOR;
std::vector<std::string> segments;
const char *a = nullptr, *b = nullptr;
while (true) {
/*!stags:re2c format = "const char * @@;"; */
/*!stags:re2c format = "@@ = nullptr;"; */
/*!re2c
re2c:flags:tags = 1;
re2c:yyfill:enable = 0;
re2c:define:YYCTYPE = char;
re2c:flags:bit-vectors = 1;
// End of string
"\x00" { goto done; }
"/" / "/" { continue; } // Multiple slashes, eat one
* { return std::string(); }
// A. If the input buffer begins with a prefix of "../" or "./",
// then remove that prefix from the input buffer; otherwise,
"."{1,2} / "/" {
continue;
}
// B. if the input buffer begins with a prefix of "/./" or "/.",
// where "." is a complete path segment, then replace that
// prefix with "/" in the input buffer; otherwise,
"/." / [/\x00] {
continue;
}
// C. if the input buffer begins with a prefix of "/../" or "/..",
// where ".." is a complete path segment, then replace that
// prefix with "/" in the input buffer and remove the last
// segment and its preceding "/" (if any) from the output
// buffer; otherwise,
"/.." / [/\x00] {
if(!segments.empty()) {
segments.pop_back();
}
continue;
}
// D. if the input buffer consists only of "." or "..", then remove
// that from the input buffer; otherwise,
"."{1,2} "\x00" {
continue;
}
// E. move the first path segment in the input buffer to the end of
// the output buffer, including the initial "/" character (if
// any) and any subsequent characters up to, but not including,
// the next "/" character or the end of the input buffer.
path_segment = @a [^?#\x00][^/?#\x00]* @b;
path_segment / [/\x00] {
segments.emplace_back( std::string(a, b) );
continue;
}
*/
}
done:
return std::accumulate(segments.begin(), segments.end(), std::string(""));
}
uri uri::resolve(uri u) const
{
if (u.m_scheme.empty()) {
u.m_scheme = m_scheme;
if (u.m_authority.empty()) {
u.m_authority = m_authority;
if (u.m_path.empty()) {
u.m_path = m_path;
if (u.m_query.empty()) {
u.m_query = m_query;
}
} else if ('/' != u.m_path.front()) {
u.m_path = merge(u.m_path);
}
}
u.m_path = remove_dot_segments(u.m_path);
}
return u;
}
std::string uri::string() const
{
std::string str;
str += !m_scheme.empty() ? m_scheme + "://" : std::string();
str += m_authority;
str += !m_authority.empty() && m_path.empty() ? std::string("/") : m_path;
str += !m_query.empty() ? "?" + m_query : std::string();
str += !m_fragment.empty() ? "#" + m_fragment : std::string();
return str;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
size_t _write_callback(char* ptr, size_t size, size_t nmemb, void* userdata)
{
size *= nmemb;
auto callback = static_cast<std::function<bool(char*, size_t)>*>(userdata);
if ((*callback)(ptr, size)) {
return size;
}
return 0;
}
int net::download(const uri& u, unsigned int offset, unsigned int size, const std::function<bool(const char*, unsigned int)>& callback)
{
long response_code = 0;
CURL* curl = nullptr;
CURLcode err = CURLE_OK;
char range[32];
if (!(curl = curl_easy_init())) {
err = CURLE_FAILED_INIT;
curl_easy_reset(curl);
return -err;
}
if (CURLE_OK != (err = curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L))) {
curl_easy_reset(curl);
return -err;
}
if (CURLE_OK != (err = curl_easy_setopt(curl, CURLOPT_URL, u.string().c_str()))) {
curl_easy_reset(curl);
return -err;
}
if (CURLE_OK != (err = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _write_callback))) {
curl_easy_reset(curl);
return -err;
}
if (CURLE_OK != (err = curl_easy_setopt(curl, CURLOPT_WRITEDATA, static_cast<const void*>(&callback)))) {
curl_easy_reset(curl);
return -err;
}
if (0 < offset || 0 < size) {
if (0 < size) {
snprintf(range, sizeof(range), "%u-%u", offset, size);
} else {
snprintf(range, sizeof(range), "%u-", offset);
}
if (CURLE_OK != (err = curl_easy_setopt(curl, CURLOPT_RANGE, range))) {
curl_easy_reset(curl);
return -err;
}
}
if (CURLE_OK != (err = curl_easy_perform(curl))) {
curl_easy_reset(curl);
return -err;
}
if (CURLE_OK != (err = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code))) {
curl_easy_reset(curl);
return -err;
}
return response_code;
}