forked from Countly/countly-sdk-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountlyAPM.m
233 lines (172 loc) · 7.94 KB
/
CountlyAPM.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// CountlyAPM.m
//
// This code is provided under the MIT License.
//
// Please visit www.count.ly for more information.
#import "CountlyCommon.h"
@interface CountlyAPM ()
@property (nonatomic) NSMutableArray* exceptionURLs;
@end
@implementation CountlyAPM
+ (instancetype)sharedInstance
{
static CountlyAPM* s_sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{s_sharedInstance = self.new;});
return s_sharedInstance;
}
- (instancetype)init
{
if (self = [super init])
{
NSURL * url = [NSURL URLWithString:CountlyConnectionManager.sharedInstance.host];
NSString* hostAndPath = [url.host stringByAppendingString:url.path];
self.exceptionURLs = [NSMutableArray arrayWithObject:hostAndPath];
}
return self;
}
- (void)startAPM
{
NSArray* swizzling =
@[
@{@"c":NSURLConnection.class, @"f":@YES, @"s":[NSValue valueWithPointer:@selector(sendSynchronousRequest:returningResponse:error:)]},
@{@"c":NSURLConnection.class, @"f":@YES, @"s":[NSValue valueWithPointer:@selector(sendAsynchronousRequest:queue:completionHandler:)]},
@{@"c":NSURLConnection.class, @"f":@NO, @"s":[NSValue valueWithPointer:@selector(initWithRequest:delegate:)]},
@{@"c":NSURLConnection.class, @"f":@NO, @"s":[NSValue valueWithPointer:@selector(initWithRequest:delegate:startImmediately:)]},
@{@"c":NSURLConnection.class, @"f":@NO, @"s":[NSValue valueWithPointer:@selector(start)]},
@{@"c":NSURLSession.class, @"f":@NO, @"s":[NSValue valueWithPointer:@selector(dataTaskWithRequest:completionHandler:)]},
@{@"c":NSURLSession.class, @"f":@NO, @"s":[NSValue valueWithPointer:@selector(downloadTaskWithRequest:completionHandler:)]},
@{@"c":NSURLSessionTask.class, @"f":@NO, @"s":[NSValue valueWithPointer:@selector(resume)]}
];
for (NSDictionary* dict in swizzling)
{
Class c = dict[@"c"];
BOOL isClassMethod = [dict[@"f"] boolValue];
SEL originalSelector = [dict[@"s"] pointerValue];
SEL countlySelector = NSSelectorFromString([@"Countly_" stringByAppendingString:NSStringFromSelector(originalSelector)]);
Method O_method = isClassMethod ? class_getClassMethod(c, originalSelector) : class_getInstanceMethod(c, originalSelector);
Method C_method = isClassMethod ? class_getClassMethod(c, countlySelector) : class_getInstanceMethod(c, countlySelector);
method_exchangeImplementations(O_method, C_method);
}
}
- (void)addExceptionForAPM:(NSString *)string
{
NSURL* url = [NSURL URLWithString:string];
NSString* hostAndPath = [url.host stringByAppendingString:url.path];
if (![CountlyAPM.sharedInstance.exceptionURLs containsObject:hostAndPath])
{
[CountlyAPM.sharedInstance.exceptionURLs addObject:hostAndPath];
}
}
- (void)removeExceptionForAPM:(NSString *)string
{
NSURL * url = [NSURL URLWithString:string];
NSString* hostAndPath = [url.host stringByAppendingString:url.path];
[CountlyAPM.sharedInstance.exceptionURLs removeObject:hostAndPath];
}
- (BOOL)isException:(NSURLRequest *)request
{
NSString* hostAndPath = [request.URL.host stringByAppendingString:request.URL.path];
__block BOOL isException = NO;
[CountlyAPM.sharedInstance.exceptionURLs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL* stop)
{
if ([request.URL.host isEqualToString:obj] || [hostAndPath hasPrefix:obj])
{
isException = YES;
*stop = YES;
}
}];
return isException;
}
@end
#pragma mark -
@implementation NSURLConnection (CountlyAPM)
- (CountlyAPMNetworkLog *)APMNetworkLog
{
return objc_getAssociatedObject(self, @selector(APMNetworkLog));
}
- (void)setAPMNetworkLog:(CountlyAPMNetworkLog *)APMNetworkLog
{
objc_setAssociatedObject(self, @selector(APMNetworkLog), APMNetworkLog, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
+ (NSData *)Countly_sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error
{
CountlyAPMNetworkLog* nl = [CountlyAPMNetworkLog logWithRequest:request andOriginalDelegate:nil startNow:YES];
NSData *data = [self Countly_sendSynchronousRequest:request returningResponse:response error:error];
[nl finishWithStatusCode:((NSHTTPURLResponse*)*response).statusCode andDataSize:data.length];
return data;
}
+ (void)Countly_sendAsynchronousRequest:(NSURLRequest *) request queue:(NSOperationQueue *) queue completionHandler:(void (^)(NSURLResponse* response, NSData* data, NSError* connectionError)) handler
{
CountlyAPMNetworkLog* nl = [CountlyAPMNetworkLog logWithRequest:request andOriginalDelegate:nil startNow:YES];
[self Countly_sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse * response, NSData * data, NSError * connectionError)
{
[nl finishWithStatusCode:((NSHTTPURLResponse*)response).statusCode andDataSize:data.length];
if (handler)
handler(response, data, connectionError);
}];
};
- (instancetype)Countly_initWithRequest:(NSURLRequest *)request delegate:(id)delegate
{
CountlyAPMNetworkLog* nl = [CountlyAPMNetworkLog logWithRequest:request andOriginalDelegate:delegate startNow:YES];
NSURLConnection* conn = [self Countly_initWithRequest:request delegate:(nl ?: delegate) startImmediately:YES];
conn.APMNetworkLog = nl;
return conn;
}
- (instancetype)Countly_initWithRequest:(NSURLRequest *)request delegate:(id)delegate startImmediately:(BOOL)startImmediately
{
CountlyAPMNetworkLog* nl = [CountlyAPMNetworkLog logWithRequest:request andOriginalDelegate:delegate startNow:startImmediately];
NSURLConnection* conn = [self Countly_initWithRequest:request delegate:(nl ?: delegate) startImmediately:startImmediately];
conn.APMNetworkLog = nl;
return conn;
}
- (void)Countly_start
{
[self.APMNetworkLog start];
[self Countly_start];
}
@end
#pragma mark -
@implementation NSURLSessionTask (CountlyAPM)
- (CountlyAPMNetworkLog *)APMNetworkLog
{
return objc_getAssociatedObject(self, @selector(APMNetworkLog));
}
- (void)setAPMNetworkLog:(id)APMNetworkLog
{
objc_setAssociatedObject(self, @selector(APMNetworkLog), APMNetworkLog, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)Countly_resume
{
[self.APMNetworkLog start];
[self Countly_resume];
}
@end
@implementation NSURLSession (CountlyAPM)
- (NSURLSessionDataTask *)Countly_dataTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSData * data, NSURLResponse * response, NSError * error))completionHandler
{
CountlyAPMNetworkLog* nl = [CountlyAPMNetworkLog logWithRequest:request andOriginalDelegate:nil startNow:YES];
NSURLSessionDataTask* dataTask = [self Countly_dataTaskWithRequest:request completionHandler:^(NSData * data, NSURLResponse * response, NSError * error)
{
[nl finishWithStatusCode:((NSHTTPURLResponse*)response).statusCode andDataSize:data.length];
if (completionHandler)
completionHandler(data, response, error);
}];
dataTask.APMNetworkLog = nl;
return dataTask;
}
- (NSURLSessionDownloadTask *)Countly_downloadTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURL * location, NSURLResponse * response, NSError * error))completionHandler
{
CountlyAPMNetworkLog* nl = [CountlyAPMNetworkLog logWithRequest:request andOriginalDelegate:nil startNow:YES];
NSURLSessionDownloadTask* downloadTask = [self Countly_downloadTaskWithRequest:request completionHandler:^(NSURL * location, NSURLResponse * response, NSError * error)
{
NSHTTPURLResponse* HTTPresponse = (NSHTTPURLResponse*)response;
long long dataSize = [[HTTPresponse allHeaderFields][@"Content-Length"] longLongValue];
[nl finishWithStatusCode:((NSHTTPURLResponse*)response).statusCode andDataSize:dataSize];
if (completionHandler)
completionHandler(location, response, error);
}];
downloadTask.APMNetworkLog = nl;
return downloadTask;
}
@end