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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
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
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
|
- (void)setMethod: (OFString *)method
{
void *pool = objc_autoreleasePoolPush();
method = method.uppercaseString;
@try {
_method = of_http_request_method_from_string(method);
} @catch (OFInvalidArgumentException *e) {
[of_stderr writeLine: OF_LOCALIZED(@"invalid_input_method",
@"%[prog]: Invalid request method %[method]!",
@"prog", [OFApplication programName],
@"method", method)];
[OFApplication terminateWithStatus: 1];
}
objc_autoreleasePoolPop(pool);
}
- (void)setProxy: (OFString *)proxy
{
@try {
size_t pos = [proxy
rangeOfString: @":"
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) {
[of_stderr writeLine: OF_LOCALIZED(@"invalid_input_proxy",
@"%[prog]: Proxy must to be in format host:port!",
@"prog", [OFApplication programName])];
[OFApplication terminateWithStatus: 1];
}
}
- (void)applicationDidFinishLaunching
{
OFString *outputPath;
const of_options_parser_option_t options[] = {
{ 'b', @"body", 1, NULL, NULL },
{ 'c', @"continue", 0, &_continue, NULL },
{ 'f', @"force", 0, &_force, NULL },
{ 'h', @"help", 0, NULL, NULL },
{ 'H', @"header", 1, NULL, NULL },
{ '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];
sandbox.allowsStdIO = true;
sandbox.allowsReadingFiles = true;
sandbox.allowsWritingFiles = true;
sandbox.allowsCreatingFiles = true;
sandbox.allowsIPSockets = true;
sandbox.allowsDNS = true;
sandbox.allowsUserDatabaseReading = true;
sandbox.allowsTTY = true;
/* Dropped after parsing options */
sandbox.allowsUnveil = true;
[OFApplication activateSandbox: sandbox];
#endif
#ifndef OF_AMIGAOS
[OFLocale addLanguageDirectory: @LANGUAGE_DIR];
#else
[OFLocale addLanguageDirectory: @"PROGDIR:/share/ofhttp/lang"];
#endif
optionsParser = [OFOptionsParser parserWithOptions: options];
while ((option = [optionsParser nextOption]) != '\0') {
switch (option) {
case 'b':
[self setBody: optionsParser.argument];
break;
case 'h':
help(of_stdout, true, 0);
break;
case 'H':
[self addHeader: optionsParser.argument];
break;
case 'm':
[self setMethod: optionsParser.argument];
break;
case 'P':
[self setProxy: optionsParser.argument];
break;
case ':':
if (optionsParser.lastLongOption != nil)
[of_stderr writeLine:
OF_LOCALIZED(@"long_argument_missing",
@"%[prog]: Argument for option --%[opt] "
@"missing"
@"prog", [OFApplication programName],
@"opt", optionsParser.lastLongOption)];
else {
OFString *optStr = [OFString
stringWithFormat: @"%c",
optionsParser.lastOption];
[of_stderr writeLine:
OF_LOCALIZED(@"argument_missing",
@"%[prog]: Argument for option -%[opt] "
@"missing",
@"prog", [OFApplication programName],
@"opt", optStr)];
}
[OFApplication terminateWithStatus: 1];
break;
case '=':
[of_stderr writeLine:
OF_LOCALIZED(@"option_takes_no_argument",
@"%[prog]: Option --%[opt] takes no argument",
@"prog", [OFApplication programName],
@"opt", optionsParser.lastLongOption)];
[OFApplication terminateWithStatus: 1];
break;
case '?':
if (optionsParser.lastLongOption != nil)
[of_stderr writeLine:
OF_LOCALIZED(@"unknown_long_option",
@"%[prog]: Unknown option: --%[opt]",
@"prog", [OFApplication programName],
@"opt", optionsParser.lastLongOption)];
else {
OFString *optStr = [OFString
stringWithFormat: @"%c",
optionsParser.lastOption];
[of_stderr writeLine:
OF_LOCALIZED(@"unknown_option",
@"%[prog]: Unknown option: -%[opt]",
@"prog", [OFApplication programName],
@"opt", optStr)];
}
[OFApplication terminateWithStatus: 1];
break;
}
}
#ifdef OF_HAVE_SANDBOX
[sandbox unveilPath: (outputPath != nil
? outputPath : OF_PATH_CURRENT_DIRECTORY)
permissions: (_continue ? @"rwc" : @"wc")];
/* In case we use ObjOpenSSL for https later */
[sandbox unveilPath: @"/etc/ssl" permissions: @"r"];
sandbox.allowsUnveil = false;
[OFApplication activateSandbox: sandbox];
#endif
_outputPath = [outputPath copy];
_URLs = [optionsParser.remainingArguments copy];
if (_URLs.count < 1)
help(of_stderr, false, 1);
if (_quiet && _verbose) {
[of_stderr writeLine: OF_LOCALIZED(@"quiet_xor_verbose",
@"%[prog]: -q / --quiet and -v / --verbose are mutually "
@"exclusive!",
@"prog", [OFApplication programName])];
[OFApplication terminateWithStatus: 1];
}
if (_outputPath != nil && _detectFileName) {
[of_stderr writeLine: OF_LOCALIZED(
@"output_xor_detect_filename",
@"%[prog]: -o / --output and -O / --detect-filename are "
@"mutually exclusive!",
@"prog", [OFApplication programName])];
[OFApplication terminateWithStatus: 1];
}
if (_outputPath != nil && _URLs.count > 1) {
[of_stderr writeLine:
OF_LOCALIZED(@"output_only_with_one_url",
@"%[prog]: Cannot use -o / --output when more than one URL "
@"has been specified!",
@"prog", [OFApplication programName])];
[OFApplication terminateWithStatus: 1];
}
if (_insecure)
_HTTPClient.allowsInsecureRedirects = true;
_useUnicode = ([OFLocale encoding] == OF_STRING_ENCODING_UTF_8);
[self performSelector: @selector(downloadNextURL) afterDelay: 0];
}
- (void)client: (OFHTTPClient *)client
didCreateSocket: (OFTCPSocket *)sock
request: (OFHTTPRequest *)request
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
|
>
>
>
|
|
>
|
|
|
|
|
|
|
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
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
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
|
- (void)setMethod: (OFString *)method
{
void *pool = objc_autoreleasePoolPush();
method = method.uppercaseString;
@try {
_method = OFHTTPRequestMethodParseName(method);
} @catch (OFInvalidArgumentException *e) {
[OFStdErr writeLine: OF_LOCALIZED(@"invalid_input_method",
@"%[prog]: Invalid request method %[method]!",
@"prog", [OFApplication programName],
@"method", method)];
[OFApplication terminateWithStatus: 1];
}
objc_autoreleasePoolPop(pool);
}
- (void)setProxy: (OFString *)proxy
{
@try {
size_t pos = [proxy
rangeOfString: @":"
options: OFStringSearchBackwards].location;
OFString *host;
unsigned long long port;
if (pos == OFNotFound)
@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) {
[OFStdErr writeLine: OF_LOCALIZED(@"invalid_input_proxy",
@"%[prog]: Proxy must to be in format host:port!",
@"prog", [OFApplication programName])];
[OFApplication terminateWithStatus: 1];
}
}
- (void)applicationDidFinishLaunching
{
OFString *outputPath;
const OFOptionsParserOption options[] = {
{ 'b', @"body", 1, NULL, NULL },
{ 'c', @"continue", 0, &_continue, NULL },
{ 'f', @"force", 0, &_force, NULL },
{ 'h', @"help", 0, NULL, NULL },
{ 'H', @"header", 1, NULL, NULL },
{ '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;
OFUnichar option;
#ifdef OF_HAVE_SANDBOX
OFSandbox *sandbox = [OFSandbox sandbox];
sandbox.allowsStdIO = true;
sandbox.allowsReadingFiles = true;
sandbox.allowsWritingFiles = true;
sandbox.allowsCreatingFiles = true;
sandbox.allowsIPSockets = true;
sandbox.allowsDNS = true;
sandbox.allowsUserDatabaseReading = true;
sandbox.allowsTTY = true;
/* Dropped after parsing options */
sandbox.allowsUnveil = true;
[OFApplication of_activateSandbox: sandbox];
#endif
#ifndef OF_AMIGAOS
[OFLocale addLanguageDirectory: @LANGUAGE_DIR];
#else
[OFLocale addLanguageDirectory: @"PROGDIR:/share/ofhttp/lang"];
#endif
optionsParser = [OFOptionsParser parserWithOptions: options];
while ((option = [optionsParser nextOption]) != '\0') {
switch (option) {
case 'b':
[self setBody: optionsParser.argument];
break;
case 'h':
help(OFStdOut, true, 0);
break;
case 'H':
[self addHeader: optionsParser.argument];
break;
case 'm':
[self setMethod: optionsParser.argument];
break;
case 'P':
[self setProxy: optionsParser.argument];
break;
case ':':
if (optionsParser.lastLongOption != nil)
[OFStdErr writeLine:
OF_LOCALIZED(@"long_argument_missing",
@"%[prog]: Argument for option --%[opt] "
@"missing"
@"prog", [OFApplication programName],
@"opt", optionsParser.lastLongOption)];
else {
OFString *optStr = [OFString
stringWithFormat: @"%c",
optionsParser.lastOption];
[OFStdErr writeLine:
OF_LOCALIZED(@"argument_missing",
@"%[prog]: Argument for option -%[opt] "
@"missing",
@"prog", [OFApplication programName],
@"opt", optStr)];
}
[OFApplication terminateWithStatus: 1];
break;
case '=':
[OFStdErr writeLine:
OF_LOCALIZED(@"option_takes_no_argument",
@"%[prog]: Option --%[opt] takes no argument",
@"prog", [OFApplication programName],
@"opt", optionsParser.lastLongOption)];
[OFApplication terminateWithStatus: 1];
break;
case '?':
if (optionsParser.lastLongOption != nil)
[OFStdErr writeLine:
OF_LOCALIZED(@"unknown_long_option",
@"%[prog]: Unknown option: --%[opt]",
@"prog", [OFApplication programName],
@"opt", optionsParser.lastLongOption)];
else {
OFString *optStr = [OFString
stringWithFormat: @"%c",
optionsParser.lastOption];
[OFStdErr writeLine:
OF_LOCALIZED(@"unknown_option",
@"%[prog]: Unknown option: -%[opt]",
@"prog", [OFApplication programName],
@"opt", optStr)];
}
[OFApplication terminateWithStatus: 1];
break;
}
}
#ifdef OF_HAVE_SANDBOX
if (outputPath != nil)
[sandbox unveilPath: outputPath
permissions: (_continue ? @"rwc" : @"wc")];
else
[sandbox unveilPath: [[OFFileManger defaultManager]
currentDirectoryPath]
permissions: (_continue ? @"rwc" : @"wc")];
/* In case we use ObjOpenSSL for https later */
[sandbox unveilPath: @"/etc/ssl" permissions: @"r"];
sandbox.allowsUnveil = false;
[OFApplication of_activateSandbox: sandbox];
#endif
_outputPath = [outputPath copy];
_URLs = [optionsParser.remainingArguments copy];
if (_URLs.count < 1)
help(OFStdErr, false, 1);
if (_quiet && _verbose) {
[OFStdErr writeLine: OF_LOCALIZED(@"quiet_xor_verbose",
@"%[prog]: -q / --quiet and -v / --verbose are mutually "
@"exclusive!",
@"prog", [OFApplication programName])];
[OFApplication terminateWithStatus: 1];
}
if (_outputPath != nil && _detectFileName) {
[OFStdErr writeLine: OF_LOCALIZED(
@"output_xor_detect_filename",
@"%[prog]: -o / --output and -O / --detect-filename are "
@"mutually exclusive!",
@"prog", [OFApplication programName])];
[OFApplication terminateWithStatus: 1];
}
if (_outputPath != nil && _URLs.count > 1) {
[OFStdErr writeLine:
OF_LOCALIZED(@"output_only_with_one_url",
@"%[prog]: Cannot use -o / --output when more than one URL "
@"has been specified!",
@"prog", [OFApplication programName])];
[OFApplication terminateWithStatus: 1];
}
if (_insecure)
_HTTPClient.allowsInsecureRedirects = true;
_useUnicode = ([OFLocale encoding] == OFStringEncodingUTF8);
[self performSelector: @selector(downloadNextURL) afterDelay: 0];
}
- (void)client: (OFHTTPClient *)client
didCreateSocket: (OFTCPSocket *)sock
request: (OFHTTPRequest *)request
|
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
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
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
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
|
OFEnumerator OF_GENERIC(OFString *) *keyEnumerator =
[headers keyEnumerator];
OFEnumerator OF_GENERIC(OFString *) *objectEnumerator =
[headers objectEnumerator];
OFString *key, *object;
if (statusCode / 100 == 2 && _currentFileName != nil) {
[of_stdout writeString: @" "];
[of_stdout writeLine: OF_LOCALIZED(
@"info_name_unaligned",
@"Name: %[name]",
@"name", _currentFileName)];
}
while ((key = [keyEnumerator nextObject]) != nil &&
(object = [objectEnumerator nextObject]) != nil)
[of_stdout writeFormat: @" %@: %@\n",
key, object];
objc_autoreleasePoolPop(pool);
} else if (statusCode / 100 == 2 && !_detectFileNameRequest) {
[of_stdout writeString: @" "];
if (_currentFileName != nil)
[of_stdout writeLine: OF_LOCALIZED(@"info_name",
@"Name: %[name]",
@"name", _currentFileName)];
[of_stdout writeString: @" "];
[of_stdout writeLine: OF_LOCALIZED(@"info_type",
@"Type: %[type]",
@"type", type)];
[of_stdout writeString: @" "];
[of_stdout writeLine: OF_LOCALIZED(@"info_size",
@"Size: %[size]",
@"size", lengthString)];
}
}
}
- (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];
_detectedFileName = true;
/* Handle this URL on the next -[downloadNextURL] call */
_URLIndex--;
[self performSelector: @selector(downloadNextURL)
afterDelay: 0];
return;
}
if ([_outputPath isEqual: @"-"])
_output = of_stdout;
else {
if (!_continue && !_force && [[OFFileManager defaultManager]
fileExistsAtPath: _currentFileName]) {
[of_stderr writeLine:
OF_LOCALIZED(@"output_already_exists",
@"%[prog]: File %[filename] already exists!",
@"prog", [OFApplication programName],
@"filename", _currentFileName)];
_errorCode = 1;
goto next;
}
@try {
OFString *mode =
(response.statusCode == 206 ? @"a" : @"w");
_output = [[OFFile alloc] initWithPath: _currentFileName
mode: mode];
} @catch (OFOpenItemFailedException *e) {
[of_stderr writeLine:
OF_LOCALIZED(@"failed_to_open_output",
@"%[prog]: Failed to open file %[filename]: "
@"%[exception]",
@"prog", [OFApplication programName],
@"filename", _currentFileName,
@"exception", e)];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
|
|
|
|
|
|
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
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
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
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
|
OFEnumerator OF_GENERIC(OFString *) *keyEnumerator =
[headers keyEnumerator];
OFEnumerator OF_GENERIC(OFString *) *objectEnumerator =
[headers objectEnumerator];
OFString *key, *object;
if (statusCode / 100 == 2 && _currentFileName != nil) {
[OFStdOut writeString: @" "];
[OFStdOut writeLine: OF_LOCALIZED(
@"info_name_unaligned",
@"Name: %[name]",
@"name", _currentFileName)];
}
while ((key = [keyEnumerator nextObject]) != nil &&
(object = [objectEnumerator nextObject]) != nil)
[OFStdOut writeFormat: @" %@: %@\n",
key, object];
objc_autoreleasePoolPop(pool);
} else if (statusCode / 100 == 2 && !_detectFileNameRequest) {
[OFStdOut writeString: @" "];
if (_currentFileName != nil)
[OFStdOut writeLine: OF_LOCALIZED(@"info_name",
@"Name: %[name]",
@"name", _currentFileName)];
[OFStdOut writeString: @" "];
[OFStdOut writeLine: OF_LOCALIZED(@"info_type",
@"Type: %[type]",
@"type", type)];
[OFStdOut writeString: @" "];
[OFStdOut writeLine: OF_LOCALIZED(@"info_size",
@"Size: %[size]",
@"size", lengthString)];
}
}
}
- (void)client: (OFHTTPClient *)client
didPerformRequest: (OFHTTPRequest *)request
response: (OFHTTPResponse *)response
exception: (id)exception
{
if (exception != nil) {
if ([exception isKindOfClass:
[OFResolveHostFailedException class]]) {
if (!_quiet)
[OFStdOut writeString: @"\n"];
[OFStdErr 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)
[OFStdOut writeString: @"\n"];
[OFStdErr 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)
[OFStdOut writeString: @"\n"];
[OFStdErr 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)
[OFStdOut writeString: @"\n"];
[OFStdErr 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)
[OFStdOut 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");
[OFStdErr 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, OFHTTPStatusCodeString(statusCode)];
[OFStdErr 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 == OFHTTPRequestMethodHead)
goto next;
if (_detectFileNameRequest) {
_currentFileName = [fileNameFromContentDisposition(
[response.headers objectForKey: @"Content-Disposition"])
copy];
_detectedFileName = true;
/* Handle this URL on the next -[downloadNextURL] call */
_URLIndex--;
[self performSelector: @selector(downloadNextURL)
afterDelay: 0];
return;
}
if ([_outputPath isEqual: @"-"])
_output = [OFStdOut copy];
else {
if (!_continue && !_force && [[OFFileManager defaultManager]
fileExistsAtPath: _currentFileName]) {
[OFStdErr writeLine:
OF_LOCALIZED(@"output_already_exists",
@"%[prog]: File %[filename] already exists!",
@"prog", [OFApplication programName],
@"filename", _currentFileName)];
_errorCode = 1;
goto next;
}
@try {
OFString *mode =
(response.statusCode == 206 ? @"a" : @"w");
_output = [[OFFile alloc] initWithPath: _currentFileName
mode: mode];
} @catch (OFOpenItemFailedException *e) {
[OFStdErr writeLine:
OF_LOCALIZED(@"failed_to_open_output",
@"%[prog]: Failed to open file %[filename]: "
@"%[exception]",
@"prog", [OFApplication programName],
@"filename", _currentFileName,
@"exception", e)];
|