http/uri/path.rs
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
use std::convert::TryFrom;
use std::str::FromStr;
use std::{cmp, fmt, str};
use bytes::Bytes;
use super::{ErrorKind, InvalidUri};
use crate::byte_str::ByteStr;
/// Represents the path component of a URI
#[derive(Clone)]
pub struct PathAndQuery {
pub(super) data: ByteStr,
pub(super) query: u16,
}
const NONE: u16 = ::std::u16::MAX;
impl PathAndQuery {
// Not public while `bytes` is unstable.
pub(super) fn from_shared(mut src: Bytes) -> Result<Self, InvalidUri> {
let mut query = NONE;
let mut fragment = None;
// block for iterator borrow
{
let mut iter = src.as_ref().iter().enumerate();
// path ...
for (i, &b) in &mut iter {
// See https://url.spec.whatwg.org/#path-state
match b {
b'?' => {
debug_assert_eq!(query, NONE);
query = i as u16;
break;
}
b'#' => {
fragment = Some(i);
break;
}
// This is the range of bytes that don't need to be
// percent-encoded in the path. If it should have been
// percent-encoded, then error.
0x21 |
0x24..=0x3B |
0x3D |
0x40..=0x5F |
0x61..=0x7A |
0x7C |
0x7E => {},
// These are code points that are supposed to be
// percent-encoded in the path but there are clients
// out there sending them as is and httparse accepts
// to parse those requests, so they are allowed here
// for parity.
//
// For reference, those are code points that are used
// to send requests with JSON directly embedded in
// the URI path. Yes, those things happen for real.
b'"' |
b'{' | b'}' => {},
_ => return Err(ErrorKind::InvalidUriChar.into()),
}
}
// query ...
if query != NONE {
for (i, &b) in iter {
match b {
// While queries *should* be percent-encoded, most
// bytes are actually allowed...
// See https://url.spec.whatwg.org/#query-state
//
// Allowed: 0x21 / 0x24 - 0x3B / 0x3D / 0x3F - 0x7E
0x21 |
0x24..=0x3B |
0x3D |
0x3F..=0x7E => {},
b'#' => {
fragment = Some(i);
break;
}
_ => return Err(ErrorKind::InvalidUriChar.into()),
}
}
}
}
if let Some(i) = fragment {
src.truncate(i);
}
Ok(PathAndQuery {
data: unsafe { ByteStr::from_utf8_unchecked(src) },
query: query,
})
}
/// Convert a `PathAndQuery` from a static string.
///
/// This function will not perform any copying, however the string is
/// checked to ensure that it is valid.
///
/// # Panics
///
/// This function panics if the argument is an invalid path and query.
///
/// # Examples
///
/// ```
/// # use http::uri::*;
/// let v = PathAndQuery::from_static("/hello?world");
///
/// assert_eq!(v.path(), "/hello");
/// assert_eq!(v.query(), Some("world"));
/// ```
#[inline]
pub fn from_static(src: &'static str) -> Self {
let src = Bytes::from_static(src.as_bytes());
PathAndQuery::from_shared(src).unwrap()
}
/// Attempt to convert a `Bytes` buffer to a `PathAndQuery`.
///
/// This will try to prevent a copy if the type passed is the type used
/// internally, and will copy the data if it is not.
pub fn from_maybe_shared<T>(src: T) -> Result<Self, InvalidUri>
where
T: AsRef<[u8]> + 'static,
{
if_downcast_into!(T, Bytes, src, {
return PathAndQuery::from_shared(src);
});
PathAndQuery::try_from(src.as_ref())
}
pub(super) fn empty() -> Self {
PathAndQuery {
data: ByteStr::new(),
query: NONE,
}
}
pub(super) fn slash() -> Self {
PathAndQuery {
data: ByteStr::from_static("/"),
query: NONE,
}
}
pub(super) fn star() -> Self {
PathAndQuery {
data: ByteStr::from_static("*"),
query: NONE,
}
}
/// Returns the path component
///
/// The path component is **case sensitive**.
///
/// ```notrust
/// abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1
/// |--------|
/// |
/// path
/// ```
///
/// If the URI is `*` then the path component is equal to `*`.
///
/// # Examples
///
/// ```
/// # use http::uri::*;
///
/// let path_and_query: PathAndQuery = "/hello/world".parse().unwrap();
///
/// assert_eq!(path_and_query.path(), "/hello/world");
/// ```
#[inline]
pub fn path(&self) -> &str {
let ret = if self.query == NONE {
&self.data[..]
} else {
&self.data[..self.query as usize]
};
if ret.is_empty() {
return "/";
}
ret
}
/// Returns the query string component
///
/// The query component contains non-hierarchical data that, along with data
/// in the path component, serves to identify a resource within the scope of
/// the URI's scheme and naming authority (if any). The query component is
/// indicated by the first question mark ("?") character and terminated by a
/// number sign ("#") character or by the end of the URI.
///
/// ```notrust
/// abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1
/// |-------------------|
/// |
/// query
/// ```
///
/// # Examples
///
/// With a query string component
///
/// ```
/// # use http::uri::*;
/// let path_and_query: PathAndQuery = "/hello/world?key=value&foo=bar".parse().unwrap();
///
/// assert_eq!(path_and_query.query(), Some("key=value&foo=bar"));
/// ```
///
/// Without a query string component
///
/// ```
/// # use http::uri::*;
/// let path_and_query: PathAndQuery = "/hello/world".parse().unwrap();
///
/// assert!(path_and_query.query().is_none());
/// ```
#[inline]
pub fn query(&self) -> Option<&str> {
if self.query == NONE {
None
} else {
let i = self.query + 1;
Some(&self.data[i as usize..])
}
}
/// Returns the path and query as a string component.
///
/// # Examples
///
/// With a query string component
///
/// ```
/// # use http::uri::*;
/// let path_and_query: PathAndQuery = "/hello/world?key=value&foo=bar".parse().unwrap();
///
/// assert_eq!(path_and_query.as_str(), "/hello/world?key=value&foo=bar");
/// ```
///
/// Without a query string component
///
/// ```
/// # use http::uri::*;
/// let path_and_query: PathAndQuery = "/hello/world".parse().unwrap();
///
/// assert_eq!(path_and_query.as_str(), "/hello/world");
/// ```
#[inline]
pub fn as_str(&self) -> &str {
let ret = &self.data[..];
if ret.is_empty() {
return "/";
}
ret
}
}
impl<'a> TryFrom<&'a [u8]> for PathAndQuery {
type Error = InvalidUri;
#[inline]
fn try_from(s: &'a [u8]) -> Result<Self, Self::Error> {
PathAndQuery::from_shared(Bytes::copy_from_slice(s))
}
}
impl<'a> TryFrom<&'a str> for PathAndQuery {
type Error = InvalidUri;
#[inline]
fn try_from(s: &'a str) -> Result<Self, Self::Error> {
TryFrom::try_from(s.as_bytes())
}
}
impl TryFrom<String> for PathAndQuery {
type Error = InvalidUri;
#[inline]
fn try_from(s: String) -> Result<Self, Self::Error> {
TryFrom::try_from(s.as_bytes())
}
}
impl TryFrom<&String> for PathAndQuery {
type Error = InvalidUri;
#[inline]
fn try_from(s: &String) -> Result<Self, Self::Error> {
TryFrom::try_from(s.as_bytes())
}
}
impl FromStr for PathAndQuery {
type Err = InvalidUri;
#[inline]
fn from_str(s: &str) -> Result<Self, InvalidUri> {
TryFrom::try_from(s)
}
}
impl fmt::Debug for PathAndQuery {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
impl fmt::Display for PathAndQuery {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
if !self.data.is_empty() {
match self.data.as_bytes()[0] {
b'/' | b'*' => write!(fmt, "{}", &self.data[..]),
_ => write!(fmt, "/{}", &self.data[..]),
}
} else {
write!(fmt, "/")
}
}
}
// ===== PartialEq / PartialOrd =====
impl PartialEq for PathAndQuery {
#[inline]
fn eq(&self, other: &PathAndQuery) -> bool {
self.data == other.data
}
}
impl Eq for PathAndQuery {}
impl PartialEq<str> for PathAndQuery {
#[inline]
fn eq(&self, other: &str) -> bool {
self.as_str() == other
}
}
impl<'a> PartialEq<PathAndQuery> for &'a str {
#[inline]
fn eq(&self, other: &PathAndQuery) -> bool {
self == &other.as_str()
}
}
impl<'a> PartialEq<&'a str> for PathAndQuery {
#[inline]
fn eq(&self, other: &&'a str) -> bool {
self.as_str() == *other
}
}
impl PartialEq<PathAndQuery> for str {
#[inline]
fn eq(&self, other: &PathAndQuery) -> bool {
self == other.as_str()
}
}
impl PartialEq<String> for PathAndQuery {
#[inline]
fn eq(&self, other: &String) -> bool {
self.as_str() == other.as_str()
}
}
impl PartialEq<PathAndQuery> for String {
#[inline]
fn eq(&self, other: &PathAndQuery) -> bool {
self.as_str() == other.as_str()
}
}
impl PartialOrd for PathAndQuery {
#[inline]
fn partial_cmp(&self, other: &PathAndQuery) -> Option<cmp::Ordering> {
self.as_str().partial_cmp(other.as_str())
}
}
impl PartialOrd<str> for PathAndQuery {
#[inline]
fn partial_cmp(&self, other: &str) -> Option<cmp::Ordering> {
self.as_str().partial_cmp(other)
}
}
impl PartialOrd<PathAndQuery> for str {
#[inline]
fn partial_cmp(&self, other: &PathAndQuery) -> Option<cmp::Ordering> {
self.partial_cmp(other.as_str())
}
}
impl<'a> PartialOrd<&'a str> for PathAndQuery {
#[inline]
fn partial_cmp(&self, other: &&'a str) -> Option<cmp::Ordering> {
self.as_str().partial_cmp(*other)
}
}
impl<'a> PartialOrd<PathAndQuery> for &'a str {
#[inline]
fn partial_cmp(&self, other: &PathAndQuery) -> Option<cmp::Ordering> {
self.partial_cmp(&other.as_str())
}
}
impl PartialOrd<String> for PathAndQuery {
#[inline]
fn partial_cmp(&self, other: &String) -> Option<cmp::Ordering> {
self.as_str().partial_cmp(other.as_str())
}
}
impl PartialOrd<PathAndQuery> for String {
#[inline]
fn partial_cmp(&self, other: &PathAndQuery) -> Option<cmp::Ordering> {
self.as_str().partial_cmp(other.as_str())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn equal_to_self_of_same_path() {
let p1: PathAndQuery = "/hello/world&foo=bar".parse().unwrap();
let p2: PathAndQuery = "/hello/world&foo=bar".parse().unwrap();
assert_eq!(p1, p2);
assert_eq!(p2, p1);
}
#[test]
fn not_equal_to_self_of_different_path() {
let p1: PathAndQuery = "/hello/world&foo=bar".parse().unwrap();
let p2: PathAndQuery = "/world&foo=bar".parse().unwrap();
assert_ne!(p1, p2);
assert_ne!(p2, p1);
}
#[test]
fn equates_with_a_str() {
let path_and_query: PathAndQuery = "/hello/world&foo=bar".parse().unwrap();
assert_eq!(&path_and_query, "/hello/world&foo=bar");
assert_eq!("/hello/world&foo=bar", &path_and_query);
assert_eq!(path_and_query, "/hello/world&foo=bar");
assert_eq!("/hello/world&foo=bar", path_and_query);
}
#[test]
fn not_equal_with_a_str_of_a_different_path() {
let path_and_query: PathAndQuery = "/hello/world&foo=bar".parse().unwrap();
// as a reference
assert_ne!(&path_and_query, "/hello&foo=bar");
assert_ne!("/hello&foo=bar", &path_and_query);
// without reference
assert_ne!(path_and_query, "/hello&foo=bar");
assert_ne!("/hello&foo=bar", path_and_query);
}
#[test]
fn equates_with_a_string() {
let path_and_query: PathAndQuery = "/hello/world&foo=bar".parse().unwrap();
assert_eq!(path_and_query, "/hello/world&foo=bar".to_string());
assert_eq!("/hello/world&foo=bar".to_string(), path_and_query);
}
#[test]
fn not_equal_with_a_string_of_a_different_path() {
let path_and_query: PathAndQuery = "/hello/world&foo=bar".parse().unwrap();
assert_ne!(path_and_query, "/hello&foo=bar".to_string());
assert_ne!("/hello&foo=bar".to_string(), path_and_query);
}
#[test]
fn compares_to_self() {
let p1: PathAndQuery = "/a/world&foo=bar".parse().unwrap();
let p2: PathAndQuery = "/b/world&foo=bar".parse().unwrap();
assert!(p1 < p2);
assert!(p2 > p1);
}
#[test]
fn compares_with_a_str() {
let path_and_query: PathAndQuery = "/b/world&foo=bar".parse().unwrap();
// by ref
assert!(&path_and_query < "/c/world&foo=bar");
assert!("/c/world&foo=bar" > &path_and_query);
assert!(&path_and_query > "/a/world&foo=bar");
assert!("/a/world&foo=bar" < &path_and_query);
// by val
assert!(path_and_query < "/c/world&foo=bar");
assert!("/c/world&foo=bar" > path_and_query);
assert!(path_and_query > "/a/world&foo=bar");
assert!("/a/world&foo=bar" < path_and_query);
}
#[test]
fn compares_with_a_string() {
let path_and_query: PathAndQuery = "/b/world&foo=bar".parse().unwrap();
assert!(path_and_query < "/c/world&foo=bar".to_string());
assert!("/c/world&foo=bar".to_string() > path_and_query);
assert!(path_and_query > "/a/world&foo=bar".to_string());
assert!("/a/world&foo=bar".to_string() < path_and_query);
}
#[test]
fn ignores_valid_percent_encodings() {
assert_eq!("/a%20b", pq("/a%20b?r=1").path());
assert_eq!("qr=%31", pq("/a/b?qr=%31").query().unwrap());
}
#[test]
fn ignores_invalid_percent_encodings() {
assert_eq!("/a%%b", pq("/a%%b?r=1").path());
assert_eq!("/aaa%", pq("/aaa%").path());
assert_eq!("/aaa%", pq("/aaa%?r=1").path());
assert_eq!("/aa%2", pq("/aa%2").path());
assert_eq!("/aa%2", pq("/aa%2?r=1").path());
assert_eq!("qr=%3", pq("/a/b?qr=%3").query().unwrap());
}
#[test]
fn json_is_fine() {
assert_eq!(r#"/{"bread":"baguette"}"#, pq(r#"/{"bread":"baguette"}"#).path());
}
fn pq(s: &str) -> PathAndQuery {
s.parse().expect(&format!("parsing {}", s))
}
}