Differences From Artifact [bd23469364]:
- File
src/OFURI.m
— part of check-in
[bd6dd6dd00]
at
2022-09-29 21:53:01
on branch trunk
— OFURI: Make query items an array of pairs
With a dictionary the order is lost and keys cannot exist more than
once. (user: js, size: 27532) [annotate] [blame] [check-ins using]
To Artifact [91ee9c8850]:
- File src/OFURI.m — part of check-in [c75059a52f] at 2022-10-03 00:39:58 on branch trunk — OFURI: Rewrite parser to support URIs (user: js, size: 27897) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
427 428 429 430 431 432 433 434 | + (instancetype)fileURIWithPath: (OFString *)path isDirectory: (bool)isDirectory { return [[[self alloc] initFileURIWithPath: path isDirectory: isDirectory] autorelease]; } #endif | > > > > | > > > > > > > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > < < | | > | < < < < < < | | | > | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | < | < < < < < < | | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | < | < < < < < | < | < < < < < < < < < < < < < < < < < < < < < | < < | < < < < < < | < < | < < | < < < | < < < < < < < < < < < < < < < | | < < < | | > < < | 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 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 | + (instancetype)fileURIWithPath: (OFString *)path isDirectory: (bool)isDirectory { return [[[self alloc] initFileURIWithPath: path isDirectory: isDirectory] autorelease]; } #endif static void parseUserInfo(OFURI *self, const char *UTF8String, size_t length) { const char *colon; if ((colon = memchr(UTF8String, ':', length)) != NULL) { self->_percentEncodedUser = [[OFString alloc] initWithUTF8String: UTF8String length: colon - UTF8String]; self->_percentEncodedPassword = [[OFString alloc] initWithUTF8String: colon + 1 length: length - (colon - UTF8String) - 1]; OFURIVerifyIsEscaped(self->_percentEncodedPassword, [OFCharacterSet URIPasswordAllowedCharacterSet]); } else self->_percentEncodedUser = [[OFString alloc] initWithUTF8String: UTF8String length: length]; OFURIVerifyIsEscaped(self->_percentEncodedUser, [OFCharacterSet URIUserAllowedCharacterSet]); } static void parseHostPort(OFURI *self, const char *UTF8String, size_t length) { OFString *portString; if (*UTF8String == '[') { const char *end = memchr(UTF8String, ']', length); if (end == NULL) @throw [OFInvalidFormatException exception]; for (const char *iter = UTF8String + 1; iter < end; iter++) if (!OFASCIIIsDigit(*iter) && *iter != ':' && (*iter < 'a' || *iter > 'f') && (*iter < 'A' || *iter > 'F')) @throw [OFInvalidFormatException exception]; self->_percentEncodedHost = [[OFString alloc] initWithUTF8String: UTF8String length: end - UTF8String + 1]; length -= (end - UTF8String) + 1; UTF8String = end + 1; } else { const char *colon = memchr(UTF8String, ':', length); if (colon != NULL) { self->_percentEncodedHost = [[OFString alloc] initWithUTF8String: UTF8String length: colon - UTF8String]; length -= colon - UTF8String; UTF8String = colon; } else { self->_percentEncodedHost = [[OFString alloc] initWithUTF8String: UTF8String length: length]; UTF8String += length; length = 0; } OFURIVerifyIsEscaped(self->_percentEncodedHost, [OFCharacterSet URIHostAllowedCharacterSet]); } if (length == 0) return; if (length <= 1 || *UTF8String != ':') @throw [OFInvalidFormatException exception]; UTF8String++; length--; for (size_t i = 0; i < length; i++) if (!OFASCIIIsDigit(UTF8String[i])) @throw [OFInvalidFormatException exception]; portString = [OFString stringWithUTF8String: UTF8String length: length]; if (portString.unsignedLongLongValue > 65535) @throw [OFInvalidFormatException exception]; self->_port = [[OFNumber alloc] initWithUnsignedShort: (unsigned short)portString.unsignedLongLongValue]; } static size_t parseAuthority(OFURI *self, const char *UTF8String, size_t length) { size_t ret; const char *slash, *at; if ((slash = memchr(UTF8String, '/', length)) != NULL) length = slash - UTF8String; ret = length; if ((at = memchr(UTF8String, '@', length)) != NULL) { parseUserInfo(self, UTF8String, at - UTF8String); length -= at - UTF8String + 1; UTF8String = at + 1; } parseHostPort(self, UTF8String, length); return ret; } static void parsePathQueryFragment(OFURI *self, const char *UTF8String, size_t length) { const char *fragment, *query; if ((fragment = memchr(UTF8String, '#', length)) != NULL) { self->_percentEncodedFragment = [[OFString alloc] initWithUTF8String: fragment + 1 length: length - (fragment - UTF8String) - 1]; OFURIVerifyIsEscaped(self->_percentEncodedFragment, [OFCharacterSet URIQueryAllowedCharacterSet]); length = fragment - UTF8String; } if ((query = memchr(UTF8String, '?', length)) != NULL) { self->_percentEncodedQuery = [[OFString alloc] initWithUTF8String: query + 1 length: length - (query - UTF8String) - 1]; OFURIVerifyIsEscaped(self->_percentEncodedQuery, [OFCharacterSet URIFragmentAllowedCharacterSet]); length = query - UTF8String; } self->_percentEncodedPath = [[OFString alloc] initWithUTF8String: UTF8String length: length]; OFURIVerifyIsEscaped(self->_percentEncodedPath, [OFCharacterSet URIQueryAllowedCharacterSet]); } - (instancetype)initWithString: (OFString *)string { self = [super init]; @try { void *pool = objc_autoreleasePoolPush(); const char *UTF8String = string.UTF8String; size_t length = string.UTF8StringLength; const char *colon; if ((colon = strchr(UTF8String, ':')) == NULL) @throw [OFInvalidFormatException exception]; _percentEncodedScheme = [[[OFString stringWithUTF8String: UTF8String length: colon - UTF8String] lowercaseString] copy]; OFURIVerifyIsEscaped(_percentEncodedScheme, [OFCharacterSet URISchemeAllowedCharacterSet]); length -= colon - UTF8String + 1; UTF8String = colon + 1; if (length >= 2 && UTF8String[0] == '/' && UTF8String[1] == '/') { size_t authorityLength; UTF8String += 2; length -= 2; authorityLength = parseAuthority(self, UTF8String, length); UTF8String += authorityLength; length -= authorityLength; if (length > 0) OFEnsure(UTF8String[0] == '/'); } if (length > 0 && UTF8String[0] == '/') parsePathQueryFragment(self, UTF8String, length); else { _percentEncodedPath = [[OFString alloc] initWithUTF8String: UTF8String length: length]; OFURIVerifyIsEscaped(_percentEncodedPath, [OFCharacterSet URIPathAllowedCharacterSet]); } objc_autoreleasePoolPop(pool); } @catch (id e) { [self release]; @throw e; } return self; } - (instancetype)initWithString: (OFString *)string relativeToURI: (OFURI *)URI { |
︙ | ︙ | |||
682 683 684 685 686 687 688 | if ([URI->_percentEncodedPath hasSuffix: @"/"]) _percentEncodedPath = [[URI->_percentEncodedPath stringByAppendingString: relativePath] copy]; else { OFMutableString *path = [OFMutableString stringWithString: | | | 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 | if ([URI->_percentEncodedPath hasSuffix: @"/"]) _percentEncodedPath = [[URI->_percentEncodedPath stringByAppendingString: relativePath] copy]; else { OFMutableString *path = [OFMutableString stringWithString: (URI->_percentEncodedPath.length > 0 ? URI->_percentEncodedPath : @"/")]; OFRange range = [path rangeOfString: @"/" options: OFStringSearchBackwards]; if (range.location == OFNotFound) |
︙ | ︙ | |||
1116 1117 1118 1119 1120 1121 1122 | return copy; } - (OFString *)string { OFMutableString *ret = [OFMutableString string]; | | > > > > | < < < < | 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 | return copy; } - (OFString *)string { OFMutableString *ret = [OFMutableString string]; [ret appendFormat: @"%@:", _percentEncodedScheme]; if (_percentEncodedHost != nil || _port != nil || _percentEncodedUser != nil || _percentEncodedPassword != nil) [ret appendString: @"//"]; if (_percentEncodedUser != nil && _percentEncodedPassword != nil) [ret appendFormat: @"%@:%@@", _percentEncodedUser, _percentEncodedPassword]; else if (_percentEncodedUser != nil) [ret appendFormat: @"%@@", _percentEncodedUser]; if (_percentEncodedHost != nil) [ret appendString: _percentEncodedHost]; if (_port != nil) [ret appendFormat: @":%@", _port]; if (_percentEncodedPath != nil) [ret appendString: _percentEncodedPath]; if (_percentEncodedQuery != nil) [ret appendFormat: @"?%@", _percentEncodedQuery]; if (_percentEncodedFragment != nil) [ret appendFormat: @"#%@", _percentEncodedFragment]; |
︙ | ︙ |