Skip to content

Commit 455f0ef

Browse files
authored
feat: add sentry_attachment_set_content_type() (#1276)
* feat: add sentry_attachment_set_content_type() * drop content_type_owned * add _n
1 parent 931c468 commit 455f0ef

6 files changed

Lines changed: 90 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- The `sentry_attach_file`, `sentry_scope_attach_file` (and their wide-string variants), and `sentry_remove_attachment` have been added to modify the list of attachments that are sent along with sentry events after a call to `sentry_init`. ([#1266](https://github.com/getsentry/sentry-native/pull/1266))
88
- NOTE: When using the `crashpad` backend on macOS, the list of attachments that will be added at the time of a hard crash will be frozen at the time of `sentry_init`, and later modifications will not be reflected.
9+
- Add `sentry_attachment_set_content_type` to allow specifying the content type of attachments. ([#1276](https://github.com/getsentry/sentry-native/pull/1276))
910

1011
## 0.9.0
1112

include/sentry.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,6 +1833,12 @@ SENTRY_API sentry_attachment_t *sentry_scope_attach_filew_n(
18331833
sentry_scope_t *scope, const wchar_t *path, size_t path_len);
18341834
#endif
18351835

1836+
SENTRY_API void sentry_attachment_set_content_type(
1837+
sentry_attachment_t *attachment, const char *content_type);
1838+
SENTRY_API void sentry_attachment_set_content_type_n(
1839+
sentry_attachment_t *attachment, const char *content_type,
1840+
size_t content_type_len);
1841+
18361842
/* -- Session APIs -- */
18371843

18381844
typedef enum {

src/sentry_attachment.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
#include "sentry_attachment.h"
22
#include "sentry_alloc.h"
33
#include "sentry_path.h"
4+
#include "sentry_string.h"
5+
6+
void
7+
sentry_attachment_set_content_type(
8+
sentry_attachment_t *attachment, const char *content_type)
9+
{
10+
sentry_attachment_set_content_type_n(
11+
attachment, content_type, sentry__guarded_strlen(content_type));
12+
}
13+
14+
void
15+
sentry_attachment_set_content_type_n(sentry_attachment_t *attachment,
16+
const char *content_type, size_t content_type_len)
17+
{
18+
if (!attachment) {
19+
return;
20+
}
21+
22+
sentry_free(attachment->content_type);
23+
attachment->content_type
24+
= sentry__string_clone_n(content_type, content_type_len);
25+
}
426

527
static void
628
attachment_free(sentry_attachment_t *attachment)
@@ -9,6 +31,7 @@ attachment_free(sentry_attachment_t *attachment)
931
return;
1032
}
1133
sentry__path_free(attachment->path);
34+
sentry_free(attachment->content_type);
1235
sentry_free(attachment);
1336
}
1437

@@ -40,7 +63,7 @@ sentry__attachments_add(sentry_attachment_t **attachments_ptr,
4063
attachment->path = path;
4164
attachment->next = NULL;
4265
attachment->type = attachment_type;
43-
attachment->content_type = content_type;
66+
attachment->content_type = sentry__string_clone(content_type);
4467

4568
sentry_attachment_t **next_ptr = attachments_ptr;
4669

src/sentry_attachment.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ typedef enum {
2121
struct sentry_attachment_s {
2222
sentry_path_t *path;
2323
sentry_attachment_type_t type;
24-
const char *content_type;
24+
char *content_type;
2525
sentry_attachment_t *next;
2626
};
2727

tests/unit/test_attachments.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,3 +307,60 @@ SENTRY_TEST(attachments_extend)
307307
sentry__path_free(path_c);
308308
sentry__path_free(path_d);
309309
}
310+
311+
SENTRY_TEST(attachment_content_type)
312+
{
313+
SENTRY_TEST_OPTIONS_NEW(options);
314+
sentry_init(options);
315+
316+
sentry_path_t *path_txt
317+
= sentry__path_from_str(SENTRY_TEST_PATH_PREFIX ".a.txt");
318+
sentry_path_t *path_html
319+
= sentry__path_from_str(SENTRY_TEST_PATH_PREFIX ".b.html");
320+
sentry_path_t *path_c = sentry__path_from_str(SENTRY_TEST_PATH_PREFIX ".c");
321+
322+
sentry__path_write_buffer(path_txt, "plain", 5);
323+
sentry__path_write_buffer(path_html, "<html/>", 7);
324+
sentry__path_write_buffer(path_c, "int main() {}", 13);
325+
326+
sentry_attachment_t *attachment_txt
327+
= sentry_attach_file(SENTRY_TEST_PATH_PREFIX ".a.txt");
328+
sentry_attachment_set_content_type(attachment_txt, "text/plain");
329+
330+
sentry_attachment_t *attachment_html
331+
= sentry_attach_file(SENTRY_TEST_PATH_PREFIX ".b.html");
332+
sentry_attachment_set_content_type(attachment_html, "text/html");
333+
334+
sentry_attachment_t *attachment_c
335+
= sentry_attach_file(SENTRY_TEST_PATH_PREFIX ".c");
336+
sentry_attachment_set_content_type(attachment_c, NULL);
337+
338+
SENTRY_WITH_SCOPE (scope) {
339+
sentry_envelope_t *envelope = sentry__envelope_new();
340+
sentry__envelope_add_attachments(envelope, scope->attachments);
341+
342+
char *serialized = sentry_envelope_serialize(envelope, NULL);
343+
TEST_CHECK_STRING_EQUAL(serialized,
344+
"{}\n"
345+
"{\"type\":\"attachment\",\"length\":5,\"content_type\":\"text/"
346+
"plain\","
347+
"\"filename\":\".a.txt\"}\nplain\n"
348+
"{\"type\":\"attachment\",\"length\":7,\"content_type\":\"text/"
349+
"html\","
350+
"\"filename\":\".b.html\"}\n<html/>"
351+
"\n{\"type\":\"attachment\",\"length\":13,\"filename\":\".c\"}\n"
352+
"int main() {}");
353+
sentry_free(serialized);
354+
sentry_envelope_free(envelope);
355+
}
356+
357+
sentry_close();
358+
359+
sentry__path_remove(path_txt);
360+
sentry__path_remove(path_html);
361+
sentry__path_remove(path_c);
362+
363+
sentry__path_free(path_txt);
364+
sentry__path_free(path_html);
365+
sentry__path_free(path_c);
366+
}

tests/unit/tests.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
XX(assert_sdk_name)
22
XX(assert_sdk_user_agent)
33
XX(assert_sdk_version)
4+
XX(attachment_content_type)
45
XX(attachments_add_dedupe)
56
XX(attachments_add_remove)
67
XX(attachments_extend)

0 commit comments

Comments
 (0)