Skip to content

Commit a4c5ddc

Browse files
authored
ref: Envelope deserialization to Swift (#6186)
* ref: Envelope deserialization to Swift * PR feedback
1 parent 856fffe commit a4c5ddc

20 files changed

+260
-266
lines changed

Sentry.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@
11221122
FAE80C242E4695B40010A595 /* SentryEvent+Serialize.h in Headers */ = {isa = PBXBuildFile; fileRef = FAE80C232E4695AE0010A595 /* SentryEvent+Serialize.h */; };
11231123
FAEC270E2DF3526000878871 /* SentryUserFeedback.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAEC270D2DF3526000878871 /* SentryUserFeedback.swift */; };
11241124
FAEC273D2DF3933A00878871 /* NSData+Unzip.m in Sources */ = {isa = PBXBuildFile; fileRef = FAEC273C2DF3933200878871 /* NSData+Unzip.m */; };
1125-
FAEEC0522E75E55F00E79CA9 /* DataDeserialization.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAEEC04C2E75E55A00E79CA9 /* DataDeserialization.swift */; };
1125+
FAEEC0522E75E55F00E79CA9 /* SentrySerializationSwift.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAEEC04C2E75E55A00E79CA9 /* SentrySerializationSwift.swift */; };
11261126
FAEFA12F2E4FAE1900C431D9 /* SentrySDKSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAEFA1292E4FAE1700C431D9 /* SentrySDKSettings.swift */; };
11271127
FAF120182E70C08F006E1DA3 /* SentryEnvelopeHeaderHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = FAF120122E70C088006E1DA3 /* SentryEnvelopeHeaderHelper.h */; };
11281128
FAF1201A2E70C0EE006E1DA3 /* SentryEnvelopeHeaderHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = FAF120192E70C0EA006E1DA3 /* SentryEnvelopeHeaderHelper.m */; };
@@ -2470,7 +2470,7 @@
24702470
FAEC270D2DF3526000878871 /* SentryUserFeedback.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryUserFeedback.swift; sourceTree = "<group>"; };
24712471
FAEC273C2DF3933200878871 /* NSData+Unzip.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSData+Unzip.m"; sourceTree = "<group>"; };
24722472
FAEC273E2DF393E000878871 /* NSData+Unzip.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSData+Unzip.h"; sourceTree = "<group>"; };
2473-
FAEEC04C2E75E55A00E79CA9 /* DataDeserialization.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataDeserialization.swift; sourceTree = "<group>"; };
2473+
FAEEC04C2E75E55A00E79CA9 /* SentrySerializationSwift.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentrySerializationSwift.swift; sourceTree = "<group>"; };
24742474
FAEFA1292E4FAE1700C431D9 /* SentrySDKSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentrySDKSettings.swift; sourceTree = "<group>"; };
24752475
FAF120122E70C088006E1DA3 /* SentryEnvelopeHeaderHelper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryEnvelopeHeaderHelper.h; path = include/SentryEnvelopeHeaderHelper.h; sourceTree = "<group>"; };
24762476
FAF120192E70C0EA006E1DA3 /* SentryEnvelopeHeaderHelper.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryEnvelopeHeaderHelper.m; sourceTree = "<group>"; };
@@ -2606,7 +2606,7 @@
26062606
isa = PBXGroup;
26072607
children = (
26082608
62CB19242E77F8FD00AF5DA2 /* SentryDispatchSourceWrapper.swift */,
2609-
FAEEC04C2E75E55A00E79CA9 /* DataDeserialization.swift */,
2609+
FAEEC04C2E75E55A00E79CA9 /* SentrySerializationSwift.swift */,
26102610
FA94E71B2E6F26BF00576666 /* SentrySerialization+ReplayRecording.swift */,
26112611
FA94E7232E6F32FA00576666 /* SentryEnvelopeItemType.swift */,
26122612
FA458CBD2E691A6E0061B13D /* SentryProcessInfo.swift */,
@@ -5583,7 +5583,7 @@
55835583
FA90FAA82E06614E008CAAE8 /* SentryExtraPackages.swift in Sources */,
55845584
7BD86ED1264A7CF6005439DB /* SentryAppStartMeasurement.m in Sources */,
55855585
7DC27EC723997EB7006998B5 /* SentryAutoBreadcrumbTrackingIntegration.m in Sources */,
5586-
FAEEC0522E75E55F00E79CA9 /* DataDeserialization.swift in Sources */,
5586+
FAEEC0522E75E55F00E79CA9 /* SentrySerializationSwift.swift in Sources */,
55875587
D84D2CDF2C2BF9370011AF8A /* SentryReplayType.swift in Sources */,
55885588
63FE717B20DA4C1100CDBAE8 /* SentryCrashReport.c in Sources */,
55895589
D8F67B222BEAB6CC00C9197B /* SentryRRWebEvent.swift in Sources */,

