272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
-
+
-
-
+
+
-
-
+
+
-
+
|
[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];
name = name.stringByDeletingEnclosingWhitespaces;
value = [header substringWithRange:
of_range(pos + 1, [header length] - pos - 1)];
value = [value stringByDeletingEnclosingWhitespaces];
of_range(pos + 1, header.length - pos - 1)];
value = value.stringByDeletingEnclosingWhitespaces;
[_clientHeaders setObject: value
forKey: name];
}
- (void)setBody: (OFString *)path
{
uintmax_t bodySize;
[_body release];
_body = [[OFFile alloc] initWithPath: path
mode: @"r"];
bodySize = [[[OFFileManager defaultManager]
attributesOfItemAtPath: path] fileSize];
bodySize = [[OFFileManager defaultManager] attributesOfItemAtPath: path]
.fileSize;
[_clientHeaders setObject: [OFString stringWithFormat: @"%ju", bodySize]
forKey: @"Content-Length"];
}
- (void)setMethod: (OFString *)method
{
void *pool = objc_autoreleasePoolPush();
method = [method uppercaseString];
method = method.uppercaseString;
if ([method isEqual: @"GET"])
_method = OF_HTTP_REQUEST_METHOD_GET;
else if ([method isEqual: @"HEAD"])
_method = OF_HTTP_REQUEST_METHOD_HEAD;
else if ([method isEqual: @"POST"])
_method = OF_HTTP_REQUEST_METHOD_POST;
|
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
|
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
|
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
-
+
-
+
-
+
-
+
|
{ '\0', nil, 0, NULL, NULL }
};
OFOptionsParser *optionsParser;
of_unichar_t option;
#ifdef OF_HAVE_SANDBOX
OFSandbox *sandbox = [OFSandbox sandbox];
[sandbox setAllowsStdIO: true];
[sandbox setAllowsReadingFiles: true];
[sandbox setAllowsWritingFiles: true];
[sandbox setAllowsCreatingFiles: true];
[sandbox setAllowsIPSockets: true];
[sandbox setAllowsDNS: true];
[sandbox setAllowsUserDatabaseReading: true];
[sandbox setAllowsTTY: true];
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 setAllowsUnveil: true];
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]];
self.body = optionsParser.argument;
break;
case 'h':
help(of_stdout, true, 0);
break;
case 'H':
[self addHeader: [optionsParser argument]];
[self addHeader: optionsParser.argument];
break;
case 'm':
[self setMethod: [optionsParser argument]];
self.method = optionsParser.argument;
break;
case 'P':
[self setProxy: [optionsParser argument]];
self.proxy = optionsParser.argument;
break;
case ':':
if ([optionsParser lastLongOption] != nil)
if (optionsParser.lastLongOption != nil)
[of_stderr writeLine:
OF_LOCALIZED(@"long_argument_missing",
@"%[prog]: Argument for option --%[opt] "
@"missing"
@"prog", [OFApplication programName],
@"opt", [optionsParser lastLongOption])];
@"opt", optionsParser.lastLongOption)];
else {
OFString *optStr = [OFString
stringWithFormat: @"%c",
[optionsParser lastOption]];
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])];
@"opt", optionsParser.lastLongOption)];
[OFApplication terminateWithStatus: 1];
break;
case '?':
if ([optionsParser lastLongOption] != nil)
if (optionsParser.lastLongOption != nil)
[of_stderr writeLine:
OF_LOCALIZED(@"unknown_long_option",
@"%[prog]: Unknown option: --%[opt]",
@"prog", [OFApplication programName],
@"opt", [optionsParser lastLongOption])];
@"opt", optionsParser.lastLongOption)];
else {
OFString *optStr = [OFString
stringWithFormat: @"%c",
[optionsParser lastOption]];
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: @"wc"];
/* In case we use ObjOpenSSL for https later */
[sandbox unveilPath: @"/etc/ssl"
permissions: @"r"];
[sandbox setAllowsUnveil: false];
sandbox.allowsUnveil = false;
[OFApplication activateSandbox: sandbox];
#endif
_outputPath = [outputPath copy];
_URLs = [[optionsParser remainingArguments] retain];
_URLs = [optionsParser.remainingArguments copy];
if ([_URLs count] < 1)
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 && [_URLs count] > 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 setInsecureRedirectsAllowed: true];
_HTTPClient.insecureRedirectsAllowed = true;
[self performSelector: @selector(downloadNextURL)
afterDelay: 0];
}
- (void)client: (OFHTTPClient *)client
didCreateSocket: (OFTCPSocket *)sock
request: (OFHTTPRequest *)request
{
if (_insecure && [sock respondsToSelector:
@selector(setCertificateVerificationEnabled:)])
((id <OFTLSSocket>)sock).certificateVerificationEnabled = false;
}
- (void)client: (OFHTTPClient *)client
wantsRequestBody: (OFStream *)body
request: (OFHTTPRequest *)request
{
/* TODO: Do asynchronously and print status */
while (![_body isAtEndOfStream]) {
while (!_body.atEndOfStream) {
char buffer[4096];
size_t length;
length = [_body readIntoBuffer: buffer
length: 4096];
[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];
response.headers;
OFEnumerator *keyEnumerator = [headers keyEnumerator];
OFEnumerator *objectEnumerator =
OFEnumerator *objectEnumerator = [headers objectEnumerator];
[headers objectEnumerator];
OFString *key, *object;
while ((key = [keyEnumerator nextObject]) != nil &&
(object = [objectEnumerator nextObject]) != nil)
[of_stdout writeFormat: @" %@: %@\n",
key, object];
objc_autoreleasePoolPop(pool);
}
if (!_quiet)
[of_stdout writeFormat: @"☇ %@", [URL string]];
[of_stdout writeFormat: @"☇ %@", URL.string];
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_failed_resolve_host_failed",
@"%[prog]: Failed to download <%[url]>!\n"
@" Failed to resolve host: %[exception]",
@"prog", [OFApplication programName],
@"url", [[request URL] string],
@"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],
@"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])];
@"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 "
|
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
|
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
|
-
+
-
+
|
@"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],
@"url", request.URL.string,
@"error", error,
@"exception", e)];
} else if ([e isKindOfClass: [OFHTTPRequestFailedException class]]) {
[of_stderr writeLine: OF_LOCALIZED(@"download_failed",
@"%[prog]: Failed to download <%[url]>!",
@"prog", [OFApplication programName],
@"url", [[request URL] string])];
@"url", request.URL.string)];
} else
@throw e;
[self performSelector: @selector(downloadNextURL)
afterDelay: 0];
}
|
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
|
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
|
-
+
-
+
-
+
|
_errorCode = 1;
goto next;
}
@try {
OFString *mode =
([response statusCode] == 206 ? @"a" : @"w");
(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)];
_errorCode = 1;
goto next;
}
}
if (!_quiet) {
_progressBar = [[ProgressBar alloc]
initWithLength: _length
resumedFrom: _resumedFrom];
[_progressBar setReceived: _received];
_progressBar.received = _received;
[_progressBar draw];
}
[_currentFileName release];
_currentFileName = nil;
[response setDelegate: self];
response.delegate = self;
[response asyncReadIntoBuffer: _buffer
length: [OFSystemInfo pageSize]];
return;
next:
[_currentFileName release];
_currentFileName = nil;
|
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
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
|
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
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
|
-
+
-
-
+
-
+
-
-
+
+
-
+
-
+
-
-
+
+
-
-
+
+
|
_length = -1;
_received = _resumedFrom = 0;
if (_output != of_stdout)
[_output release];
_output = nil;
if (_URLIndex >= [_URLs count])
if (_URLIndex >= _URLs.count)
[OFApplication terminateWithStatus: _errorCode];
@try {
URLString = [_URLs objectAtIndex: _URLIndex++];
URL = [OFURL URLWithString: URLString];
} @catch (OFInvalidFormatException *e) {
[of_stderr writeLine: OF_LOCALIZED(@"invalid_url",
@"%[prog]: Invalid URL: <%[url]>!",
@"prog", [OFApplication programName],
@"url", URLString)];
_errorCode = 1;
goto next;
}
if (![[URL scheme] isEqual: @"http"] &&
![[URL scheme] isEqual: @"https"]) {
if (![URL.scheme isEqual: @"http"] && ![URL.scheme isEqual: @"https"]) {
[of_stderr writeLine: OF_LOCALIZED(@"invalid_scheme",
@"%[prog]: Invalid scheme: <%[url]>!",
@"prog", [OFApplication programName],
@"url", URLString)];
_errorCode = 1;
goto next;
}
clientHeaders = [[_clientHeaders mutableCopy] autorelease];
if (_detectFileName && !_detectedFileName) {
if (!_quiet)
[of_stdout writeFormat: @"⠒ %@", [URL string]];
[of_stdout writeFormat: @"⠒ %@", URL.string];
request = [OFHTTPRequest requestWithURL: URL];
[request setHeaders: clientHeaders];
[request setMethod: OF_HTTP_REQUEST_METHOD_HEAD];
request.headers = clientHeaders;
request.method = OF_HTTP_REQUEST_METHOD_HEAD;
_detectFileNameRequest = true;
[_HTTPClient asyncPerformRequest: request];
return;
}
[_currentFileName release];
_currentFileName = nil;
_detectedFileName = false;
if (!_quiet)
[of_stdout writeFormat: @"⇣ %@", [URL string]];
[of_stdout writeFormat: @"⇣ %@", URL.string];
if (_outputPath != nil)
_currentFileName = [_outputPath copy];
if (_currentFileName == nil)
_currentFileName = [[[URL path] lastPathComponent] copy];
_currentFileName = [URL.path.lastPathComponent copy];
if (_continue) {
@try {
uintmax_t size = [[[OFFileManager defaultManager]
attributesOfItemAtPath: _currentFileName] fileSize];
uintmax_t size = [[OFFileManager defaultManager]
attributesOfItemAtPath: _currentFileName].fileSize;
OFString *range;
if (size > INTMAX_MAX)
@throw [OFOutOfRangeException exception];
_resumedFrom = (intmax_t)size;
range = [OFString stringWithFormat: @"bytes=%jd-",
_resumedFrom];
[clientHeaders setObject: range
forKey: @"Range"];
} @catch (OFRetrieveItemAttributesFailedException *e) {
}
}
request = [OFHTTPRequest requestWithURL: URL];
[request setHeaders: clientHeaders];
[request setMethod: _method];
request.headers = clientHeaders;
request.method = _method;
_detectFileNameRequest = false;
[_HTTPClient asyncPerformRequest: request];
return;
next:
[self performSelector: @selector(downloadNextURL)
afterDelay: 0];
}
@end
|