Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Castle/Internal/CASAPIClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ - (NSURLSessionDataTask *)dataTaskWithPath:(NSString *)path

// Parse response data
if(data) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSString *contentType = httpResponse.allHeaderFields[@"Content-Type"];

// Check for application/json content-type and parse response
Expand Down
8 changes: 7 additions & 1 deletion Castle/Internal/CASCustom.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,17 @@ + (nullable instancetype)eventWithName:(nullable NSString *)name properties:(nul
CASLog(@"Event name can't be nil.");
return nil;
}

if([name isEqualToString:@""]) {
CASLog(@"Event names must be at least one (1) character long.");
return nil;
}

if(!properties) {
// prevent dropping from nil properties in event, as it is allowed
// per signature
properties = @{};
}

BOOL valid = [CASEvent propertiesContainValidData:properties];
if(!valid) {
Expand Down
20 changes: 13 additions & 7 deletions Castle/Internal/CASEventQueue.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,18 @@ - (instancetype)init
{
self = [super init];
if (self) {
dispatch_sync(CASEventStorageQueue(), ^{
// Read stored queue from disk
self.eventQueue = [self storedQueue].mutableCopy;

// Initialize API client
self.client = [CASAPIClient clientWithConfiguration:[Castle configuration]];
// Immediate initialize
self.eventQueue = [[NSMutableArray alloc] init];
self.client = [CASAPIClient clientWithConfiguration:[Castle configuration]];

dispatch_async(CASEventStorageQueue(), ^{
NSArray *storedEvents = [self storedQueue];
if (storedEvents.count > 0) {
// Prepend to current queue
NSMutableArray *combined = [storedEvents mutableCopy];
[combined addObjectsFromArray:self.eventQueue];
self.eventQueue = combined;
}
});
}
return self;
Expand Down Expand Up @@ -233,7 +239,7 @@ - (void)flush

self.task = nil;

CASLog(@"Successfully flushed (%ld) events: %@", monitorModel.events.count, [monitorModel JSONPayload]);
CASLog(@"Successfully flushed (%ld) events", monitorModel.events.count);

if ([self eventQueueExceedsFlushLimit] && self.eventQueue.count > 0) {
CASLog(@"Current event queue still exceeds flush limit. Flush again");
Expand Down
22 changes: 6 additions & 16 deletions Castle/Internal/CASRequestInterceptor.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,17 @@ + (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b

- (void)startLoading
{
dispatch_sync(dispatch_get_main_queue(), ^{
// Always flush the queue when a request is intercepted
[Castle flush];
});

NSMutableURLRequest *newRequest = [self.request mutableCopy];
[NSURLProtocol setProperty:@YES forKey:CASRecursiveRequestFlagProperty inRequest:newRequest];

dispatch_sync(dispatch_get_main_queue(), ^{
// Set custom header
[newRequest setValue:[Castle createRequestToken] forHTTPHeaderField:CastleRequestTokenHeaderName];
});
// Set custom header
[newRequest setValue:[Castle createRequestToken] forHTTPHeaderField:CastleRequestTokenHeaderName];

NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.protocolClasses = [config.protocolClasses arrayByAddingObject:self.class];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];

NSURLSessionDataTask *task = [session dataTaskWithRequest:newRequest];
[task resume];
self.task = [session dataTaskWithRequest:newRequest];
[self.task resume];
}

- (void)stopLoading
Expand All @@ -76,10 +68,8 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPer
if (response) {
NSMutableURLRequest *redirectRequest = [newRequest mutableCopy];

dispatch_sync(dispatch_get_main_queue(), ^{
// Set custom header
[redirectRequest setValue:[Castle createRequestToken] forHTTPHeaderField:CastleRequestTokenHeaderName];
});
// Set custom header
[redirectRequest setValue:[Castle createRequestToken] forHTTPHeaderField:CastleRequestTokenHeaderName];

[[self client] URLProtocol:self wasRedirectedToRequest:redirectRequest redirectResponse:response];

Expand Down
5 changes: 0 additions & 5 deletions Castle/Public/Castle.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

#import <UIKit/UIKit.h>

#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>

#import "CASAPIClient.h"
#import "CASUtils.h"
#import "CASEvent.h"
Expand Down Expand Up @@ -81,8 +78,6 @@ - (NSURL *)baseURL

NSString *const CastleRequestTokenHeaderName = @"X-Castle-Request-Token";

static CTTelephonyNetworkInfo *_telephonyNetworkInfo;

@interface Castle ()
@property (nonatomic, strong, nullable) CastleConfiguration *configuration;
@property (nonatomic, strong, nullable) CASEventQueue *eventQueue;
Expand Down