| ︙ | | | ︙ | |
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
OFStreamDelegate>
{
OFArray OF_GENERIC(OFString *) *_URLs;
size_t _URLIndex;
int _errorCode;
OFString *_outputPath, *_currentFileName;
bool _continue, _force, _detectFileName, _detectFileNameRequest;
bool _detectedFileName, _quiet, _verbose, _insecure;
OFStream *_body;
of_http_request_method_t _method;
OFMutableDictionary *_clientHeaders;
OFHTTPClient *_HTTPClient;
char *_buffer;
OFStream *_output;
unsigned long long _received, _length, _resumedFrom;
|
|
|
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
OFStreamDelegate>
{
OFArray OF_GENERIC(OFString *) *_URLs;
size_t _URLIndex;
int _errorCode;
OFString *_outputPath, *_currentFileName;
bool _continue, _force, _detectFileName, _detectFileNameRequest;
bool _detectedFileName, _quiet, _verbose, _insecure, _ignoreStatus;
OFStream *_body;
of_http_request_method_t _method;
OFMutableDictionary *_clientHeaders;
OFHTTPClient *_HTTPClient;
char *_buffer;
OFStream *_output;
unsigned long long _received, _length, _resumedFrom;
|
| ︙ | | | ︙ | |
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
@"-P --proxy "
@" Specify SOCKS5 proxy\n "
@"-q --quiet "
@" Quiet mode (no output, except errors)\n "
@"-v --verbose "
@" Verbose mode (print headers)\n "
@" --insecure "
@" Ignore TLS errors and allow insecure redirects")];
}
[OFApplication terminateWithStatus: status];
}
static OFString *
fileNameFromContentDisposition(OFString *contentDisposition)
|
|
>
>
|
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
@"-P --proxy "
@" Specify SOCKS5 proxy\n "
@"-q --quiet "
@" Quiet mode (no output, except errors)\n "
@"-v --verbose "
@" Verbose mode (print headers)\n "
@" --insecure "
@" Ignore TLS errors and allow insecure redirects\n "
@" --ignore-status "
@" Ignore HTTP status code")];
}
[OFApplication terminateWithStatus: status];
}
static OFString *
fileNameFromContentDisposition(OFString *contentDisposition)
|
| ︙ | | | ︙ | |
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
_clientHeaders = [[OFMutableDictionary alloc]
initWithObject: @"OFHTTP"
forKey: @"User-Agent"];
_HTTPClient = [[OFHTTPClient alloc] init];
_HTTPClient.delegate = self;
_buffer = [self allocMemoryWithSize: [OFSystemInfo pageSize]];
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)addHeader: (OFString *)header
{
size_t pos = [header rangeOfString: @":"].location;
OFString *name, *value;
if (pos == OF_NOT_FOUND) {
[of_stderr writeLine: OF_LOCALIZED(@"invalid_input_header",
@"%[prog]: Headers must to be in format name:value!",
@"prog", [OFApplication programName])];
[OFApplication terminateWithStatus: 1];
}
name = [header substringWithRange: of_range(0, pos)];
name = name.stringByDeletingEnclosingWhitespaces;
value = [header substringWithRange:
of_range(pos + 1, header.length - pos - 1)];
value = value.stringByDeletingEnclosingWhitespaces;
[_clientHeaders setObject: value
forKey: name];
}
- (void)setBody: (OFString *)path
{
|
|
|
|
|
<
|
|
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
_clientHeaders = [[OFMutableDictionary alloc]
initWithObject: @"OFHTTP"
forKey: @"User-Agent"];
_HTTPClient = [[OFHTTPClient alloc] init];
_HTTPClient.delegate = self;
_buffer = of_malloc(1, [OFSystemInfo pageSize]);
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)addHeader: (OFString *)header
{
size_t pos = [header rangeOfString: @":"].location;
OFString *name, *value;
if (pos == OF_NOT_FOUND) {
[of_stderr writeLine: OF_LOCALIZED(@"invalid_input_header",
@"%[prog]: Headers must to be in format name:value!",
@"prog", [OFApplication programName])];
[OFApplication terminateWithStatus: 1];
}
name = [header substringToIndex: pos]
.stringByDeletingEnclosingWhitespaces;
value = [header substringFromIndex: pos + 1]
.stringByDeletingEnclosingWhitespaces;
[_clientHeaders setObject: value
forKey: name];
}
- (void)setBody: (OFString *)path
{
|
| ︙ | | | ︙ | |
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
options: OF_STRING_SEARCH_BACKWARDS].location;
OFString *host;
unsigned long long port;
if (pos == OF_NOT_FOUND)
@throw [OFInvalidFormatException exception];
host = [proxy substringWithRange: of_range(0, pos)];
port = [proxy substringWithRange: of_range(pos + 1,
proxy.length - pos - 1)].unsignedLongLongValue;
if (port > UINT16_MAX)
@throw [OFOutOfRangeException exception];
[OFTCPSocket setSOCKS5Host: host];
[OFTCPSocket setSOCKS5Port: (uint16_t)port];
} @catch (OFInvalidFormatException *e) {
|
|
|
|
|
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
options: OF_STRING_SEARCH_BACKWARDS].location;
OFString *host;
unsigned long long port;
if (pos == OF_NOT_FOUND)
@throw [OFInvalidFormatException exception];
host = [proxy substringToIndex: pos];
port = [proxy substringFromIndex: pos + 1]
.unsignedLongLongValue;
if (port > UINT16_MAX)
@throw [OFOutOfRangeException exception];
[OFTCPSocket setSOCKS5Host: host];
[OFTCPSocket setSOCKS5Port: (uint16_t)port];
} @catch (OFInvalidFormatException *e) {
|
| ︙ | | | ︙ | |
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
{ 'm', @"method", 1, NULL, NULL },
{ 'o', @"output", 1, NULL, &outputPath },
{ 'O', @"detect-filename", 0, &_detectFileName, NULL },
{ 'P', @"socks5-proxy", 1, NULL, NULL },
{ 'q', @"quiet", 0, &_quiet, NULL },
{ 'v', @"verbose", 0, &_verbose, NULL },
{ '\0', @"insecure", 0, &_insecure, NULL },
{ '\0', nil, 0, NULL, NULL }
};
OFOptionsParser *optionsParser;
of_unichar_t option;
#ifdef OF_HAVE_SANDBOX
OFSandbox *sandbox = [OFSandbox sandbox];
|
>
|
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
{ 'm', @"method", 1, NULL, NULL },
{ 'o', @"output", 1, NULL, &outputPath },
{ 'O', @"detect-filename", 0, &_detectFileName, NULL },
{ 'P', @"socks5-proxy", 1, NULL, NULL },
{ 'q', @"quiet", 0, &_quiet, NULL },
{ 'v', @"verbose", 0, &_verbose, NULL },
{ '\0', @"insecure", 0, &_insecure, NULL },
{ '\0', @"ignore-status", 0, &_ignoreStatus, NULL },
{ '\0', nil, 0, NULL, NULL }
};
OFOptionsParser *optionsParser;
of_unichar_t option;
#ifdef OF_HAVE_SANDBOX
OFSandbox *sandbox = [OFSandbox sandbox];
|
| ︙ | | | ︙ | |
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
|
[body writeBuffer: buffer
length: length];
}
}
- (bool)client: (OFHTTPClient *)client
shouldFollowRedirect: (OFURL *)URL
statusCode: (int)statusCode
request: (OFHTTPRequest *)request
response: (OFHTTPResponse *)response
{
if (_verbose) {
void *pool = objc_autoreleasePoolPush();
OFDictionary OF_GENERIC(OFString *, OFString *) *headers =
response.headers;
|
|
|
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
|
[body writeBuffer: buffer
length: length];
}
}
- (bool)client: (OFHTTPClient *)client
shouldFollowRedirect: (OFURL *)URL
statusCode: (short)statusCode
request: (OFHTTPRequest *)request
response: (OFHTTPResponse *)response
{
if (_verbose) {
void *pool = objc_autoreleasePoolPush();
OFDictionary OF_GENERIC(OFString *, OFString *) *headers =
response.headers;
|
| ︙ | | | ︙ | |
630
631
632
633
634
635
636
637
638
639
640
641
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
676
677
678
679
680
681
682
683
684
685
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
|
[of_stdout writeFormat: @"☇ %@", URL.string];
_length = 0;
return true;
}
- (void)client: (OFHTTPClient *)client
didFailWithException: (id)e
request: (OFHTTPRequest *)request
{
if ([e isKindOfClass: [OFResolveHostFailedException class]]) {
if (!_quiet)
[of_stdout writeString: @"\n"];
[of_stderr writeLine:
OF_LOCALIZED(@"download_resolve_host_failed",
@"%[prog]: Failed to download <%[url]>!\n"
@" Failed to resolve host: %[exception]",
@"prog", [OFApplication programName],
@"url", request.URL.string,
@"exception", e)];
} else if ([e isKindOfClass: [OFConnectionFailedException class]]) {
if (!_quiet)
[of_stdout writeString: @"\n"];
[of_stderr writeLine:
OF_LOCALIZED(@"download_failed_connection_failed",
@"%[prog]: Failed to download <%[url]>!\n"
@" Connection failed: %[exception]",
@"prog", [OFApplication programName],
@"url", request.URL.string,
@"exception", e)];
} else if ([e isKindOfClass: [OFInvalidServerReplyException class]]) {
if (!_quiet)
[of_stdout writeString: @"\n"];
[of_stderr writeLine:
OF_LOCALIZED(@"download_failed_invalid_server_reply",
@"%[prog]: Failed to download <%[url]>!\n"
@" Invalid server reply!",
@"prog", [OFApplication programName],
@"url", request.URL.string)];
} else if ([e isKindOfClass: [OFUnsupportedProtocolException class]]) {
if (!_quiet)
[of_stdout writeString: @"\n"];
[of_stderr writeLine: OF_LOCALIZED(@"no_ssl_library",
@"%[prog]: No TLS library loaded!\n"
@" In order to download via https, you need to preload an "
@"TLS library for ObjFW\n"
@" such as ObjOpenSSL!",
@"prog", [OFApplication programName])];
} else if ([e isKindOfClass: [OFReadOrWriteFailedException class]]) {
OFString *error = OF_LOCALIZED(
@"download_failed_read_or_write_failed_any",
@"Read or write failed");
if (!_quiet)
[of_stdout writeString: @"\n"];
if ([e isKindOfClass: [OFReadFailedException class]])
error = OF_LOCALIZED(
@"download_failed_read_or_write_failed_read",
@"Read failed");
else if ([e isKindOfClass: [OFWriteFailedException class]])
error = OF_LOCALIZED(
@"download_failed_read_or_write_failed_write",
@"Write failed");
[of_stderr writeLine:
OF_LOCALIZED(@"download_failed_read_or_write_failed",
@"%[prog]: Failed to download <%[url]>!\n"
@" %[error]: %[exception]",
@"prog", [OFApplication programName],
@"url", request.URL.string,
@"error", error,
@"exception", e)];
} else if ([e isKindOfClass: [OFHTTPRequestFailedException class]]) {
short statusCode = [[e response] statusCode];
OFString *codeString = [OFString stringWithFormat: @"%d %@",
statusCode, of_http_status_code_to_string(statusCode)];
[of_stderr writeLine: OF_LOCALIZED(@"download_failed",
@"%[prog]: Failed to download <%[url]>!\n"
@" HTTP status code: %[code]",
@"prog", [OFApplication programName],
@"url", request.URL.string,
@"code", codeString)];
} else
@throw e;
_errorCode = 1;
[self performSelector: @selector(downloadNextURL)
afterDelay: 0];
}
- (bool)stream: (OFStream *)response
didReadIntoBuffer: (void *)buffer
length: (size_t)length
exception: (id)exception
{
if (exception != nil) {
OFString *URL;
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
632
633
634
635
636
637
638
639
640
641
642
643
644
645
|
[of_stdout writeFormat: @"☇ %@", URL.string];
_length = 0;
return true;
}
- (bool)stream: (OFStream *)response
didReadIntoBuffer: (void *)buffer
length: (size_t)length
exception: (id)exception
{
if (exception != nil) {
OFString *URL;
|
| ︙ | | | ︙ | |
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
|
}
return true;
}
- (void)client: (OFHTTPClient *)client
didReceiveHeaders: (OFDictionary OF_GENERIC(OFString *, OFString *) *)headers
statusCode: (int)statusCode
request: (OFHTTPRequest *)request
{
if (statusCode != 206)
_resumedFrom = 0;
if (!_quiet) {
OFString *lengthString =
[headers objectForKey: @"Content-Length"];
OFString *type = [headers objectForKey: @"Content-Type"];
[of_stdout writeFormat: @" ➜ %d\n", statusCode];
if (type == nil)
type = OF_LOCALIZED(@"type_unknown", @"unknown");
if (lengthString != nil) {
_length = lengthString.unsignedLongLongValue;
|
|
|
|
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
|
}
return true;
}
- (void)client: (OFHTTPClient *)client
didReceiveHeaders: (OFDictionary OF_GENERIC(OFString *, OFString *) *)headers
statusCode: (short)statusCode
request: (OFHTTPRequest *)request
{
if (statusCode != 206)
_resumedFrom = 0;
if (!_quiet) {
OFString *lengthString =
[headers objectForKey: @"Content-Length"];
OFString *type = [headers objectForKey: @"Content-Type"];
[of_stdout writeFormat: @" ➜ %hd\n", statusCode];
if (type == nil)
type = OF_LOCALIZED(@"type_unknown", @"unknown");
if (lengthString != nil) {
_length = lengthString.unsignedLongLongValue;
|
| ︙ | | | ︙ | |
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
|
}
}
}
- (void)client: (OFHTTPClient *)client
didPerformRequest: (OFHTTPRequest *)request
response: (OFHTTPResponse *)response
{
if (_method == OF_HTTP_REQUEST_METHOD_HEAD)
goto next;
if (_detectFileNameRequest) {
_currentFileName = [fileNameFromContentDisposition(
[response.headers objectForKey: @"Content-Disposition"])
copy];
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
|
}
}
}
- (void)client: (OFHTTPClient *)client
didPerformRequest: (OFHTTPRequest *)request
response: (OFHTTPResponse *)response
exception: (id)exception
{
if (exception != nil) {
if ([exception isKindOfClass:
[OFResolveHostFailedException class]]) {
if (!_quiet)
[of_stdout writeString: @"\n"];
[of_stderr writeLine:
OF_LOCALIZED(@"download_resolve_host_failed",
@"%[prog]: Failed to download <%[url]>!\n"
@" Failed to resolve host: %[exception]",
@"prog", [OFApplication programName],
@"url", request.URL.string,
@"exception", exception)];
} else if ([exception isKindOfClass:
[OFConnectionFailedException class]]) {
if (!_quiet)
[of_stdout writeString: @"\n"];
[of_stderr writeLine:
OF_LOCALIZED(@"download_failed_connection_failed",
@"%[prog]: Failed to download <%[url]>!\n"
@" Connection failed: %[exception]",
@"prog", [OFApplication programName],
@"url", request.URL.string,
@"exception", exception)];
} else if ([exception isKindOfClass:
[OFInvalidServerReplyException class]]) {
if (!_quiet)
[of_stdout writeString: @"\n"];
[of_stderr writeLine: OF_LOCALIZED(
@"download_failed_invalid_server_reply",
@"%[prog]: Failed to download <%[url]>!\n"
@" Invalid server reply!",
@"prog", [OFApplication programName],
@"url", request.URL.string)];
} else if ([exception isKindOfClass:
[OFUnsupportedProtocolException class]]) {
if (!_quiet)
[of_stdout writeString: @"\n"];
[of_stderr writeLine: OF_LOCALIZED(@"no_ssl_library",
@"%[prog]: No TLS library loaded!\n"
@" In order to download via https, you need to "
@"preload an TLS library for ObjFW\n"
@" such as ObjOpenSSL!",
@"prog", [OFApplication programName])];
} else if ([exception isKindOfClass:
[OFReadOrWriteFailedException class]]) {
OFString *error = OF_LOCALIZED(
@"download_failed_read_or_write_failed_any",
@"Read or write failed");
if (!_quiet)
[of_stdout writeString: @"\n"];
if ([exception isKindOfClass:
[OFReadFailedException class]])
error = OF_LOCALIZED(
@"download_failed_read_or_write_failed_"
@"read",
@"Read failed");
else if ([exception isKindOfClass:
[OFWriteFailedException class]])
error = OF_LOCALIZED(
@"download_failed_read_or_write_failed_"
@"write",
@"Write failed");
[of_stderr writeLine: OF_LOCALIZED(
@"download_failed_read_or_write_failed",
@"%[prog]: Failed to download <%[url]>!\n"
@" %[error]: %[exception]",
@"prog", [OFApplication programName],
@"url", request.URL.string,
@"error", error,
@"exception", exception)];
} else if ([exception isKindOfClass:
[OFHTTPRequestFailedException class]]) {
short statusCode;
OFString *codeString;
if (_ignoreStatus) {
exception = nil;
goto after_exception_handling;
}
statusCode = response.statusCode;
codeString = [OFString stringWithFormat: @"%hd %@",
statusCode,
of_http_status_code_to_string(statusCode)];
[of_stderr writeLine: OF_LOCALIZED(@"download_failed",
@"%[prog]: Failed to download <%[url]>!\n"
@" HTTP status code: %[code]",
@"prog", [OFApplication programName],
@"url", request.URL.string,
@"code", codeString)];
} else
@throw exception;
_errorCode = 1;
[self performSelector: @selector(downloadNextURL)
afterDelay: 0];
return;
}
after_exception_handling:
if (_method == OF_HTTP_REQUEST_METHOD_HEAD)
goto next;
if (_detectFileNameRequest) {
_currentFileName = [fileNameFromContentDisposition(
[response.headers objectForKey: @"Content-Disposition"])
copy];
|
| ︙ | | | ︙ | |