@@ -26,10 +26,11 @@ #import "OFHTTPRequest.h" #import "OFHTTPResponse.h" #import "OFKernelEventObserver.h" #import "OFNumber.h" #import "OFRunLoop.h" +#import "OFSocket+Private.h" #import "OFString.h" #import "OFTCPSocket.h" #import "OFURL.h" #import "OFAlreadyConnectedException.h" @@ -45,13 +46,11 @@ #import "OFTruncatedDataException.h" #import "OFUnsupportedProtocolException.h" #import "OFUnsupportedVersionException.h" #import "OFWriteFailedException.h" -#import "socket_helpers.h" - -#define REDIRECTS_DEFAULT 10 +static const unsigned int defaultRedirects = 10; OF_DIRECT_MEMBERS @interface OFHTTPClientRequestHandler: OFObject { @public @@ -114,11 +113,11 @@ static OFString * constructRequestString(OFHTTPRequest *request) { void *pool = objc_autoreleasePoolPush(); - of_http_request_method_t method = request.method; + OFHTTPRequestMethod method = request.method; OFURL *URL = request.URL; OFString *path; OFString *user = URL.user, *password = URL.password; OFMutableString *requestString; OFMutableDictionary OF_GENERIC(OFString *, OFString *) *headers; @@ -130,11 +129,11 @@ path = URL.URLEncodedPath; else path = @"/"; requestString = [OFMutableString stringWithFormat: - @"%s %@", of_http_request_method_to_string(method), path]; + @"%s %@", OFHTTPRequestMethodName(method), path]; if (URL.query != nil) { [requestString appendString: @"?"]; [requestString appendString: URL.URLEncodedQuery]; } @@ -217,37 +216,36 @@ { unsigned char *str = (unsigned char *)str_; bool firstLetter = true; while (*str != '\0') { - if (!of_ascii_isalpha(*str)) { + if (!OFASCIIIsAlpha(*str)) { firstLetter = true; str++; continue; } *str = (firstLetter - ? of_ascii_toupper(*str) - : of_ascii_tolower(*str)); + ? OFASCIIToUpper(*str) : OFASCIIToLower(*str)); firstLetter = false; str++; } } static bool -defaultShouldFollow(of_http_request_method_t method, short statusCode) +defaultShouldFollow(OFHTTPRequestMethod method, short statusCode) { bool follow; /* * 301, 302 and 307 should only redirect with user confirmation if the * request method is not GET or HEAD. Asking the delegate and getting * true returned is considered user confirmation. */ - if (method == OF_HTTP_REQUEST_METHOD_GET || - method == OF_HTTP_REQUEST_METHOD_HEAD) + if (method == OFHTTPRequestMethodGet || + method == OFHTTPRequestMethodHead) follow = true; /* 303 should always be redirected and converted to a GET request. */ else if (statusCode == 303) follow = true; else @@ -319,11 +317,11 @@ else keepAlive = true; } else { if (connectionHeader != nil) keepAlive = ([connectionHeader caseInsensitiveCompare: - @"keep-alive"] == OF_ORDERED_SAME); + @"keep-alive"] == OFOrderedSame); else keepAlive = false; } if (keepAlive) { @@ -330,11 +328,11 @@ response.of_keepAlive = true; _client->_socket = [sock retain]; _client->_lastURL = [URL copy]; _client->_lastWasHEAD = - (_request.method == OF_HTTP_REQUEST_METHOD_HEAD); + (_request.method == OFHTTPRequestMethodHead); _client->_lastResponse = [response retain]; } if (_redirects > 0 && (_status == 301 || _status == 302 || _status == 303 || _status == 307) && @@ -346,20 +344,20 @@ newURL = [OFURL URLWithString: location relativeToURL: URL]; newURLScheme = newURL.scheme; if ([newURLScheme caseInsensitiveCompare: @"http"] != - OF_ORDERED_SAME && + OFOrderedSame && [newURLScheme caseInsensitiveCompare: @"https"] != - OF_ORDERED_SAME) + OFOrderedSame) follow = false; if (!_client->_allowsInsecureRedirects && [URL.scheme caseInsensitiveCompare: @"https"] == - OF_ORDERED_SAME && + OFOrderedSame && [newURLScheme caseInsensitiveCompare: @"http"] == - OF_ORDERED_SAME) + OFOrderedSame) follow = false; if (follow && [_client->_delegate respondsToSelector: @selector( client:shouldFollowRedirect:statusCode:request:response:)]) follow = [_client->_delegate client: _client @@ -399,11 +397,11 @@ if ([key hasPrefix: @"Content-"] || [key hasPrefix: @"Transfer-"]) [newHeaders removeObjectForKey: key]; - newRequest.method = OF_HTTP_REQUEST_METHOD_GET; + newRequest.method = OFHTTPRequestMethodGet; } newRequest.URL = newURL; newRequest.headers = newHeaders; @@ -458,27 +456,26 @@ if (![line hasPrefix: @"HTTP/"] || line.length < 9 || [line characterAtIndex: 8] != ' ') @throw [OFInvalidServerReplyException exception]; - _version = [[line substringWithRange: of_range(5, 3)] copy]; + _version = [[line substringWithRange: OFRangeMake(5, 3)] copy]; if (![_version isEqual: @"1.0"] && ![_version isEqual: @"1.1"]) @throw [OFUnsupportedVersionException exceptionWithVersion: _version]; - status = [line substringWithRange: of_range(9, 3)].longLongValue; + status = [line substringWithRange: OFRangeMake(9, 3)].longLongValue; if (status < 0 || status > 599) @throw [OFInvalidServerReplyException exception]; _status = (short)status; return true; } -- (bool)handleServerHeader: (OFString *)line - socket: (OFTCPSocket *)sock +- (bool)handleServerHeader: (OFString *)line socket: (OFTCPSocket *)sock { OFString *key, *value, *old; const char *lineC, *tmp; char *keyC; @@ -507,20 +504,20 @@ lineC = line.UTF8String; if ((tmp = strchr(lineC, ':')) == NULL) @throw [OFInvalidServerReplyException exception]; - keyC = of_alloc(tmp - lineC + 1, 1); + keyC = OFAllocMemory(tmp - lineC + 1, 1); memcpy(keyC, lineC, tmp - lineC); keyC[tmp - lineC] = '\0'; normalizeKey(keyC); @try { key = [OFString stringWithUTF8StringNoCopy: keyC freeWhenDone: true]; } @catch (id e) { - free(keyC); + OFFreeMemory(keyC); @throw e; } do { tmp++; @@ -567,11 +564,11 @@ return ret; } - (OFString *)stream: (OFStream *)stream didWriteString: (OFString *)string - encoding: (of_string_encoding_t)encoding + encoding: (OFStringEncoding)encoding bytesWritten: (size_t)bytesWritten exception: (id)exception { OFDictionary OF_GENERIC(OFString *, OFString *) *headers; bool chunked; @@ -699,16 +696,16 @@ OFNumber *URLPort; [_client close]; if ([URL.scheme caseInsensitiveCompare: @"https"] == - OF_ORDERED_SAME) { - if (of_tls_socket_class == Nil) + OFOrderedSame) { + if (OFTLSSocketClass == Nil) @throw [OFUnsupportedProtocolException exceptionWithURL: URL]; - sock = [[[of_tls_socket_class alloc] init] autorelease]; + sock = [[[OFTLSSocketClass alloc] init] autorelease]; port = 443; } else { sock = [OFTCPSocket socket]; port = 80; } @@ -995,19 +992,19 @@ if (line == nil) return 0; pos = [line rangeOfString: @";"].location; - if (pos != OF_NOT_FOUND) + if (pos != OFNotFound) line = [line substringToIndex: pos]; if (line.length < 1) { /* * We have read the empty string because the socket is * at end of stream. */ - if (_socket.atEndOfStream && pos == OF_NOT_FOUND) + if (_socket.atEndOfStream && pos == OFNotFound) @throw [OFTruncatedDataException exception]; else @throw [OFInvalidServerReplyException exception]; } @@ -1206,11 +1203,11 @@ [super dealloc]; } - (OFHTTPResponse *)performRequest: (OFHTTPRequest *)request { - return [self performRequest: request redirects: REDIRECTS_DEFAULT]; + return [self performRequest: request redirects: defaultRedirects]; } - (OFHTTPResponse *)performRequest: (OFHTTPRequest *)request redirects: (unsigned int)redirects { @@ -1228,22 +1225,22 @@ return [response autorelease]; } - (void)asyncPerformRequest: (OFHTTPRequest *)request { - [self asyncPerformRequest: request redirects: REDIRECTS_DEFAULT]; + [self asyncPerformRequest: request redirects: defaultRedirects]; } - (void)asyncPerformRequest: (OFHTTPRequest *)request redirects: (unsigned int)redirects { void *pool = objc_autoreleasePoolPush(); OFURL *URL = request.URL; OFString *scheme = URL.scheme; - if ([scheme caseInsensitiveCompare: @"http"] != OF_ORDERED_SAME && - [scheme caseInsensitiveCompare: @"https"] != OF_ORDERED_SAME) + if ([scheme caseInsensitiveCompare: @"http"] != OFOrderedSame && + [scheme caseInsensitiveCompare: @"https"] != OFOrderedSame) @throw [OFUnsupportedProtocolException exceptionWithURL: URL]; if (_inProgress) /* TODO: Find a better exception */ @throw [OFAlreadyConnectedException exception];