Skip to content

Commit

Permalink
Adds shouldWaitUntilDone to LFHTTPRequest for making synchronous calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukhnos D. Liu committed Jun 16, 2009
1 parent 440cbfc commit b0f93bb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
11 changes: 7 additions & 4 deletions LFWebAPIKit/LFHTTPRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,18 @@ extern NSString *LFHTTPRequestPOSTMethod;
size_t _readBufferSize;

id _sessionInfo;

BOOL _shouldWaitUntilDone;
}
// + (NSData *)fetchDataSynchronouslyFromURL:(NSURL *)url;
// + (NSData *)fetchDataSynchronouslyFromURL:(NSURL *)url byPostingData:(NSData *)data;
// + (NSData *)fetchDataSynchronouslyFromURL:(NSURL *)url byPostingMultipartData:(NSArray *)arrayOfData;
// + (NSData *)fetchDataSynchronouslyFromURL:(NSURL *)url byPostingDictionary:(NSDictionary *)dictionary;

- (id)init;
- (BOOL)isRunning;
- (void)cancel;
- (void)cancelWithoutDelegateMessage;

- (BOOL)shouldWaitUntilDone;
- (void)setShouldWaitUnitlDone:(BOOL)waitUntilDone;

- (BOOL)performMethod:(NSString *)methodName onURL:(NSURL *)url withData:(NSData *)data;

#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_4
Expand Down
35 changes: 25 additions & 10 deletions LFWebAPIKit/LFHTTPRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ - (void)readStreamEndEncountered
if (![self isRunning]) {
return;
}

[self cleanUp];

if ([_delegate respondsToSelector:@selector(httpRequestDidComplete:)]) {
Expand Down Expand Up @@ -392,15 +392,6 @@ - (BOOL)_performMethod:(NSString *)methodName onURL:(NSURL *)url withData:(NSDat
CFHTTPMessageSetBody(request, (CFDataRef)data);
}

// for debug only, we comment them out here
/*
NSDictionary *headerCheck = (NSDictionary*)CFHTTPMessageCopyAllHeaderFields(request);
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_4
NSMakeCollectable((CFTypeRef)headerCheck);
#endif
[headerCheck release];
*/

CFReadStreamRef tmpReadStream;

if (inputStream) {
Expand Down Expand Up @@ -494,6 +485,19 @@ - (BOOL)_performMethod:(NSString *)methodName onURL:(NSURL *)url withData:(NSDat
[[NSRunLoop currentRunLoop] addTimer:_requestMessageBodyTracker forMode:NSModalPanelRunLoopMode];
#endif

if (_shouldWaitUntilDone) {
NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop];
NSString *currentMode = [currentRunLoop currentMode];

if (![currentMode length]) {
currentMode = NSDefaultRunLoopMode;
}

while ([self isRunning]) {
[currentRunLoop runMode:currentMode beforeDate:[NSDate distantFuture]];
}
}

return YES;
}

Expand Down Expand Up @@ -628,11 +632,22 @@ - (void)setReadBufferSize:(size_t)newSize
bzero(_readBuffer, newSize);
}

- (BOOL)shouldWaitUntilDone
{
return _shouldWaitUntilDone;
}

- (void)setShouldWaitUnitlDone:(BOOL)waitUntilDone
{
_shouldWaitUntilDone = waitUntilDone;
}

@end

void LFHRReadStreamClientCallBack(CFReadStreamRef stream, CFStreamEventType eventType, void *clientCallBackInfo)
{
id pool = [NSAutoreleasePool new];

LFHTTPRequest *request = (LFHTTPRequest *)clientCallBackInfo;
switch (eventType) {
case kCFStreamEventHasBytesAvailable:
Expand Down

0 comments on commit b0f93bb

Please sign in to comment.