Skip to content

Commit 036fbad

Browse files
committed
modify some code
1 parent 3e74fd5 commit 036fbad

File tree

2 files changed

+51
-55
lines changed

2 files changed

+51
-55
lines changed

src/sentry_transport.c

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,54 @@ sentry_transport_free(sentry_transport_t *transport)
150150
sentry_free(transport);
151151
}
152152

153+
static bool
154+
gzipped_with_compression(const char *body, const size_t body_len,
155+
char **compressed_body, size_t *compressed_body_len)
156+
{
157+
if (!body || body_len == 0) {
158+
return false;
159+
}
160+
161+
z_stream stream;
162+
memset(&stream, 0, sizeof(stream));
163+
stream.next_in = body;
164+
stream.avail_in = body_len;
165+
166+
int err = deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
167+
MAX_WBITS + 16, 9, Z_DEFAULT_STRATEGY);
168+
if (err != Z_OK) {
169+
SENTRY_TRACEF("deflateInit2 failed: %d", err);
170+
return false;
171+
}
172+
173+
size_t len = compressBound(body_len);
174+
char *buffer = sentry_malloc(len);
175+
if (!buffer) {
176+
deflateEnd(&stream);
177+
return false;
178+
}
179+
180+
while (err == Z_OK) {
181+
stream.next_out = buffer + stream.total_out;
182+
stream.avail_out = len - stream.total_out;
183+
err = deflate(&stream, Z_FINISH);
184+
}
185+
186+
if (err != Z_STREAM_END) {
187+
SENTRY_TRACEF("deflate failed: %d", err);
188+
sentry_free(buffer);
189+
buffer = NULL;
190+
deflateEnd(&stream);
191+
return false;
192+
}
193+
194+
*compressed_body_len = stream.total_out;
195+
*compressed_body = buffer;
196+
197+
deflateEnd(&stream);
198+
return true;
199+
}
200+
153201
sentry_prepared_http_request_t *
154202
sentry__prepare_http_request(sentry_envelope_t *envelope,
155203
const sentry_dsn_t *dsn, const sentry_rate_limiter_t *rl,
@@ -169,14 +217,16 @@ sentry__prepare_http_request(sentry_envelope_t *envelope,
169217

170218
char *compressed_body = NULL;
171219
size_t compressed_body_len = 0;
172-
bool compressed = sentry__gzipped_with_compression(
220+
bool compressed = gzipped_with_compression(
173221
body, body_len, &compressed_body, &compressed_body_len);
174222
if (compressed) {
175223
if (body_owned) {
176224
sentry_free(body);
225+
body_owned = false;
177226
}
178227
body = compressed_body;
179228
body_len = compressed_body_len;
229+
body_owned = true;
180230
}
181231

182232
sentry_prepared_http_request_t *req
@@ -243,51 +293,3 @@ sentry__prepared_http_request_free(sentry_prepared_http_request_t *req)
243293
}
244294
sentry_free(req);
245295
}
246-
247-
bool
248-
sentry__gzipped_with_compression(const char *body, const size_t body_len,
249-
char **compressed_body, size_t *compressed_body_len)
250-
{
251-
if (!body || body_len == 0) {
252-
return false;
253-
}
254-
255-
z_stream stream;
256-
memset(&stream, 0, sizeof(stream));
257-
stream.next_in = body;
258-
stream.avail_in = body_len;
259-
260-
int err = deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
261-
MAX_WBITS + 16, 9, Z_DEFAULT_STRATEGY);
262-
if (err != Z_OK) {
263-
SENTRY_TRACEF("deflateInit2 failed: %d", err);
264-
return false;
265-
}
266-
267-
size_t len = compressBound(body_len);
268-
char *buffer = sentry_malloc(len);
269-
if (!buffer) {
270-
deflateEnd(&stream);
271-
return false;
272-
}
273-
274-
while (err == Z_OK) {
275-
stream.next_out = buffer + stream.total_out;
276-
stream.avail_out = len - stream.total_out;
277-
err = deflate(&stream, Z_FINISH);
278-
}
279-
280-
if (err != Z_STREAM_END) {
281-
SENTRY_TRACEF("deflate failed: %d", err);
282-
sentry_free(buffer);
283-
buffer = NULL;
284-
deflateEnd(&stream);
285-
return false;
286-
}
287-
288-
*compressed_body_len = stream.total_out;
289-
*compressed_body = buffer;
290-
291-
deflateEnd(&stream);
292-
return true;
293-
}

src/sentry_transport.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,4 @@ sentry_prepared_http_request_t *sentry__prepare_http_request(
8989
*/
9090
void sentry__prepared_http_request_free(sentry_prepared_http_request_t *req);
9191

92-
/**
93-
* Gzip
94-
*/
95-
bool sentry__gzipped_with_compression(const char *body, const size_t body_len,
96-
char **compressed_body, size_t *compressed_body_len);
97-
9892
#endif

0 commit comments

Comments
 (0)