1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 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 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 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 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 733 734 735 736 737 738 739 740 741 742 743 744 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 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 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
use std::borrow::{Borrow, Cow};
use std::convert::AsRef;
use std::cmp::Ordering;
use std::str::Utf8Error;
use std::fmt;
use ref_cast::RefCast;
use stable_pattern::{Pattern, Searcher, ReverseSearcher, Split, SplitInternal};
use crate::uri::fmt::{DEFAULT_ENCODE_SET, percent_encode, percent_encode_bytes};
use crate::uncased::UncasedStr;
/// A reference to a string inside of a raw HTTP message.
///
/// A `RawStr` is an unsanitized, unvalidated, and undecoded raw string from an
/// HTTP message. It exists to separate validated string inputs, represented by
/// the `String`, `&str`, and `Cow<str>` types, from unvalidated inputs,
/// represented by `&RawStr`.
///
/// # Validation
///
/// An `&RawStr` should be converted into one of the validated string input
/// types through methods on `RawStr`. These methods are summarized below:
///
/// * **[`url_decode()`]** - used to decode a raw string in a form value
/// context
/// * **[`percent_decode()`], [`percent_decode_lossy()`]** - used to
/// percent-decode a raw string, typically in a URL context
/// * **[`html_escape()`]** - used to decode a string for use in HTML
/// templates
/// * **[`as_str()`]** - used when the `RawStr` is known to be safe in the
/// context of its intended use. Use sparingly and with care!
/// * **[`as_uncased_str()`]** - used when the `RawStr` is known to be safe in
/// the context of its intended, uncased use
///
/// **Note:** Template engines like Tera and Handlebars all functions like
/// [`html_escape()`] on all rendered template outputs by default.
///
/// [`as_str()`]: RawStr::as_str()
/// [`as_uncased_str()`]: RawStr::as_uncased_str()
/// [`url_decode()`]: RawStr::url_decode()
/// [`html_escape()`]: RawStr::html_escape()
/// [`percent_decode()`]: RawStr::percent_decode()
/// [`percent_decode_lossy()`]: RawStr::percent_decode_lossy()
///
/// # Usage
///
/// A `RawStr` is a dynamically sized type (just like `str`). It is always used
/// through a reference an as `&RawStr` (just like &str).
#[repr(transparent)]
#[derive(RefCast, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawStr(str);
impl ToOwned for RawStr {
type Owned = RawStrBuf;
fn to_owned(&self) -> Self::Owned {
RawStrBuf(self.to_string())
}
}
/// An owned version of [`RawStr`].
#[repr(transparent)]
#[derive(RefCast, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawStrBuf(String);
impl RawStrBuf {
/// Cost-free conversion from `self` into a `String`.
///
/// # Example
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::RawStrBuf;
///
/// let raw = RawStrBuf::from(format!("hello {}", "world"));
/// let string = raw.into_string();
/// ```
pub fn into_string(self) -> String {
self.0
}
}
impl RawStr {
/// Constructs an `&RawStr` from a string-like type at no cost.
///
/// # Example
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// let raw_str = RawStr::new("Hello, world!");
///
/// // `into` can also be used; note that the type must be specified
/// let raw_str: &RawStr = "Hello, world!".into();
/// ```
pub fn new<S: AsRef<str> + ?Sized>(string: &S) -> &RawStr {
RawStr::ref_cast(string.as_ref())
}
/// Construct a `Cow<RawStr>` from a `Cow<Str>`. Does not allocate.
///
/// See [`RawStr::into_cow_str()`] for the inverse operation.
///
/// # Example
///
/// ```rust
/// # extern crate rocket;
/// use std::borrow::Cow;
/// use rocket::http::RawStr;
///
/// let cow_str = Cow::from("hello!");
/// let cow_raw = RawStr::from_cow_str(cow_str);
/// assert_eq!(cow_raw.as_str(), "hello!");
/// ```
pub fn from_cow_str(cow: Cow<'_, str>) -> Cow<'_, RawStr> {
match cow {
Cow::Borrowed(b) => Cow::Borrowed(b.into()),
Cow::Owned(b) => Cow::Owned(b.into()),
}
}
/// Construct a `Cow<str>` from a `Cow<RawStr>`. Does not allocate.
///
/// See [`RawStr::from_cow_str()`] for the inverse operation.
///
/// # Example
///
/// ```rust
/// # extern crate rocket;
/// use std::borrow::Cow;
/// use rocket::http::RawStr;
///
/// let cow_raw = Cow::from(RawStr::new("hello!"));
/// let cow_str = RawStr::into_cow_str(cow_raw);
/// assert_eq!(&*cow_str, "hello!");
/// ```
pub fn into_cow_str(cow: Cow<'_, RawStr>) -> Cow<'_, str> {
match cow {
Cow::Borrowed(b) => Cow::Borrowed(b.as_str()),
Cow::Owned(b) => Cow::Owned(b.into_string()),
}
}
/// Percent-decodes `self`.
fn _percent_decode(&self) -> percent_encoding::PercentDecode<'_> {
percent_encoding::percent_decode(self.as_bytes())
}
/// Returns a percent-decoded version of the string.
///
/// # Errors
///
/// Returns an `Err` if the percent encoded values are not valid UTF-8.
///
/// # Example
///
/// With a valid string:
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// let raw_str = RawStr::new("Hello%21");
/// let decoded = raw_str.percent_decode();
/// assert_eq!(decoded, Ok("Hello!".into()));
/// ```
///
/// With an invalid string:
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// let bad_raw_str = RawStr::new("%FF");
/// assert!(bad_raw_str.percent_decode().is_err());
/// ```
#[inline(always)]
pub fn percent_decode(&self) -> Result<Cow<'_, str>, Utf8Error> {
// don't let `percent-encoding` return a random empty string
if self.is_empty() {
return Ok(self.as_str().into());
}
self._percent_decode().decode_utf8()
}
/// Returns a percent-decoded version of the string. Any invalid UTF-8
/// percent-encoded byte sequences will be replaced � U+FFFD, the
/// replacement character.
///
/// # Example
///
/// With a valid string:
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// let raw_str = RawStr::new("Hello%21");
/// let decoded = raw_str.percent_decode_lossy();
/// assert_eq!(decoded, "Hello!");
/// ```
///
/// With an invalid string:
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// let bad_raw_str = RawStr::new("a=%FF");
/// assert_eq!(bad_raw_str.percent_decode_lossy(), "a=�");
/// ```
#[inline(always)]
pub fn percent_decode_lossy(&self) -> Cow<'_, str> {
// don't let `percent-encoding` return a random empty string
if self.is_empty() {
return self.as_str().into();
}
self._percent_decode().decode_utf8_lossy()
}
/// Replaces '+' with ' ' in `self`, allocating only when necessary.
fn _replace_plus(&self) -> Cow<'_, str> {
let string = self.as_str();
let mut allocated = String::new(); // this is allocation free
for i in memchr::memchr_iter(b'+', string.as_bytes()) {
if allocated.is_empty() {
allocated = string.into();
}
// SAFETY:
//
// 1. The caller must ensure that the content of the slice is valid
// UTF-8 before the borrow ends and the underlying `str` is used.
//
// `allocated[i]` is `+` since that is what we searched for. The
// `+` char is ASCII => the character is one byte wide. ' ' is
// also one byte and ASCII => UTF-8. The replacement of `+` with
// ` ` thus yields a valid UTF-8 string.
unsafe { allocated.as_bytes_mut()[i] = b' '; }
}
match allocated.is_empty() {
true => Cow::Borrowed(string),
false => Cow::Owned(allocated)
}
}
/// Returns a percent-encoded version of the string.
///
/// # Example
///
/// With a valid string:
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// let raw_str = RawStr::new("Hello%21");
/// let decoded = raw_str.percent_decode();
/// assert_eq!(decoded, Ok("Hello!".into()));
/// ```
///
/// With an invalid string:
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// let bad_raw_str = RawStr::new("%FF");
/// assert!(bad_raw_str.percent_decode().is_err());
/// ```
#[inline(always)]
pub fn percent_encode(&self) -> Cow<'_, RawStr> {
Self::from_cow_str(percent_encode::<DEFAULT_ENCODE_SET>(self))
}
/// Returns a percent-encoded version of `bytes`.
///
/// # Example
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// // Note: Rocket should never hand you a bad `&RawStr`.
/// let bytes = &[93, 12, 0, 13, 1];
/// let encoded = RawStr::percent_encode_bytes(&bytes[..]);
/// ```
#[inline(always)]
pub fn percent_encode_bytes(bytes: &[u8]) -> Cow<'_, RawStr> {
Self::from_cow_str(percent_encode_bytes::<DEFAULT_ENCODE_SET>(bytes))
}
/// Returns a URL-decoded version of the string. This is identical to
/// percent decoding except that `+` characters are converted into spaces.
/// This is the encoding used by form values.
///
/// # Errors
///
/// Returns an `Err` if the percent encoded values are not valid UTF-8.
///
/// # Example
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// let raw_str = RawStr::new("Hello%2C+world%21");
/// let decoded = raw_str.url_decode();
/// assert_eq!(decoded.unwrap(), "Hello, world!");
/// ```
pub fn url_decode(&self) -> Result<Cow<'_, str>, Utf8Error> {
let string = self._replace_plus();
match percent_encoding::percent_decode(string.as_bytes()).decode_utf8()? {
Cow::Owned(s) => Ok(Cow::Owned(s)),
Cow::Borrowed(_) => Ok(string)
}
}
/// Returns a URL-decoded version of the string.
///
/// Any invalid UTF-8 percent-encoded byte sequences will be replaced �
/// U+FFFD, the replacement character. This is identical to lossy percent
/// decoding except that `+` characters are converted into spaces. This is
/// the encoding used by form values.
///
/// # Example
///
/// With a valid string:
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// let raw_str: &RawStr = "Hello%2C+world%21".into();
/// let decoded = raw_str.url_decode_lossy();
/// assert_eq!(decoded, "Hello, world!");
/// ```
///
/// With an invalid string:
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// let bad_raw_str = RawStr::new("a+b=%FF");
/// assert_eq!(bad_raw_str.url_decode_lossy(), "a b=�");
/// ```
pub fn url_decode_lossy(&self) -> Cow<'_, str> {
let string = self._replace_plus();
match percent_encoding::percent_decode(string.as_bytes()).decode_utf8_lossy() {
Cow::Owned(s) => Cow::Owned(s),
Cow::Borrowed(_) => string
}
}
/// Returns an HTML escaped version of `self`. Allocates only when
/// characters need to be escaped.
///
/// The following characters are escaped: `&`, `<`, `>`, `"`, `'`, `/`,
/// <code>`</code>. **This suffices as long as the escaped string is not
/// used in an execution context such as inside of <script> or <style>
/// tags!** See the [OWASP XSS Prevention Rules] for more information.
///
/// [OWASP XSS Prevention Rules]: https://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet#XSS_Prevention_Rules
///
/// # Example
///
/// Strings with HTML sequences are escaped:
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// let raw_str: &RawStr = "<b>Hi!</b>".into();
/// let escaped = raw_str.html_escape();
/// assert_eq!(escaped, "<b>Hi!</b>");
///
/// let raw_str: &RawStr = "Hello, <i>world!</i>".into();
/// let escaped = raw_str.html_escape();
/// assert_eq!(escaped, "Hello, <i>world!</i>");
/// ```
///
/// Strings without HTML sequences remain untouched:
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// let raw_str: &RawStr = "Hello!".into();
/// let escaped = raw_str.html_escape();
/// assert_eq!(escaped, "Hello!");
///
/// let raw_str: &RawStr = "大阪".into();
/// let escaped = raw_str.html_escape();
/// assert_eq!(escaped, "大阪");
/// ```
// NOTE: This is the ~fastest (a table-based implementation is slightly
// faster) implementation benchmarked for dense-ish escaping. For sparser
// texts, a regex-based-find solution is much faster.
pub fn html_escape(&self) -> Cow<'_, str> {
let mut escaped = false;
let mut allocated = Vec::new(); // this is allocation free
for c in self.as_bytes() {
match *c {
b'&' | b'<' | b'>' | b'"' | b'\'' | b'/' | b'`' => {
if !escaped {
let i = (c as *const u8 as usize) - (self.as_ptr() as usize);
allocated = Vec::with_capacity(self.len() * 2);
allocated.extend_from_slice(&self.as_bytes()[..i]);
}
match *c {
b'&' => allocated.extend_from_slice(b"&"),
b'<' => allocated.extend_from_slice(b"<"),
b'>' => allocated.extend_from_slice(b">"),
b'"' => allocated.extend_from_slice(b"""),
b'\'' => allocated.extend_from_slice(b"'"),
b'/' => allocated.extend_from_slice(b"/"),
// Old versions of IE treat a ` as a '.
b'`' => allocated.extend_from_slice(b"`"),
_ => unreachable!()
}
escaped = true;
}
_ if escaped => allocated.push(*c),
_ => { }
}
}
if escaped {
// This use of `unsafe` is only correct if the bytes in `allocated`
// form a valid UTF-8 string. We prove this by cases:
//
// 1. In the `!escaped` branch, capacity for the vector is first
// allocated. No characters are pushed to `allocated` prior to
// this branch running since the `escaped` flag isn't set. To
// enter this branch, the current byte must be a valid ASCII
// character. This implies that every byte preceding forms a
// valid UTF-8 string since ASCII characters in UTF-8 are never
// part of a multi-byte sequence. Thus, extending the `allocated`
// vector with these bytes results in a valid UTF-8 string in
// `allocated`.
//
// 2. After the `!escaped` branch, `allocated` is extended with a
// byte string of valid ASCII characters. Thus, `allocated` is
// still a valid UTF-8 string.
//
// 3. In the `_ if escaped` branch, the byte is simply pushed into
// the `allocated` vector. At this point, `allocated` may contain
// an invalid UTF-8 string as we are not a known boundary.
// However, note that this byte is part of a known valid string
// (`self`). If the byte is not part of a multi-byte sequence, it
// is ASCII, and no further justification is needed. If the byte
// _is_ part of a multi-byte sequence, it is _not_ ASCII, and
// thus will not fall into the escaped character set and it will
// be pushed into `allocated` subsequently, resulting in a valid
// UTF-8 string in `allocated`.
unsafe { Cow::Owned(String::from_utf8_unchecked(allocated)) }
} else {
Cow::Borrowed(self.as_str())
}
}
/// Returns the length of `self`.
///
/// This length is in bytes, not [`char`]s or graphemes. In other words,
/// it may not be what a human considers the length of the string.
///
/// # Example
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// let raw_str = RawStr::new("Hello, world!");
/// assert_eq!(raw_str.len(), 13);
/// ```
#[inline]
pub const fn len(&self) -> usize {
self.0.len()
}
/// Returns `true` if `self` has a length of zero bytes.
///
/// # Example
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// let raw_str = RawStr::new("Hello, world!");
/// assert!(!raw_str.is_empty());
///
/// let raw_str = RawStr::new("");
/// assert!(raw_str.is_empty());
/// ```
#[inline]
pub const fn is_empty(&self) -> bool {
self.len() == 0
}
/// Converts `self` into an `&str`.
///
/// This method should be used sparingly. **Only use this method when you
/// are absolutely certain that doing so is safe.**
///
/// # Example
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// let raw_str = RawStr::new("Hello, world!");
/// assert_eq!(raw_str.as_str(), "Hello, world!");
/// ```
#[inline(always)]
pub const fn as_str(&self) -> &str {
&self.0
}
/// Converts `self` into an `&[u8]`.
///
/// # Example
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// let raw_str = RawStr::new("hi");
/// assert_eq!(raw_str.as_bytes(), &[0x68, 0x69]);
/// ```
#[inline(always)]
pub const fn as_bytes(&self) -> &[u8] {
self.0.as_bytes()
}
/// Converts a string slice to a raw pointer.
///
/// As string slices are a slice of bytes, the raw pointer points to a
/// [`u8`]. This pointer will be pointing to the first byte of the string
/// slice.
///
/// The caller must ensure that the returned pointer is never written to.
/// If you need to mutate the contents of the string slice, use [`as_mut_ptr`].
///
/// [`as_mut_ptr`]: str::as_mut_ptr
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// let raw_str = RawStr::new("hi");
/// let ptr = raw_str.as_ptr();
/// ```
pub const fn as_ptr(&self) -> *const u8 {
self.as_str().as_ptr()
}
/// Converts `self` into an `&UncasedStr`.
///
/// This method should be used sparingly. **Only use this method when you
/// are absolutely certain that doing so is safe.**
///
/// # Example
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// let raw_str = RawStr::new("Content-Type");
/// assert!(raw_str.as_uncased_str() == "content-TYPE");
/// ```
#[inline(always)]
pub fn as_uncased_str(&self) -> &UncasedStr {
self.as_str().into()
}
/// Returns `true` if the given pattern matches a sub-slice of
/// this string slice.
///
/// Returns `false` if it does not.
///
/// The pattern can be a `&str`, [`char`], a slice of [`char`]s, or a
/// function or closure that determines if a character matches.
///
/// [`char`]: prim@char
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// let bananas = RawStr::new("bananas");
///
/// assert!(bananas.contains("nana"));
/// assert!(!bananas.contains("apples"));
/// ```
#[inline]
pub fn contains<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool {
pat.is_contained_in(self.as_str())
}
/// Returns `true` if the given pattern matches a prefix of this
/// string slice.
///
/// Returns `false` if it does not.
///
/// The pattern can be a `&str`, [`char`], a slice of [`char`]s, or a
/// function or closure that determines if a character matches.
///
/// [`char`]: prim@char
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// let bananas = RawStr::new("bananas");
///
/// assert!(bananas.starts_with("bana"));
/// assert!(!bananas.starts_with("nana"));
/// ```
pub fn starts_with<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool {
pat.is_prefix_of(self.as_str())
}
/// Returns `true` if the given pattern matches a suffix of this
/// string slice.
///
/// Returns `false` if it does not.
///
/// The pattern can be a `&str`, [`char`], a slice of [`char`]s, or a
/// function or closure that determines if a character matches.
///
/// [`char`]: prim@char
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// let bananas = RawStr::new("bananas");
///
/// assert!(bananas.ends_with("anas"));
/// assert!(!bananas.ends_with("nana"));
/// ```
pub fn ends_with<'a, P>(&'a self, pat: P) -> bool
where P: Pattern<'a>, <P as Pattern<'a>>::Searcher: ReverseSearcher<'a>
{
pat.is_suffix_of(self.as_str())
}
/// Returns the byte index of the first character of this string slice that
/// matches the pattern.
///
/// Returns [`None`] if the pattern doesn't match.
///
/// The pattern can be a `&str`, [`char`], a slice of [`char`]s, or a
/// function or closure that determines if a character matches.
///
/// [`char`]: prim@char
///
/// # Example
///
/// ```
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// let s = RawStr::new("Löwe 老虎 Léopard Gepardi");
///
/// assert_eq!(s.find('L'), Some(0));
/// assert_eq!(s.find('é'), Some(14));
/// assert_eq!(s.find("pard"), Some(17));
/// ```
#[inline]
pub fn find<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize> {
pat.into_searcher(self.as_str()).next_match().map(|(i, _)| i)
}
/// An iterator over substrings of this string slice, separated by
/// characters matched by a pattern.
///
/// The pattern can be a `&str`, [`char`], a slice of [`char`]s, or a
/// function or closure that determines if a character matches.
///
/// [`char`]: prim@char
///
/// # Examples
///
/// Simple patterns:
///
/// ```
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// let v: Vec<_> = RawStr::new("Mary had a little lamb")
/// .split(' ')
/// .map(|r| r.as_str())
/// .collect();
///
/// assert_eq!(v, ["Mary", "had", "a", "little", "lamb"]);
/// ```
#[inline]
pub fn split<'a, P>(&'a self, pat: P) -> impl DoubleEndedIterator<Item = &'a RawStr>
where P: Pattern<'a>,
<P as stable_pattern::Pattern<'a>>::Searcher: stable_pattern::DoubleEndedSearcher<'a>
{
let split: Split<'_, P> = Split(SplitInternal {
start: 0,
end: self.len(),
matcher: pat.into_searcher(self.as_str()),
allow_trailing_empty: true,
finished: false,
});
split.map(|s| s.into())
}
/// Splits `self` into two pieces: the piece _before_ the first byte `b` and
/// the piece _after_ (not including `b`). Returns the tuple (`before`,
/// `after`). If `b` is not in `self`, or `b` is not an ASCII characters,
/// returns the entire string `self` as `before` and the empty string as
/// `after`.
///
/// # Example
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// let haystack = RawStr::new("a good boy!");
///
/// let (before, after) = haystack.split_at_byte(b'a');
/// assert_eq!(before, "");
/// assert_eq!(after, " good boy!");
///
/// let (before, after) = haystack.split_at_byte(b' ');
/// assert_eq!(before, "a");
/// assert_eq!(after, "good boy!");
///
/// let (before, after) = haystack.split_at_byte(b'o');
/// assert_eq!(before, "a g");
/// assert_eq!(after, "od boy!");
///
/// let (before, after) = haystack.split_at_byte(b'!');
/// assert_eq!(before, "a good boy");
/// assert_eq!(after, "");
///
/// let (before, after) = haystack.split_at_byte(b'?');
/// assert_eq!(before, "a good boy!");
/// assert_eq!(after, "");
///
/// let haystack = RawStr::new("");
/// let (before, after) = haystack.split_at_byte(b' ');
/// assert_eq!(before, "");
/// assert_eq!(after, "");
/// ```
#[inline]
pub fn split_at_byte(&self, b: u8) -> (&RawStr, &RawStr) {
if !b.is_ascii() {
return (self, &self[0..0]);
}
match memchr::memchr(b, self.as_bytes()) {
// SAFETY: `b` is a character boundary since it's ASCII, `i` is in
// bounds in `self` (or else None), and i is at most len - 1, so i +
// 1 is at most len.
Some(i) => unsafe {
let s = self.as_str();
let start = s.get_unchecked(0..i);
let end = s.get_unchecked((i + 1)..self.len());
(start.into(), end.into())
},
None => (self, &self[0..0])
}
}
/// Returns a string slice with the prefix removed.
///
/// If the string starts with the pattern `prefix`, returns substring after
/// the prefix, wrapped in `Some`. This method removes the prefix exactly
/// once.
///
/// If the string does not start with `prefix`, returns `None`.
///
/// The pattern can be a `&str`, `char`, a slice of `char`s, or a function
/// or closure that determines if a character matches.
///
/// # Examples
///
/// ```
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// assert_eq!(RawStr::new("foo:bar").strip_prefix("foo:").unwrap(), "bar");
/// assert_eq!(RawStr::new("foofoo").strip_prefix("foo").unwrap(), "foo");
/// assert!(RawStr::new("foo:bar").strip_prefix("bar").is_none());
/// ```
#[inline]
pub fn strip_prefix<'a, P: Pattern<'a>>(&'a self, prefix: P) -> Option<&'a RawStr> {
prefix.strip_prefix_of(self.as_str()).map(RawStr::new)
}
/// Returns a string slice with the suffix removed.
///
/// If the string ends with the pattern `suffix`, returns the substring
/// before the suffix, wrapped in `Some`. Unlike `trim_end_matches`, this
/// method removes the suffix exactly once.
///
/// If the string does not end with `suffix`, returns `None`.
///
/// The pattern can be a `&str`, `char`, a slice of `char`s, or a function
/// or closure that determines if a character matches.
///
/// # Examples
///
/// ```
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// assert_eq!(RawStr::new("bar:foo").strip_suffix(":foo").unwrap(), "bar");
/// assert_eq!(RawStr::new("foofoo").strip_suffix("foo").unwrap(), "foo");
/// assert!(RawStr::new("bar:foo").strip_suffix("bar").is_none());
/// ```
#[inline]
pub fn strip_suffix<'a, P>(&'a self, suffix: P) -> Option<&'a RawStr>
where P: Pattern<'a>,<P as Pattern<'a>>::Searcher: ReverseSearcher<'a>,
{
suffix.strip_suffix_of(self.as_str()).map(RawStr::new)
}
/// Returns a string slice with leading and trailing whitespace removed.
///
/// 'Whitespace' is defined according to the terms of the Unicode Derived
/// Core Property `White_Space`, which includes newlines.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// let s = RawStr::new("\n Hello\tworld\t\n");
///
/// assert_eq!("Hello\tworld", s.trim());
/// ```
#[inline]
pub fn trim(&self) -> &RawStr {
RawStr::new(self.as_str().trim_matches(|c: char| c.is_whitespace()))
}
/// Parses this string slice into another type.
///
/// Because `parse` is so general, it can cause problems with type
/// inference. As such, `parse` is one of the few times you'll see
/// the syntax affectionately known as the 'turbofish': `::<>`. This
/// helps the inference algorithm understand specifically which type
/// you're trying to parse into.
///
/// # Errors
///
/// Will return `Err` if it's not possible to parse this string slice into
/// the desired type.
///
/// # Examples
///
/// Basic usage
///
/// ```
/// # extern crate rocket;
/// use rocket::http::RawStr;
///
/// let four: u32 = RawStr::new("4").parse().unwrap();
///
/// assert_eq!(4, four);
/// ```
#[inline]
pub fn parse<F: std::str::FromStr>(&self) -> Result<F, F::Err> {
std::str::FromStr::from_str(self.as_str())
}
}
#[cfg(feature = "serde")]
mod serde {
use serde_::{ser, de, Serialize, Deserialize};
use super::*;
impl Serialize for RawStr {
fn serialize<S>(&self, ser: S) -> Result<S::Ok, S::Error>
where S: ser::Serializer
{
self.as_str().serialize(ser)
}
}
impl<'de: 'a, 'a> Deserialize<'de> for &'a RawStr {
fn deserialize<D>(de: D) -> Result<Self, D::Error>
where D: de::Deserializer<'de>
{
<&'a str as Deserialize<'de>>::deserialize(de).map(RawStr::new)
}
}
}
impl fmt::Debug for RawStr {
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl<'a> From<&'a str> for &'a RawStr {
#[inline(always)]
fn from(string: &'a str) -> &'a RawStr {
RawStr::new(string)
}
}
impl<'a> From<&'a RawStr> for Cow<'a, RawStr> {
fn from(raw: &'a RawStr) -> Self {
Cow::Borrowed(raw)
}
}
impl From<RawStrBuf> for Cow<'_, RawStr> {
fn from(raw: RawStrBuf) -> Self {
Cow::Owned(raw)
}
}
macro_rules! impl_partial {
($A:ty : $B:ty as $T:ty) => (
impl PartialEq<$A> for $B {
#[inline(always)]
fn eq(&self, other: &$A) -> bool {
let left: $T = self.as_ref();
let right: $T = other.as_ref();
left == right
}
}
impl PartialOrd<$A> for $B {
#[inline(always)]
fn partial_cmp(&self, other: &$A) -> Option<Ordering> {
let left: $T = self.as_ref();
let right: $T = other.as_ref();
left.partial_cmp(right)
}
}
);
($A:ty : $B:ty) => (impl_partial!($A : $B as &str);)
}
impl_partial!(RawStr : &RawStr);
impl_partial!(&RawStr : RawStr);
impl_partial!(str : RawStr);
impl_partial!(str : &RawStr);
impl_partial!(&str : RawStr);
impl_partial!(&&str : RawStr);
impl_partial!(Cow<'_, str> : RawStr);
impl_partial!(Cow<'_, str> : &RawStr);
impl_partial!(RawStr : Cow<'_, str>);
impl_partial!(&RawStr : Cow<'_, str>);
impl_partial!(Cow<'_, RawStr> : RawStr as &RawStr);
impl_partial!(Cow<'_, RawStr> : &RawStr as &RawStr);
impl_partial!(RawStr : Cow<'_, RawStr> as &RawStr);
impl_partial!(&RawStr : Cow<'_, RawStr> as &RawStr);
impl_partial!(String : RawStr);
impl_partial!(String : &RawStr);
impl_partial!(RawStr : String);
impl_partial!(&RawStr : String);
impl_partial!(RawStr : str);
impl_partial!(RawStr : &str);
impl_partial!(RawStr : &&str);
impl_partial!(&RawStr : str);
impl AsRef<str> for RawStr {
#[inline(always)]
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl AsRef<std::ffi::OsStr> for RawStr {
#[inline(always)]
fn as_ref(&self) -> &std::ffi::OsStr {
self.as_str().as_ref()
}
}
impl AsRef<RawStr> for str {
#[inline(always)]
fn as_ref(&self) -> &RawStr {
RawStr::new(self)
}
}
impl AsRef<RawStr> for RawStr {
#[inline(always)]
fn as_ref(&self) -> &RawStr {
self
}
}
impl AsRef<[u8]> for RawStr {
#[inline(always)]
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}
impl<I: core::slice::SliceIndex<str, Output=str>> core::ops::Index<I> for RawStr {
type Output = RawStr;
#[inline]
fn index(&self, index: I) -> &Self::Output {
self.as_str()[index].into()
}
}
impl std::borrow::Borrow<str> for RawStr {
#[inline(always)]
fn borrow(&self) -> &str {
self.as_str()
}
}
impl std::borrow::Borrow<RawStr> for &str {
#[inline(always)]
fn borrow(&self) -> &RawStr {
(*self).into()
}
}
impl fmt::Display for RawStr {
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl AsRef<RawStr> for RawStrBuf {
#[inline(always)]
fn as_ref(&self) -> &RawStr {
RawStr::new(self.0.as_str())
}
}
impl Borrow<RawStr> for RawStrBuf {
#[inline(always)]
fn borrow(&self) -> &RawStr {
self.as_ref()
}
}
impl std::ops::Deref for RawStrBuf {
type Target = RawStr;
#[inline(always)]
fn deref(&self) -> &Self::Target {
self.as_ref()
}
}
impl fmt::Display for RawStrBuf {
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl fmt::Debug for RawStrBuf {
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl From<String> for RawStrBuf {
#[inline(always)]
fn from(string: String) -> Self {
RawStrBuf(string)
}
}
impl From<&str> for RawStrBuf {
#[inline(always)]
fn from(string: &str) -> Self {
string.to_string().into()
}
}
impl From<&RawStr> for RawStrBuf {
#[inline(always)]
fn from(raw: &RawStr) -> Self {
raw.to_string().into()
}
}
#[cfg(test)]
mod tests {
use super::RawStr;
#[test]
fn can_compare() {
let raw_str = RawStr::new("abc");
assert_eq!(raw_str, "abc");
assert_eq!("abc", raw_str.as_str());
assert_eq!(raw_str, RawStr::new("abc"));
assert_eq!(raw_str, "abc".to_string());
assert_eq!("abc".to_string(), raw_str.as_str());
}
}