Sources/Sentry/PrivateSentrySDKOnly.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ + (void)captureEnvelope:(SentryEnvelope *)envelope
5050

5151
+ (nullable SentryEnvelope *)envelopeWithData:(NSData *)data
5252
{
53-
return [SentrySerialization envelopeWithData:data];
53+
return [SentrySerializationSwift envelopeWithData:data];
5454
}
5555

5656
#if !SDK_V9

Sources/Sentry/Public/SentryEnvelopeItemHeader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ SENTRY_NO_INIT
3131

3232
- (instancetype)initWithType:(NSString *)type
3333
length:(NSUInteger)length
34-
contentType:(NSString *)contentType
34+
contentType:(NSString *_Nullable)contentType
3535
itemCount:(NSNumber *)itemCount;
3636

3737
/**

Sources/Sentry/SentryAttachment.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ - (instancetype)initWithPath:(NSString *)path
8888
}
8989

9090
SentryAttachmentType
91-
typeForSentryAttachmentName(NSString *name)
91+
typeForSentryAttachmentName(NSString *_Nullable name)
9292
{
9393
if ([name isEqualToString:kSentryAttachmentTypeNameViewHierarchy]) {
9494
return kSentryAttachmentTypeViewHierarchy;

Sources/Sentry/SentryFileManager.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ - (nullable SentrySession *)readSession:(NSString *)sessionFilePath
974974
return nil;
975975
}
976976
}
977-
SentrySession *currentSession = [DataDeserialization sessionWithData:currentData];
977+
SentrySession *currentSession = [SentrySerializationSwift sessionWithData:currentData];
978978
if (nil == currentSession) {
979979
SENTRY_LOG_ERROR(
980980
@"Data stored in session: '%@' was not parsed as session.", sessionFilePath);
@@ -993,7 +993,7 @@ - (SentryAppState *_Nullable)readAppStateFrom:(NSString *)path
993993
SENTRY_LOG_WARN(@"No app state data found at %@", path);
994994
return nil;
995995
}
996-
return [DataDeserialization appStateWithData:currentData];
996+
return [SentrySerializationSwift appStateWithData:currentData];
997997
}
998998

999999
- (void)deleteAppStateFrom:(NSString *)path
@@ -1073,7 +1073,7 @@ - (void)handleEnvelopesLimit
10731073
[envelopePathsCopy removeObjectAtIndex:i];
10741074

10751075
NSData *envelopeData = [[NSFileManager defaultManager] contentsAtPath:envelopeFilePath];
1076-
SentryEnvelope *envelope = [SentrySerialization envelopeWithData:envelopeData];
1076+
SentryEnvelope *envelope = [SentrySerializationSwift envelopeWithData:envelopeData];
10771077

10781078
BOOL didMigrateSessionInit =
10791079
[SentryMigrateSessionInit migrateSessionInit:envelope

Sources/Sentry/SentryHttpTransport.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ - (void)sendAllCachedEnvelopes
339339

340340
envelopeFilePath = envelopeFileContents.path;
341341

342-
envelope = [SentrySerialization envelopeWithData:envelopeFileContents.contents];
342+
envelope = [SentrySerializationSwift envelopeWithData:envelopeFileContents.contents];
343343
if (nil == envelope) {
344344
SENTRY_LOG_DEBUG(@"Envelope contained no deserializable data.");
345345
[self deleteEnvelopeAndSendNext:envelopeFilePath];

Sources/Sentry/SentryMigrateSessionInit.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ + (BOOL)migrateSessionInit:(nullable SentryEnvelope *)envelope
2424
@"Could not migrate session init, because the envelope item has no data.");
2525
continue;
2626
}
27-
SentrySession *session = [DataDeserialization sessionWithData:data];
27+
SentrySession *session = [SentrySerializationSwift sessionWithData:data];
2828
if (nil != session && [session.flagInit boolValue]) {
2929
BOOL didSetInitFlag =
3030
[self setInitFlagOnNextEnvelopeWithSameSessionId:session
@@ -56,7 +56,7 @@ + (BOOL)setInitFlagOnNextEnvelopeWithSameSessionId:(SentrySession *)session
5656
continue;
5757
}
5858

59-
SentryEnvelope *envelope = [SentrySerialization envelopeWithData:envelopeData];
59+
SentryEnvelope *envelope = [SentrySerializationSwift envelopeWithData:envelopeData];
6060

6161
if (nil != envelope) {
6262
BOOL didSetInitFlag = [self setInitFlagIfContainsSameSessionId:session.sessionId
@@ -84,7 +84,7 @@ + (BOOL)setInitFlagIfContainsSameSessionId:(NSUUID *)sessionId
8484
@"Could not migrate session init, because the envelope item has no data.");
8585
continue;
8686
}
87-
SentrySession *localSession = [DataDeserialization sessionWithData:data];
87+
SentrySession *localSession = [SentrySerializationSwift sessionWithData:data];
8888

8989
if (nil != localSession && [localSession.sessionId isEqual:sessionId]) {
9090
[localSession setFlagInit];

Sources/Sentry/SentrySerialization.m

Lines changed: 0 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -76,165 +76,6 @@ + (NSData *_Nullable)dataWithEnvelope:(SentryEnvelope *)envelope
7676
return envelopeData;
7777
}
7878

79-
+ (SentryEnvelope *_Nullable)envelopeWithData:(NSData *)data
80-
{
81-
SentryEnvelopeHeader *envelopeHeader = nil;
82-
const unsigned char *bytes = [data bytes];
83-
NSUInteger envelopeHeaderIndex = 0;
84-
85-
for (NSUInteger i = 0; i < data.length; ++i) {
86-
if (bytes[i] == '\n') {
87-
envelopeHeaderIndex = i;
88-
// Envelope header end
89-
NSData *headerData = [NSData dataWithBytes:bytes length:i];
90-
#ifdef DEBUG
91-
NSString *headerString = [[NSString alloc] initWithData:headerData
92-
encoding:NSUTF8StringEncoding];
93-
SENTRY_LOG_DEBUG(@"Header %@", headerString);
94-
#endif
95-
NSError *error = nil;
96-
NSDictionary *headerDictionary = [NSJSONSerialization JSONObjectWithData:headerData
97-
options:0
98-
error:&error];
99-
if (nil != error) {
100-
SENTRY_LOG_ERROR(@"Failed to parse envelope header %@", error);
101-
break;
102-
}
103-
104-
SentryId *eventId = nil;
105-
NSString *eventIdAsString = headerDictionary[@"event_id"];
106-
if (nil != eventIdAsString) {
107-
eventId = [[SentryId alloc] initWithUUIDString:eventIdAsString];
108-
}
109-
110-
SentrySdkInfo *sdkInfo = nil;
111-
if (nil != headerDictionary[@"sdk"] &&
112-
[headerDictionary[@"sdk"] isKindOfClass:[NSDictionary class]]) {
113-
sdkInfo = [[SentrySdkInfo alloc]
114-
initWithDict:SENTRY_UNWRAP_NULLABLE(NSDictionary, headerDictionary[@"sdk"])];
115-
}
116-
117-
SentryTraceContext *traceContext = nil;
118-
if (nil != headerDictionary[@"trace"] &&
119-
[headerDictionary[@"trace"] isKindOfClass:[NSDictionary class]]) {
120-
traceContext = [[SentryTraceContext alloc]
121-
initWithDict:SENTRY_UNWRAP_NULLABLE(NSDictionary, headerDictionary[@"trace"])];
122-
}
123-
124-
envelopeHeader = [[SentryEnvelopeHeader alloc] initWithId:eventId
125-
sdkInfo:sdkInfo
126-
traceContext:traceContext];
127-
128-
if (headerDictionary[@"sent_at"] != nil &&
129-
[headerDictionary[@"sent_at"] isKindOfClass:[NSString class]]) {
130-
envelopeHeader.sentAt = sentry_fromIso8601String(
131-
SENTRY_UNWRAP_NULLABLE(NSString, headerDictionary[@"sent_at"]));
132-
}
133-
134-
break;
135-
}
136-
}
137-
138-
if (nil == envelopeHeader) {
139-
SENTRY_LOG_ERROR(@"Invalid envelope. No header found.");
140-
return nil;
141-
}
142-
143-
if (envelopeHeaderIndex == 0) {
144-
SENTRY_LOG_ERROR(@"EnvelopeHeader was parsed, its index is expected.");
145-
return nil;
146-
}
147-
148-
// Parse items
149-
NSUInteger itemHeaderStart = envelopeHeaderIndex + 1;
150-
151-
NSMutableArray<SentryEnvelopeItem *> *items = [NSMutableArray new];
152-
NSUInteger endOfEnvelope = data.length - 1;
153-
154-
for (NSUInteger i = itemHeaderStart; i <= endOfEnvelope; ++i) {
155-
if (bytes[i] == '\n' || i == endOfEnvelope) {
156-
157-
NSData *itemHeaderData =
158-
[data subdataWithRange:NSMakeRange(itemHeaderStart, i - itemHeaderStart)];
159-
#ifdef DEBUG
160-
NSString *itemHeaderString = [[NSString alloc] initWithData:itemHeaderData
161-
encoding:NSUTF8StringEncoding];
162-
SENTRY_LOG_DEBUG(@"Item Header %@", itemHeaderString);
163-
#endif
164-
NSError *error = nil;
165-
NSDictionary *headerDictionary = [NSJSONSerialization JSONObjectWithData:itemHeaderData
166-
options:0
167-
error:&error];
168-
if (nil != error) {
169-
SENTRY_LOG_ERROR(@"Failed to parse envelope item header %@", error);
170-
return nil;
171-
}
172-
NSString *_Nullable nullableType = [headerDictionary valueForKey:@"type"];
173-
if (nil == nullableType) {
174-
SENTRY_LOG_ERROR(@"Envelope item type is required.");
175-
break;
176-
}
177-
NSString *_Nonnull type = SENTRY_UNWRAP_NULLABLE(NSString, nullableType);
178-
179-
NSNumber *bodyLengthNumber = [headerDictionary valueForKey:@"length"];
180-
NSUInteger bodyLength = [bodyLengthNumber unsignedIntegerValue];
181-
if (endOfEnvelope == i && bodyLength != 0) {
182-
SENTRY_LOG_ERROR(
183-
@"Envelope item has no data but header indicates it's length is %d.",
184-
(int)bodyLength);
185-
break;
186-
}
187-
188-
NSString *filename = [headerDictionary valueForKey:@"filename"];
189-
NSString *contentType = [headerDictionary valueForKey:@"content_type"];
190-
NSString *attachmentType = [headerDictionary valueForKey:@"attachment_type"];
191-
NSNumber *itemCount = [headerDictionary valueForKey:@"item_count"];
192-
193-
SentryEnvelopeItemHeader *itemHeader;
194-
if (nil != filename) {
195-
itemHeader = [[SentryEnvelopeAttachmentHeader alloc]
196-
initWithType:type
197-
length:bodyLength
198-
filename:filename
199-
contentType:contentType
200-
attachmentType:typeForSentryAttachmentName(attachmentType)];
201-
} else if (nil != itemCount) {
202-
itemHeader = [[SentryEnvelopeItemHeader alloc] initWithType:type
203-
length:bodyLength
204-
contentType:contentType
205-
itemCount:itemCount];
206-
} else {
207-
itemHeader = [[SentryEnvelopeItemHeader alloc] initWithType:type length:bodyLength];
208-
}
209-
210-
if (endOfEnvelope == i) {
211-
i++; // 0 byte attachment
212-
}
213-
214-
if (bodyLength > 0 && data.length < (i + 1 + bodyLength)) {
215-
SENTRY_LOG_ERROR(@"Envelope is corrupted or has invalid data. Trying to read %li "
216-
@"bytes by skipping %li from a buffer of %li bytes.",
217-
(unsigned long)data.length, (unsigned long)bodyLength, (long)(i + 1));
218-
return nil;
219-
}
220-
221-
NSData *itemBody = [data subdataWithRange:NSMakeRange(i + 1, bodyLength)];
222-
SentryEnvelopeItem *envelopeItem = [[SentryEnvelopeItem alloc] initWithHeader:itemHeader
223-
data:itemBody];
224-
[items addObject:envelopeItem];
225-
i = itemHeaderStart = i + 1 + [bodyLengthNumber integerValue];
226-
}
227-
}
228-
229-
if (items.count == 0) {
230-
SENTRY_LOG_ERROR(@"Envelope has no items.");
231-
return nil;
232-
}
233-
234-
SentryEnvelope *envelope = [[SentryEnvelope alloc] initWithHeader:envelopeHeader items:items];
235-
return envelope;
236-
}
237-
23879
+ (NSData *_Nullable)dataWithSession:(SentrySession *)session
23980
{
24081
return [self dataWithJSONObject:[session serialize]];

Sources/Sentry/include/SentryAttachment+Private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ typedef NS_ENUM(NSInteger, SentryAttachmentType) {
1616

1717
NSString *nameForSentryAttachmentType(SentryAttachmentType attachmentType);
1818

19-
SentryAttachmentType typeForSentryAttachmentName(NSString *name);
19+
SentryAttachmentType typeForSentryAttachmentName(NSString *_Nullable name);
2020

2121
@interface SentryAttachment ()
2222
SENTRY_NO_INIT

Sources/Sentry/include/SentrySerialization.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ NS_ASSUME_NONNULL_BEGIN
1515

1616
+ (NSData *_Nullable)dataWithEnvelope:(SentryEnvelope *)envelope;
1717

18-
+ (SentryEnvelope *_Nullable)envelopeWithData:(NSData *)data;
19-
2018
/**
2119
* Retrieves the json object from an event envelope item data.
2220
*/

0 commit comments

Comments
 (0)