-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.hpp
284 lines (206 loc) · 6.23 KB
/
lib.hpp
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#pragma once
#include "nlohmann/json.hpp"
#include <boost/format.hpp>
#include <cstddef>
#include <cstdio>
#include <cstring>
#include <curl/curl.h>
#include <curl/easy.h>
#include <memory>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
#include <wren.hpp>
using json = nlohmann::json;
namespace lib {
void abort(WrenVM* vm, const std::string& msg);
std::string wren_type_to_string(const WrenType& type);
namespace fs {
/// Allocator for the std/fs/File class.
void fileAlloc(WrenVM* vm);
/// Deallocates File data.
void fileFinalize(void* data);
void fileWrite(WrenVM* vm);
void fileRead(WrenVM* vm);
void fileClose(WrenVM* vm);
void cwd(WrenVM* vm);
void canonical(WrenVM* vm);
void exists(WrenVM* vm);
void separator(WrenVM* vm);
void removeFile(WrenVM* vm);
void list_all_recursive(WrenVM* vm);
void list_all(WrenVM* vm);
void isDirectory(WrenVM* vm);
void mkdir(WrenVM* vm);
void extension(WrenVM* vm);
void basename(WrenVM* vm);
void filename(WrenVM* vm);
} // namespace fs
namespace os {
void getEnv(WrenVM* vm);
void setEnv(WrenVM* vm);
struct ArgHolder {
int argCount;
std::vector<std::unique_ptr<char[]>> argsStorage;
std::unique_ptr<char*[]> args;
ArgHolder(int argc, char** argv)
: argCount(argc)
, args(new char*[argc]) {
argsStorage.reserve(argc);
for (int i = 0; i < argc; ++i) {
size_t length = std::strlen(argv[i]) + 1; // +1 for null terminator
argsStorage.push_back(std::make_unique<char[]>(length));
std::strcpy(argsStorage.back().get(), argv[i]);
args[i] = argsStorage.back().get();
}
}
};
void processArguments(WrenVM* vm);
void cwd(WrenVM* vm);
void pid(WrenVM* vm);
void ppid(WrenVM* vm);
void runtimeOS(WrenVM* vm);
void runtimeArch(WrenVM* vm);
void processExec(WrenVM* vm);
void processExit(WrenVM* vm);
void typeOf(WrenVM* vm);
}
namespace net::http {
inline size_t writeFunction(void* ptr, const size_t size, const size_t nmemb, std::string* data) {
data->append(static_cast<char*>(ptr), size * nmemb);
return size * nmemb;
}
struct Client {
Client() {
this->curl = curl_easy_init();
if (this->curl == nullptr) {
throw std::runtime_error("Failed to initialize CURL");
}
this->header_list = nullptr;
// Default CURL arguments:
this->followLocation(true);
this->autoReferer(true);
this->noProgress(true);
this->maxRedirects();
this->keepTCPAlive(true);
}
~Client() {
if (this->curl == nullptr) {
return;
}
if (this->header_list != nullptr) {
// Cleans up header list on CURL client
curl_slist_free_all(this->header_list);
}
curl_easy_cleanup(this->curl);
this->curl = nullptr;
}
enum class HttpMethod { GET, POST, PUT, DELETE, HEAD, OPTIONS, PATCH, TRACE };
void header(std::string const& key, std::string const& val) {
std::string newBuff = (boost::format("%1%: %2%") % key % val).str();
this->header_list = curl_slist_append(this->header_list, newBuff.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, this->header_list);
}
void url(std::string const& url) const {
curl_easy_setopt(this->curl, CURLOPT_URL, url.c_str());
}
void followLocation(const bool answer) const {
curl_easy_setopt(this->curl, CURLOPT_FOLLOWLOCATION, (answer) ? 1L : 0L);
}
void autoReferer(const bool answer) const {
curl_easy_setopt(this->curl, CURLOPT_AUTOREFERER, (answer) ? 1L : 0L);
}
void noProgress(const bool answer) const {
curl_easy_setopt(this->curl, CURLOPT_NOPROGRESS, (answer) ? 1L : 0L);
}
void maxRedirects(const long answer = 50L) const {
curl_easy_setopt(this->curl, CURLOPT_MAXREDIRS, answer);
}
void keepTCPAlive(const bool answer) const {
curl_easy_setopt(this->curl, CURLOPT_TCP_KEEPALIVE, (answer) ? 1L : 0L);
}
void method(const HttpMethod method) const {
std::string method_str;
try {
method_str = this->getHttpMethodString(method);
} catch (std::invalid_argument& e) {
method_str = "GET";
}
curl_easy_setopt(this->curl, CURLOPT_CUSTOMREQUEST, method_str.c_str());
}
void body(std::string const& data) {
this->_body = data;
// Specify the data to send
curl_easy_setopt(this->curl, CURLOPT_POSTFIELDS, this->_body.c_str());
// Set the size of the data to send
curl_easy_setopt(this->curl, CURLOPT_POSTFIELDSIZE, this->_body.length());
}
/**
* fetch(string? url) returns a pair<header, resp> containing the HTTP response headers and body.
* @throws std::runtime_error
*/
[[nodiscard]] std::pair<std::string, std::string> fetch(std::string const& url = "") const {
if (!url.empty()) {
this->url(url);
}
std::string response_string;
std::string header_string;
curl_easy_setopt(this->curl, CURLOPT_WRITEFUNCTION, writeFunction);
curl_easy_setopt(this->curl, CURLOPT_WRITEDATA, &response_string);
curl_easy_setopt(this->curl, CURLOPT_HEADERDATA, &header_string);
CURLcode res = curl_easy_perform(this->curl);
if (res != CURLE_OK) {
throw std::runtime_error((boost::format("CURL Error: %1%") % curl_easy_strerror(res)).str().c_str());
}
return std::make_pair(header_string, response_string);
}
private:
CURL* curl;
std::string _url;
curl_slist* header_list;
std::string _body;
static std::string getHttpMethodString(HttpMethod method) {
switch (method) {
case HttpMethod::GET:
return "GET";
case HttpMethod::POST:
return "POST";
case HttpMethod::PUT:
return "PUT";
case HttpMethod::DELETE:
return "DELETE";
case HttpMethod::HEAD:
return "HEAD";
case HttpMethod::OPTIONS:
return "OPTIONS";
case HttpMethod::PATCH:
return "PATCH";
case HttpMethod::TRACE:
return "TRACE";
default:
throw std::invalid_argument("Invalid HTTP method");
}
}
};
void requestAlloc(WrenVM* vm);
void requestDealloc(void* data);
void send(WrenVM* vm);
void setMethod(WrenVM* vm);
void setHeader(WrenVM* vm);
void setBody(WrenVM* vm);
}
namespace encoding {
void base64_encode(WrenVM* vm);
void base64_decode(WrenVM* vm);
void base32_encode(WrenVM* vm);
void base32_decode(WrenVM* vm);
void base16_encode(WrenVM* vm);
void base16_decode(WrenVM* vm);
void md_to_html(WrenVM* vm);
}
namespace regex {
void match(WrenVM* vm);
void contains(WrenVM* vm);
}
} // namespace lib