Differences From Artifact [07b706d2dd]:
- File src/OFHTTPClient.m — part of check-in [ea4a43461c] at 2013-01-23 21:52:54 on branch trunk — Never compare to @"", use length instead. (user: js, size: 12269) [annotate] [blame] [check-ins using]
To Artifact [9a386c8c54]:
- File
src/OFHTTPClient.m
— part of check-in
[bdf9c4d96b]
at
2013-01-26 00:13:56
on branch trunk
— Make OFHTTPRequestReply a stream.
This also implements chunked transfer encoding for OFHTTPServer and adds
a property for the protocol version to OFHTTPRequest(Reply).Additionally, this makes it possible to add an async version of
OFHTTPClient. (user: js, size: 10827) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
23 24 25 26 27 28 29 | #import "OFHTTPRequest.h" #import "OFHTTPRequestReply.h" #import "OFString.h" #import "OFURL.h" #import "OFTCPSocket.h" #import "OFDictionary.h" #import "OFDataArray.h" | < | 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #import "OFHTTPRequest.h" #import "OFHTTPRequestReply.h" #import "OFString.h" #import "OFURL.h" #import "OFTCPSocket.h" #import "OFDictionary.h" #import "OFDataArray.h" #import "OFHTTPRequestFailedException.h" #import "OFInvalidEncodingException.h" #import "OFInvalidFormatException.h" #import "OFInvalidServerReplyException.h" #import "OFOutOfMemoryException.h" #import "OFOutOfRangeException.h" |
︙ | ︙ | |||
57 58 59 60 61 62 63 64 65 66 67 68 69 70 | *str = (firstLetter ? toupper(*str) : tolower(*str)); firstLetter = NO; str++; } } @implementation OFHTTPClient + (instancetype)client { return [[[self alloc] init] autorelease]; } | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > < < < < < < < < < < < < < < < < < < < | < | < < < < < | 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | *str = (firstLetter ? toupper(*str) : tolower(*str)); firstLetter = NO; str++; } } @interface OFHTTPClientReply: OFHTTPRequestReply { OFTCPSocket *sock; BOOL chunked, atEndOfStream; size_t toRead; } - initWithSocket: (OFTCPSocket*)sock; @end @implementation OFHTTPClientReply - initWithSocket: (OFTCPSocket*)sock_ { self = [super init]; sock = [sock_ retain]; return self; } - (void)dealloc { [sock release]; [super dealloc]; } - (void)setHeaders: (OFDictionary*)headers_ { [super setHeaders: headers_]; chunked = [[headers_ objectForKey: @"Transfer-Encoding"] isEqual: @"chunked"]; } - (size_t)lowlevelReadIntoBuffer: (void*)buffer length: (size_t)length { if (!chunked) return [sock readIntoBuffer: buffer length: length]; if (toRead > 0) { if (length > toRead) length = toRead; length = [sock readIntoBuffer: buffer length: length]; toRead -= length; if (toRead == 0) if ([[sock readLine] length] > 0) @throw [OFInvalidServerReplyException exceptionWithClass: [self class]]; return length; } else { void *pool = objc_autoreleasePoolPush(); OFString *line; of_range_t range; @try { line = [sock readLine]; } @catch (OFInvalidEncodingException *e) { @throw [OFInvalidServerReplyException exceptionWithClass: [self class]]; } range = [line rangeOfString: @";"]; if (range.location != OF_NOT_FOUND) line = [line substringWithRange: of_range(0, range.location)]; @try { toRead = (size_t)[line hexadecimalValue]; } @catch (OFInvalidFormatException *e) { @throw [OFInvalidServerReplyException exceptionWithClass: [self class]]; } if (toRead == 0) { [sock close]; atEndOfStream = YES; } objc_autoreleasePoolPop(pool); return 0; } } - (BOOL)lowlevelIsAtEndOfStream { if (!chunked) return [sock isAtEndOfStream]; return atEndOfStream; } - (int)fileDescriptorForReading { return [sock fileDescriptorForReading]; } - (size_t)pendingBytes { return [super pendingBytes] + [sock pendingBytes]; } - (void)close { [sock close]; } @end @implementation OFHTTPClient + (instancetype)client { return [[[self alloc] init] autorelease]; } - (void)setDelegate: (id <OFHTTPClientDelegate>)delegate_ { delegate = delegate_; } - (id <OFHTTPClientDelegate>)delegate { return delegate; } - (void)setInsecureRedirectsAllowed: (BOOL)allowed { insecureRedirectsAllowed = allowed; } - (BOOL)insecureRedirectsAllowed { return insecureRedirectsAllowed; } - (OFHTTPRequestReply*)performRequest: (OFHTTPRequest*)request { return [self performRequest: request redirects: 10]; } - (OFHTTPRequestReply*)performRequest: (OFHTTPRequest*)request redirects: (size_t)redirects { void *pool = objc_autoreleasePoolPush(); OFURL *URL = [request URL]; OFString *scheme = [URL scheme]; of_http_request_type_t requestType = [request requestType]; OFDictionary *headers = [request headers]; OFDataArray *POSTData = [request POSTData]; OFTCPSocket *sock; OFHTTPClientReply *reply; OFString *line, *path, *version; OFMutableDictionary *serverHeaders; OFEnumerator *keyEnumerator, *objectEnumerator; OFString *key, *object; int status; const char *type = NULL; if (![scheme isEqual: @"http"] && ![scheme isEqual: @"https"]) @throw [OFUnsupportedProtocolException exceptionWithClass: [self class] URL: URL]; if ([scheme isEqual: @"http"]) |
︙ | ︙ | |||
175 176 177 178 179 180 181 | if (requestType == OF_HTTP_REQUEST_TYPE_POST) type = "POST"; if ([(path = [URL path]) length] == 0) path = @"/"; if ([URL query] != nil) | | | | > | 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | if (requestType == OF_HTTP_REQUEST_TYPE_POST) type = "POST"; if ([(path = [URL path]) length] == 0) path = @"/"; if ([URL query] != nil) [sock writeFormat: @"%s %@?%@ HTTP/%@\r\n", type, path, [URL query], [request protocolVersionString]]; else [sock writeFormat: @"%s %@ HTTP/%@\r\n", type, path, [request protocolVersionString]]; if ([URL port] == 80) [sock writeFormat: @"Host: %@\r\n", [URL host]]; else [sock writeFormat: @"Host: %@:%d\r\n", [URL host], [URL port]]; |
︙ | ︙ | |||
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | return [self performRequest: newRequest redirects: redirects - 1]; } [serverHeaders setObject: value forKey: key]; } if ([delegate respondsToSelector: @selector(client:didReceiveHeaders:statusCode:request:)]) [delegate client: self didReceiveHeaders: serverHeaders statusCode: status request: request]; | > > < < < | < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | < | | 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 | return [self performRequest: newRequest redirects: redirects - 1]; } [serverHeaders setObject: value forKey: key]; } [serverHeaders makeImmutable]; if ([delegate respondsToSelector: @selector(client:didReceiveHeaders:statusCode:request:)]) [delegate client: self didReceiveHeaders: serverHeaders statusCode: status request: request]; reply = [[OFHTTPClientReply alloc] initWithSocket: sock]; [reply setProtocolVersionFromString: version]; [reply setStatusCode: status]; [reply setHeaders: serverHeaders]; objc_autoreleasePoolPop(pool); [reply autorelease]; if (status / 100 != 2) @throw [OFHTTPRequestFailedException exceptionWithClass: [self class] request: request reply: reply]; return reply; } @end |