Advertisement
Guest User

Untitled

a guest
Sep 14th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (void)_performSendingCrashReports {
  2.   NSMutableDictionary *approvedCrashReports = [NSMutableDictionary dictionaryWithDictionary:[[NSUserDefaults standardUserDefaults] dictionaryForKey: kApprovedCrashReports]];
  3.   
  4.   NSError *error = NULL;
  5.     
  6.   NSString *userid = @"";
  7.   NSString *contact = @"";
  8.   NSString *description = @"";
  9.   
  10.   if (self.delegate != nil && [self.delegate respondsToSelector:@selector(crashReportUserID)]) {
  11.     userid = [self.delegate crashReportUserID] ?: @"";
  12.   }
  13.     
  14.   if (self.delegate != nil && [self.delegate respondsToSelector:@selector(crashReportContact)]) {
  15.     contact = [self.delegate crashReportContact] ?: @"";
  16.   }
  17.     
  18.   if (self.delegate != nil && [self.delegate respondsToSelector:@selector(crashReportDescription)]) {
  19.     description = [self.delegate crashReportDescription] ?: @"";
  20.   }
  21.     
  22.   NSMutableString *crashes = nil;
  23.   _crashIdenticalCurrentVersion = NO;
  24.   
  25.   for (NSUInteger i=0; i < [_crashFiles count]; i++) {
  26.     NSString *filename = [_crashesDir stringByAppendingPathComponent:[_crashFiles objectAtIndex:i]];
  27.     NSData *crashData = [NSData dataWithContentsOfFile:filename];
  28.         
  29.     if ([crashData length] > 0) {
  30.       PLCrashReport *report = [[[PLCrashReport alloc] initWithData:crashData error:&error] autorelease];
  31.             
  32.       if (report == nil) {
  33.         NSLog(@"Could not parse crash report");
  34.         continue;
  35.       }
  36.       
  37.       NSString *crashLogString = [PLCrashReportTextFormatter stringValueForCrashReport:report withTextFormat:PLCrashReportTextFormatiOS];
  38.       
  39.       if ([report.applicationInfo.applicationVersion compare:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]] == NSOrderedSame) {
  40.         _crashIdenticalCurrentVersion = YES;
  41.       }
  42.             
  43.       if (crashes == nil) {
  44.         crashes = [NSMutableString string];
  45.       }
  46.       
  47.       [crashes appendFormat:@"<crash><applicationname>%s</applicationname><bundleidentifier>%@</bundleidentifier><systemversion>%@</systemversion><platform>%@</platform><senderversion>%@</senderversion><version>%@</version><log><![CDATA[%@]]></log><userid>%@</userid><contact>%@</contact><description><![CDATA[%@]]></description></crash>",
  48.        [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleExecutable"] UTF8String],
  49.        report.applicationInfo.applicationIdentifier,
  50.        report.systemInfo.operatingSystemVersion,
  51.        [self _getDevicePlatform],
  52.        [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"],
  53.        report.applicationInfo.applicationVersion,
  54.        [crashLogString stringByReplacingOccurrencesOfString:@"]]>" withString:@"]]" @"]]><![CDATA[" @">" options:NSLiteralSearch range:NSMakeRange(0,crashLogString.length)],
  55.        userid,
  56.        contact,
  57.        [description stringByReplacingOccurrencesOfString:@"]]>" withString:@"]]" @"]]><![CDATA[" @">" options:NSLiteralSearch range:NSMakeRange(0,description.length)]];
  58.       
  59.       
  60.       // store this crash report as user approved, so if it fails it will retry automatically
  61.       [approvedCrashReports setObject:[NSNumber numberWithBool:YES] forKey:[_crashFiles objectAtIndex:i]];
  62.     } else {
  63.       // we cannot do anything with this report, so delete it
  64.       [self.fileManager removeItemAtPath:filename error:&error];
  65.     }
  66.   }
  67.     
  68.   [[NSUserDefaults standardUserDefaults] setObject:approvedCrashReports forKey:kApprovedCrashReports];
  69.   [[NSUserDefaults standardUserDefaults] synchronize];
  70.   
  71.   if (crashes != nil) {
  72.     BWQuincyLog(@"Sending crash reports:\n%@", crashes);
  73.     [self _postXML:[NSString stringWithFormat:@"<crashes>%@</crashes>", crashes]
  74.              toURL:[NSURL URLWithString:self.submissionURL]];
  75.     
  76.   }
  77. }
  78. [7/26/55 BE 5:22:19 PM] Aemgtz:
  79. - (void)_postXML:(NSString*)xml toURL:(NSURL*)url {
  80.   NSMutableURLRequest *request = nil;
  81.   NSString *boundary = @"----FOO";
  82.   
  83.   if (self.appIdentifier) {
  84.     request = [NSMutableURLRequest requestWithURL:
  85.                [NSURL URLWithString:[NSString stringWithFormat:@"%@api/2/apps/%@/crashes?sdk=%@&sdk_version=%@",
  86.                                      self.submissionURL,
  87.                                      [self.appIdentifier stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
  88.                                      SDK_NAME,
  89.                                      SDK_VERSION
  90.                                      ]
  91.                 ]];
  92.   } else {
  93.     request = [NSMutableURLRequest requestWithURL:url];
  94.   }
  95.   
  96.   [request setCachePolicy: NSURLRequestReloadIgnoringLocalCacheData];
  97.   [request setValue:@"Quincy/iOS" forHTTPHeaderField:@"User-Agent"];
  98.   [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
  99.   [request setTimeoutInterval: 15];
  100.   [request setHTTPMethod:@"POST"];
  101.   NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
  102.   [request setValue:contentType forHTTPHeaderField:@"Content-type"];
  103.     
  104.   NSMutableData *postBody =  [NSMutableData data];
  105.   [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  106.   if (self.appIdentifier) {
  107.     [postBody appendData:[@"Content-Disposition: form-data; name=\"xml\"; filename=\"crash.xml\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  108.     [postBody appendData:[[NSString stringWithFormat:@"Content-Type: text/xml\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  109.   } else {
  110.     [postBody appendData:[@"Content-Disposition: form-data; name=\"xmlstring\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  111.     }
  112.   [postBody appendData:[xml dataUsingEncoding:NSUTF8StringEncoding]];
  113.   [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  114.   
  115.   [request setHTTPBody:postBody];
  116.     
  117.   _serverResult = CrashReportStatusUnknown;
  118.   _statusCode = 200;
  119.     
  120.   //Release when done in the delegate method
  121.   _responseData = [[NSMutableData alloc] init];
  122.     
  123.   if (self.delegate != nil && [self.delegate respondsToSelector:@selector(connectionOpened)]) {
  124.     [self.delegate connectionOpened];
  125.   }
  126.     
  127.   _urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
  128.   
  129.   if (!_urlConnection) {
  130.     BWQuincyLog(@"Sending crash reports could not start!");
  131.     _sendingInProgress = NO;
  132.   } else {
  133.     BWQuincyLog(@"Sending crash reports started.");
  134.   }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement