|
2 | 2 | #include "sentry_alloc.h" |
3 | 3 | #include "sentry_envelope.h" |
4 | 4 | #include "sentry_options.h" |
5 | | -#include "sentry_ratelimiter.h" |
6 | | -#include "sentry_string.h" |
7 | | - |
8 | | -#ifdef SENTRY_TRANSPORT_COMPRESSION |
9 | | -# include "zlib.h" |
10 | | -#endif |
11 | | - |
12 | | -#define ENVELOPE_MIME "application/x-sentry-envelope" |
13 | | -#ifdef SENTRY_TRANSPORT_COMPRESSION |
14 | | -// The headers we use are: `x-sentry-auth`, `content-type`, `content-encoding`, |
15 | | -// `content-length` |
16 | | -# define MAX_HTTP_HEADERS 4 |
17 | | -#else |
18 | | -// The headers we use are: `x-sentry-auth`, `content-type`, `content-length` |
19 | | -# define MAX_HTTP_HEADERS 3 |
20 | | -#endif |
21 | 5 |
|
22 | 6 | struct sentry_transport_s { |
23 | 7 | void (*send_envelope_func)(sentry_envelope_t *envelope, void *state); |
@@ -158,162 +142,8 @@ sentry_transport_free(sentry_transport_t *transport) |
158 | 142 | sentry_free(transport); |
159 | 143 | } |
160 | 144 |
|
161 | | -#ifdef SENTRY_UNITTEST |
162 | 145 | void * |
163 | | -sentry__transport_get_bgworker(sentry_transport_t *transport) |
| 146 | +sentry__transport_get_state(sentry_transport_t *transport) |
164 | 147 | { |
165 | | - // For HTTP transports (curl/winhttp), the transport state is the bgworker |
166 | 148 | return transport ? transport->state : NULL; |
167 | 149 | } |
168 | | -#endif |
169 | | - |
170 | | -#ifdef SENTRY_TRANSPORT_COMPRESSION |
171 | | -static bool |
172 | | -gzipped_with_compression(const char *body, const size_t body_len, |
173 | | - char **compressed_body, size_t *compressed_body_len) |
174 | | -{ |
175 | | - if (!body || body_len == 0) { |
176 | | - return false; |
177 | | - } |
178 | | - |
179 | | - z_stream stream; |
180 | | - memset(&stream, 0, sizeof(stream)); |
181 | | - stream.next_in = (unsigned char *)body; |
182 | | - stream.avail_in = (unsigned int)body_len; |
183 | | - |
184 | | - int err = deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, |
185 | | - MAX_WBITS + 16, 9, Z_DEFAULT_STRATEGY); |
186 | | - if (err != Z_OK) { |
187 | | - SENTRY_WARNF("deflateInit2 failed: %d", err); |
188 | | - return false; |
189 | | - } |
190 | | - |
191 | | - size_t len = compressBound((unsigned long)body_len); |
192 | | - char *buffer = sentry_malloc(len); |
193 | | - if (!buffer) { |
194 | | - deflateEnd(&stream); |
195 | | - return false; |
196 | | - } |
197 | | - |
198 | | - while (err == Z_OK) { |
199 | | - stream.next_out = (unsigned char *)(buffer + stream.total_out); |
200 | | - stream.avail_out = (unsigned int)(len - stream.total_out); |
201 | | - err = deflate(&stream, Z_FINISH); |
202 | | - } |
203 | | - |
204 | | - if (err != Z_STREAM_END) { |
205 | | - SENTRY_WARNF("deflate failed: %d", err); |
206 | | - sentry_free(buffer); |
207 | | - buffer = NULL; |
208 | | - deflateEnd(&stream); |
209 | | - return false; |
210 | | - } |
211 | | - |
212 | | - *compressed_body_len = stream.total_out; |
213 | | - *compressed_body = buffer; |
214 | | - |
215 | | - deflateEnd(&stream); |
216 | | - return true; |
217 | | -} |
218 | | -#endif |
219 | | - |
220 | | -sentry_prepared_http_request_t * |
221 | | -sentry__prepare_http_request(sentry_envelope_t *envelope, |
222 | | - const sentry_dsn_t *dsn, const sentry_rate_limiter_t *rl, |
223 | | - const char *user_agent) |
224 | | -{ |
225 | | - if (!dsn || !dsn->is_valid) { |
226 | | - return NULL; |
227 | | - } |
228 | | - |
229 | | - size_t body_len = 0; |
230 | | - bool body_owned = true; |
231 | | - char *body = sentry_envelope_serialize_ratelimited( |
232 | | - envelope, rl, &body_len, &body_owned); |
233 | | - if (!body) { |
234 | | - return NULL; |
235 | | - } |
236 | | - |
237 | | -#ifdef SENTRY_TRANSPORT_COMPRESSION |
238 | | - bool compressed = false; |
239 | | - char *compressed_body = NULL; |
240 | | - size_t compressed_body_len = 0; |
241 | | - compressed = gzipped_with_compression( |
242 | | - body, body_len, &compressed_body, &compressed_body_len); |
243 | | - if (compressed) { |
244 | | - if (body_owned) { |
245 | | - sentry_free(body); |
246 | | - body_owned = false; |
247 | | - } |
248 | | - body = compressed_body; |
249 | | - body_len = compressed_body_len; |
250 | | - body_owned = true; |
251 | | - } |
252 | | -#endif |
253 | | - |
254 | | - sentry_prepared_http_request_t *req |
255 | | - = SENTRY_MAKE(sentry_prepared_http_request_t); |
256 | | - if (!req) { |
257 | | - if (body_owned) { |
258 | | - sentry_free(body); |
259 | | - } |
260 | | - return NULL; |
261 | | - } |
262 | | - req->headers = sentry_malloc( |
263 | | - sizeof(sentry_prepared_http_header_t) * MAX_HTTP_HEADERS); |
264 | | - if (!req->headers) { |
265 | | - sentry_free(req); |
266 | | - if (body_owned) { |
267 | | - sentry_free(body); |
268 | | - } |
269 | | - return NULL; |
270 | | - } |
271 | | - req->headers_len = 0; |
272 | | - |
273 | | - req->method = "POST"; |
274 | | - req->url = sentry__dsn_get_envelope_url(dsn); |
275 | | - |
276 | | - sentry_prepared_http_header_t *h; |
277 | | - h = &req->headers[req->headers_len++]; |
278 | | - h->key = "x-sentry-auth"; |
279 | | - h->value = sentry__dsn_get_auth_header(dsn, user_agent); |
280 | | - |
281 | | - h = &req->headers[req->headers_len++]; |
282 | | - h->key = "content-type"; |
283 | | - h->value = sentry__string_clone(ENVELOPE_MIME); |
284 | | - |
285 | | -#ifdef SENTRY_TRANSPORT_COMPRESSION |
286 | | - if (compressed) { |
287 | | - h = &req->headers[req->headers_len++]; |
288 | | - h->key = "content-encoding"; |
289 | | - h->value = sentry__string_clone("gzip"); |
290 | | - } |
291 | | -#endif |
292 | | - |
293 | | - h = &req->headers[req->headers_len++]; |
294 | | - h->key = "content-length"; |
295 | | - h->value = sentry__int64_to_string((int64_t)body_len); |
296 | | - |
297 | | - req->body = body; |
298 | | - req->body_len = body_len; |
299 | | - req->body_owned = body_owned; |
300 | | - |
301 | | - return req; |
302 | | -} |
303 | | - |
304 | | -void |
305 | | -sentry__prepared_http_request_free(sentry_prepared_http_request_t *req) |
306 | | -{ |
307 | | - if (!req) { |
308 | | - return; |
309 | | - } |
310 | | - sentry_free(req->url); |
311 | | - for (size_t i = 0; i < req->headers_len; i++) { |
312 | | - sentry_free(req->headers[i].value); |
313 | | - } |
314 | | - sentry_free(req->headers); |
315 | | - if (req->body_owned) { |
316 | | - sentry_free(req->body); |
317 | | - } |
318 | | - sentry_free(req); |
319 | | -} |
0 commit comments