Skip to content
This repository was archived by the owner on Oct 25, 2022. It is now read-only.
Open
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
2 changes: 1 addition & 1 deletion BBHTTP.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "BBHTTP"
s.version = "0.9.9"
s.version = "1.0.1"
s.summary = "A modern HTTP client framework for iOS/OSX built on top of libcurl."
s.homepage = "https://github.com/brunodecarvalho/BBHTTP"
s.license = { :type => "Apache License, Version 2.0", :file => "LICENSE" }
Expand Down
Empty file modified BBHTTP/BBHTTP.h
100644 → 100755
Empty file.
Empty file modified BBHTTP/BBHTTPExecutor.h
100644 → 100755
Empty file.
57 changes: 34 additions & 23 deletions BBHTTP/BBHTTPExecutor.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ static size_t BBHTTPExecutorReadHeader(uint8_t* buffer, size_t size, size_t leng
// Subsequent callbacks will keep hitting BBHTTPExecutorReadHeader()
return length;
}

// End of headers reached, data will follow
BBHTTPLogTrace(@"%@ | All headers received.", context);
BOOL canProceed = YES;
if ([context isCurrentResponse100Continue]) {
if ([context isCurrentResponse100Continue] ||
[context isCurrentResponse30XRedirect]) {
// Subsequent callbacks will hit BBHTTPExecutorReadStatusLine()
[context finishCurrentResponse];
} else {
Expand Down Expand Up @@ -135,15 +136,13 @@ static size_t BBHTTPExecutorReceiveCallback(uint8_t* buffer, size_t size, size_t
case BBHTTPResponseStateReady:
case BBHTTPResponseStateReadingStatusLine:
return BBHTTPExecutorReadStatusLine(buffer, size, length, context);

case BBHTTPResponseStateReadingHeaders:
return BBHTTPExecutorReadHeader(buffer, size, length, context);

case BBHTTPResponseStateReadingData:
return BBHTTPExecutorAppendData(buffer, size, length, context);

default:
// never happens...
// never happen
return 0;
}
}
Expand Down Expand Up @@ -237,10 +236,10 @@ - (instancetype)initWithId:(NSString*)identifier
_availableCurlHandles = [NSMutableArray array];
_allCurlHandles = [NSMutableArray array];

NSString* syncQueueId = [NSString stringWithFormat:@"com.biasedbit.HTTPExecutorSyncQueue-%@", identifier];
NSString* syncQueueId = [NSString stringWithFormat:@"BBHTTP.HTTPExecutorSyncQueue-%@", identifier];
_synchronizationQueue = dispatch_queue_create([syncQueueId UTF8String], DISPATCH_QUEUE_SERIAL);

NSString* requestQueueId = [NSString stringWithFormat:@"com.biasedbit.HTTPExecutorRequestQueue-%@", identifier];
NSString* requestQueueId = [NSString stringWithFormat:@"BBHTTP.HTTPExecutorRequestQueue-%@", identifier];
_requestExecutionQueue = dispatch_queue_create([requestQueueId UTF8String], DISPATCH_QUEUE_CONCURRENT);
}

Expand Down Expand Up @@ -498,37 +497,49 @@ - (void)executeContext:(BBHTTPRequestContext*)context withCurlHandle:(CURL*)hand
curl_easy_setopt(handle, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(handle, CURLOPT_READDATA, NULL);
}

// Setup - response handling callback
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, BBHTTPExecutorReceiveCallback);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, context);

// Setup - configure timeouts
curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, context.request.connectionTimeout);
curl_easy_setopt(handle, CURLOPT_LOW_SPEED_LIMIT, context.request.downloadTimeout.bytesPerSecond);
curl_easy_setopt(handle, CURLOPT_LOW_SPEED_TIME, context.request.downloadTimeout.duration);
curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, request.connectionTimeout);
curl_easy_setopt(handle, CURLOPT_LOW_SPEED_LIMIT, request.downloadTimeout.bytesPerSecond);
curl_easy_setopt(handle, CURLOPT_LOW_SPEED_TIME, request.downloadTimeout.duration);

