Differences From Artifact [3ef2bcb62b]:
- File
utils/ofhttp/OFHTTP.m
— part of check-in
[6dff0f5922]
at
2017-01-07 02:34:39
on branch trunk
— Always use "." for the decimal point
This is achieved by replacing the locale's decimal point with "." after
formatting and replacing "." with the locale's decimal point before
parsing.To still use the decimal point from the locale for formatting, the new
flag "," is introduced to formats. This is useful for just printing a
string to the user that is not saved to a file or sent via a network.While this is an ugly hack, there is no better way to do this other than
implementing the functionality of printf and strtod myself, as POSIX
does not specify versions of these that take a locale as an argument.
While this is a lot of work and error-prone, I will most likely end up
doing this eventually.This commit also enables the locale in OFApplication to notice when
things break. As a nice side effect, ofhttp now uses the locale's
decimal point in its user interface. (user: js, size: 20848) [annotate] [blame] [check-ins using]
To Artifact [6d271e2d00]:
- File
utils/ofhttp/OFHTTP.m
— part of check-in
[06bcb21fc7]
at
2017-01-09 06:26:04
on branch trunk
— Add OFLocalization
This singleton gives access to all things locale, including the ability
to get localized strings.This also adds the OF_LOCALIZED() macro. Its first argument is an ID for
the string to be localized and its second argument is the fallback
string to be used if it cannot retrieve the localized string. Following
that are variable name / value pairs to be replaced in the localized
string.Getting translated strings is not implemented yet: Instead, it always
uses the fallback string.This also switches ofhttp to localized strings. (user: js, size: 23924) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #import "OFHTTPRequest.h" #import "OFHTTPResponse.h" #import "OFOptionsParser.h" #import "OFStdIOStream.h" #import "OFSystemInfo.h" #import "OFTCPSocket.h" #import "OFURL.h" #import "OFAddressTranslationFailedException.h" #import "OFConnectionFailedException.h" #import "OFHTTPRequestFailedException.h" #import "OFInvalidFormatException.h" #import "OFInvalidServerReplyException.h" #import "OFOpenItemFailedException.h" | > | 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #import "OFHTTPRequest.h" #import "OFHTTPResponse.h" #import "OFOptionsParser.h" #import "OFStdIOStream.h" #import "OFSystemInfo.h" #import "OFTCPSocket.h" #import "OFURL.h" #import "OFLocalization.h" #import "OFAddressTranslationFailedException.h" #import "OFConnectionFailedException.h" #import "OFHTTPRequestFailedException.h" #import "OFInvalidFormatException.h" #import "OFInvalidServerReplyException.h" #import "OFOpenItemFailedException.h" |
︙ | ︙ | |||
68 69 70 71 72 73 74 | @end OF_APPLICATION_DELEGATE(OFHTTP) static void help(OFStream *stream, bool full, int status) { | | > | | | | 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | @end OF_APPLICATION_DELEGATE(OFHTTP) static void help(OFStream *stream, bool full, int status) { [of_stderr writeString: OF_LOCALIZED(@"usage", @"Usage: %[prog] -[cehHmoOPqv] url1 [url2 ...]\n", @"prog", [OFApplication programName])]; if (full) [stream writeString: OF_LOCALIZED(@"full_usage", @"\nOptions:\n " @"-b --body " @" Specify the file to send as body\n " @"-c --continue " @" Continue download of existing file\n " @"-f --force " @" Force / overwrite existing file\n " |
︙ | ︙ | |||
96 97 98 99 100 101 102 | @"-O --detect-filename" @" Do a HEAD request to detect the file name\n " @"-P --proxy " @" Specify SOCKS5 proxy\n " @"-q --quiet " @" Quiet mode (no output, except errors)\n " @"-v --verbose " | | | 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | @"-O --detect-filename" @" Do a HEAD request to detect the file name\n " @"-P --proxy " @" Specify SOCKS5 proxy\n " @"-q --quiet " @" Quiet mode (no output, except errors)\n " @"-v --verbose " @" Verbose mode (print headers)\n")]; [OFApplication terminateWithStatus: status]; } @implementation OFHTTP - init { |
︙ | ︙ | |||
131 132 133 134 135 136 137 | - (void)addHeader: (OFString*)header { size_t pos = [header rangeOfString: @":"].location; OFString *name, *value; if (pos == OF_NOT_FOUND) { | | | | | 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | - (void)addHeader: (OFString*)header { size_t pos = [header rangeOfString: @":"].location; OFString *name, *value; if (pos == OF_NOT_FOUND) { [of_stderr writeString: OF_LOCALIZED(@"invalid_input_header", @"%[prog]: Headers must to be in format name:value!\n", @"prog", [OFApplication programName])]; [OFApplication terminateWithStatus: 1]; } name = [header substringWithRange: of_range(0, pos)]; name = [name stringByDeletingEnclosingWhitespaces]; value = [header substringWithRange: |
︙ | ︙ | |||
181 182 183 184 185 186 187 | else if ([method isEqual: @"PUT"]) _method = OF_HTTP_REQUEST_METHOD_PUT; else if ([method isEqual: @"DELETE"]) _method = OF_HTTP_REQUEST_METHOD_DELETE; else if ([method isEqual: @"TRACE"]) _method = OF_HTTP_REQUEST_METHOD_TRACE; else { | > | | | | 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | else if ([method isEqual: @"PUT"]) _method = OF_HTTP_REQUEST_METHOD_PUT; else if ([method isEqual: @"DELETE"]) _method = OF_HTTP_REQUEST_METHOD_DELETE; else if ([method isEqual: @"TRACE"]) _method = OF_HTTP_REQUEST_METHOD_TRACE; else { [of_stderr writeString: OF_LOCALIZED(@"invalid_input_method", @"%[prog]: Invalid request method %[method]!\n", @"prog", [OFApplication programName], @"method", method)]; [OFApplication terminateWithStatus: 1]; } objc_autoreleasePoolPop(pool); } - (void)setProxy: (OFString*)proxy |
︙ | ︙ | |||
212 213 214 215 216 217 218 | if (port > UINT16_MAX) @throw [OFOutOfRangeException exception]; [OFTCPSocket setSOCKS5Host: host]; [OFTCPSocket setSOCKS5Port: (uint16_t)port]; } @catch (OFInvalidFormatException *e) { | | | | | 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | if (port > UINT16_MAX) @throw [OFOutOfRangeException exception]; [OFTCPSocket setSOCKS5Host: host]; [OFTCPSocket setSOCKS5Port: (uint16_t)port]; } @catch (OFInvalidFormatException *e) { [of_stderr writeString: OF_LOCALIZED(@"invalid_input_proxy", @"%[prog]: Proxy must to be in format host:port!\n", @"prog", [OFApplication programName])]; [OFApplication terminateWithStatus: 1]; } } - (void)applicationDidFinishLaunching { OFString *outputPath; |
︙ | ︙ | |||
259 260 261 262 263 264 265 | [self setMethod: [optionsParser argument]]; break; case 'P': [self setProxy: [optionsParser argument]]; break; case ':': if ([optionsParser lastLongOption] != nil) | | > | > | | | > > > | > | > | | > | | > | | | > | | | | > > > | > | | | > > | | | | > | | | | 262 263 264 265 266 267 268 269 270 271 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 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 340 341 342 343 344 345 346 347 | [self setMethod: [optionsParser argument]]; break; case 'P': [self setProxy: [optionsParser argument]]; break; case ':': if ([optionsParser lastLongOption] != nil) [of_stderr writeString: OF_LOCALIZED(@"long_argument_missing", @"%[prog]: Argument for option --%[opt] " "missing\n" @"prog", [OFApplication programName], @"opt", [optionsParser lastLongOption])]; else { OFString *optStr = [OFString stringWithFormat: @"%c", [optionsParser lastOption]]; [of_stderr writeString: OF_LOCALIZED(@"argument_missing", @"%[prog]: Argument for option -%[opt] " "missing\n", @"prog", [OFApplication programName], @"opt", optStr)]; } [OFApplication terminateWithStatus: 1]; break; case '=': [of_stderr writeString: OF_LOCALIZED(@"takes_no_argument", @"%[prog]: Option --%[opt] takes no argument\n", @"prog", [OFApplication programName], @"opt", [optionsParser lastLongOption])]; [OFApplication terminateWithStatus: 1]; break; case '?': if ([optionsParser lastLongOption] != nil) [of_stderr writeString: OF_LOCALIZED(@"unknown_long_option", @"%[prog]: Unknown option: --%[opt]\n", @"prog", [OFApplication programName], @"opt", [optionsParser lastLongOption])]; else { OFString *optStr = [OFString stringWithFormat: @"%c", [optionsParser lastOption]]; [of_stderr writeString: OF_LOCALIZED(@"unknown_option", @"%[prog]: Unknown option: -%[opt]\n", @"prog", [OFApplication programName], @"opt", optStr)]; } [OFApplication terminateWithStatus: 1]; break; } } _outputPath = [outputPath copy]; _URLs = [[optionsParser remainingArguments] retain]; if ([_URLs count] < 1) help(of_stderr, false, 1); if (_quiet && _verbose) { [of_stderr writeString: OF_LOCALIZED(@"quiet_xor_verbose", @"%[prog]: -q / --quiet and -v / --verbose are mutually " @"exclusive!\n", @"prog", [OFApplication programName])]; [OFApplication terminateWithStatus: 1]; } if (_outputPath != nil && [_URLs count] > 1) { [of_stderr writeString: OF_LOCALIZED(@"output_only_with_one_url", @"%[prog]: Cannot use -o / --output when more than one URL " @"has been specified!\n", @"prog", [OFApplication programName])]; [OFApplication terminateWithStatus: 1]; } [self performSelector: @selector(downloadNextURL) afterDelay: 0]; } |
︙ | ︙ | |||
363 364 365 366 367 368 369 | @try { response = [_HTTPClient performRequest: request]; } @catch (OFAddressTranslationFailedException *e) { if (!_quiet) [of_stdout writeString: @"\n"]; | | > > | | | > | > > | | | > | > > | | | > | | | | | > > | > > | > > | | > > | | | > > > | | | | 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 | @try { response = [_HTTPClient performRequest: request]; } @catch (OFAddressTranslationFailedException *e) { if (!_quiet) [of_stdout writeString: @"\n"]; [of_stderr writeString: OF_LOCALIZED(@"download_failed_address_translation", @"%[prog]: Failed to download <%[url]>!\n" @" Address translation failed: %[exception]\n", @"prog", [OFApplication programName], @"url", [[request URL] string], @"exception", e)]; } @catch (OFConnectionFailedException *e) { if (!_quiet) [of_stdout writeString: @"\n"]; [of_stderr writeString: OF_LOCALIZED(@"download_failed_connection_failed", @"%[prog]: Failed to download <%[url]>!\n" @" Connection failed: %[exception]\n", @"prog", [OFApplication programName], @"url", [[request URL] string], @"exception", e)]; } @catch (OFInvalidServerReplyException *e) { if (!_quiet) [of_stdout writeString: @"\n"]; [of_stderr writeString: OF_LOCALIZED(@"download_failed_invalid_server_reply", @"%[prog]: Failed to download <%[url]>!\n" @" Invalid server reply!\n", @"prog", [OFApplication programName], @"url", [[request URL] string])]; } @catch (OFUnsupportedProtocolException *e) { if (!_quiet) [of_stdout writeString: @"\n"]; [of_stderr writeString: OF_LOCALIZED(@"no_ssl_library", @"%[prog]: No SSL library loaded!\n" @" In order to download via https, you need to preload an " @"SSL library for ObjFW\n" "such as ObjOpenSSL!\n", @"prog", [OFApplication programName])]; } @catch (OFReadOrWriteFailedException *e) { 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 writeString: OF_LOCALIZED(@"download_failed_read_or_write_failed", @"%[prog]: Failed to download <%[url]>!\n" @" %[error]: %[exception]\n", @"prog", [OFApplication programName], @"url", [[request URL] string], @"error", error, @"exception", e)]; } @catch (OFHTTPRequestFailedException *e) { if (!_quiet) [of_stdout writeFormat: @" ➜ %d\n", [[e response] statusCode]]; [of_stderr writeString: OF_LOCALIZED(@"download_failed", @"%[prog]: Failed to download <%[url]>!\n", @"prog", [OFApplication programName], @"url", [[request URL] string])]; } if (!_quiet && response != nil) [of_stdout writeFormat: @" ➜ %d\n", [response statusCode]]; return response; } |
︙ | ︙ | |||
560 561 562 563 564 565 566 | [_progressBar release]; _progressBar = nil; if (!_quiet) [of_stdout writeString: @"\n Error!\n"]; URL = [_URLs objectAtIndex: _URLIndex - 1]; | | > > | > > | > | 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 | [_progressBar release]; _progressBar = nil; if (!_quiet) [of_stdout writeString: @"\n Error!\n"]; URL = [_URLs objectAtIndex: _URLIndex - 1]; [of_stderr writeString: OF_LOCALIZED(@"download_failed_exception", @"%[prog]: Failed to download <%[url]>: %[exception]\n", @"prog", [OFApplication programName], @"url", URL, @"exception", e)]; _errorCode = 1; goto next; } _received += length; [_output writeBuffer: buffer length: length]; [_progressBar setReceived: _received]; if ([response isAtEndOfStream] || (_length >= 0 && _received >= _length)) { [_progressBar stop]; [_progressBar draw]; [_progressBar release]; _progressBar = nil; if (!_quiet) [of_stdout writeString: OF_LOCALIZED(@"download_done", @"\n Done!\n")]; goto next; } return true; next: |
︙ | ︙ | |||
620 621 622 623 624 625 626 | if (_URLIndex >= [_URLs count]) [OFApplication terminateWithStatus: _errorCode]; @try { URLString = [_URLs objectAtIndex: _URLIndex++]; URL = [OFURL URLWithString: URLString]; } @catch (OFInvalidFormatException *e) { | > | | | > | | | | 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 | if (_URLIndex >= [_URLs count]) [OFApplication terminateWithStatus: _errorCode]; @try { URLString = [_URLs objectAtIndex: _URLIndex++]; URL = [OFURL URLWithString: URLString]; } @catch (OFInvalidFormatException *e) { [of_stderr writeString: OF_LOCALIZED(@"invalid_url", @"%[prog]: Invalid URL: <%[url]>!\n", @"prog", [OFApplication programName], @"url", URLString)]; _errorCode = 1; goto next; } if (![[URL scheme] isEqual: @"http"] && ![[URL scheme] isEqual: @"https"]) { [of_stderr writeString: OF_LOCALIZED(@"invalid_scheme", @"%[prog]: Invalid scheme: <%[scheme]:>!\n", @"prog", [OFApplication programName], @"scheme", URLString)]; _errorCode = 1; goto next; } clientHeaders = [[_clientHeaders mutableCopy] autorelease]; |
︙ | ︙ | |||
704 705 706 707 708 709 710 | type = [headers objectForKey: @"Content-Type"]; if (lengthString != nil) _length = [lengthString decimalValue]; if (!_quiet) { if (type == nil) | | | | > > > | | > > > | | > > > | | > > > > | | < > > > > | > > > > > > > | | > | | > | > | > | > > | 751 752 753 754 755 756 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 | type = [headers objectForKey: @"Content-Type"]; if (lengthString != nil) _length = [lengthString decimalValue]; if (!_quiet) { if (type == nil) type = OF_LOCALIZED(@"type_unknown", @"unknown"); if (_length >= 0) { if (_resumedFrom + _length >= GIBIBYTE) { lengthString = [OFString stringWithFormat: @"%,.2f", (float)(_resumedFrom + _length) / GIBIBYTE]; lengthString = OF_LOCALIZED(@"size_gib", @"%[num] GiB", @"num", lengthString); } else if (_resumedFrom + _length >= MEBIBYTE) { lengthString = [OFString stringWithFormat: @"%,.2f", (float)(_resumedFrom + _length) / MEBIBYTE]; lengthString = OF_LOCALIZED(@"size_mib", @"%[num] MiB", @"num", lengthString); } else if (_resumedFrom + _length >= KIBIBYTE) { lengthString = [OFString stringWithFormat: @"%,.2f", (float)(_resumedFrom + _length) / KIBIBYTE]; lengthString = OF_LOCALIZED(@"size_kib", @"%[num] KiB", @"num", lengthString); } else { lengthString = [OFString stringWithFormat: @"%jd", _resumedFrom + _length]; lengthString = OF_LOCALIZED(@"size_bytes", @"%[num] bytes", @"num", lengthString); } } else lengthString = OF_LOCALIZED(@"size_unknown", @"unknown"); if (_verbose) { void *pool = objc_autoreleasePoolPush(); OFDictionary OF_GENERIC(OFString*, OFString*) *headers = [response headers]; OFEnumerator *keyEnumerator = [headers keyEnumerator]; OFEnumerator *objectEnumerator = [headers objectEnumerator]; OFString *key, *object; [of_stdout writeString: OF_LOCALIZED(@"info_name_nopad", @" Name: %[name]\n", @"name", fileName)]; while ((key = [keyEnumerator nextObject]) != nil && (object = [objectEnumerator nextObject]) != nil) [of_stdout writeFormat: @" %@: %@\n", key, object]; objc_autoreleasePoolPop(pool); } else { [of_stdout writeString: OF_LOCALIZED(@"info_name", @" Name: %[name]\n", @"name", fileName)]; [of_stdout writeString: OF_LOCALIZED(@"info_type", @" Type: %[type]\n", @"type", type)]; [of_stdout writeString: OF_LOCALIZED(@"info_size", @" Size: %[size]\n", @"size", lengthString)]; } } if ([_outputPath isEqual: @"-"]) _output = of_stdout; else { if (!_continue && !_force && [fileManager fileExistsAtPath: fileName]) { [of_stderr writeString: OF_LOCALIZED(@"ouput_already_exists", @"%[prog]: File %[filename] already exists!\n", @"prog", [OFApplication programName], @"filename", fileName)]; _errorCode = 1; goto next; } @try { OFString *mode = ([response statusCode] == 206 ? @"ab" : @"wb"); _output = [[OFFile alloc] initWithPath: fileName mode: mode]; } @catch (OFOpenItemFailedException *e) { [of_stderr writeString: OF_LOCALIZED(@"failed_to_open_output", @"%[prog]: Failed to open file %[filename]: " @"%[exception]\n", @"prog", [OFApplication programName], @"filename",fileName, @"exception", e)]; _errorCode = 1; goto next; } } if (!_quiet) { |
︙ | ︙ |