diff --git a/CHANGELOG.md b/CHANGELOG.md index 64e5f1d63..b7790739e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - 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)) - 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. +- Add `sentry_attachment_set_content_type` to allow specifying the content type of attachments. ([#1276](https://github.com/getsentry/sentry-native/pull/1276)) ## 0.9.0 diff --git a/include/sentry.h b/include/sentry.h index 91c584418..a06dbd234 100644 --- a/include/sentry.h +++ b/include/sentry.h @@ -1833,6 +1833,12 @@ SENTRY_API sentry_attachment_t *sentry_scope_attach_filew_n( sentry_scope_t *scope, const wchar_t *path, size_t path_len); #endif +SENTRY_API void sentry_attachment_set_content_type( + sentry_attachment_t *attachment, const char *content_type); +SENTRY_API void sentry_attachment_set_content_type_n( + sentry_attachment_t *attachment, const char *content_type, + size_t content_type_len); + /* -- Session APIs -- */ typedef enum { diff --git a/src/sentry_attachment.c b/src/sentry_attachment.c index b30630774..bafd647d9 100644 --- a/src/sentry_attachment.c +++ b/src/sentry_attachment.c @@ -1,6 +1,28 @@ #include "sentry_attachment.h" #include "sentry_alloc.h" #include "sentry_path.h" +#include "sentry_string.h" + +void +sentry_attachment_set_content_type( + sentry_attachment_t *attachment, const char *content_type) +{ + sentry_attachment_set_content_type_n( + attachment, content_type, sentry__guarded_strlen(content_type)); +} + +void +sentry_attachment_set_content_type_n(sentry_attachment_t *attachment, + const char *content_type, size_t content_type_len) +{ + if (!attachment) { + return; + } + + sentry_free(attachment->content_type); + attachment->content_type + = sentry__string_clone_n(content_type, content_type_len); +} static void attachment_free(sentry_attachment_t *attachment) @@ -9,6 +31,7 @@ attachment_free(sentry_attachment_t *attachment) return; } sentry__path_free(attachment->path); + sentry_free(attachment->content_type); sentry_free(attachment); } @@ -40,7 +63,7 @@ sentry__attachments_add(sentry_attachment_t **attachments_ptr, attachment->path = path; attachment->next = NULL; attachment->type = attachment_type; - attachment->content_type = content_type; + attachment->content_type = sentry__string_clone(content_type); sentry_attachment_t **next_ptr = attachments_ptr; diff --git a/src/sentry_attachment.h b/src/sentry_attachment.h index cc69263e1..b403b8453 100644 --- a/src/sentry_attachment.h +++ b/src/sentry_attachment.h @@ -21,7 +21,7 @@ typedef enum { struct sentry_attachment_s { sentry_path_t *path; sentry_attachment_type_t type; - const char *content_type; + char *content_type; sentry_attachment_t *next; }; diff --git a/tests/unit/test_attachments.c b/tests/unit/test_attachments.c index 08eb59086..d3215cb93 100644 --- a/tests/unit/test_attachments.c +++ b/tests/unit/test_attachments.c @@ -307,3 +307,60 @@ SENTRY_TEST(attachments_extend) sentry__path_free(path_c); sentry__path_free(path_d); } + +SENTRY_TEST(attachment_content_type) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + sentry_path_t *path_txt + = sentry__path_from_str(SENTRY_TEST_PATH_PREFIX ".a.txt"); + sentry_path_t *path_html + = sentry__path_from_str(SENTRY_TEST_PATH_PREFIX ".b.html"); + sentry_path_t *path_c = sentry__path_from_str(SENTRY_TEST_PATH_PREFIX ".c"); + + sentry__path_write_buffer(path_txt, "plain", 5); + sentry__path_write_buffer(path_html, "", 7); + sentry__path_write_buffer(path_c, "int main() {}", 13); + + sentry_attachment_t *attachment_txt + = sentry_attach_file(SENTRY_TEST_PATH_PREFIX ".a.txt"); + sentry_attachment_set_content_type(attachment_txt, "text/plain"); + + sentry_attachment_t *attachment_html + = sentry_attach_file(SENTRY_TEST_PATH_PREFIX ".b.html"); + sentry_attachment_set_content_type(attachment_html, "text/html"); + + sentry_attachment_t *attachment_c + = sentry_attach_file(SENTRY_TEST_PATH_PREFIX ".c"); + sentry_attachment_set_content_type(attachment_c, NULL); + + SENTRY_WITH_SCOPE (scope) { + sentry_envelope_t *envelope = sentry__envelope_new(); + sentry__envelope_add_attachments(envelope, scope->attachments); + + char *serialized = sentry_envelope_serialize(envelope, NULL); + TEST_CHECK_STRING_EQUAL(serialized, + "{}\n" + "{\"type\":\"attachment\",\"length\":5,\"content_type\":\"text/" + "plain\"," + "\"filename\":\".a.txt\"}\nplain\n" + "{\"type\":\"attachment\",\"length\":7,\"content_type\":\"text/" + "html\"," + "\"filename\":\".b.html\"}\n" + "\n{\"type\":\"attachment\",\"length\":13,\"filename\":\".c\"}\n" + "int main() {}"); + sentry_free(serialized); + sentry_envelope_free(envelope); + } + + sentry_close(); + + sentry__path_remove(path_txt); + sentry__path_remove(path_html); + sentry__path_remove(path_c); + + sentry__path_free(path_txt); + sentry__path_free(path_html); + sentry__path_free(path_c); +} diff --git a/tests/unit/tests.inc b/tests/unit/tests.inc index 56badaefc..abd15deae 100644 --- a/tests/unit/tests.inc +++ b/tests/unit/tests.inc @@ -1,6 +1,7 @@ XX(assert_sdk_name) XX(assert_sdk_user_agent) XX(assert_sdk_version) +XX(attachment_content_type) XX(attachments_add_dedupe) XX(attachments_add_remove) XX(attachments_extend)