// Setup - speed limits
if ([context.request isUpload] && (context.request.uploadSpeedLimit > 0)) {
curl_easy_setopt(handle, CURLOPT_MAX_SEND_SPEED_LARGE, context.request.uploadSpeedLimit);
if ([request isUpload] && (request.uploadSpeedLimit > 0)) {
curl_easy_setopt(handle, CURLOPT_MAX_SEND_SPEED_LARGE, request.uploadSpeedLimit);
}

if (context.request.downloadSpeedLimit > 0) {
curl_easy_setopt(handle, CURLOPT_MAX_RECV_SPEED_LARGE, context.request.downloadSpeedLimit);
if (request.downloadSpeedLimit > 0) {
curl_easy_setopt(handle, CURLOPT_MAX_RECV_SPEED_LARGE, request.downloadSpeedLimit);
}

// Setup - configure redirections
// if (context.request.maxRedirects == 0) {
// curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, NO);
// } else {
// curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, YES);
// curl_easy_setopt(handle, CURLOPT_MAXREDIRS, context.request.maxRedirects);
// }
if (request.maxRedirects == 0) {
curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 0L);
} else {
curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(handle, CURLOPT_MAXREDIRS, request.maxRedirects);
curl_easy_setopt(handle, CURLOPT_AUTOREFERER, 1L);
}

// Setup - misc configuration
curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(handle, CURLOPT_FAILONERROR, 0L); // Handle >= 400 codes as success at this layer
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, context.request.allowInvalidSSLCertificates ? 0L : 1L);
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, request.allowInvalidSSLCertificates ? 0L : 1L);

if (request.postField.length > 0)
{
curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE, request.postField.length);
curl_easy_setopt(handle, CURLOPT_POSTFIELDS, request.postField.bytes);
}

if (request.noBody)
{
curl_easy_setopt(handle, CURLOPT_NOBODY, 1L);
}

BBHTTPLogInfo(@"%@ | Request starting…", context);

Expand Down
Empty file modified BBHTTP/BBHTTPRequest+Convenience.h
100644 → 100755
Empty file.
Empty file modified BBHTTP/BBHTTPRequest+Convenience.m
100644 → 100755
Empty file.
11 changes: 8 additions & 3 deletions BBHTTP/BBHTTPRequest.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@

struct BBTransferSpeed {
NSUInteger bytesPerSecond;
NSTimeInterval duration;
NSUInteger duration;
};
typedef struct BBTransferSpeed BBTransferSpeed;



#pragma mark - Utility Functions

extern BBTransferSpeed BBTransferSpeedMake(NSUInteger bytesPerSecond, NSTimeInterval duration);
extern BBTransferSpeed BBTransferSpeedMake(NSUInteger bytesPerSecond, NSUInteger duration);
extern NSString* NSStringFromBBTransferSpeed(BBTransferSpeed transferSpeed);


Expand Down Expand Up @@ -374,7 +374,7 @@ extern NSString* NSStringFromBBTransferSpeed(BBTransferSpeed transferSpeed);
/// @name Configuring other request properties
/// ------------------------------------------

/* TODO: Not yet properly supported. */
/* Maximum number of redirects allowed. */
@property(assign, nonatomic) NSUInteger maxRedirects;

/**
Expand Down Expand Up @@ -483,4 +483,9 @@ extern NSString* NSStringFromBBTransferSpeed(BBTransferSpeed transferSpeed);
/** Flag that indicates whether this request was cancelled. */
@property(assign, nonatomic, readonly, getter = wasCancelled) BOOL cancelled;

/** Specify data to POST to server. */
@property (nonatomic, strong) NSData* postField;
/** Do not get the body contents. */
@property (nonatomic, assign) BOOL noBody;

@end
26 changes: 14 additions & 12 deletions BBHTTP/BBHTTPRequest.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

#pragma mark - Utility Functions

