@@ -13,10 +13,12 @@ * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" + +#include #import "OFHTTPRequest.h" #import "OFString.h" #import "OFURL.h" #import "OFDictionary.h" @@ -27,10 +29,58 @@ #import "macros.h" #import "OFInvalidFormatException.h" #import "OFOutOfRangeException.h" #import "OFUnsupportedVersionException.h" + +const char* +of_http_request_method_to_string(of_http_request_method_t method) +{ + switch (method) { + case OF_HTTP_REQUEST_METHOD_OPTIONS: + return "OPTIONS"; + case OF_HTTP_REQUEST_METHOD_GET: + return "GET"; + case OF_HTTP_REQUEST_METHOD_HEAD: + return "HEAD"; + case OF_HTTP_REQUEST_METHOD_POST: + return "POST"; + case OF_HTTP_REQUEST_METHOD_PUT: + return "PUT"; + case OF_HTTP_REQUEST_METHOD_DELETE: + return "DELETE"; + case OF_HTTP_REQUEST_METHOD_TRACE: + return "TRACE"; + case OF_HTTP_REQUEST_METHOD_CONNECT: + return "CONNECT"; + } + + return NULL; +} + +of_http_request_method_t +of_http_request_method_from_string(const char *string) +{ + if (!strcmp(string, "OPTIONS")) + return OF_HTTP_REQUEST_METHOD_OPTIONS; + if (!strcmp(string, "GET")) + return OF_HTTP_REQUEST_METHOD_GET; + if (!strcmp(string, "HEAD")) + return OF_HTTP_REQUEST_METHOD_HEAD; + if (!strcmp(string, "POST")) + return OF_HTTP_REQUEST_METHOD_POST; + if (!strcmp(string, "PUT")) + return OF_HTTP_REQUEST_METHOD_PUT; + if (!strcmp(string, "DELETE")) + return OF_HTTP_REQUEST_METHOD_DELETE; + if (!strcmp(string, "TRACE")) + return OF_HTTP_REQUEST_METHOD_TRACE; + if (!strcmp(string, "CONNECT")) + return OF_HTTP_REQUEST_METHOD_CONNECT; + + @throw [OFInvalidFormatException exception]; +} @implementation OFHTTPRequest + (instancetype)request { return [[[self alloc] init] autorelease]; @@ -43,11 +93,11 @@ - init { self = [super init]; - _requestType = OF_HTTP_REQUEST_TYPE_GET; + _method = OF_HTTP_REQUEST_METHOD_GET; _protocolVersion.major = 1; _protocolVersion.minor = 1; return self; } @@ -80,11 +130,11 @@ - copy { OFHTTPRequest *copy = [[OFHTTPRequest alloc] init]; @try { - copy->_requestType = _requestType; + copy->_method = _method; copy->_protocolVersion = _protocolVersion; [copy setURL: _URL]; [copy setHeaders: _headers]; [copy setPOSTData: _POSTData]; [copy setMIMEType: _MIMEType]; @@ -104,11 +154,11 @@ if (![object isKindOfClass: [OFHTTPRequest class]]) return false; request = object; - if (request->_requestType != _requestType || + if (request->_method != _method || request->_protocolVersion.major != _protocolVersion.major || request->_protocolVersion.minor != _protocolVersion.minor || ![request->_URL isEqual: _URL] || ![request->_headers isEqual: _headers] || ![request->_POSTData isEqual: _POSTData] || @@ -123,11 +173,11 @@ { uint32_t hash; OF_HASH_INIT(hash); - OF_HASH_ADD(hash, _requestType); + OF_HASH_ADD(hash, _method); OF_HASH_ADD(hash, _protocolVersion.major); OF_HASH_ADD(hash, _protocolVersion.minor); OF_HASH_ADD_HASH(hash, [_URL hash]); OF_HASH_ADD_HASH(hash, [_headers hash]); OF_HASH_ADD_HASH(hash, [_POSTData hash]); @@ -147,18 +197,18 @@ - (OFURL*)URL { OF_GETTER(_URL, true) } -- (void)setRequestType: (of_http_request_type_t)requestType +- (void)setMethod: (of_http_request_method_t)method { - _requestType = requestType; + _method = method; } -- (of_http_request_type_t)requestType +- (of_http_request_method_t)method { - return _requestType; + return _method; } - (void)setProtocolVersion: (of_http_request_protocol_version_t)protocolVersion { if (protocolVersion.major != 1 || protocolVersion.minor > 1) @@ -246,43 +296,31 @@ } - (OFString*)description { void *pool = objc_autoreleasePoolPush(); - const char *requestTypeStr = NULL; + const char *method = of_http_request_method_to_string(_method); OFString *indentedHeaders, *indentedPOSTData, *ret; - switch (_requestType) { - case OF_HTTP_REQUEST_TYPE_GET: - requestTypeStr = "GET"; - break; - case OF_HTTP_REQUEST_TYPE_POST: - requestTypeStr = "POST"; - break; - case OF_HTTP_REQUEST_TYPE_HEAD: - requestTypeStr = "HEAD"; - break; - } - indentedHeaders = [[_headers description] stringByReplacingOccurrencesOfString: @"\n" withString: @"\n\t"]; indentedPOSTData = [[_POSTData description] stringByReplacingOccurrencesOfString: @"\n" withString: @"\n\t"]; ret = [[OFString alloc] initWithFormat: @"<%@:\n\tURL = %@\n" - @"\tRequest type = %s\n" + @"\tMethod = %s\n" @"\tHeaders = %@\n" @"\tPOST data = %@\n" @"\tPOST data MIME type = %@\n" @"\tRemote address = %@\n" @">", - [self class], _URL, requestTypeStr, indentedHeaders, - indentedPOSTData, _MIMEType, _remoteAddress]; + [self class], _URL, method, indentedHeaders, indentedPOSTData, + _MIMEType, _remoteAddress]; objc_autoreleasePoolPop(pool); return [ret autorelease]; } @end