@@ -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 ]];
0 commit comments