BBTransferSpeed BBTransferSpeedMake(NSUInteger bytesPerSecond, NSTimeInterval duration)
BBTransferSpeed BBTransferSpeedMake(NSUInteger bytesPerSecond, NSUInteger duration)
{
BBTransferSpeed transferSpeed;
transferSpeed.bytesPerSecond = bytesPerSecond;
Expand All @@ -38,7 +38,7 @@ BBTransferSpeed BBTransferSpeedMake(NSUInteger bytesPerSecond, NSTimeInterval du

NSString* NSStringFromBBTransferSpeed(BBTransferSpeed transferSpeed)
{
return [NSString stringWithFormat:@"%lu/s for %.0f seconds",
return [NSString stringWithFormat:@"%lu/s for %lu seconds",
(unsigned long)transferSpeed.bytesPerSecond, transferSpeed.duration];
}

Expand Down Expand Up @@ -88,10 +88,10 @@ - (instancetype)initWithURL:(NSURL*)url verb:(NSString*)verb andProtocolVersion:
_startTimestamp = -1;
_endTimestamp = -1;
_version = version;
_maxRedirects = 0;
_allowInvalidSSLCertificates = NO;
_maxRedirects = 3;
_allowInvalidSSLCertificates = YES;
_connectionTimeout = 10;
_downloadTimeout = BBTransferSpeedMake(1024, 20);
_downloadTimeout = BBTransferSpeedMake(1, 20);
_uploadSpeedLimit = 0;
_downloadSpeedLimit = 0;
_callbackQueue = dispatch_get_main_queue();
Expand Down Expand Up @@ -379,15 +379,17 @@ - (BOOL)cancel
if (_startTimestamp < 0) _startTimestamp = now;
_endTimestamp = now;

if (_finishBlock != nil) {
dispatch_async(_callbackQueue, ^{
dispatch_async(_callbackQueue, ^{

if (_finishBlock != nil)
{
_finishBlock(self);

_uploadProgressBlock = nil;
_downloadProgressBlock = nil;
_finishBlock = nil;
});
}
}
_uploadProgressBlock = nil;
_downloadProgressBlock = nil;

});

return YES;
}
Expand Down
Empty file modified BBHTTP/BBHTTPResponse.h
100644 → 100755
Empty file.
9 changes: 7 additions & 2 deletions BBHTTP/BBHTTPResponse.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ + (BBHTTPResponse*)responseWithStatusLine:(NSString*)statusLine
BBHTTPProtocolVersion version = BBHTTPProtocolVersionFromNSString(versionString);
NSUInteger statusCode = (NSUInteger)[statusCodeString integerValue];

NSString* message = [statusLine substringFromIndex:NSMaxRange(statusCodeRange) + 1];
NSUInteger index = NSMaxRange(statusCodeRange) + 1;
if (index > statusLine.length - 1)
{
index = statusLine.length - 1;
}

NSString* message = [statusLine substringFromIndex:index];

BBHTTPResponse* response = [[self alloc] initWithVersion:version code:statusCode andMessage:message];

Expand Down Expand Up @@ -128,7 +134,6 @@ - (BOOL)isSuccessful
return _successful;
}


#pragma mark Debug

- (NSString*)description
Expand Down
Empty file modified BBHTTP/Handlers/BBHTTPAccumulator.h
100644 → 100755
Empty file.
Empty file modified BBHTTP/Handlers/BBHTTPAccumulator.m
100644 → 100755
Empty file.
Empty file modified BBHTTP/Handlers/BBHTTPContentHandler.h
100644 → 100755
Empty file.
Empty file modified BBHTTP/Handlers/BBHTTPFileWriter.h
100644 → 100755
Empty file.
Empty file modified BBHTTP/Handlers/BBHTTPFileWriter.m
100644 → 100755
Empty file.
Empty file modified BBHTTP/Handlers/BBHTTPImageDecoder.h
100644 → 100755
Empty file.
Empty file modified BBHTTP/Handlers/BBHTTPImageDecoder.m
100644 → 100755
Empty file.
Empty file modified BBHTTP/Handlers/BBHTTPSelectiveDiscarder.h
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion BBHTTP/Handlers/BBHTTPSelectiveDiscarder.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ - (instancetype)init
{
self = [super init];
if (self != nil) {
_acceptableResponses = @[@200, @201, @202, @203, @204];
_acceptableResponses = @[@200, @201, @202, @203, @204, @206];
_acceptableContentTypes = nil; // accept everything
}

Expand Down
Empty file modified BBHTTP/Handlers/BBHTTPStreamWriter.h
100644 → 100755
Empty file.
Empty file modified BBHTTP/Handlers/BBHTTPStreamWriter.m
100644 → 100755
Empty file.
Empty file modified BBHTTP/Handlers/BBHTTPToStringConverter.h
100644 → 100755
Empty file.
Empty file modified BBHTTP/Handlers/BBHTTPToStringConverter.m
100644 → 100755
Empty file.
Empty file modified BBHTTP/Handlers/BBJSONParser.h
100644 → 100755
Empty file.
Empty file modified BBHTTP/Handlers/BBJSONParser.m
100644 → 100755
Empty file.
Empty file modified BBHTTP/Internal/BBHTTPRequest+PrivateInterface.h
100644 → 100755
Empty file.
54 changes: 32 additions & 22 deletions BBHTTP/Internal/BBHTTPRequest+PrivateInterface.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ - (BOOL)executionStarted
if ([self hasFinished]) return NO;

_startTimestamp = BBHTTPCurrentTimeMillis();
if (self.startBlock != nil) {
dispatch_async(self.callbackQueue, ^{

dispatch_async(self.callbackQueue, ^{

if (self.startBlock != nil)
{
self.startBlock();

self.startBlock = nil;
});
}
}
});

return YES;
}
Expand All @@ -56,16 +58,19 @@ - (BOOL)executionFailedWithFinalResponse:(BBHTTPResponse*)response error:(NSErro
_error = error;
_response = response;

if (self.finishBlock != nil) {
dispatch_async(self.callbackQueue, ^{
dispatch_async(self.callbackQueue, ^{

if (self.finishBlock != nil)
{
self.finishBlock(self);

self.uploadProgressBlock = nil;
self.downloadProgressBlock = nil;
self.finishBlock = nil;
});
}

}

self.uploadProgressBlock = nil;
self.downloadProgressBlock = nil;

});

return YES;
}

Expand All @@ -75,12 +80,15 @@ - (BOOL)uploadProgressedToCurrent:(NSUInteger)current ofTotal:(NSUInteger)total

_sentBytes = current;

if (self.uploadProgressBlock != nil) {
dispatch_async(self.callbackQueue, ^{
dispatch_async(self.callbackQueue, ^{

if (self.uploadProgressBlock != nil)
{
self.uploadProgressBlock(current, total);
});
}
}

});

return YES;
}

