-
Notifications
You must be signed in to change notification settings - Fork 1
/
natives.cpp
267 lines (217 loc) · 8.38 KB
/
natives.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
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
#include "globals.h"
#include <string.h>
#include <algorithm>
int g_default_timeout = 10000;
#define GET_CURL_CONTEXT_MACRO \
CurlContext* context = g_handle_manager.GetCurlPointer(params[1]); \
if(context == NULL || context->in_event_thread) \
return 2;
cell_t CurlNew(IPluginContext *pContext, const cell_t *params) {
CURL* c = curl_easy_init();
if(c == NULL)
return 0;
CurlContext* context = new CurlContext(c, pContext);
int handle = g_handle_manager.NewHandle((void*)context, HANDLE_CURL);
if(handle == 0) {
delete context;
return 0;
}
context->sourcepawn_handle = handle;
context->sourcepawn_userdata = params[1];
curl_easy_setopt(context->curl, CURLOPT_TIMEOUT_MS, g_default_timeout);
curl_easy_setopt(context->curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(context->curl, CURLOPT_MAXREDIRS, 10);
curl_easy_setopt(context->curl, CURLOPT_USERAGENT, "Sourcemod " SMEXT_CONF_NAME " " SMEXT_CONF_VERSION);
return handle;
}
cell_t Close(IPluginContext *pContext, const cell_t *params) {
CurlContext* context = g_handle_manager.GetCurlPointer(params[1]);
if(context == NULL) {
return 2;
} else if(context->in_event_thread) {
context->handle_closed = true;
} else {
g_handle_manager.FreeHandle(params[1]);
}
return 0;
}
cell_t CurlGet(IPluginContext *pContext, const cell_t *params) {
GET_CURL_CONTEXT_MACRO
char* url;
pContext->LocalToString(params[2], &url);
curl_easy_setopt(context->curl, CURLOPT_URL, url);
context->sourcepawn_callback = params[3];
AddCurlJob(context);
return 0;
}
cell_t CurlPost(IPluginContext *pContext, const cell_t *params) {
GET_CURL_CONTEXT_MACRO
char* url;
char* post;
pContext->LocalToString(params[2], &url);
pContext->LocalToString(params[3], &post);
curl_easy_setopt(context->curl, CURLOPT_URL, url);
curl_easy_setopt(context->curl, CURLOPT_POST, 1);
curl_easy_setopt(context->curl, CURLOPT_COPYPOSTFIELDS, post);
context->sourcepawn_callback = params[4];
AddCurlJob(context);
return 0;
}
cell_t CurlPostRaw(IPluginContext *pContext, const cell_t *params) {
GET_CURL_CONTEXT_MACRO
char* url;
char* post;
pContext->LocalToString(params[2], &url);
pContext->LocalToString(params[3], &post);
curl_easy_setopt(context->curl, CURLOPT_URL, url);
curl_easy_setopt(context->curl, CURLOPT_TIMEOUT_MS, g_default_timeout);
curl_easy_setopt(context->curl, CURLOPT_POST, 1);
curl_easy_setopt(context->curl, CURLOPT_POSTFIELDSIZE, params[4]);
curl_easy_setopt(context->curl, CURLOPT_COPYPOSTFIELDS, post);
context->sourcepawn_callback = params[5];
AddCurlJob(context);
return 0;
}
cell_t CurlPostRawCompress(IPluginContext *pContext, const cell_t *params) {
GET_CURL_CONTEXT_MACRO
char* url;
char* post;
pContext->LocalToString(params[2], &url);
pContext->LocalToString(params[3], &post);
int postLength = params[4];
context->post = (char*)malloc(postLength);
context->postLength = postLength;
memcpy(context->post, post, postLength);
curl_easy_setopt(context->curl, CURLOPT_URL, url);
curl_easy_setopt(context->curl, CURLOPT_TIMEOUT_MS, g_default_timeout);
curl_easy_setopt(context->curl, CURLOPT_POST, 1);
context->sourcepawn_callback = params[5];
AddCurlJob(context);
return 0;
}
cell_t CurlStart(IPluginContext *pContext, const cell_t *params) {
GET_CURL_CONTEXT_MACRO
context->sourcepawn_callback = params[2];
AddCurlJob(context);
return 0;
}
cell_t CurlGetData(IPluginContext *pContext, const cell_t *params) {
GET_CURL_CONTEXT_MACRO
char* buffer;
pContext->LocalToString(params[2], &buffer);
int sbuffer_size = params[3];
int cbuffer_size = context->buffer.size();
int min = sbuffer_size < cbuffer_size ? sbuffer_size : cbuffer_size;
memcpy(buffer, &context->buffer[0], min);
if(sbuffer_size > cbuffer_size)
buffer[cbuffer_size+1] = 0;
return 0;
}
cell_t CurlSetInt(IPluginContext *pContext, const cell_t *params) {
GET_CURL_CONTEXT_MACRO
return curl_easy_setopt(context->curl, (CURLoption)params[2], (long)params[3]);
}
cell_t CurlSetString(IPluginContext *pContext, const cell_t *params) {
GET_CURL_CONTEXT_MACRO
char* value;
pContext->LocalToString(params[3], &value);
return curl_easy_setopt(context->curl, (CURLoption)params[2], value);
}
cell_t CurlGetInt(IPluginContext *pContext, const cell_t *params) {
GET_CURL_CONTEXT_MACRO
long output;
cell_t* sp_output;
CURLcode curlcode = curl_easy_getinfo(context->curl, (CURLINFO)params[2], &output);
if(curlcode == 0) {
pContext->LocalToPhysAddr(params[3], &sp_output);
*sp_output = (cell_t)output;
}
return curlcode;
}
cell_t CurlGetString(IPluginContext *pContext, const cell_t *params) {
GET_CURL_CONTEXT_MACRO
char* result;
CURLcode curlcode = curl_easy_getinfo(context->curl, (CURLINFO)params[2], &result);
pContext->StringToLocal(params[3], params[4], result);
return curlcode;
}
cell_t CurlAddHeader(IPluginContext *pContext, const cell_t *params) {
GET_CURL_CONTEXT_MACRO
char* header;
pContext->LocalToString(params[2], &header);
context->AddHeader(header);
return 0;
}
cell_t CurlSetGlobalInt(IPluginContext *pContext, const cell_t *params) {
ChangeCurlSettings((CURLMoption)params[1], params[2]);
return 0;
}
cell_t CurlErrorString(IPluginContext *pContext, const cell_t *params) {
const char* error = curl_easy_strerror((CURLcode)params[1]);
pContext->StringToLocal(params[2], params[3], error);
return 0;
}
cell_t CurlGetErrorMessage(IPluginContext *pContext, const cell_t *params) {
GET_CURL_CONTEXT_MACRO
pContext->StringToLocal(params[2], params[3], context->curl_error_message);
return 0;
}
cell_t CurlVersion(IPluginContext *pContext, const cell_t *params) {
char* version = curl_version();
pContext->StringToLocal(params[1], params[2], version);
return 0;
}
cell_t CurlGetResponseHeaderSize(IPluginContext *pContext, const cell_t *params) {
GET_CURL_CONTEXT_MACRO
char* headerNameRaw;
pContext->LocalToString(params[2], &headerNameRaw);
std::string headerName(headerNameRaw);
std::transform(headerName.begin(), headerName.end(), headerName.begin(), ::tolower);
if(context->response_headers.find(headerName) == context->response_headers.end())
{
return -1;
}
else
{
return context->response_headers[headerName].size();
}
}
cell_t CurlGetResponseHeader(IPluginContext *pContext, const cell_t *params) {
GET_CURL_CONTEXT_MACRO
char* headerNameRaw;
pContext->LocalToString(params[2], &headerNameRaw);
std::string headerName(headerNameRaw);
std::transform(headerName.begin(), headerName.end(), headerName.begin(), ::tolower);
if(context->response_headers.find(headerName) == context->response_headers.end())
{
return -1;
}
else
{
auto result = context->response_headers[headerName];
pContext->StringToLocal(params[3], params[4], result.data());
return result.size();
}
}
sp_nativeinfo_t MyNatives[] = {
{"Async_Close", Close},
{"Async_CurlNew", CurlNew},
{"Async_CurlGet", CurlGet},
{"Async_CurlPost", CurlPost},
{"Async_CurlPostRaw", CurlPostRaw},
{"Async_CurlPostRawCompress", CurlPostRawCompress},
{"Async_CurlGetData", CurlGetData},
{"Async_CurlAddHeader", CurlAddHeader},
{"Async_CurlStart", CurlStart},
{"Async_CurlSetInt", CurlSetInt},
{"Async_CurlSetString", CurlSetString},
{"Async_CurlGetInt", CurlGetInt},
{"Async_CurlGetString", CurlGetString},
{"Async_CurlSetGlobalInt", CurlSetGlobalInt},
{"Async_CurlErrorString", CurlErrorString},
{"Async_CurlVersion", CurlVersion},
{"Async_CurlGetResponseHeaderSize", CurlGetResponseHeaderSize},
{"Async_CurlGetResponseHeader", CurlGetResponseHeader},
{"Async_CurlGetErrorMessage", CurlGetErrorMessage},
{NULL, NULL},
};