Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix OHHTTPStubsProtocol threading #96

Merged
merged 4 commits into from
May 3, 2015
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
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
_You can download previous versions [here](https://github.com/AliSoftware/OHHTTPStubs/tags) and latest version [here](https://github.com/AliSoftware/OHHTTPStubs/zipball/master) (ZIP files generated by GitHub on the fly)_

## [3.1.13](https://github.com/AliSoftware/OHHTTPStubs/releases/tag/3.1.13)

* Fix threading in NSURLProtocol subclass calling NSURLProtocolClient callbacks from wrong thread. ([@nsprogrammer](https://github.com/nsprogrammer), [#96](https://github.com/AliSoftware/OHHTTPStubs/pull/96))

## [3.1.12](https://github.com/AliSoftware/OHHTTPStubs/releases/tag/3.1.12)

* Fixed issue with HTTP 300 return code (multiple-choice) that is not supposed to redirect. ([@tarbrain](https://github.com/tarbrain), [#92](https://github.com/AliSoftware/OHHTTPStubs/pull/92))
Expand Down Expand Up @@ -212,4 +216,4 @@ _You will now have to indicate the folder containing headers for `OHHTTPStubs` i

## [0.1.0](https://github.com/AliSoftware/OHHTTPStubs/tree/0.1.0)

* Initial version
* Initial version
30 changes: 18 additions & 12 deletions OHHTTPStubs/Sources/OHHTTPStubs.m
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ - (OHHTTPStubsDescriptor*)firstStubPassingTestForRequest:(NSURLRequest*)request
@interface OHHTTPStubsProtocol()
@property(assign) BOOL stopped;
@property(strong) OHHTTPStubsDescriptor* stub;
@property(assign) CFRunLoopRef clientRunLoop;
- (void)executeOnClientRunLoopAfterDelay:(NSTimeInterval)delayInSeconds block:(dispatch_block_t)block;
@end

@implementation OHHTTPStubsProtocol
Expand Down Expand Up @@ -309,6 +311,7 @@ - (NSCachedURLResponse *)cachedResponse

- (void)startLoading
{
self.clientRunLoop = CFRunLoopGetCurrent();
NSURLRequest* request = self.request;
id<NSURLProtocolClient> client = self.client;

Expand Down Expand Up @@ -365,16 +368,16 @@ - (void)startLoading
if (((responseStub.statusCode > 300) && (responseStub.statusCode < 400)) && redirectLocationURL)
{
NSURLRequest* redirectRequest = [NSURLRequest requestWithURL:redirectLocationURL];
execute_after(responseStub.requestTime, ^{
[self executeOnClientRunLoopAfterDelay:responseStub.requestTime block:^{
if (!self.stopped)
{
[client URLProtocol:self wasRedirectedToRequest:redirectRequest redirectResponse:urlResponse];
}
});
}];
}
else
{
execute_after(responseStub.requestTime,^{
[self executeOnClientRunLoopAfterDelay:responseStub.requestTime block:^{
if (!self.stopped)
{
[client URLProtocol:self didReceiveResponse:urlResponse cacheStoragePolicy:NSURLCacheStorageNotAllowed];
Expand All @@ -397,16 +400,16 @@ - (void)startLoading
}
}];
}
});
}];
}
} else {
// Send the canned error
execute_after(responseStub.responseTime, ^{
[self executeOnClientRunLoopAfterDelay:responseStub.responseTime block:^{
if (!self.stopped)
{
[client URLProtocol:self didFailWithError:responseStub.error];
}
});
}];
}
}

Expand Down Expand Up @@ -482,10 +485,10 @@ - (void) streamDataForClient:(id<NSURLProtocolClient>)client
if (chunkSizeToRead == 0)
{
// Nothing to read at this pass, but probably later
execute_after(timingInfo.slotTime, ^{
[self executeOnClientRunLoopAfterDelay:timingInfo.slotTime block:^{
[self streamDataForClient:client fromStream:inputStream
timingInfo:timingInfo completion:completion];
});
}];
} else {
uint8_t* buffer = (uint8_t*)malloc(sizeof(uint8_t)*chunkSizeToRead);
NSInteger bytesRead = [inputStream read:buffer maxLength:chunkSizeToRead];
Expand All @@ -494,11 +497,11 @@ - (void) streamDataForClient:(id<NSURLProtocolClient>)client
NSData * data = [NSData dataWithBytes:buffer length:bytesRead];
// Wait for 'slotTime' seconds before sending the chunk.
// If bytesRead < chunkSizePerSlot (because we are near the EOF), adjust slotTime proportionally to the bytes remaining
execute_after(((double)bytesRead / (double)chunkSizeToRead) * timingInfo.slotTime, ^{
[self executeOnClientRunLoopAfterDelay:((double)bytesRead / (double)chunkSizeToRead) * timingInfo.slotTime block:^{
[client URLProtocol:self didLoadData:data];
[self streamDataForClient:client fromStream:inputStream
timingInfo:timingInfo completion:completion];
});
}];
}
else
{
Expand Down Expand Up @@ -526,10 +529,13 @@ - (void) streamDataForClient:(id<NSURLProtocolClient>)client
// Delayed execution utility methods
/////////////////////////////////////////////

static void execute_after(NSTimeInterval delayInSeconds, dispatch_block_t block)
- (void)executeOnClientRunLoopAfterDelay:(NSTimeInterval)delayInSeconds block:(dispatch_block_t)block
{
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block);
dispatch_after(popTime, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
CFRunLoopPerformBlock(self.clientRunLoop, kCFRunLoopDefaultMode, block);
CFRunLoopWakeUp(self.clientRunLoop);
});
}

@end