Expand All @@ -89,12 +97,14 @@ - (BOOL)downloadProgressedToCurrent:(NSUInteger)current ofTotal:(NSUInteger)tota
if ([self hasFinished]) return NO;

_receivedBytes = current;

if (self.downloadProgressBlock != nil) {
dispatch_async(self.callbackQueue, ^{

dispatch_async(self.callbackQueue, ^{

if (self.downloadProgressBlock != nil)
{
self.downloadProgressBlock(current, total);
});
}
}
});

return YES;
}
Expand Down
1 change: 1 addition & 0 deletions BBHTTP/Internal/BBHTTPRequestContext.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,6 @@ typedef NS_ENUM(NSUInteger, BBHTTPResponseState) {
@property(assign, nonatomic, readonly) BBHTTPResponseState state;

- (BOOL)isCurrentResponse100Continue;
- (BOOL)isCurrentResponse30XRedirect;

@end
15 changes: 14 additions & 1 deletion BBHTTP/Internal/BBHTTPRequestContext.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ - (BOOL)finishCurrentResponse
nextState = BBHTTPResponseStateReadingStatusLine; // ... unless it's a 100-Continue; if so, go back to the start
// TODO I'm assuming 100-Continue's never have data...
_uploadAccepted = YES;
} else if ([self isCurrentResponse30XRedirect]) {
// reset header and decrease redirect count
//_request.maxRedirects--;
// 301, 302 continue readStatusline
nextState = BBHTTPResponseStateReadingStatusLine;
_uploadAccepted = YES;

} else if (!_discardBodyForCurrentResponse) {
NSError* error = nil;
parsedContent = [_request.responseContentHandler parseContent:&error];
Expand Down Expand Up @@ -295,7 +302,6 @@ - (BOOL)appendDataToCurrentResponse:(uint8_t*)bytes withLength:(NSUInteger)lengt
return NO;
}


#pragma mark Querying context information

- (BBHTTPResponse*)lastResponse
Expand All @@ -310,6 +316,13 @@ - (BOOL)isCurrentResponse100Continue
return _currentResponse.code == 100;
}

- (BOOL)isCurrentResponse30XRedirect
{
if (_currentResponse == nil) return NO;

return _currentResponse.code == 301 || _currentResponse.code == 302;
}


#pragma mark Private helpers

Expand Down
6 changes: 3 additions & 3 deletions BBHTTP/Internal/BBHTTPUtils.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@ extern void BBHTTPLog(NSUInteger level, NSString* prefix, NSString* (^statement)
#pragma mark - DRY macros

#define BBHTTPErrorWithFormat(c, fmt, ...) \
[NSError errorWithDomain:@"com.biasedbit.http" code:c \
[NSError errorWithDomain:@"BBHTTP" code:c \
userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:fmt, ##__VA_ARGS__]}]

// We need this variant because passing a non-statically initialized NSString* instance as fmt raises a warning
#define BBHTTPError(c, description) \
[NSError errorWithDomain:@"com.biasedbit.http" code:c \
[NSError errorWithDomain:@"BBHTTP" code:c \
userInfo:@{NSLocalizedDescriptionKey: description}]

#define BBHTTPErrorWithReason(c, description, reason) \
[NSError errorWithDomain:@"com.biasedbit.http" code:c \
[NSError errorWithDomain:@"BBHTTP" code:c \
userInfo:@{NSLocalizedDescriptionKey: description, NSLocalizedFailureReasonErrorKey: reason}]

#define BBHTTPEnsureNotNil(value) NSAssert((value) != nil, @"%s cannot be nil", #value)
Expand Down
Empty file modified BBHTTP/Internal/BBHTTPUtils.m
100644 → 100755
Empty file.
Empty file modified BBHTTP/Internal/BBJSONDictionary.h
100644 → 100755
Empty file.
Empty file modified BBHTTP/Internal/BBJSONDictionary.m
100644 → 100755
Empty file.
Loading