-
-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathsentry_transport.c
310 lines (275 loc) · 7.93 KB
/
sentry_transport.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
#include "sentry_transport.h"
#include "sentry_alloc.h"
#include "sentry_envelope.h"
#include "sentry_options.h"
#include "sentry_ratelimiter.h"
#include "sentry_string.h"
#ifdef SENTRY_TRANSPORT_COMPRESSION
# include "zlib.h"
#endif
#define ENVELOPE_MIME "application/x-sentry-envelope"
#ifdef SENTRY_TRANSPORT_COMPRESSION
// The headers we use are: `x-sentry-auth`, `content-type`, `content-encoding`,
// `content-length`
# define MAX_HTTP_HEADERS 4
#else
// The headers we use are: `x-sentry-auth`, `content-type`, `content-length`
# define MAX_HTTP_HEADERS 3
#endif
typedef struct sentry_transport_s {
void (*send_envelope_func)(sentry_envelope_t *envelope, void *state);
int (*startup_func)(const sentry_options_t *options, void *state);
int (*shutdown_func)(uint64_t timeout, void *state);
int (*flush_func)(uint64_t timeout, void *state);
void (*free_func)(void *state);
size_t (*dump_func)(sentry_run_t *run, void *state);
void *state;
bool running;
} sentry_transport_t;
sentry_transport_t *
sentry_transport_new(
void (*send_func)(sentry_envelope_t *envelope, void *state))
{
sentry_transport_t *transport = SENTRY_MAKE(sentry_transport_t);
if (!transport) {
return NULL;
}
memset(transport, 0, sizeof(sentry_transport_t));
transport->send_envelope_func = send_func;
return transport;
}
void
sentry_transport_set_state(sentry_transport_t *transport, void *state)
{
transport->state = state;
}
void
sentry_transport_set_free_func(
sentry_transport_t *transport, void (*free_func)(void *state))
{
transport->free_func = free_func;
}
void
sentry_transport_set_startup_func(sentry_transport_t *transport,
int (*startup_func)(const sentry_options_t *options, void *state))
{
transport->startup_func = startup_func;
}
void
sentry_transport_set_shutdown_func(sentry_transport_t *transport,
int (*shutdown_func)(uint64_t timeout, void *state))
{
transport->shutdown_func = shutdown_func;
}
void
sentry_transport_set_flush_func(sentry_transport_t *transport,
int (*flush_func)(uint64_t timeout, void *state))
{
transport->flush_func = flush_func;
}
void
sentry__transport_send_envelope(
sentry_transport_t *transport, sentry_envelope_t *envelope)
{
if (!envelope) {
return;
}
if (!transport) {
SENTRY_WARN("discarding envelope due to invalid transport");
sentry_envelope_free(envelope);
return;
}
SENTRY_DEBUG("sending envelope");
transport->send_envelope_func(envelope, transport->state);
}
int
sentry__transport_startup(
sentry_transport_t *transport, const sentry_options_t *options)
{
if (transport->startup_func) {
SENTRY_DEBUG("starting transport");
int rv = transport->startup_func(options, transport->state);
transport->running = rv == 0;
return rv;
}
return 0;
}
int
sentry__transport_flush(sentry_transport_t *transport, uint64_t timeout)
{
if (transport->flush_func && transport->running) {
SENTRY_DEBUG("flushing transport");
return transport->flush_func(timeout, transport->state);
}
return 0;
}
int
sentry__transport_shutdown(sentry_transport_t *transport, uint64_t timeout)
{
if (transport->shutdown_func && transport->running) {
SENTRY_DEBUG("shutting down transport");
transport->running = false;
return transport->shutdown_func(timeout, transport->state);
}
return 0;
}
void
sentry__transport_set_dump_func(sentry_transport_t *transport,
size_t (*dump_func)(sentry_run_t *run, void *state))
{
transport->dump_func = dump_func;
}
size_t
sentry__transport_dump_queue(sentry_transport_t *transport, sentry_run_t *run)
{
if (!transport || !transport->dump_func) {
return 0;
}
size_t dumped = transport->dump_func(run, transport->state);
if (dumped) {
SENTRY_DEBUGF("dumped %zu in-flight envelopes to disk", dumped);
}
return dumped;
}
void
sentry_transport_free(sentry_transport_t *transport)
{
if (!transport) {
return;
}
if (transport->free_func) {
transport->free_func(transport->state);
}
sentry_free(transport);
}
#ifdef SENTRY_TRANSPORT_COMPRESSION
static bool
gzipped_with_compression(const char *body, const size_t body_len,
char **compressed_body, size_t *compressed_body_len)
{
if (!body || body_len == 0) {
return false;
}
z_stream stream;
memset(&stream, 0, sizeof(stream));
stream.next_in = (unsigned char *)body;
stream.avail_in = (unsigned int)body_len;
int err = deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
MAX_WBITS + 16, 9, Z_DEFAULT_STRATEGY);
if (err != Z_OK) {
SENTRY_WARNF("deflateInit2 failed: %d", err);
return false;
}
size_t len = compressBound((unsigned long)body_len);
char *buffer = sentry_malloc(len);
if (!buffer) {
deflateEnd(&stream);
return false;
}
while (err == Z_OK) {
stream.next_out = (unsigned char *)(buffer + stream.total_out);
stream.avail_out = (unsigned int)(len - stream.total_out);
err = deflate(&stream, Z_FINISH);
}
if (err != Z_STREAM_END) {
SENTRY_WARNF("deflate failed: %d", err);
sentry_free(buffer);
buffer = NULL;
deflateEnd(&stream);
return false;
}
*compressed_body_len = stream.total_out;
*compressed_body = buffer;
deflateEnd(&stream);
return true;
}
#endif
sentry_prepared_http_request_t *
sentry__prepare_http_request(sentry_envelope_t *envelope,
const sentry_dsn_t *dsn, const sentry_rate_limiter_t *rl,
const char *user_agent)
{
if (!dsn || !dsn->is_valid) {
return NULL;
}
size_t body_len = 0;
bool body_owned = true;
char *body = sentry_envelope_serialize_ratelimited(
envelope, rl, &body_len, &body_owned);
if (!body) {
return NULL;
}
#ifdef SENTRY_TRANSPORT_COMPRESSION
bool compressed = false;
char *compressed_body = NULL;
size_t compressed_body_len = 0;
compressed = gzipped_with_compression(
body, body_len, &compressed_body, &compressed_body_len);
if (compressed) {
if (body_owned) {
sentry_free(body);
body_owned = false;
}
body = compressed_body;
body_len = compressed_body_len;
body_owned = true;
}
#endif
sentry_prepared_http_request_t *req
= SENTRY_MAKE(sentry_prepared_http_request_t);
if (!req) {
if (body_owned) {
sentry_free(body);
}
return NULL;
}
req->headers = sentry_malloc(
sizeof(sentry_prepared_http_header_t) * MAX_HTTP_HEADERS);
if (!req->headers) {
sentry_free(req);
if (body_owned) {
sentry_free(body);
}
return NULL;
}
req->headers_len = 0;
req->method = "POST";
req->url = sentry__dsn_get_envelope_url(dsn);
sentry_prepared_http_header_t *h;
h = &req->headers[req->headers_len++];
h->key = "x-sentry-auth";
h->value = sentry__dsn_get_auth_header(dsn, user_agent);
h = &req->headers[req->headers_len++];
h->key = "content-type";
h->value = sentry__string_clone(ENVELOPE_MIME);
#ifdef SENTRY_TRANSPORT_COMPRESSION
if (compressed) {
h = &req->headers[req->headers_len++];
h->key = "content-encoding";
h->value = sentry__string_clone("gzip");
}
#endif
h = &req->headers[req->headers_len++];
h->key = "content-length";
h->value = sentry__int64_to_string((int64_t)body_len);
req->body = body;
req->body_len = body_len;
req->body_owned = body_owned;
return req;
}
void
sentry__prepared_http_request_free(sentry_prepared_http_request_t *req)
{
if (!req) {
return;
}
sentry_free(req->url);
for (size_t i = 0; i < req->headers_len; i++) {
sentry_free(req->headers[i].value);
}
sentry_free(req->headers);
if (req->body_owned) {
sentry_free(req->body);
}
sentry_free(req);
}