-
-
Notifications
You must be signed in to change notification settings - Fork 169
/
sentry_transport_curl.c
316 lines (275 loc) · 9.75 KB
/
sentry_transport_curl.c
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include "sentry_alloc.h"
#include "sentry_core.h"
#include "sentry_database.h"
#include "sentry_envelope.h"
#include "sentry_options.h"
#include "sentry_ratelimiter.h"
#include "sentry_string.h"
#include "sentry_sync.h"
#include "sentry_transport.h"
#include "sentry_utils.h"
#include <curl/curl.h>
#include <curl/easy.h>
#include <string.h>
typedef struct curl_transport_state_s {
sentry_dsn_t *dsn;
CURL *curl_handle;
char *user_agent;
char *proxy;
char *ca_certs;
sentry_rate_limiter_t *ratelimiter;
bool debug;
} curl_bgworker_state_t;
struct header_info {
char *x_sentry_rate_limits;
char *retry_after;
};
static curl_bgworker_state_t *
sentry__curl_bgworker_state_new(void)
{
curl_bgworker_state_t *state = SENTRY_MAKE(curl_bgworker_state_t);
if (!state) {
return NULL;
}
memset(state, 0, sizeof(curl_bgworker_state_t));
state->ratelimiter = sentry__rate_limiter_new();
return state;
}
static void
sentry__curl_bgworker_state_free(void *_state)
{
curl_bgworker_state_t *state = _state;
if (state->curl_handle) {
curl_easy_cleanup(state->curl_handle);
curl_global_cleanup();
}
sentry__dsn_decref(state->dsn);
sentry__rate_limiter_free(state->ratelimiter);
sentry_free(state->ca_certs);
sentry_free(state->user_agent);
sentry_free(state->proxy);
sentry_free(state);
}
static int
sentry__curl_transport_start(
const sentry_options_t *options, void *transport_state)
{
static bool curl_initialized = false;
if (!curl_initialized) {
CURLcode rv = curl_global_init(CURL_GLOBAL_ALL);
if (rv != CURLE_OK) {
SENTRY_WARNF("`curl_global_init` failed with code `%d`", (int)rv);
return 1;
}
curl_version_info_data *version_data
= curl_version_info(CURLVERSION_NOW);
if (!version_data) {
SENTRY_WARN("Failed to retrieve `curl_version_info`");
return 1;
}
sentry_version_t curl_version = {
.major = (version_data->version_num >> 16) & 0xff,
.minor = (version_data->version_num >> 8) & 0xff,
.patch = version_data->version_num & 0xff,
};
if (!sentry__check_min_version(
curl_version, (sentry_version_t) { 7, 10, 7 })) {
SENTRY_WARNF("`libcurl` is at unsupported version `%u.%u.%u`",
curl_version.major, curl_version.minor, curl_version.patch);
return 1;
}
if ((version_data->features & CURL_VERSION_ASYNCHDNS) == 0) {
SENTRY_WARN("`libcurl` was not compiled with feature `AsynchDNS`");
return 1;
}
}
sentry_bgworker_t *bgworker = (sentry_bgworker_t *)transport_state;
curl_bgworker_state_t *state = sentry__bgworker_get_state(bgworker);
state->dsn = sentry__dsn_incref(options->dsn);
state->proxy = sentry__string_clone(options->proxy);
state->user_agent = sentry__string_clone(options->user_agent);
state->ca_certs = sentry__string_clone(options->ca_certs);
state->curl_handle = curl_easy_init();
state->debug = options->debug;
sentry__bgworker_setname(bgworker, options->transport_thread_name);
if (!state->curl_handle) {
// In this case we don’t start the worker at all, which means we can
// still dump all unsent envelopes to disk on shutdown.
SENTRY_WARN("`curl_easy_init` failed");
return 1;
}
return sentry__bgworker_start(bgworker);
}
static int
sentry__curl_transport_flush(uint64_t timeout, void *transport_state)
{
sentry_bgworker_t *bgworker = (sentry_bgworker_t *)transport_state;
return sentry__bgworker_flush(bgworker, timeout);
}
static int
sentry__curl_transport_shutdown(uint64_t timeout, void *transport_state)
{
sentry_bgworker_t *bgworker = (sentry_bgworker_t *)transport_state;
return sentry__bgworker_shutdown(bgworker, timeout);
}
static size_t
swallow_data(
char *UNUSED(ptr), size_t size, size_t nmemb, void *UNUSED(userdata))
{
return size * nmemb;
}
static size_t
header_callback(char *buffer, size_t size, size_t nitems, void *userdata)
{
size_t bytes = size * nitems;
struct header_info *info = userdata;
char *header = sentry__string_clone_n(buffer, bytes);
if (!header) {
return bytes;
}
char *sep = strchr(header, ':');
if (sep) {
*sep = '\0';
sentry__string_ascii_lower(header);
if (sentry__string_eq(header, "retry-after")) {
info->retry_after = sentry__string_clone(sep + 1);
} else if (sentry__string_eq(header, "x-sentry-rate-limits")) {
info->x_sentry_rate_limits = sentry__string_clone(sep + 1);
}
}
sentry_free(header);
return bytes;
}
static void
sentry__curl_send_task(void *_envelope, void *_state)
{
sentry_envelope_t *envelope = (sentry_envelope_t *)_envelope;
curl_bgworker_state_t *state = (curl_bgworker_state_t *)_state;
sentry_prepared_http_request_t *req = sentry__prepare_http_request(
envelope, state->dsn, state->ratelimiter, state->user_agent);
if (!req) {
return;
}
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "expect:");
for (size_t i = 0; i < req->headers_len; i++) {
char buf[512];
size_t written = (size_t)snprintf(buf, sizeof(buf), "%s:%s",
req->headers[i].key, req->headers[i].value);
if (written >= sizeof(buf)) {
continue;
}
buf[written] = '\0';
headers = curl_slist_append(headers, buf);
}
CURL *curl = state->curl_handle;
curl_easy_reset(curl);
if (state->debug) {
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, stderr);
// CURLOPT_WRITEFUNCTION will `fwrite` by default
} else {
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, swallow_data);
}
curl_easy_setopt(curl, CURLOPT_URL, req->url);
curl_easy_setopt(curl, CURLOPT_POST, (long)1);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, req->body);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)req->body_len);
curl_easy_setopt(curl, CURLOPT_USERAGENT, SENTRY_SDK_USER_AGENT);
char error_buf[CURL_ERROR_SIZE];
error_buf[0] = 0;
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buf);
struct header_info info;
info.retry_after = NULL;
info.x_sentry_rate_limits = NULL;
curl_easy_setopt(curl, CURLOPT_HEADERDATA, (void *)&info);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback);
if (state->proxy) { // TODO do we need to process the proxy here?
curl_easy_setopt(curl, CURLOPT_PROXY, state->proxy);
}
if (state->ca_certs) {
curl_easy_setopt(curl, CURLOPT_CAINFO, state->ca_certs);
}
CURLcode rv = curl_easy_perform(curl);
if (rv == CURLE_OK) {
long response_code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
if (info.x_sentry_rate_limits) {
sentry__rate_limiter_update_from_header(
state->ratelimiter, info.x_sentry_rate_limits);
} else if (info.retry_after) {
sentry__rate_limiter_update_from_http_retry_after(
state->ratelimiter, info.retry_after);
} else if (response_code == 429) {
sentry__rate_limiter_update_from_429(state->ratelimiter);
}
} else {
size_t len = strlen(error_buf);
if (len) {
if (error_buf[len - 1] == '\n') {
error_buf[len - 1] = 0;
}
SENTRY_WARNF("`curl_easy_perform` failed with code `%d`: %s",
(int)rv, error_buf);
} else {
SENTRY_WARNF("`curl_easy_perform` failed with code `%d`: %s",
(int)rv, curl_easy_strerror(rv));
}
}
curl_slist_free_all(headers);
sentry_free(info.retry_after);
sentry_free(info.x_sentry_rate_limits);
sentry__prepared_http_request_free(req);
}
static void
sentry__curl_transport_send_envelope(
sentry_envelope_t *envelope, void *transport_state)
{
sentry_bgworker_t *bgworker = (sentry_bgworker_t *)transport_state;
sentry__bgworker_submit(bgworker, sentry__curl_send_task,
(void (*)(void *))sentry_envelope_free, envelope);
}
static bool
sentry__curl_dump_task(void *envelope, void *run)
{
sentry__run_write_envelope(
(sentry_run_t *)run, (sentry_envelope_t *)envelope);
return true;
}
size_t
sentry__curl_dump_queue(sentry_run_t *run, void *transport_state)
{
sentry_bgworker_t *bgworker = (sentry_bgworker_t *)transport_state;
return sentry__bgworker_foreach_matching(
bgworker, sentry__curl_send_task, sentry__curl_dump_task, run);
}
sentry_transport_t *
sentry__transport_new_default(void)
{
SENTRY_DEBUG("initializing curl transport");
curl_bgworker_state_t *state = sentry__curl_bgworker_state_new();
if (!state) {
return NULL;
}
sentry_bgworker_t *bgworker
= sentry__bgworker_new(state, sentry__curl_bgworker_state_free);
if (!bgworker) {
return NULL;
}
sentry_transport_t *transport
= sentry_transport_new(sentry__curl_transport_send_envelope);
if (!transport) {
sentry__bgworker_decref(bgworker);
return NULL;
}
sentry_transport_set_state(transport, bgworker);
sentry_transport_set_free_func(
transport, (void (*)(void *))sentry__bgworker_decref);
sentry_transport_set_startup_func(transport, sentry__curl_transport_start);
sentry_transport_set_flush_func(transport, sentry__curl_transport_flush);
sentry_transport_set_shutdown_func(
transport, sentry__curl_transport_shutdown);
sentry__transport_set_dump_func(transport, sentry__curl_dump_queue);
return transport;
}