Differences From Artifact [09db0decaa]:
- File src/OFURL.m — part of check-in [2f555742c0] at 2017-11-05 19:53:31 on branch trunk — Make -[stringByURLEncoding] take an OFCharacterSet (user: js, size: 17780) [annotate] [blame] [check-ins using]
To Artifact [674edf5266]:
- File
src/OFURL.m
— part of check-in
[a15b403a11]
at
2017-11-06 23:01:57
on branch trunk
— OFURL: Store the URL-encoded version internally
This allows retrieving it how it was originally stored, which is useful
for example for query strings, as these often URL-encode allowed
characters in order to allow pairs of the form foo=bar&bar=qux. (user: js, size: 17901) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
344 345 346 347 348 349 350 | if (strncmp(tmp, "://", 3) != 0) @throw [OFInvalidFormatException exception]; for (tmp2 = UTF8String; tmp2 < tmp; tmp2++) *tmp2 = of_ascii_tolower(*tmp2); | > | | < | | | | | | < > | < < < < > | < | | | | > | < | 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 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 | if (strncmp(tmp, "://", 3) != 0) @throw [OFInvalidFormatException exception]; for (tmp2 = UTF8String; tmp2 < tmp; tmp2++) *tmp2 = of_ascii_tolower(*tmp2); _URLEncodedScheme = [[OFString alloc] initWithUTF8String: UTF8String length: tmp - UTF8String]; UTF8String = tmp + 3; if ((tmp = strchr(UTF8String, '/')) != NULL) { *tmp = '\0'; tmp++; } if ((tmp2 = strchr(UTF8String, '@')) != NULL) { char *tmp3; *tmp2 = '\0'; tmp2++; if ((tmp3 = strchr(UTF8String, ':')) != NULL) { *tmp3 = '\0'; tmp3++; _URLEncodedUser = [[OFString alloc] initWithUTF8String: UTF8String]; _URLEncodedPassword = [[OFString alloc] initWithUTF8String: tmp3]; } else _URLEncodedUser = [[OFString alloc] initWithUTF8String: UTF8String]; UTF8String = tmp2; } if ((tmp2 = strchr(UTF8String, ':')) != NULL) { OFString *portString; *tmp2 = '\0'; tmp2++; _URLEncodedHost = [[OFString alloc] initWithUTF8String: UTF8String]; portString = [OFString stringWithUTF8String: tmp2]; if ([portString decimalValue] > 65535) @throw [OFInvalidFormatException exception]; _port = [[OFNumber alloc] initWithUInt16: (uint16_t)[portString decimalValue]]; } else _URLEncodedHost = [[OFString alloc] initWithUTF8String: UTF8String]; if ((UTF8String = tmp) != NULL) { if ((tmp = strchr(UTF8String, '#')) != NULL) { *tmp = '\0'; _URLEncodedFragment = [[OFString alloc] initWithUTF8String: tmp + 1]; } if ((tmp = strchr(UTF8String, '?')) != NULL) { *tmp = '\0'; _URLEncodedQuery = [[OFString alloc] initWithUTF8String: tmp + 1]; } UTF8String--; *UTF8String = '/'; _URLEncodedPath = [[OFString alloc] initWithUTF8String: UTF8String]; } objc_autoreleasePoolPop(pool); } @catch (id e) { [self release]; @throw e; } @finally { |
︙ | ︙ | |||
447 448 449 450 451 452 453 | self = [super init]; @try { void *pool = objc_autoreleasePoolPush(); char *tmp; | | | | | > | < > | < > | < | < | > | | < | > | | 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 | self = [super init]; @try { void *pool = objc_autoreleasePoolPush(); char *tmp; _URLEncodedScheme = [URL->_URLEncodedScheme copy]; _URLEncodedHost = [URL->_URLEncodedHost copy]; _port = [URL->_port copy]; _URLEncodedUser = [URL->_URLEncodedUser copy]; _URLEncodedPassword = [URL->_URLEncodedPassword copy]; if ((UTF8String2 = of_strdup([string UTF8String])) == NULL) @throw [OFOutOfMemoryException exceptionWithRequestedSize: [string UTF8StringLength]]; UTF8String = UTF8String2; if ((tmp = strchr(UTF8String, '#')) != NULL) { *tmp = '\0'; _URLEncodedFragment = [[OFString alloc] initWithUTF8String: tmp + 1]; } if ((tmp = strchr(UTF8String, '?')) != NULL) { *tmp = '\0'; _URLEncodedQuery = [[OFString alloc] initWithUTF8String: tmp + 1]; } if (*UTF8String == '/') _URLEncodedPath = [[OFString alloc] initWithUTF8String: UTF8String]; else { OFString *path, *s; path = [OFString stringWithUTF8String: UTF8String]; if ([URL->_URLEncodedPath hasSuffix: @"/"]) s = [URL->_URLEncodedPath stringByAppendingString: path]; else s = [OFString stringWithFormat: @"%@/../%@", URL->_URLEncodedPath, path]; _URLEncodedPath = [[s stringByStandardizingURLPath] copy]; } objc_autoreleasePoolPop(pool); } @catch (id e) { [self release]; @throw e; } @finally { |
︙ | ︙ | |||
576 577 578 579 580 581 582 | } return self; } - (void)dealloc { | | | | | | | | > | > | > | > | > | > | > | | | | | | | | | | < | | < | | < | < | | | < | | | | > > | > | > > | < < | 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 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 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 | } return self; } - (void)dealloc { [_URLEncodedScheme release]; [_URLEncodedHost release]; [_port release]; [_URLEncodedUser release]; [_URLEncodedPassword release]; [_URLEncodedPath release]; [_URLEncodedQuery release]; [_URLEncodedFragment release]; [super dealloc]; } - (bool)isEqual: (id)object { OFURL *URL; if (![object isKindOfClass: [OFURL class]]) return false; URL = object; if (URL->_URLEncodedScheme != _URLEncodedScheme && ![URL->_URLEncodedScheme isEqual: _URLEncodedScheme]) return false; if (URL->_URLEncodedHost != _URLEncodedHost && ![URL->_URLEncodedHost isEqual: _URLEncodedHost]) return false; if (URL->_port != _port && ![URL->_port isEqual: _port]) return false; if (URL->_URLEncodedUser != _URLEncodedUser && ![URL->_URLEncodedUser isEqual: _URLEncodedUser]) return false; if (URL->_URLEncodedPassword != _URLEncodedPassword && ![URL->_URLEncodedPassword isEqual: _URLEncodedPassword]) return false; if (URL->_URLEncodedPath != _URLEncodedPath && ![URL->_URLEncodedPath isEqual: _URLEncodedPath]) return false; if (URL->_URLEncodedQuery != _URLEncodedQuery && ![URL->_URLEncodedQuery isEqual: _URLEncodedQuery]) return false; if (URL->_URLEncodedFragment != _URLEncodedFragment && ![URL->_URLEncodedFragment isEqual: _URLEncodedFragment]) return false; return true; } - (uint32_t)hash { uint32_t hash; OF_HASH_INIT(hash); OF_HASH_ADD_HASH(hash, [_URLEncodedScheme hash]); OF_HASH_ADD_HASH(hash, [_URLEncodedHost hash]); OF_HASH_ADD_HASH(hash, [_port hash]); OF_HASH_ADD_HASH(hash, [_URLEncodedUser hash]); OF_HASH_ADD_HASH(hash, [_URLEncodedPassword hash]); OF_HASH_ADD_HASH(hash, [_URLEncodedPath hash]); OF_HASH_ADD_HASH(hash, [_URLEncodedQuery hash]); OF_HASH_ADD_HASH(hash, [_URLEncodedFragment hash]); OF_HASH_FINALIZE(hash); return hash; } - (OFString *)scheme { return [_URLEncodedScheme stringByURLDecoding]; } - (OFString *)URLEncodedScheme { return _URLEncodedScheme; } - (OFString *)host { return [_URLEncodedHost stringByURLDecoding]; } - (OFString *)URLEncodedHost { return _URLEncodedHost; } - (OFNumber *)port { return _port; } - (OFString *)user { return [_URLEncodedUser stringByURLDecoding]; } - (OFString *)URLEncodedUser { return _URLEncodedUser; } - (OFString *)password { return [_URLEncodedPassword stringByURLDecoding]; } - (OFString *)URLEncodedPassword { return _URLEncodedPassword; } - (OFString *)path { return [_URLEncodedPath stringByURLDecoding]; } - (OFString *)URLEncodedPath { return _URLEncodedPath; } - (OFArray *)pathComponents { return [[self path] componentsSeparatedByString: @"/"]; } - (OFString *)lastPathComponent { void *pool = objc_autoreleasePoolPush(); OFString *path = [self path]; const char *UTF8String, *lastComponent; size_t length; OFString *ret; if (path == nil) { objc_autoreleasePoolPop(pool); return nil; } if ([path isEqual: @"/"]) { objc_autoreleasePoolPop(pool); return @""; } if ([path hasSuffix: @"/"]) path = [path substringWithRange: of_range(0, [path length] - 1)]; UTF8String = lastComponent = [path UTF8String]; length = [path UTF8StringLength]; |
︙ | ︙ | |||
744 745 746 747 748 749 750 | objc_autoreleasePoolPop(pool); return [ret autorelease]; } - (OFString *)query { | | | < | < | | | | | | | | < | | | < | | | | | | | | | | | < < | | | | 745 746 747 748 749 750 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 | objc_autoreleasePoolPop(pool); return [ret autorelease]; } - (OFString *)query { return [_URLEncodedQuery stringByURLDecoding]; } - (OFString *)URLEncodedQuery { return _URLEncodedQuery; } - (OFString *)fragment { return [_URLEncodedFragment stringByURLDecoding]; } - (OFString *)URLEncodedFragment { return _URLEncodedFragment; } - (id)copy { return [self retain]; } - (id)mutableCopy { OFMutableURL *copy = [[OFMutableURL alloc] init]; @try { [copy setURLEncodedScheme: _URLEncodedScheme]; [copy setURLEncodedHost: _URLEncodedHost]; [copy setPort: _port]; [copy setURLEncodedUser: _URLEncodedUser]; [copy setURLEncodedPassword: _URLEncodedPassword]; [copy setURLEncodedPath: _URLEncodedPath]; [copy setURLEncodedQuery: _URLEncodedQuery]; [copy setURLEncodedFragment: _URLEncodedFragment]; } @catch (id e) { [copy release]; @throw e; } return copy; } - (OFString *)string { OFMutableString *ret = [OFMutableString string]; [ret appendFormat: @"%@://", _URLEncodedScheme]; if (_URLEncodedUser != nil && _URLEncodedPassword != nil) [ret appendFormat: @"%@:%@@", _URLEncodedUser, _URLEncodedPassword]; else if (_URLEncodedUser != nil) [ret appendFormat: @"%@@", _URLEncodedUser]; if (_URLEncodedHost != nil) [ret appendString: _URLEncodedHost]; if (_port != nil) [ret appendFormat: @":%@", _port]; if (_URLEncodedPath != nil) { if (![_URLEncodedPath hasPrefix: @"/"]) @throw [OFInvalidFormatException exception]; [ret appendString: _URLEncodedPath]; } if (_URLEncodedQuery != nil) [ret appendFormat: @"?%@", _URLEncodedQuery]; if (_URLEncodedFragment != nil) [ret appendFormat: @"#%@", _URLEncodedFragment]; [ret makeImmutable]; return ret; } - (OFString *)fileSystemRepresentation { void *pool = objc_autoreleasePoolPush(); OFString *path; if (![_URLEncodedScheme isEqual: @"file"]) @throw [OFInvalidArgumentException exception]; if (![_URLEncodedPath hasPrefix: @"/"]) @throw [OFInvalidFormatException exception]; path = [self path]; if ([path hasSuffix: @"/"]) path = [path substringWithRange: of_range(0, [path length] - 1)]; #ifndef OF_PATH_STARTS_WITH_SLASH path = [path substringWithRange: of_range(1, [path length] - 1)]; |
︙ | ︙ |