Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions include/sentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -1833,6 +1833,9 @@ 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);

/* -- Session APIs -- */

typedef enum {
Expand Down
20 changes: 20 additions & 0 deletions src/sentry_attachment.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
#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)
{
if (!attachment) {
return;
}

if (attachment->content_type_owned) {
sentry_free((void *)attachment->content_type);
}
attachment->content_type = sentry__string_clone(content_type);
attachment->content_type_owned = true;
}

static void
attachment_free(sentry_attachment_t *attachment)
Expand All @@ -9,6 +25,9 @@ attachment_free(sentry_attachment_t *attachment)
return;
}
sentry__path_free(attachment->path);
if (attachment->content_type_owned) {
sentry_free((void *)attachment->content_type);
}
sentry_free(attachment);
}

Expand Down Expand Up @@ -41,6 +60,7 @@ sentry__attachments_add(sentry_attachment_t **attachments_ptr,
attachment->next = NULL;
attachment->type = attachment_type;
attachment->content_type = content_type;
attachment->content_type_owned = false;

sentry_attachment_t **next_ptr = attachments_ptr;

Expand Down
1 change: 1 addition & 0 deletions src/sentry_attachment.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct sentry_attachment_s {
sentry_path_t *path;
sentry_attachment_type_t type;
const char *content_type;
bool content_type_owned;
sentry_attachment_t *next;
};

Expand Down
57 changes: 57 additions & 0 deletions tests/unit/test_attachments.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<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<html/>"
"\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);
}
1 change: 1 addition & 0 deletions tests/unit/tests.inc
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Loading