282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
-
+
|
- (OFHTTPResponse*)performRequest: (OFHTTPRequest*)request
{
return [self performRequest: request
redirects: 10];
}
- (OFTCPSocket*)OF_createSocketForRequest: (OFHTTPRequest*)request
- (OFTCPSocket*)OF_closeAndCreateSocketForRequest: (OFHTTPRequest*)request
{
OFURL *URL = [request URL];
OFTCPSocket *socket;
[self close];
if ([[URL scheme] isEqual: @"https"]) {
|
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
-
+
|
OFString *key, *object;
int status;
if (![scheme isEqual: @"http"] && ![scheme isEqual: @"https"])
@throw [OFUnsupportedProtocolException exceptionWithURL: URL];
/* Can we reuse the socket? */
if (_socket != nil && [[_lastURL scheme] isEqual: [URL scheme]] &&
if (_socket != nil && [[_lastURL scheme] isEqual: scheme] &&
[[_lastURL host] isEqual: [URL host]] &&
[_lastURL port] == [URL port]) {
/*
* Set _socket to nil, so that in case of an error it won't be
* reused. If everything is successfull, we set _socket again
* at the end.
*/
|
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
-
+
+
-
-
+
+
+
-
+
-
|
[_lastResponse readIntoBuffer: buffer
length: 512];
}
[_lastResponse release];
_lastResponse = nil;
} else
socket = [self OF_createSocketForRequest: request];
socket = [self OF_closeAndCreateSocketForRequest: request];
/*
* As a work around for a bug with split packets in lighttpd when using
* HTTPS, we construct the complete request in a buffer string and then
* send it all at once.
*/
if ([URL query] != nil)
requestString = [OFMutableString stringWithFormat:
@"%s /%@?%@ HTTP/%@\r\n",
of_http_request_method_to_string(method), [URL path],
[URL query], [request protocolVersionString]];
else
requestString = [OFMutableString stringWithFormat:
@"%s /%@ HTTP/%@\r\n",
of_http_request_method_to_string(method), [URL path],
[request protocolVersionString]];
if (([scheme isEqual: @"http"] && [URL port] != 80) ||
if ([URL port] == 80)
[requestString appendFormat: @"Host: %@\r\n", [URL host]];
([scheme isEqual: @"https"] && [URL port] != 443))
[requestString appendFormat: @"Host: %@:%d\r\n",
[URL host], [URL port]];
else
[requestString appendFormat: @"Host: %@:%d\r\n", [URL host],
[requestString appendFormat: @"Host: %@\r\n", [URL host]];
[URL port]];
user = [URL user];
password = [URL password];
if ([user length] > 0 || [password length] > 0) {
OFDataArray *authorization = [OFDataArray dataArray];
|
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
465
466
467
468
469
470
471
|
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
465
466
467
468
469
470
471
472
|
-
+
-
+
|
@try {
[socket writeString: requestString];
} @catch (OFWriteFailedException *e) {
if ([e errNo] != ECONNRESET && [e errNo] != EPIPE)
@throw e;
/* Reconnect in case a keep-alive connection timed out */
socket = [self OF_createSocketForRequest: request];
socket = [self OF_closeAndCreateSocketForRequest: request];
[socket writeString: requestString];
}
if (entity != nil)
[socket writeBuffer: [entity items]
length: [entity count] * [entity itemSize]];
@try {
line = [socket readLine];
} @catch (OFInvalidEncodingException *e) {
@throw [OFInvalidServerReplyException exception];
}
/*
* It's possible that the write succeeds on a connection that is
* keep-alive, but the connection has already been closed by the remote
* end due to a timeout. In this case, we need to reconnect.
*/
if (line == nil) {
socket = [self OF_createSocketForRequest: request];
socket = [self OF_closeAndCreateSocketForRequest: request];
[socket writeString: requestString];
if (entity != nil)
[socket writeBuffer: [entity items]
length: [entity count] *
[entity itemSize]];
|