-
Notifications
You must be signed in to change notification settings - Fork 696
/
KSCrashReportSinkQuincyHockey.m
536 lines (469 loc) · 19.2 KB
/
KSCrashReportSinkQuincyHockey.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
//
// KSCrashReportSinkQuincyHockey.m
//
// Created by Karl Stenerud on 2012-02-26.
//
// Copyright (c) 2012 Karl Stenerud. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall remain in place
// in this source code.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import "KSCrashReportSinkQuincyHockey.h"
#import "KSCrashReportFields.h"
#import "KSHTTPMultipartPostBody.h"
#import "KSHTTPRequestSender.h"
#import "NSData+KSGZip.h"
#import "KSCrashReportFilterAppleFmt.h"
#import "KSCrashReportFilterBasic.h"
#import "KSJSONCodecObjC.h"
#import "KSReachabilityKSCrash.h"
#import "Container+DeepSearch.h"
#import "NSError+SimpleConstructor.h"
#import <mach/machine.h>
#import "KSCrashMonitor_System.h"
#import "NSString+URLEncode.h"
//#define KSLogger_LocalLevel TRACE
#import "KSLogger.h"
#define kFilterKeyStandard @"standard"
#define kFilterKeyApple @"apple"
#define kQuincyUUIDUserDefault @"QuincyKitAppInstallationUUID"
@interface KSCrashReportSinkQuincy ()
@property(nonatomic, readwrite, retain) NSString* userIDKey;
@property(nonatomic, readwrite, retain) NSString* userNameKey;
@property(nonatomic, readwrite, retain) NSString* contactEmailKey;
@property(nonatomic, readwrite, retain) NSArray* crashDescriptionKeys;
@property(nonatomic,readwrite,retain) NSURL* url;
@property(nonatomic,readwrite,retain) KSReachableOperationKSCrash* reachableOperation;
@end
@implementation KSCrashReportSinkQuincy
@synthesize url = _url;
@synthesize userIDKey = _userIDKey;
@synthesize userNameKey = _userNameKey;
@synthesize contactEmailKey = _contactEmailKey;
@synthesize crashDescriptionKeys = _crashDescriptionKeys;
@synthesize reachableOperation = _reachableOperation;
@synthesize waitUntilReachable = _waitUntilReachable;
+ (KSCrashReportSinkQuincy*) sinkWithURL:(NSURL*) url
userIDKey:(NSString*) userIDKey
userNameKey:(NSString*) userNameKey
contactEmailKey:(NSString*) contactEmailKey
crashDescriptionKeys:(NSArray*) crashDescriptionKeys
{
return [[self alloc] initWithURL:url
userIDKey:userIDKey
userNameKey:userNameKey
contactEmailKey:contactEmailKey
crashDescriptionKeys:crashDescriptionKeys];
}
- (id) initWithURL:(NSURL*) url
userIDKey:(NSString*) userIDKey
userNameKey:(NSString*) userNameKey
contactEmailKey:(NSString*) contactEmailKey
crashDescriptionKeys:(NSArray*) crashDescriptionKeys
{
if((self = [super init]))
{
self.url = url;
self.userIDKey = userIDKey;
self.userNameKey = userNameKey;
self.contactEmailKey = contactEmailKey;
self.crashDescriptionKeys = crashDescriptionKeys;
self.waitUntilReachable = YES;
}
return self;
}
- (id <KSCrashReportFilter>) defaultCrashReportFilterSet
{
return [KSCrashReportFilterPipeline filterWithFilters:
[KSCrashReportFilterCombine filterWithFiltersAndKeys:
[KSCrashReportFilterPassthrough filter],
kFilterKeyStandard,
[KSCrashReportFilterAppleFmt filterWithReportStyle:KSAppleReportStyleSymbolicatedSideBySide],
kFilterKeyApple,
nil],
self,
nil];
}
- (NSString*) cdataEscaped:(NSString*) string
{
return [string stringByReplacingOccurrencesOfString:@"]]>"
withString:@"]]" @"]]><![CDATA[" @">"
options:NSLiteralSearch
range:NSMakeRange(0,string.length)];
}
- (NSString*) blankForNil:(NSString*) string
{
return string == nil ? @"" : string;
}
- (NSString*) descriptionForReport:(NSDictionary*) report keys:(NSArray*) keys
{
NSMutableString* str = [NSMutableString string];
NSUInteger count = [keys count];
for(NSUInteger i = 0; i < count; i++)
{
NSString* stringValue = nil;
NSString* key = [keys objectAtIndex:i];
id value = [report objectForKeyPath:key];
if([value isKindOfClass:[NSString class]])
{
stringValue = value;
}
else if([value isKindOfClass:[NSDictionary class]] || [value isKindOfClass:[NSArray class]])
{
NSError* error = nil;
NSData* encoded = [KSJSONCodec encode:value options:KSJSONEncodeOptionSorted | KSJSONEncodeOptionPretty error:&error];
if(error != nil)
{
KSLOG_ERROR(@"Could not encode report section %@: %@", key, error);
continue;
}
stringValue = [[NSString alloc] initWithData:encoded encoding:NSUTF8StringEncoding];
}
else if(value == nil)
{
KSLOG_WARN(@"Report section %@ not found", key);
}
else
{
KSLOG_ERROR(@"Could not encode report section %@: Don't know how to encode class %@", key, [value class]);
}
if(stringValue != nil)
{
if(i > 0)
{
[str appendString:@"\n\n"];
}
[str appendFormat:@"%@:\n", key];
[str appendString:stringValue];
}
}
return str;
}
- (NSString*) quincyInstallUUID
{
static NSString *installUUID = nil;
static dispatch_once_t predicate;
dispatch_once(&predicate,
^{
NSString *priorValue = [[NSUserDefaults standardUserDefaults] stringForKey:kQuincyUUIDUserDefault];
if (priorValue)
{
installUUID = priorValue;
}
else
{
CFUUIDRef uuidObj = CFUUIDCreate(NULL);
installUUID = (NSString*) CFBridgingRelease(CFUUIDCreateString(NULL, uuidObj));
CFRelease(uuidObj);
[[NSUserDefaults standardUserDefaults] setObject:installUUID forKey:kQuincyUUIDUserDefault];
[[NSUserDefaults standardUserDefaults] synchronize];
}
});
return installUUID;
}
- (NSString*) quincyArchFromCpuType:(cpu_type_t)cpuType cpuSubType:(cpu_subtype_t)cpuSubType
{
NSString *arch = @"???";
switch (cpuType)
{
case CPU_TYPE_ARM:
switch (cpuSubType)
{
case CPU_SUBTYPE_ARM_V6:
arch = @"armv6";
break;
case CPU_SUBTYPE_ARM_V7:
arch = @"armv7";
break;
case CPU_SUBTYPE_ARM_V7S:
arch = @"armv7s";
break;
default:
arch = @"arm-unknown";
break;
}
break;
case CPU_TYPE_ARM64:
switch (cpuSubType)
{
case CPU_SUBTYPE_ARM_ALL:
arch = @"arm64";
break;
case CPU_SUBTYPE_ARM64E:
arch = @"arm64e";
break;
#ifdef CPU_SUBTYPE_ARM_V8
case CPU_SUBTYPE_ARM_V8:
arch = @"arm64";
break;
#endif
default:
arch = @"arm64-unknown";
break;
}
break;
case CPU_TYPE_X86:
arch = @"i386";
break;
case CPU_TYPE_X86_64:
arch = @"x86_64";
break;
case CPU_TYPE_POWERPC:
arch = @"powerpc";
break;
}
return arch;
}
- (NSString*) uuidsFromReport:(NSDictionary*) standardReport
{
NSMutableString* uuidString = [NSMutableString string];
NSArray* binaryImages = [standardReport objectForKey:@KSCrashField_BinaryImages];
if(binaryImages == nil)
{
return @"";
}
NSDictionary* systemInfo = [standardReport objectForKey:@KSCrashField_System];
NSString* processPath = [systemInfo objectForKey:@KSCrashField_ExecutablePath];
NSString* appContainerPath = [[processPath stringByDeletingLastPathComponent] stringByDeletingLastPathComponent];
for(NSDictionary* image in binaryImages)
{
NSString* imagePath = [[image objectForKey:@KSCrashField_Name] stringByStandardizingPath];
NSString* imageType;
if(processPath && [imagePath isEqualToString:processPath])
{
imageType = @"app";
}
else if(appContainerPath && [imagePath hasPrefix:appContainerPath])
{
imageType = @"framework";
}
else
{
// Only include the UUID information for the app binary or frameworks contained in
// the app.
continue;
}
NSString* uuid = [image objectForKey:@KSCrashField_UUID];
if(uuid == nil)
{
uuid = @"???";
}
else
{
uuid = [[uuid lowercaseString] stringByReplacingOccurrencesOfString:@"-" withString:@""];
}
cpu_type_t cpuType = [[image objectForKey:@KSCrashField_CPUType] intValue];
cpu_subtype_t cpuSubType = [[image objectForKey:@KSCrashField_CPUSubType] intValue];
NSString* arch = [self quincyArchFromCpuType:cpuType cpuSubType:cpuSubType];
[uuidString appendFormat:@"<uuid type=\"%@\" arch=\"%@\">%@</uuid>", imageType, arch, uuid];
}
return uuidString;
}
- (NSString*) toQuincyFormat:(NSDictionary*) reportTuple
{
NSDictionary* report = [reportTuple objectForKey:kFilterKeyStandard];
NSString* appleReport = [reportTuple objectForKey:kFilterKeyApple];
NSDictionary* systemDict = [report objectForKey:@KSCrashField_System];
NSString* userID = self.userIDKey == nil ? nil : [self blankForNil:[report objectForKeyPath:self.userIDKey]];
NSString* userName = self.userNameKey == nil ? nil : [self blankForNil:[report objectForKeyPath:self.userNameKey]];
NSString* contactEmail = self.contactEmailKey == nil ? nil : [self blankForNil:[report objectForKeyPath:self.contactEmailKey]];
NSString* crashReportDescription = [self.crashDescriptionKeys count] == 0 ? nil : [self descriptionForReport:report keys:self.crashDescriptionKeys];
NSString* uuids = [self uuidsFromReport:report];
NSDictionary* reportInfo = [report objectForKey:@KSCrashField_Report];
NSString* result = [NSString stringWithFormat:
@"\n <crash>\n"
@" <applicationname>%@</applicationname>\n"
@" <uuids>\n"
@" %@\n"
@" </uuids>\n"
@" <bundleidentifier>%@</bundleidentifier>\n"
@" <systemversion>%@</systemversion>\n"
@" <platform>%@</platform>\n"
@" <senderversion>%@</senderversion>\n"
@" <version>%@</version>\n"
@" <uuid>%@</uuid>\n"
@" <log><![CDATA[%@]]></log>\n"
@" <userid>%@</userid>\n"
@" <username>%@</username>\n"
@" <contact>%@</contact>\n"
@" <installstring>%@</installstring>\n"
@" <description><![CDATA[%@]]></description>\n"
@" </crash>",
[systemDict objectForKey:@"CFBundleExecutable"],
uuids,
[systemDict objectForKey:@"CFBundleIdentifier"],
[systemDict objectForKey:@"system_version"],
[systemDict objectForKey:@"machine"],
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"],
[systemDict objectForKey:@"CFBundleVersion"],
[reportInfo objectForKey:@KSCrashField_ID],
[self cdataEscaped:appleReport],
userID,
userName,
contactEmail,
[self quincyInstallUUID],
[self cdataEscaped:crashReportDescription]];
return result;
}
- (NSData*) toQuincyBody:(NSArray*) reports
{
NSMutableString* xmlString = [NSMutableString stringWithString:@"<crashes>"];
for(NSDictionary* report in reports)
{
NSString* reportString = [self toQuincyFormat:report];
[xmlString appendString:reportString];
}
[xmlString appendString:@"</crashes>"];
return [xmlString dataUsingEncoding:NSUTF8StringEncoding];
}
- (void) filterReports:(NSArray*) reports
bodyName:(NSString*) bodyName
bodyContentType:(NSString*) bodyContentType
bodyFilename:(NSString*) bodyFilename
onCompletion:(KSCrashReportFilterCompletion) onCompletion
{
if(self.url == nil)
{
if(onCompletion != nil)
{
onCompletion(reports, NO, [NSError errorWithDomain:[[self class] description]
code:0
description:@"url was nil"]);
}
return;
}
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:self.url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:15];
KSHTTPMultipartPostBody* body = [KSHTTPMultipartPostBody body];
[body appendData:[self toQuincyBody:reports]
name:bodyName
contentType:bodyContentType
filename:bodyFilename];
request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
request.timeoutInterval = 15;
request.HTTPMethod = @"POST";
request.HTTPBody = [body data];
[request setValue:@"Quincy/iOS" forHTTPHeaderField:@"User-Agent"];
[request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
[request setValue:body.contentType forHTTPHeaderField:@"Content-type"];
dispatch_block_t sendOperation = ^
{
KSLOG_TRACE(@"Sending request to %@", request.URL);
[[KSHTTPRequestSender sender] sendRequest:request
onSuccess:^(__unused NSHTTPURLResponse* response,
__unused NSData* data)
{
KSLOG_DEBUG(@"Post successful");
kscrash_callCompletion(onCompletion, reports, YES, nil);
} onFailure:^(NSHTTPURLResponse* response, NSData* data)
{
NSString* text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
KSLOG_DEBUG(@"Post failed. Code %d", response.statusCode);
KSLOG_TRACE(@"Response text:\n%@", text);
kscrash_callCompletion(onCompletion, reports, NO,
[NSError errorWithDomain:[[self class] description]
code:response.statusCode
description:text]);
} onError:^(NSError* error)
{
KSLOG_DEBUG(@"Posting error: %@", error);
kscrash_callCompletion(onCompletion, reports, NO, error);
}];
};
if(self.waitUntilReachable)
{
KSLOG_TRACE(@"Starting reachable operation to host %@", [self.url host]);
self.reachableOperation = [KSReachableOperationKSCrash operationWithHost:[self.url host]
allowWWAN:YES
block:sendOperation];
}
else
{
sendOperation();
}
}
- (void) filterReports:(NSArray*) reports
onCompletion:(KSCrashReportFilterCompletion) onCompletion
{
[self filterReports:reports
bodyName:@"xmlstring"
bodyContentType:nil
bodyFilename:nil
onCompletion:onCompletion];
}
@end
@interface KSCrashReportSinkHockey ()
@property(nonatomic,readwrite,retain) NSString* appIdentifier;
@end
@implementation KSCrashReportSinkHockey
@synthesize appIdentifier = _appIdentifier;
+ (KSCrashReportSinkHockey*) sinkWithAppIdentifier:(NSString*) appIdentifier
userIDKey:(NSString*) userIDKey
userNameKey:(NSString*) userNameKey
contactEmailKey:(NSString*) contactEmailKey
crashDescriptionKeys:(NSArray*) crashDescriptionKeys
{
return [[self alloc] initWithAppIdentifier:appIdentifier
userIDKey:userIDKey
userNameKey:userNameKey
contactEmailKey:contactEmailKey
crashDescriptionKeys:crashDescriptionKeys];
}
- (id) initWithAppIdentifier:(NSString*) appIdentifier
userIDKey:(NSString*) userIDKey
userNameKey:(NSString*) userNameKey
contactEmailKey:(NSString*) contactEmailKey
crashDescriptionKeys:(NSArray*) crashDescriptionKeys
{
if((self = [super initWithURL:[self urlWithAppIdentifier:appIdentifier]
userIDKey:userIDKey
userNameKey:userNameKey
contactEmailKey:contactEmailKey
crashDescriptionKeys:crashDescriptionKeys]))
{
self.appIdentifier = appIdentifier;
}
return self;
}
- (void) filterReports:(NSArray*) reports
onCompletion:(KSCrashReportFilterCompletion) onCompletion
{
if(self.appIdentifier == nil)
{
if(onCompletion != nil)
{
onCompletion(reports, NO, [NSError errorWithDomain:[[self class] description]
code:0
description:@"appIdentifier was nil"]);
}
return;
}
[self filterReports:reports
bodyName:@"xml"
bodyContentType:@"text/xml"
bodyFilename:@"crash.xml"
onCompletion:onCompletion];
}
- (NSURL*) urlWithAppIdentifier:(NSString*) appIdentifier
{
NSString* urlString = [NSString stringWithFormat:@"https://sdk.hockeyapp.net/api/2/apps/%@/crashes",
[appIdentifier URLEncoded]];
return [NSURL URLWithString:urlString];
}
@end