-
-
Notifications
You must be signed in to change notification settings - Fork 341
/
Copy pathSentryNSURLRequest.m
123 lines (106 loc) · 4.42 KB
/
SentryNSURLRequest.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#import "SentryNSURLRequest.h"
#import "NSData+SentryCompression.h"
#import "SentryClient.h"
#import "SentryDsn.h"
#import "SentryError.h"
#import "SentryEvent.h"
#import "SentryHub.h"
#import "SentryLog.h"
#import "SentryMeta.h"
#import "SentrySDK+Private.h"
#import "SentrySerialization.h"
NS_ASSUME_NONNULL_BEGIN
NSString *const SentryServerVersionString = @"7";
NSTimeInterval const SentryRequestTimeout = 15;
@interface
SentryNSURLRequest ()
@property (nonatomic, strong) SentryDsn *dsn;
@end
@implementation SentryNSURLRequest
- (_Nullable instancetype)initStoreRequestWithDsn:(SentryDsn *)dsn
andEvent:(SentryEvent *)event
didFailWithError:(NSError *_Nullable *_Nullable)error
{
NSDictionary *serialized = [event serialize];
NSData *jsonData = [SentrySerialization dataWithJSONObject:serialized error:error];
if (nil == jsonData) {
if (error) {
// TODO: We're possibly overriding an error set by the actual
// code that failed ^
*error = NSErrorFromSentryError(
kSentryErrorJsonConversionError, @"Event cannot be converted to JSON");
}
return nil;
}
if ([SentrySDK.currentHub getClient].options.debug == YES) {
SENTRY_LOG_DEBUG(@"Sending JSON -------------------------------");
SENTRY_LOG_DEBUG(
@"%@", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);
SENTRY_LOG_DEBUG(@"--------------------------------------------");
}
return [self initStoreRequestWithDsn:dsn andData:jsonData didFailWithError:error];
}
- (_Nullable instancetype)initStoreRequestWithDsn:(SentryDsn *)dsn
andData:(NSData *)data
didFailWithError:(NSError *_Nullable *_Nullable)error
{
NSURL *apiURL = [dsn getStoreEndpoint];
self = [super initWithURL:apiURL
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:SentryRequestTimeout];
if (self) {
NSString *authHeader = newAuthHeader(dsn.url);
self.HTTPMethod = @"POST";
[self setValue:authHeader forHTTPHeaderField:@"X-Sentry-Auth"];
[self setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[self setValue:SentryMeta.sdkName forHTTPHeaderField:@"User-Agent"];
[self setValue:@"gzip" forHTTPHeaderField:@"Content-Encoding"];
self.HTTPBody = [data sentry_gzippedWithCompressionLevel:-1 error:error];
}
return self;
}
// TODO: Get refactored out to be a single init method
- (_Nullable instancetype)initEnvelopeRequestWithDsn:(SentryDsn *)dsn
andData:(NSData *)data
didFailWithError:(NSError *_Nullable *_Nullable)error
{
NSURL *apiURL = [dsn getEnvelopeEndpoint];
self = [super initWithURL:apiURL
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:SentryRequestTimeout];
if (self) {
NSString *authHeader = newAuthHeader(dsn.url);
self.HTTPMethod = @"POST";
[self setValue:authHeader forHTTPHeaderField:@"X-Sentry-Auth"];
[self setValue:@"application/x-sentry-envelope" forHTTPHeaderField:@"Content-Type"];
[self setValue:SentryMeta.sdkName forHTTPHeaderField:@"User-Agent"];
[self setValue:@"gzip" forHTTPHeaderField:@"Content-Encoding"];
self.HTTPBody = [data sentry_gzippedWithCompressionLevel:-1 error:error];
}
return self;
}
static NSString *
newHeaderPart(NSString *key, id value)
{
return [NSString stringWithFormat:@"%@=%@", key, value];
}
static NSString *
newAuthHeader(NSURL *url)
{
NSMutableString *string = [NSMutableString stringWithString:@"Sentry "];
[string appendFormat:@"%@,", newHeaderPart(@"sentry_version", SentryServerVersionString)];
[string
appendFormat:@"%@,",
newHeaderPart(@"sentry_client",
[NSString stringWithFormat:@"%@/%@", SentryMeta.sdkName, SentryMeta.versionString])];
[string
appendFormat:@"%@,",
newHeaderPart(@"sentry_timestamp", @((NSInteger)[[NSDate date] timeIntervalSince1970]))];
[string appendFormat:@"%@", newHeaderPart(@"sentry_key", url.user)];
if (nil != url.password) {
[string appendFormat:@",%@", newHeaderPart(@"sentry_secret", url.password)];
}
return string;
}
@end
NS_ASSUME_NONNULL_END