Skip to content

Commit 9300c82

Browse files
jpnurmiclaude
andcommitted
refactor(envelope): move TUS resolve logic from transport to envelope
Rename sentry__envelope_item_set_attachment_ref to sentry__envelope_item_set_attachment_ref_path for consistency with the existing getter. Add sentry__envelope_item_set_attachment_ref_location in the envelope module, replacing tus_resolve_item in the transport. This keeps JSON payload manipulation encapsulated in the envelope layer. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 12e7c88 commit 9300c82

5 files changed

Lines changed: 47 additions & 38 deletions

File tree

src/sentry_database.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ write_large_attachment(sentry_envelope_item_t *item,
225225
}
226226

227227
if (rv == 0) {
228-
sentry__envelope_item_set_attachment_ref(item, dst);
228+
sentry__envelope_item_set_attachment_ref_path(item, dst);
229229
if (is_inline) {
230230
sentry__envelope_item_set_header(
231231
item, "inline", sentry_value_new_bool(false));

src/sentry_envelope.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ envelope_add_attachment_ref(sentry_envelope_t *envelope,
672672
}
673673

674674
void
675-
sentry__envelope_item_set_attachment_ref(
675+
sentry__envelope_item_set_attachment_ref_path(
676676
sentry_envelope_item_t *item, const sentry_path_t *path)
677677
{
678678
size_t old_len = 0;
@@ -709,6 +709,46 @@ sentry__envelope_item_is_attachment_ref(const sentry_envelope_item_t *item)
709709
return ct && strcmp(ct, "application/vnd.sentry.attachment-ref") == 0;
710710
}
711711

712+
void
713+
sentry__envelope_item_set_attachment_ref_location(
714+
sentry_envelope_item_t *item, const char *location)
715+
{
716+
bool is_inline = sentry_value_is_true(
717+
sentry__envelope_item_get_header(item, "inline"));
718+
719+
sentry_value_t obj;
720+
if (is_inline) {
721+
obj = sentry_value_new_object();
722+
} else {
723+
size_t old_len = 0;
724+
const char *old_payload
725+
= sentry__envelope_item_get_payload(item, &old_len);
726+
obj = (old_payload && old_len > 0)
727+
? sentry__value_from_json(old_payload, old_len)
728+
: sentry_value_new_object();
729+
if (sentry_value_is_null(obj)) {
730+
obj = sentry_value_new_object();
731+
}
732+
sentry_value_remove_by_key(obj, "path");
733+
}
734+
735+
const char *ref_ct = sentry_value_as_string(
736+
sentry__envelope_item_get_header(item, "ref_content_type"));
737+
if (ref_ct && *ref_ct != '\0') {
738+
sentry_value_set_by_key(
739+
obj, "content_type", sentry_value_new_string(ref_ct));
740+
}
741+
742+
sentry_value_set_by_key(obj, "location", sentry_value_new_string(location));
743+
744+
sentry_jsonwriter_t *jw = sentry__jsonwriter_new_sb(NULL);
745+
sentry__jsonwriter_write_value(jw, obj);
746+
sentry_value_decref(obj);
747+
size_t json_len = 0;
748+
char *json = sentry__jsonwriter_into_string(jw, &json_len);
749+
sentry__envelope_item_set_payload(item, json, json_len);
750+
}
751+
712752
bool
713753
sentry__envelope_has_attachment_refs(const sentry_envelope_t *envelope)
714754
{

src/sentry_envelope.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,10 @@ const char *sentry__envelope_item_get_payload(
164164
const sentry_envelope_item_t *item, size_t *payload_len_out);
165165
void sentry__envelope_item_set_payload(
166166
sentry_envelope_item_t *item, char *payload, size_t payload_len);
167-
void sentry__envelope_item_set_attachment_ref(
167+
void sentry__envelope_item_set_attachment_ref_path(
168168
sentry_envelope_item_t *item, const sentry_path_t *path);
169+
void sentry__envelope_item_set_attachment_ref_location(
170+
sentry_envelope_item_t *item, const char *location);
169171

170172
bool sentry__envelope_item_is_attachment_ref(
171173
const sentry_envelope_item_t *item);

src/transports/sentry_http_transport.c

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
#include "sentry_alloc.h"
33
#include "sentry_database.h"
44
#include "sentry_envelope.h"
5-
#include "sentry_json.h"
65
#include "sentry_options.h"
76
#include "sentry_ratelimiter.h"
87
#include "sentry_retry.h"
98
#include "sentry_string.h"
109
#include "sentry_transport.h"
1110
#include "sentry_utils.h"
12-
#include "sentry_value.h"
1311

1412
#ifdef SENTRY_TRANSPORT_COMPRESSION
1513
# include "zlib.h"
@@ -289,37 +287,6 @@ http_send_request(
289287
return resp.status_code;
290288
}
291289

292-
static void
293-
tus_resolve_item(
294-
sentry_envelope_item_t *item, const char *location, bool is_inline)
295-
{
296-
sentry_value_t loc_json;
297-
if (is_inline) {
298-
loc_json = sentry_value_new_object();
299-
const char *ref_ct = sentry_value_as_string(
300-
sentry__envelope_item_get_header(item, "ref_content_type"));
301-
if (ref_ct && *ref_ct != '\0') {
302-
sentry_value_set_by_key(
303-
loc_json, "content_type", sentry_value_new_string(ref_ct));
304-
}
305-
} else {
306-
size_t old_len = 0;
307-
const char *old_payload
308-
= sentry__envelope_item_get_payload(item, &old_len);
309-
loc_json = sentry__value_from_json(old_payload, old_len);
310-
sentry_value_remove_by_key(loc_json, "path");
311-
}
312-
sentry_value_set_by_key(
313-
loc_json, "location", sentry_value_new_string(location));
314-
315-
sentry_jsonwriter_t *jw = sentry__jsonwriter_new_sb(NULL);
316-
sentry__jsonwriter_write_value(jw, loc_json);
317-
sentry_value_decref(loc_json);
318-
size_t new_len = 0;
319-
char *new_payload = sentry__jsonwriter_into_string(jw, &new_len);
320-
sentry__envelope_item_set_payload(item, new_payload, new_len);
321-
}
322-
323290
// TODO: with future creation-only TUS, this becomes a creation-only request
324291
// (prepare_tus_request_common without a body) that obtains the location, and
325292
// the actual data upload is queued as a separate bgworker task
@@ -381,7 +348,7 @@ tus_upload_item(http_transport_state_t *state, sentry_envelope_item_t *item)
381348
int status_code = ok ? resp.status_code : -1;
382349

383350
if (ok && resp.status_code == 201 && resp.location) {
384-
tus_resolve_item(item, resp.location, is_inline);
351+
sentry__envelope_item_set_attachment_ref_location(item, resp.location);
385352
}
386353

387354
http_response_cleanup(&resp);

tests/unit/test_envelopes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ SENTRY_TEST(attachment_ref_inline)
804804
item, "ref_content_type", sentry_value_new_string("application/x-dmp"));
805805

806806
sentry_path_t *path = sentry__path_from_str("/tmp/test.dmp");
807-
sentry__envelope_item_set_attachment_ref(item, path);
807+
sentry__envelope_item_set_attachment_ref_path(item, path);
808808

809809
// verify the payload is valid JSON with correct keys
810810
size_t payload_len = 0;

0 commit comments

Comments
 (0)