︙ | | | ︙ | |
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
*
* Alternatively, it may be distributed under the terms of the GNU General
* Public License, either version 2 or 3, which can be found in the file
* LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
* file.
*/
#define OF_HTTPCLIENT_M
#include "config.h"
#include <errno.h>
#include <string.h>
#import "OFHTTPClient.h"
#import "OFData.h"
#import "OFDictionary.h"
#import "OFHTTPRequest.h"
#import "OFHTTPResponse.h"
#import "OFKernelEventObserver.h"
#import "OFNumber.h"
#import "OFRunLoop.h"
#import "OFString.h"
#import "OFTCPSocket.h"
#import "OFTLSStream.h"
#import "OFURL.h"
#import "OFAlreadyConnectedException.h"
#import "OFHTTPRequestFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidEncodingException.h"
#import "OFInvalidFormatException.h"
#import "OFInvalidServerReplyException.h"
#import "OFNotImplementedException.h"
#import "OFNotOpenException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"
#import "OFTruncatedDataException.h"
#import "OFUnsupportedProtocolException.h"
#import "OFUnsupportedVersionException.h"
|
|
|
|
|
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
*
* Alternatively, it may be distributed under the terms of the GNU General
* Public License, either version 2 or 3, which can be found in the file
* LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
* file.
*/
#define OF_HTTP_CLIENT_M
#include "config.h"
#include <errno.h>
#include <string.h>
#import "OFHTTPClient.h"
#import "OFData.h"
#import "OFDictionary.h"
#import "OFHTTPRequest.h"
#import "OFHTTPResponse.h"
#import "OFKernelEventObserver.h"
#import "OFNumber.h"
#import "OFRunLoop.h"
#import "OFString.h"
#import "OFTCPSocket.h"
#import "OFTLSStream.h"
#import "OFURI.h"
#import "OFAlreadyConnectedException.h"
#import "OFHTTPRequestFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidEncodingException.h"
#import "OFInvalidFormatException.h"
#import "OFInvalidServerResponseException.h"
#import "OFNotImplementedException.h"
#import "OFNotOpenException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"
#import "OFTruncatedDataException.h"
#import "OFUnsupportedProtocolException.h"
#import "OFUnsupportedVersionException.h"
|
︙ | | | ︙ | |
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
|
@end
static OFString *
constructRequestString(OFHTTPRequest *request)
{
void *pool = objc_autoreleasePoolPush();
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;
bool hasContentLength, chunked;
OFEnumerator OF_GENERIC(OFString *) *keyEnumerator, *objectEnumerator;
OFString *key, *object;
if (URL.path != nil)
path = URL.URLEncodedPath;
else
path = @"/";
requestString = [OFMutableString stringWithFormat:
@"%s %@", OFHTTPRequestMethodName(method), path];
if (URL.query != nil) {
[requestString appendString: @"?"];
[requestString appendString: URL.URLEncodedQuery];
}
[requestString appendString: @" HTTP/"];
[requestString appendString: request.protocolVersionString];
[requestString appendString: @"\r\n"];
headers = [[request.headers mutableCopy] autorelease];
if (headers == nil)
headers = [OFMutableDictionary dictionary];
if ([headers objectForKey: @"Host"] == nil) {
OFNumber *port = URL.port;
if (port != nil) {
OFString *host = [OFString stringWithFormat:
@"%@:%@", URL.URLEncodedHost, port];
[headers setObject: host forKey: @"Host"];
} else
[headers setObject: URL.URLEncodedHost forKey: @"Host"];
}
if ((user.length > 0 || password.length > 0) &&
[headers objectForKey: @"Authorization"] == nil) {
OFMutableData *authorizationData = [OFMutableData data];
OFString *authorization;
|
|
|
|
|
|
|
|
|
|
>
|
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
|
@end
static OFString *
constructRequestString(OFHTTPRequest *request)
{
void *pool = objc_autoreleasePoolPush();
OFHTTPRequestMethod method = request.method;
OFURI *URI = request.URI;
OFString *path;
OFString *user = URI.user, *password = URI.password;
OFMutableString *requestString;
OFMutableDictionary OF_GENERIC(OFString *, OFString *) *headers;
bool hasContentLength, chunked;
OFEnumerator OF_GENERIC(OFString *) *keyEnumerator, *objectEnumerator;
OFString *key, *object;
if (URI.path.length > 0)
path = URI.percentEncodedPath;
else
path = @"/";
requestString = [OFMutableString stringWithFormat:
@"%s %@", OFHTTPRequestMethodName(method), path];
if (URI.query != nil) {
[requestString appendString: @"?"];
[requestString appendString: URI.percentEncodedQuery];
}
[requestString appendString: @" HTTP/"];
[requestString appendString: request.protocolVersionString];
[requestString appendString: @"\r\n"];
headers = [[request.headers mutableCopy] autorelease];
if (headers == nil)
headers = [OFMutableDictionary dictionary];
if ([headers objectForKey: @"Host"] == nil) {
OFNumber *port = URI.port;
if (port != nil) {
OFString *host = [OFString stringWithFormat:
@"%@:%@", URI.percentEncodedHost, port];
[headers setObject: host forKey: @"Host"];
} else
[headers setObject: URI.percentEncodedHost
forKey: @"Host"];
}
if ((user.length > 0 || password.length > 0) &&
[headers objectForKey: @"Authorization"] == nil) {
OFMutableData *authorizationData = [OFMutableData data];
OFString *authorization;
|
︙ | | | ︙ | |
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
didPerformRequest: _request
response: nil
exception: exception];
}
- (void)createResponseWithStreamOrThrow: (OFStream *)stream
{
OFURL *URL = _request.URL;
OFHTTPClientResponse *response;
OFString *connectionHeader;
bool keepAlive;
OFString *location;
id exception;
response = [[[OFHTTPClientResponse alloc] initWithStream: stream]
|
|
|
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
didPerformRequest: _request
response: nil
exception: exception];
}
- (void)createResponseWithStreamOrThrow: (OFStream *)stream
{
OFURI *URI = _request.URI;
OFHTTPClientResponse *response;
OFString *connectionHeader;
bool keepAlive;
OFString *location;
id exception;
response = [[[OFHTTPClientResponse alloc] initWithStream: stream]
|
︙ | | | ︙ | |
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
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
|
keepAlive = false;
}
if (keepAlive) {
response.of_keepAlive = true;
_client->_stream = [stream retain];
_client->_lastURL = [URL copy];
_client->_lastWasHEAD =
(_request.method == OFHTTPRequestMethodHead);
_client->_lastResponse = [response retain];
}
if (_redirects > 0 && (_status == 301 || _status == 302 ||
_status == 303 || _status == 307) &&
(location = [_serverHeaders objectForKey: @"Location"]) != nil) {
bool follow = true;
OFURL *newURL;
OFString *newURLScheme;
newURL = [OFURL URLWithString: location
relativeToURL: URL];
newURLScheme = newURL.scheme;
if ([newURLScheme caseInsensitiveCompare: @"http"] !=
OFOrderedSame &&
[newURLScheme caseInsensitiveCompare: @"https"] !=
OFOrderedSame)
follow = false;
if (!_client->_allowsInsecureRedirects &&
[URL.scheme caseInsensitiveCompare: @"https"] ==
OFOrderedSame &&
[newURLScheme caseInsensitiveCompare: @"http"] ==
OFOrderedSame)
follow = false;
if (follow && [_client->_delegate respondsToSelector: @selector(
client:shouldFollowRedirect:statusCode:request:response:)])
follow = [_client->_delegate client: _client
shouldFollowRedirect: newURL
statusCode: _status
request: _request
response: response];
else if (follow)
follow = defaultShouldFollow(_request.method, _status);
if (follow) {
OFDictionary OF_GENERIC(OFString *, OFString *)
*headers = _request.headers;
OFHTTPRequest *newRequest =
[[_request copy] autorelease];
OFMutableDictionary *newHeaders =
[[headers mutableCopy] autorelease];
if (![newURL.host isEqual: URL.host])
[newHeaders removeObjectForKey: @"Host"];
/*
* 303 means the request should be converted to a GET
* request before redirection. This also means stripping
* the entity of the request.
*/
|
|
|
|
|
<
|
|
|
|
|
|
>
|
|
|
|
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
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
|
keepAlive = false;
}
if (keepAlive) {
response.of_keepAlive = true;
_client->_stream = [stream retain];
_client->_lastURI = [URI copy];
_client->_lastWasHEAD =
(_request.method == OFHTTPRequestMethodHead);
_client->_lastResponse = [response retain];
}
if (_redirects > 0 && (_status == 301 || _status == 302 ||
_status == 303 || _status == 307) &&
(location = [_serverHeaders objectForKey: @"Location"]) != nil) {
bool follow = true;
OFURI *newURI;
OFString *newURIScheme;
newURI = [OFURI URIWithString: location relativeToURI: URI];
newURIScheme = newURI.scheme;
if ([newURIScheme caseInsensitiveCompare: @"http"] !=
OFOrderedSame &&
[newURIScheme caseInsensitiveCompare: @"https"] !=
OFOrderedSame)
follow = false;
if (!_client->_allowsInsecureRedirects &&
[URI.scheme caseInsensitiveCompare: @"https"] ==
OFOrderedSame &&
[newURIScheme caseInsensitiveCompare: @"http"] ==
OFOrderedSame)
follow = false;
if (follow && [_client->_delegate respondsToSelector:
@selector(client:shouldFollowRedirectToURI:statusCode:
request:response:)])
follow = [_client->_delegate client: _client
shouldFollowRedirectToURI: newURI
statusCode: _status
request: _request
response: response];
else if (follow)
follow = defaultShouldFollow(_request.method, _status);
if (follow) {
OFDictionary OF_GENERIC(OFString *, OFString *)
*headers = _request.headers;
OFHTTPRequest *newRequest =
[[_request copy] autorelease];
OFMutableDictionary *newHeaders =
[[headers mutableCopy] autorelease];
if (![newURI.host isEqual: URI.host])
[newHeaders removeObjectForKey: @"Host"];
/*
* 303 means the request should be converted to a GET
* request before redirection. This also means stripping
* the entity of the request.
*/
|
︙ | | | ︙ | |
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
[key hasPrefix: @"Transfer-"])
[newHeaders
removeObjectForKey: key];
newRequest.method = OFHTTPRequestMethodGet;
}
newRequest.URL = newURL;
newRequest.headers = newHeaders;
_client->_inProgress = false;
[_client asyncPerformRequest: newRequest
redirects: _redirects - 1];
return;
|
|
|
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
[key hasPrefix: @"Transfer-"])
[newHeaders
removeObjectForKey: key];
newRequest.method = OFHTTPRequestMethodGet;
}
newRequest.URI = newURI;
newRequest.headers = newHeaders;
_client->_inProgress = false;
[_client asyncPerformRequest: newRequest
redirects: _redirects - 1];
return;
|
︙ | | | ︙ | |
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
|
if (line == nil) {
[self closeAndReconnect];
return false;
}
if (![line hasPrefix: @"HTTP/"] || line.length < 9 ||
[line characterAtIndex: 8] != ' ')
@throw [OFInvalidServerReplyException exception];
_version = [[line substringWithRange: OFRangeMake(5, 3)] copy];
if (![_version isEqual: @"1.0"] && ![_version isEqual: @"1.1"])
@throw [OFUnsupportedVersionException
exceptionWithVersion: _version];
status = [line substringWithRange: OFRangeMake(9, 3)].longLongValue;
if (status < 0 || status > 599)
@throw [OFInvalidServerReplyException exception];
_status = (short)status;
return true;
}
- (bool)handleServerHeader: (OFString *)line stream: (OFStream *)stream
{
OFString *key, *value, *old;
const char *lineC, *tmp;
char *keyC;
if (line == nil)
@throw [OFInvalidServerReplyException exception];
if (line.length == 0) {
[_serverHeaders makeImmutable];
if ([_client->_delegate respondsToSelector: @selector(client:
didReceiveHeaders:statusCode:request:)])
[_client->_delegate client: _client
|
|
|
|
|
|
|
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
if (line == nil) {
[self closeAndReconnect];
return false;
}
if (![line hasPrefix: @"HTTP/"] || line.length < 9 ||
[line characterAtIndex: 8] != ' ')
@throw [OFInvalidServerResponseException exception];
_version = [[line substringWithRange: OFMakeRange(5, 3)] copy];
if (![_version isEqual: @"1.0"] && ![_version isEqual: @"1.1"])
@throw [OFUnsupportedVersionException
exceptionWithVersion: _version];
status = [line substringWithRange: OFMakeRange(9, 3)].longLongValue;
if (status < 0 || status > 599)
@throw [OFInvalidServerResponseException exception];
_status = (short)status;
return true;
}
- (bool)handleServerHeader: (OFString *)line stream: (OFStream *)stream
{
OFString *key, *value, *old;
const char *lineC, *tmp;
char *keyC;
if (line == nil)
@throw [OFInvalidServerResponseException exception];
if (line.length == 0) {
[_serverHeaders makeImmutable];
if ([_client->_delegate respondsToSelector: @selector(client:
didReceiveHeaders:statusCode:request:)])
[_client->_delegate client: _client
|
︙ | | | ︙ | |
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
|
return false;
}
lineC = line.UTF8String;
if ((tmp = strchr(lineC, ':')) == NULL)
@throw [OFInvalidServerReplyException exception];
keyC = OFAllocMemory(tmp - lineC + 1, 1);
memcpy(keyC, lineC, tmp - lineC);
keyC[tmp - lineC] = '\0';
normalizeKey(keyC);
@try {
|
|
|
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
|
return false;
}
lineC = line.UTF8String;
if ((tmp = strchr(lineC, ':')) == NULL)
@throw [OFInvalidServerResponseException exception];
keyC = OFAllocMemory(tmp - lineC + 1, 1);
memcpy(keyC, lineC, tmp - lineC);
keyC[tmp - lineC] = '\0';
normalizeKey(keyC);
@try {
|
︙ | | | ︙ | |
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
|
exception: (id)exception
{
bool ret;
if (exception != nil) {
if ([exception isKindOfClass:
[OFInvalidEncodingException class]])
exception = [OFInvalidServerReplyException exception];
[self raiseException: exception];
return false;
}
@try {
if (_firstLine) {
|
|
>
|
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
|
exception: (id)exception
{
bool ret;
if (exception != nil) {
if ([exception isKindOfClass:
[OFInvalidEncodingException class]])
exception =
[OFInvalidServerResponseException exception];
[self raiseException: exception];
return false;
}
@try {
if (_firstLine) {
|
︙ | | | ︙ | |
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
|
if ([_client->_delegate respondsToSelector:
@selector(client:didCreateTCPSocket:request:)])
[_client->_delegate client: _client
didCreateTCPSocket: sock
request: _request];
if ([_request.URL.scheme caseInsensitiveCompare: @"https"] ==
OFOrderedSame) {
OFTLSStream *stream;
@try {
stream = [OFTLSStream streamWithStream: sock];
} @catch (OFNotImplementedException *e) {
[self raiseException:
[OFUnsupportedProtocolException
exceptionWithURL: _request.URL]];
return;
}
if ([_client->_delegate respondsToSelector:
@selector(client:didCreateTLSStream:request:)])
[_client->_delegate client: _client
didCreateTLSStream: stream
request: _request];
stream.delegate = self;
[stream asyncPerformClientHandshakeWithHost: _request.URL.host];
} else {
sock.delegate = self;
[self performSelector: @selector(handleStream:)
withObject: sock
afterDelay: 0];
}
}
|
|
|
|
|
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
|
if ([_client->_delegate respondsToSelector:
@selector(client:didCreateTCPSocket:request:)])
[_client->_delegate client: _client
didCreateTCPSocket: sock
request: _request];
if ([_request.URI.scheme caseInsensitiveCompare: @"https"] ==
OFOrderedSame) {
OFTLSStream *stream;
@try {
stream = [OFTLSStream streamWithStream: sock];
} @catch (OFNotImplementedException *e) {
[self raiseException:
[OFUnsupportedProtocolException
exceptionWithURI: _request.URI]];
return;
}
if ([_client->_delegate respondsToSelector:
@selector(client:didCreateTLSStream:request:)])
[_client->_delegate client: _client
didCreateTLSStream: stream
request: _request];
stream.delegate = self;
[stream asyncPerformClientHandshakeWithHost: _request.URI.host];
} else {
sock.delegate = self;
[self performSelector: @selector(handleStream:)
withObject: sock
afterDelay: 0];
}
}
|
︙ | | | ︙ | |
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
|
[self performSelector: @selector(handleStream:)
withObject: stream
afterDelay: 0];
}
- (void)start
{
OFURL *URL = _request.URL;
OFStream *stream;
/* Can we reuse the last socket? */
if (_client->_stream != nil && !_client->_stream.atEndOfStream &&
[_client->_lastURL.scheme isEqual: URL.scheme] &&
[_client->_lastURL.host isEqual: URL.host] &&
(_client->_lastURL.port == URL.port ||
[_client->_lastURL.port isEqual: URL.port]) &&
(_client->_lastWasHEAD || _client->_lastResponse.atEndOfStream)) {
/*
* Set _stream to nil, so that in case of an error it won't be
* reused. If everything is successful, we set _stream again
* at the end.
*/
stream = [_client->_stream autorelease];
_client->_stream = nil;
[_client->_lastURL release];
_client->_lastURL = nil;
[_client->_lastResponse release];
_client->_lastResponse = nil;
stream.delegate = self;
[self performSelector: @selector(handleStream:)
withObject: stream
afterDelay: 0];
} else
[self closeAndReconnect];
}
- (void)closeAndReconnect
{
@try {
OFURL *URL = _request.URL;
OFTCPSocket *sock;
uint16_t port;
OFNumber *URLPort;
[_client close];
sock = [OFTCPSocket socket];
if ([URL.scheme caseInsensitiveCompare: @"https"] ==
OFOrderedSame)
port = 443;
else
port = 80;
URLPort = URL.port;
if (URLPort != nil)
port = URLPort.unsignedShortValue;
sock.delegate = self;
[sock asyncConnectToHost: URL.host port: port];
} @catch (id e) {
[self raiseException: e];
}
}
@end
@implementation OFHTTPClientRequestBodyStream
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
|
[self performSelector: @selector(handleStream:)
withObject: stream
afterDelay: 0];
}
- (void)start
{
OFURI *URI = _request.URI;
OFStream *stream;
/* Can we reuse the last socket? */
if (_client->_stream != nil && !_client->_stream.atEndOfStream &&
[_client->_lastURI.scheme isEqual: URI.scheme] &&
[_client->_lastURI.host isEqual: URI.host] &&
(_client->_lastURI.port == URI.port ||
[_client->_lastURI.port isEqual: URI.port]) &&
(_client->_lastWasHEAD || _client->_lastResponse.atEndOfStream)) {
/*
* Set _stream to nil, so that in case of an error it won't be
* reused. If everything is successful, we set _stream again
* at the end.
*/
stream = [_client->_stream autorelease];
_client->_stream = nil;
[_client->_lastURI release];
_client->_lastURI = nil;
[_client->_lastResponse release];
_client->_lastResponse = nil;
stream.delegate = self;
[self performSelector: @selector(handleStream:)
withObject: stream
afterDelay: 0];
} else
[self closeAndReconnect];
}
- (void)closeAndReconnect
{
@try {
OFURI *URI = _request.URI;
OFTCPSocket *sock;
uint16_t port;
OFNumber *URIPort;
[_client close];
sock = [OFTCPSocket socket];
if ([URI.scheme caseInsensitiveCompare: @"https"] ==
OFOrderedSame)
port = 443;
else
port = 80;
URIPort = URI.port;
if (URIPort != nil)
port = URIPort.unsignedShortValue;
sock.delegate = self;
[sock asyncConnectToHost: URI.host port: port];
} @catch (id e) {
[self raiseException: e];
}
}
@end
@implementation OFHTTPClientRequestBodyStream
|
︙ | | | ︙ | |
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
|
_chunked = [[headers objectForKey: @"Transfer-Encoding"]
isEqual: @"chunked"];
contentLength = [headers objectForKey: @"Content-Length"];
if (contentLength != nil) {
if (_chunked || contentLength.length == 0)
@throw [OFInvalidServerReplyException exception];
_hasContentLength = true;
@try {
unsigned long long toRead =
contentLength.unsignedLongLongValue;
if (toRead > LLONG_MAX)
@throw [OFOutOfRangeException exception];
_toRead = (long long)toRead;
} @catch (OFInvalidFormatException *e) {
@throw [OFInvalidServerReplyException exception];
}
}
}
- (size_t)lowlevelReadIntoBuffer: (void *)buffer length: (size_t)length
{
if (_stream == nil)
|
|
|
|
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
|
_chunked = [[headers objectForKey: @"Transfer-Encoding"]
isEqual: @"chunked"];
contentLength = [headers objectForKey: @"Content-Length"];
if (contentLength != nil) {
if (_chunked || contentLength.length == 0)
@throw [OFInvalidServerResponseException exception];
_hasContentLength = true;
@try {
unsigned long long toRead =
contentLength.unsignedLongLongValue;
if (toRead > LLONG_MAX)
@throw [OFOutOfRangeException exception];
_toRead = (long long)toRead;
} @catch (OFInvalidFormatException *e) {
@throw [OFInvalidServerResponseException exception];
}
}
}
- (size_t)lowlevelReadIntoBuffer: (void *)buffer length: (size_t)length
{
if (_stream == nil)
|
︙ | | | ︙ | |
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
|
if (_toRead == -2) {
char tmp[2];
switch ([_stream readIntoBuffer: tmp length: 2]) {
case 2:
_toRead++;
if (tmp[1] != '\n')
@throw [OFInvalidServerReplyException
exception];
case 1:
_toRead++;
if (tmp[0] != '\r')
@throw [OFInvalidServerReplyException
exception];
}
if (_setAtEndOfStream && _toRead == 0)
_atEndOfStream = true;
return 0;
} else if (_toRead == -1) {
char tmp;
if ([_stream readIntoBuffer: &tmp length: 1] == 1) {
_toRead++;
if (tmp != '\n')
@throw [OFInvalidServerReplyException
exception];
}
if (_setAtEndOfStream && _toRead == 0)
_atEndOfStream = true;
return 0;
|
|
|
|
|
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
|
if (_toRead == -2) {
char tmp[2];
switch ([_stream readIntoBuffer: tmp length: 2]) {
case 2:
_toRead++;
if (tmp[1] != '\n')
@throw [OFInvalidServerResponseException
exception];
case 1:
_toRead++;
if (tmp[0] != '\r')
@throw [OFInvalidServerResponseException
exception];
}
if (_setAtEndOfStream && _toRead == 0)
_atEndOfStream = true;
return 0;
} else if (_toRead == -1) {
char tmp;
if ([_stream readIntoBuffer: &tmp length: 1] == 1) {
_toRead++;
if (tmp != '\n')
@throw [OFInvalidServerResponseException
exception];
}
if (_setAtEndOfStream && _toRead == 0)
_atEndOfStream = true;
return 0;
|
︙ | | | ︙ | |
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
|
void *pool = objc_autoreleasePoolPush();
OFString *line;
size_t pos;
@try {
line = [_stream tryReadLine];
} @catch (OFInvalidEncodingException *e) {
@throw [OFInvalidServerReplyException exception];
}
if (line == nil)
return 0;
pos = [line rangeOfString: @";"].location;
if (pos != OFNotFound)
line = [line substringToIndex: pos];
if (line.length < 1) {
/*
* We have read the empty string because the stream is
* at end of stream.
*/
if (_stream.atEndOfStream && pos == OFNotFound)
@throw [OFTruncatedDataException exception];
else
@throw [OFInvalidServerReplyException
exception];
}
@try {
unsigned long long toRead =
[line unsignedLongLongValueWithBase: 16];
if (toRead > LLONG_MAX)
@throw [OFOutOfRangeException exception];
_toRead = (long long)toRead;
} @catch (OFInvalidFormatException *e) {
@throw [OFInvalidServerReplyException exception];
}
if (_toRead == 0) {
_setAtEndOfStream = true;
_toRead = -2;
}
|
|
|
|
|
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
|
void *pool = objc_autoreleasePoolPush();
OFString *line;
size_t pos;
@try {
line = [_stream tryReadLine];
} @catch (OFInvalidEncodingException *e) {
@throw [OFInvalidServerResponseException exception];
}
if (line == nil)
return 0;
pos = [line rangeOfString: @";"].location;
if (pos != OFNotFound)
line = [line substringToIndex: pos];
if (line.length < 1) {
/*
* We have read the empty string because the stream is
* at end of stream.
*/
if (_stream.atEndOfStream && pos == OFNotFound)
@throw [OFTruncatedDataException exception];
else
@throw [OFInvalidServerResponseException
exception];
}
@try {
unsigned long long toRead =
[line unsignedLongLongValueWithBase: 16];
if (toRead > LLONG_MAX)
@throw [OFOutOfRangeException exception];
_toRead = (long long)toRead;
} @catch (OFInvalidFormatException *e) {
@throw [OFInvalidServerResponseException exception];
}
if (_toRead == 0) {
_setAtEndOfStream = true;
_toRead = -2;
}
|
︙ | | | ︙ | |
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
|
@selector(client:didReceiveHeaders:statusCode:request:)])
[_delegate client: client
didReceiveHeaders: headers
statusCode: statusCode
request: request];
}
- (bool)client: (OFHTTPClient *)client
shouldFollowRedirect: (OFURL *)URL
statusCode: (short)statusCode
request: (OFHTTPRequest *)request
response: (OFHTTPResponse *)response
{
if ([_delegate respondsToSelector: @selector(client:
shouldFollowRedirect:statusCode:request:response:)])
return [_delegate client: client
shouldFollowRedirect: URL
statusCode: statusCode
request: request
response: response];
else
return defaultShouldFollow(request.method, statusCode);
}
@end
@implementation OFHTTPClient
@synthesize delegate = _delegate;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
|
@selector(client:didReceiveHeaders:statusCode:request:)])
[_delegate client: client
didReceiveHeaders: headers
statusCode: statusCode
request: request];
}
- (bool)client: (OFHTTPClient *)client
shouldFollowRedirectToURI: (OFURI *)URI
statusCode: (short)statusCode
request: (OFHTTPRequest *)request
response: (OFHTTPResponse *)response
{
if ([_delegate respondsToSelector: @selector(
client:shouldFollowRedirectToURI:statusCode:request:response:)])
return [_delegate client: client
shouldFollowRedirectToURI: URI
statusCode: statusCode
request: request
response: response];
else
return defaultShouldFollow(request.method, statusCode);
}
@end
@implementation OFHTTPClient
@synthesize delegate = _delegate;
|
︙ | | | ︙ | |
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
|
[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"] != OFOrderedSame &&
[scheme caseInsensitiveCompare: @"https"] != OFOrderedSame)
@throw [OFUnsupportedProtocolException exceptionWithURL: URL];
if (_inProgress)
/* TODO: Find a better exception */
@throw [OFAlreadyConnectedException exception];
_inProgress = true;
[[[[OFHTTPClientRequestHandler alloc]
initWithClient: self
request: request
redirects: redirects] autorelease] start];
objc_autoreleasePoolPop(pool);
}
- (void)close
{
[_stream release];
_stream = nil;
[_lastURL release];
_lastURL = nil;
[_lastResponse release];
_lastResponse = nil;
}
@end
|
|
|
|
<
|
|
|
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
|
[self asyncPerformRequest: request redirects: defaultRedirects];
}
- (void)asyncPerformRequest: (OFHTTPRequest *)request
redirects: (unsigned int)redirects
{
void *pool = objc_autoreleasePoolPush();
OFURI *URI = request.URI;
OFString *scheme = URI.scheme;
if ([scheme caseInsensitiveCompare: @"http"] != OFOrderedSame &&
[scheme caseInsensitiveCompare: @"https"] != OFOrderedSame)
@throw [OFUnsupportedProtocolException exceptionWithURI: URI];
if (_inProgress)
@throw [OFAlreadyConnectedException exception];
_inProgress = true;
[[[[OFHTTPClientRequestHandler alloc]
initWithClient: self
request: request
redirects: redirects] autorelease] start];
objc_autoreleasePoolPop(pool);
}
- (void)close
{
[_stream release];
_stream = nil;
[_lastURI release];
_lastURI = nil;
[_lastResponse release];
_lastResponse = nil;
}
@end
|