A simple scripting language in C++
Ferenc Szontágh
2025-04-19 bc2e09a3b7a4e414814b56be71ec5c540b8eb4d9
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
// CurlModule implementation: HTTP GET and POST using libcurl
#include "CurlModule.hpp"
#include "Modules/ModuleManager.hpp"
#include "Symbols/Value.hpp"
#include <curl/curl.h>
#include <stdexcept>
#include <string>
#include <vector>
#include <algorithm>
 
// Callback for libcurl to write received data into a std::string
static size_t write_callback(void* ptr, size_t size, size_t nmemb, void* userdata) {
    auto* buffer = static_cast<std::string*>(userdata);
    buffer->append(static_cast<char*>(ptr), size * nmemb);
    return size * nmemb;
}
 
// Register module functions
void Modules::CurlModule::registerModule() {
    auto& mgr = Modules::ModuleManager::instance();
    // Register HTTP GET: curlGet(url)
    mgr.registerFunction("curlGet", [this](const std::vector<Symbols::Value>& args) -> Symbols::Value {
        return this->curlGet(args);
    });
    // Register HTTP POST: curlPost(url, data)
    mgr.registerFunction("curlPost", [this](const std::vector<Symbols::Value>& args) -> Symbols::Value {
        return this->curlPost(args);
    });
}
 
 
Symbols::Value Modules::CurlModule::curlPost(const std::vector<Symbols::Value>& args) {
    // curlPost: url, data [, options]
    if (args.size() < 2 || args.size() > 3) {
        throw std::runtime_error("curlPost: expects url, data, and optional options object");
    }
    std::string url  = Symbols::Value::to_string(args[0]);
    std::string data = Symbols::Value::to_string(args[1]);
    struct curl_slist *headers = nullptr;
    long timeoutSec = 0;
    bool follow = false;
    bool haveContentType = false;
    if (args.size() == 3) {
        using namespace Symbols;
        if (args[2].getType() != Variables::Type::OBJECT) {
            throw std::runtime_error("curlPost: options must be object");
        }
        const auto & obj = std::get<Value::ObjectMap>(args[2].get());
        for (const auto & kv : obj) {
            const std::string & key = kv.first;
            const Value & v = kv.second;
            if (key == "timeout") {
                using namespace Variables;
                switch (v.getType()) {
                    case Type::INTEGER:
                        timeoutSec = v.get<int>(); break;
                    case Type::DOUBLE:
                        timeoutSec = static_cast<long>(v.get<double>()); break;
                    case Type::FLOAT:
                        timeoutSec = static_cast<long>(v.get<float>()); break;
                    default:
                        throw std::runtime_error("curlPost: timeout must be number");
                }
            } else if (key == "follow_redirects") {
                if (v.getType() != Variables::Type::BOOLEAN) {
                    throw std::runtime_error("curlPost: follow_redirects must be boolean");
                }
                follow = v.get<bool>();
            } else if (key == "headers") {
                if (v.getType() != Variables::Type::OBJECT) {
                    throw std::runtime_error("curlPost: headers must be object");
                }
                const auto & hobj = std::get<Value::ObjectMap>(v.get());
                for (const auto & hk : hobj) {
                    if (hk.second.getType() != Variables::Type::STRING) {
                        throw std::runtime_error("curlPost: header values must be string");
                    }
                    std::string hdr = hk.first + ": " + hk.second.get<std::string>();
                    std::string lower = hk.first;
                    std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
                    if (lower == "content-type") {
                        haveContentType = true;
                    }
                    headers = curl_slist_append(headers, hdr.c_str());
                }
            } else {
                throw std::runtime_error("curlPost: unknown option '" + key + "'");
            }
        }
    }
    CURL * curl = curl_easy_init();
    if (!curl) {
        if (headers) curl_slist_free_all(headers);
        throw std::runtime_error("curl: failed to initialize");
    }
    std::string response;
    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
    if (timeoutSec > 0) {
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeoutSec);
    }
    if (follow) {
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
    }
    if (!haveContentType) {
        headers = curl_slist_append(headers, "Content-Type: application/json");
    }
    if (headers) {
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    }
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
    CURLcode res = curl_easy_perform(curl);
    if (headers) curl_slist_free_all(headers);
    if (res != CURLE_OK) {
        std::string error = curl_easy_strerror(res);
        curl_easy_cleanup(curl);
        throw std::runtime_error("curl: request failed: " + error);
    }
    curl_easy_cleanup(curl);
    return Symbols::Value(response);
}
 
Symbols::Value Modules::CurlModule::curlGet(const std::vector<Symbols::Value>& args) {
    // curlGet: url [, options]
    if (args.size() < 1 || args.size() > 2) {
        throw std::runtime_error("curlGet: expects url and optional options object");
    }
    std::string url = Symbols::Value::to_string(args[0]);
    // parse options
    struct curl_slist *headers = nullptr;
    long timeoutSec = 0;
    bool follow = false;
    if (args.size() == 2) {
        using namespace Symbols;
        if (args[1].getType() != Variables::Type::OBJECT) {
            throw std::runtime_error("curlGet: options must be object");
        }
        const auto & obj = std::get<Value::ObjectMap>(args[1].get());
        for (const auto & kv : obj) {
            const std::string & key = kv.first;
            const Value & v = kv.second;
            if (key == "timeout") {
                using namespace Variables;
                switch (v.getType()) {
                    case Type::INTEGER:
                        timeoutSec = v.get<int>();
                        break;
                    case Type::DOUBLE:
                        timeoutSec = static_cast<long>(v.get<double>());
                        break;
                    case Type::FLOAT:
                        timeoutSec = static_cast<long>(v.get<float>());
                        break;
                    default:
                        throw std::runtime_error("curlGet: timeout must be number");
                }
            } else if (key == "follow_redirects") {
                if (v.getType() != Symbols::Variables::Type::BOOLEAN) {
                    throw std::runtime_error("curlGet: follow_redirects must be boolean");
                }
                follow = v.get<bool>();
            } else if (key == "headers") {
                if (v.getType() != Symbols::Variables::Type::OBJECT) {
                    throw std::runtime_error("curlGet: headers must be object");
                }
                const auto & hobj = std::get<Value::ObjectMap>(v.get());
                for (const auto & hk : hobj) {
                    if (hk.second.getType() != Symbols::Variables::Type::STRING) {
                        throw std::runtime_error("curlGet: header values must be string");
                    }
                    std::string line = hk.first + ": " + hk.second.get<std::string>();
                    headers = curl_slist_append(headers, line.c_str());
                }
            } else {
                throw std::runtime_error("curlGet: unknown option '" + key + "'");
            }
        }
    }
    // initialize handle
    CURL * curl = curl_easy_init();
    if (!curl) {
        if (headers) curl_slist_free_all(headers);
        throw std::runtime_error("curl: failed to initialize");
    }
    std::string response;
    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
    if (timeoutSec > 0) {
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeoutSec);
    }
    if (follow) {
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
    }
    if (headers) {
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    }
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
    CURLcode res = curl_easy_perform(curl);
    if (headers) {
        curl_slist_free_all(headers);
    }
    if (res != CURLE_OK) {
        std::string error = curl_easy_strerror(res);
        curl_easy_cleanup(curl);
        throw std::runtime_error("curl: request failed: " + error);
    }
    curl_easy_cleanup(curl);
    return Symbols::Value(response);
}