@@ -19,14 +19,19 @@ #import "OFHTTPRequest.h" #import "OFString.h" #import "OFURL.h" #import "OFDictionary.h" #import "OFDataArray.h" +#import "OFArray.h" #import "autorelease.h" #import "macros.h" +#import "OFInvalidFormatException.h" +#import "OFOutOfRangeException.h" +#import "OFUnsupportedVersionException.h" + @implementation OFHTTPRequest + (instancetype)request { return [[[self alloc] init] autorelease]; } @@ -39,10 +44,12 @@ - init { self = [super init]; requestType = OF_HTTP_REQUEST_TYPE_GET; + protocolVersion.major = 1; + protocolVersion.minor = 1; return self; } - initWithURL: (OFURL*)URL_ @@ -87,10 +94,58 @@ - (of_http_request_type_t)requestType { return requestType; } + +- (void)setProtocolVersion: (of_http_request_protocol_version_t)protocolVersion_ +{ + if (protocolVersion_.major != 1 || protocolVersion.minor > 1) + @throw [OFUnsupportedVersionException + exceptionWithClass: [self class] + version: [OFString stringWithFormat: @"%u.%u", + protocolVersion_.major, + protocolVersion_.minor]]; + + protocolVersion = protocolVersion_; +} + +- (of_http_request_protocol_version_t)protocolVersion +{ + return protocolVersion; +} + +- (void)setProtocolVersionFromString: (OFString*)string +{ + void *pool = objc_autoreleasePoolPush(); + OFArray *components = [string componentsSeparatedByString: @"."]; + intmax_t major, minor; + of_http_request_protocol_version_t protocolVersion_; + + if ([components count] != 2) + @throw [OFInvalidFormatException + exceptionWithClass: [self class]]; + + major = [[components firstObject] decimalValue]; + minor = [[components lastObject] decimalValue]; + + if (major < 0 || major > UINT8_MAX || minor < 0 || minor > UINT8_MAX) + @throw [OFOutOfRangeException exceptionWithClass: [self class]]; + + protocolVersion_.major = (uint8_t)major; + protocolVersion_.minor = (uint8_t)minor; + + [self setProtocolVersion: protocolVersion_]; + + objc_autoreleasePoolPop(pool); +} + +- (OFString*)protocolVersionString +{ + return [OFString stringWithFormat: @"%u.%u", protocolVersion.major, + protocolVersion.minor]; +} - (void)setHeaders: (OFDictionary*)headers_ { OF_SETTER(headers, headers_, YES, 1) }