rama_http/matcher/mod.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 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
//! [`service::Matcher`]s implementations to match on [`http::Request`]s.
//!
//! See [`service::matcher` module] for more information.
//!
//! [`service::Matcher`]: rama_core::matcher::Matcher
//! [`http::Request`]: crate::Request
//! [`service::matcher` module]: rama_core
use crate::Request;
use rama_core::{context::Extensions, matcher::IteratorMatcherExt, Context};
use rama_net::{address::Domain, stream::matcher::SocketMatcher};
use std::fmt;
use std::sync::Arc;
mod method;
#[doc(inline)]
pub use method::MethodMatcher;
mod domain;
#[doc(inline)]
pub use domain::DomainMatcher;
pub mod uri;
pub use uri::UriMatcher;
mod version;
#[doc(inline)]
pub use version::VersionMatcher;
mod path;
#[doc(inline)]
pub use path::{PathMatcher, UriParams, UriParamsDeserializeError};
mod header;
#[doc(inline)]
pub use header::HeaderMatcher;
/// A matcher that is used to match an http [`Request`]
pub struct HttpMatcher<State, Body> {
kind: HttpMatcherKind<State, Body>,
negate: bool,
}
impl<State, Body> Clone for HttpMatcher<State, Body> {
fn clone(&self) -> Self {
Self {
kind: self.kind.clone(),
negate: self.negate,
}
}
}
impl<State, Body> fmt::Debug for HttpMatcher<State, Body> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("HttpMatcher")
.field("kind", &self.kind)
.field("negate", &self.negate)
.finish()
}
}
/// A matcher that is used to match an http [`Request`]
pub enum HttpMatcherKind<State, Body> {
/// zero or more [`HttpMatcher`]s that all need to match in order for the matcher to return `true`.
All(Vec<HttpMatcher<State, Body>>),
/// [`MethodMatcher`], a matcher that matches one or more HTTP methods.
Method(MethodMatcher),
/// [`PathMatcher`], a matcher based on the URI path.
Path(PathMatcher),
/// [`DomainMatcher`], a matcher based on the (sub)domain of the request's URI.
Domain(DomainMatcher),
/// [`VersionMatcher`], a matcher based on the HTTP version of the request.
Version(VersionMatcher),
/// zero or more [`HttpMatcher`]s that at least one needs to match in order for the matcher to return `true`.
Any(Vec<HttpMatcher<State, Body>>),
/// [`UriMatcher`], a matcher the request's URI, using a substring or regex pattern.
Uri(UriMatcher),
/// [`HeaderMatcher`], a matcher based on the [`Request`]'s headers.
Header(HeaderMatcher),
/// [`SocketMatcher`], a matcher that matches on the [`SocketAddr`] of the peer.
///
/// [`SocketAddr`]: std::net::SocketAddr
Socket(SocketMatcher<State, Request<Body>>),
/// A custom matcher that implements [`rama_core::matcher::Matcher`].
Custom(Arc<dyn rama_core::matcher::Matcher<State, Request<Body>>>),
}
impl<State, Body> Clone for HttpMatcherKind<State, Body> {
fn clone(&self) -> Self {
match self {
Self::All(inner) => Self::All(inner.clone()),
Self::Method(inner) => Self::Method(*inner),
Self::Path(inner) => Self::Path(inner.clone()),
Self::Domain(inner) => Self::Domain(inner.clone()),
Self::Version(inner) => Self::Version(*inner),
Self::Any(inner) => Self::Any(inner.clone()),
Self::Uri(inner) => Self::Uri(inner.clone()),
Self::Header(inner) => Self::Header(inner.clone()),
Self::Socket(inner) => Self::Socket(inner.clone()),
Self::Custom(inner) => Self::Custom(inner.clone()),
}
}
}
impl<State, Body> fmt::Debug for HttpMatcherKind<State, Body> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::All(inner) => f.debug_tuple("All").field(inner).finish(),
Self::Method(inner) => f.debug_tuple("Method").field(inner).finish(),
Self::Path(inner) => f.debug_tuple("Path").field(inner).finish(),
Self::Domain(inner) => f.debug_tuple("Domain").field(inner).finish(),
Self::Version(inner) => f.debug_tuple("Version").field(inner).finish(),
Self::Any(inner) => f.debug_tuple("Any").field(inner).finish(),
Self::Uri(inner) => f.debug_tuple("Uri").field(inner).finish(),
Self::Header(inner) => f.debug_tuple("Header").field(inner).finish(),
Self::Socket(inner) => f.debug_tuple("Socket").field(inner).finish(),
Self::Custom(_) => f.debug_tuple("Custom").finish(),
}
}
}
impl<State, Body> HttpMatcher<State, Body> {
/// Create a new matcher that matches one or more HTTP methods.
///
/// See [`MethodMatcher`] for more information.
pub fn method(method: MethodMatcher) -> Self {
Self {
kind: HttpMatcherKind::Method(method),
negate: false,
}
}
/// Create a matcher that also matches one or more HTTP methods on top of the existing [`HttpMatcher`] matchers.
///
/// See [`MethodMatcher`] for more information.
pub fn and_method(self, method: MethodMatcher) -> Self {
self.and(Self::method(method))
}
/// Create a matcher that can also match one or more HTTP methods as an alternative to the existing [`HttpMatcher`] matchers.
///
/// See [`MethodMatcher`] for more information.
pub fn or_method(self, method: MethodMatcher) -> Self {
self.or(Self::method(method))
}
/// Create a new matcher that matches [`MethodMatcher::DELETE`] requests.
///
/// See [`MethodMatcher`] for more information.
pub fn method_delete() -> Self {
Self {
kind: HttpMatcherKind::Method(MethodMatcher::DELETE),
negate: false,
}
}
/// Add a new matcher that also matches [`MethodMatcher::DELETE`] on top of the existing [`HttpMatcher`] matchers.
///
/// See [`MethodMatcher`] for more information.
pub fn and_method_delete(self) -> Self {
self.and(Self::method_delete())
}
/// Add a new matcher that can also match [`MethodMatcher::DELETE`]
/// as an alternative tothe existing [`HttpMatcher`] matchers.
///
/// See [`MethodMatcher`] for more information.
pub fn or_method_delete(self) -> Self {
self.or(Self::method_delete())
}
/// Create a new matcher that matches [`MethodMatcher::GET`] requests.
///
/// See [`MethodMatcher`] for more information.
pub fn method_get() -> Self {
Self {
kind: HttpMatcherKind::Method(MethodMatcher::GET),
negate: false,
}
}
/// Add a new matcher that also matches [`MethodMatcher::GET`] on top of the existing [`HttpMatcher`] matchers.
///
/// See [`MethodMatcher`] for more information.
pub fn and_method_get(self) -> Self {
self.and(Self::method_get())
}
/// Add a new matcher that can also match [`MethodMatcher::GET`]
/// as an alternative tothe existing [`HttpMatcher`] matchers.
///
/// See [`MethodMatcher`] for more information.
pub fn or_method_get(self) -> Self {
self.or(Self::method_get())
}
/// Create a new matcher that matches [`MethodMatcher::HEAD`] requests.
///
/// See [`MethodMatcher`] for more information.
pub fn method_head() -> Self {
Self {
kind: HttpMatcherKind::Method(MethodMatcher::HEAD),
negate: false,
}
}
/// Add a new matcher that also matches [`MethodMatcher::HEAD`] on top of the existing [`HttpMatcher`] matchers.
///
/// See [`MethodMatcher`] for more information.
pub fn and_method_head(self) -> Self {
self.and(Self::method_head())
}
/// Add a new matcher that can also match [`MethodMatcher::HEAD`]
/// as an alternative tothe existing [`HttpMatcher`] matchers.
///
/// See [`MethodMatcher`] for more information.
pub fn or_method_head(self) -> Self {
self.or(Self::method_head())
}
/// Create a new matcher that matches [`MethodMatcher::OPTIONS`] requests.
///
/// See [`MethodMatcher`] for more information.
pub fn method_options() -> Self {
Self {
kind: HttpMatcherKind::Method(MethodMatcher::OPTIONS),
negate: false,
}
}
/// Add a new matcher that also matches [`MethodMatcher::OPTIONS`] on top of the existing [`HttpMatcher`] matchers.
///
/// See [`MethodMatcher`] for more information.
pub fn and_method_options(self) -> Self {
self.and(Self::method_options())
}
/// Add a new matcher that can also match [`MethodMatcher::OPTIONS`]
/// as an alternative tothe existing [`HttpMatcher`] matchers.
///
/// See [`MethodMatcher`] for more information.
pub fn or_method_options(self) -> Self {
self.or(Self::method_options())
}
/// Create a new matcher that matches [`MethodMatcher::PATCH`] requests.
///
/// See [`MethodMatcher`] for more information.
pub fn method_patch() -> Self {
Self {
kind: HttpMatcherKind::Method(MethodMatcher::PATCH),
negate: false,
}
}
/// Add a new matcher that also matches [`MethodMatcher::PATCH`] on top of the existing [`HttpMatcher`] matchers.
///
/// See [`MethodMatcher`] for more information.
pub fn and_method_patch(self) -> Self {
self.and(Self::method_patch())
}
/// Add a new matcher that can also match [`MethodMatcher::PATCH`]
/// as an alternative tothe existing [`HttpMatcher`] matchers.
///
/// See [`MethodMatcher`] for more information.
pub fn or_method_patch(self) -> Self {
self.or(Self::method_patch())
}
/// Create a new matcher that matches [`MethodMatcher::POST`] requests.
///
/// See [`MethodMatcher`] for more information.
pub fn method_post() -> Self {
Self {
kind: HttpMatcherKind::Method(MethodMatcher::POST),
negate: false,
}
}
/// Add a new matcher that also matches [`MethodMatcher::POST`] on top of the existing [`HttpMatcher`] matchers.
///
/// See [`MethodMatcher`] for more information.
pub fn and_method_post(self) -> Self {
self.and(Self::method_post())
}
/// Add a new matcher that can also match [`MethodMatcher::POST`]
/// as an alternative tothe existing [`HttpMatcher`] matchers.
///
/// See [`MethodMatcher`] for more information.
pub fn or_method_post(self) -> Self {
self.or(Self::method_post())
}
/// Create a new matcher that matches [`MethodMatcher::PUT`] requests.
///
/// See [`MethodMatcher`] for more information.
pub fn method_put() -> Self {
Self {
kind: HttpMatcherKind::Method(MethodMatcher::PUT),
negate: false,
}
}
/// Add a new matcher that also matches [`MethodMatcher::PUT`] on top of the existing [`HttpMatcher`] matchers.
///
/// See [`MethodMatcher`] for more information.
pub fn and_method_put(self) -> Self {
self.and(Self::method_put())
}
/// Add a new matcher that can also match [`MethodMatcher::PUT`]
/// as an alternative tothe existing [`HttpMatcher`] matchers.
///
/// See [`MethodMatcher`] for more information.
pub fn or_method_put(self) -> Self {
self.or(Self::method_put())
}
/// Create a new matcher that matches [`MethodMatcher::TRACE`] requests.
///
/// See [`MethodMatcher`] for more information.
pub fn method_trace() -> Self {
Self {
kind: HttpMatcherKind::Method(MethodMatcher::TRACE),
negate: false,
}
}
/// Add a new matcher that also matches [`MethodMatcher::TRACE`] on top of the existing [`HttpMatcher`] matchers.
///
/// See [`MethodMatcher`] for more information.
pub fn and_method_trace(self) -> Self {
self.and(Self::method_trace())
}
/// Add a new matcher that can also match [`MethodMatcher::TRACE`]
/// as an alternative tothe existing [`HttpMatcher`] matchers.
///
/// See [`MethodMatcher`] for more information.
pub fn or_method_trace(self) -> Self {
self.or(Self::method_trace())
}
/// Create a [`DomainMatcher`] matcher, matching on the exact given [`Domain`].
pub fn domain(domain: Domain) -> Self {
Self {
kind: HttpMatcherKind::Domain(DomainMatcher::exact(domain)),
negate: false,
}
}
/// Create a [`DomainMatcher`] matcher, matching on the exact given [`Domain`]
/// or a subdomain of it.
pub fn subdomain(domain: Domain) -> Self {
Self {
kind: HttpMatcherKind::Domain(DomainMatcher::sub(domain)),
negate: false,
}
}
/// Create a [`DomainMatcher`] matcher to also match on top of the existing set of [`HttpMatcher`] matchers.
///
/// See [`Self::domain`] for more information.
pub fn and_domain(self, domain: Domain) -> Self {
self.and(Self::domain(domain))
}
/// Create a sub [`DomainMatcher`] matcher to also match on top of the existing set of [`HttpMatcher`] matchers.
///
/// See [`Self::subdomain`] for more information.
pub fn and_subdomain(self, domain: Domain) -> Self {
self.and(Self::subdomain(domain))
}
/// Create a [`DomainMatcher`] matcher to match as an alternative to the existing set of [`HttpMatcher`] matchers.
///
/// See [`Self::domain`] for more information.
pub fn or_domain(self, domain: Domain) -> Self {
self.or(Self::domain(domain))
}
/// Create a sub [`DomainMatcher`] matcher to match as an alternative to the existing set of [`HttpMatcher`] matchers.
///
/// See [`Self::subdomain`] for more information.
pub fn or_subdomain(self, domain: Domain) -> Self {
self.or(Self::subdomain(domain))
}
/// Create a [`VersionMatcher`] matcher.
pub fn version(version: VersionMatcher) -> Self {
Self {
kind: HttpMatcherKind::Version(version),
negate: false,
}
}
/// Add a [`VersionMatcher`] matcher to matcher on top of the existing set of [`HttpMatcher`] matchers.
///
/// See [`VersionMatcher`] for more information.
pub fn and_version(self, version: VersionMatcher) -> Self {
self.and(Self::version(version))
}
/// Create a [`VersionMatcher`] matcher to match as an alternative to the existing set of [`HttpMatcher`] matchers.
///
/// See [`VersionMatcher`] for more information.
pub fn or_version(self, version: VersionMatcher) -> Self {
self.or(Self::version(version))
}
/// Create a [`UriMatcher`] matcher.
pub fn uri(re: impl AsRef<str>) -> Self {
Self {
kind: HttpMatcherKind::Uri(UriMatcher::new(re)),
negate: false,
}
}
/// Create a [`UriMatcher`] matcher to match on top of the existing set of [`HttpMatcher`] matchers.
///
/// See [`UriMatcher`] for more information.
pub fn and_uri(self, re: impl AsRef<str>) -> Self {
self.and(Self::uri(re))
}
/// Create a [`UriMatcher`] matcher to match as an alternative to the existing set of [`HttpMatcher`] matchers.
///
/// See [`UriMatcher`] for more information.
pub fn or_uri(self, re: impl AsRef<str>) -> Self {
self.or(Self::uri(re))
}
/// Create a [`PathMatcher`] matcher.
pub fn path(path: impl AsRef<str>) -> Self {
Self {
kind: HttpMatcherKind::Path(PathMatcher::new(path)),
negate: false,
}
}
/// Add a [`PathMatcher`] to match on top of the existing set of [`HttpMatcher`] matchers.
///
/// See [`PathMatcher`] for more information.
pub fn and_path(self, path: impl AsRef<str>) -> Self {
self.and(Self::path(path))
}
/// Create a [`PathMatcher`] matcher to match as an alternative to the existing set of [`HttpMatcher`] matchers.
///
/// See [`PathMatcher`] for more information.
pub fn or_path(self, path: impl AsRef<str>) -> Self {
self.or(Self::path(path))
}
/// Create a [`HeaderMatcher`] matcher.
pub fn header(name: http::header::HeaderName, value: http::header::HeaderValue) -> Self {
Self {
kind: HttpMatcherKind::Header(HeaderMatcher::is(name, value)),
negate: false,
}
}
/// Add a [`HeaderMatcher`] to match on top of the existing set of [`HttpMatcher`] matchers.
///
/// See [`HeaderMatcher`] for more information.
pub fn and_header(
self,
name: http::header::HeaderName,
value: http::header::HeaderValue,
) -> Self {
self.and(Self::header(name, value))
}
/// Create a [`HeaderMatcher`] matcher to match as an alternative to the existing set of [`HttpMatcher`] matchers.
///
/// See [`HeaderMatcher`] for more information.
pub fn or_header(
self,
name: http::header::HeaderName,
value: http::header::HeaderValue,
) -> Self {
self.or(Self::header(name, value))
}
/// Create a [`HeaderMatcher`] matcher when the given header exists
/// to match on the existence of a header.
pub fn header_exists(name: http::header::HeaderName) -> Self {
Self {
kind: HttpMatcherKind::Header(HeaderMatcher::exists(name)),
negate: false,
}
}
/// Add a [`HeaderMatcher`] to match when the given header exists
/// on top of the existing set of [`HttpMatcher`] matchers.
///
/// See [`HeaderMatcher`] for more information.
pub fn and_header_exists(self, name: http::header::HeaderName) -> Self {
self.and(Self::header_exists(name))
}
/// Create a [`HeaderMatcher`] matcher to match when the given header exists
/// as an alternative to the existing set of [`HttpMatcher`] matchers.
///
/// See [`HeaderMatcher`] for more information.
pub fn or_header_exists(self, name: http::header::HeaderName) -> Self {
self.or(Self::header_exists(name))
}
/// Create a [`HeaderMatcher`] matcher to match on it containing the given value.
pub fn header_contains(
name: http::header::HeaderName,
value: http::header::HeaderValue,
) -> Self {
Self {
kind: HttpMatcherKind::Header(HeaderMatcher::contains(name, value)),
negate: false,
}
}
/// Add a [`HeaderMatcher`] to match when it contains the given value
/// on top of the existing set of [`HttpMatcher`] matchers.
///
/// See [`HeaderMatcher`] for more information.
pub fn and_header_contains(
self,
name: http::header::HeaderName,
value: http::header::HeaderValue,
) -> Self {
self.and(Self::header_contains(name, value))
}
/// Create a [`HeaderMatcher`] matcher to match if it contains the given value
/// as an alternative to the existing set of [`HttpMatcher`] matchers.
///
/// See [`HeaderMatcher`] for more information.
pub fn or_header_contains(
self,
name: http::header::HeaderName,
value: http::header::HeaderValue,
) -> Self {
self.or(Self::header_contains(name, value))
}
/// Create a [`SocketMatcher`] matcher.
pub fn socket(socket: SocketMatcher<State, Request<Body>>) -> Self {
Self {
kind: HttpMatcherKind::Socket(socket),
negate: false,
}
}
/// Add a [`SocketMatcher`] matcher to match on top of the existing set of [`HttpMatcher`] matchers.
///
/// See [`SocketMatcher`] for more information.
pub fn and_socket(self, socket: SocketMatcher<State, Request<Body>>) -> Self {
self.and(Self::socket(socket))
}
/// Create a [`SocketMatcher`] matcher to match as an alternative to the existing set of [`HttpMatcher`] matchers.
///
/// See [`SocketMatcher`] for more information.
pub fn or_socket(self, socket: SocketMatcher<State, Request<Body>>) -> Self {
self.or(Self::socket(socket))
}
/// Create a [`PathMatcher`] matcher to match for a GET request.
pub fn get(path: impl AsRef<str>) -> Self {
Self::method_get().and_path(path)
}
/// Create a matcher that matches according to a custom predicate.
///
/// See [`rama_core::matcher::Matcher`] for more information.
pub fn custom<M>(matcher: M) -> Self
where
M: rama_core::matcher::Matcher<State, Request<Body>>,
{
Self {
kind: HttpMatcherKind::Custom(Arc::new(matcher)),
negate: false,
}
}
/// Add a custom matcher to match on top of the existing set of [`HttpMatcher`] matchers.
///
/// See [`rama_core::matcher::Matcher`] for more information.
pub fn and_custom<M>(self, matcher: M) -> Self
where
M: rama_core::matcher::Matcher<State, Request<Body>>,
{
self.and(Self::custom(matcher))
}
/// Create a custom matcher to match as an alternative to the existing set of [`HttpMatcher`] matchers.
///
/// See [`rama_core::matcher::Matcher`] for more information.
pub fn or_custom<M>(self, matcher: M) -> Self
where
M: rama_core::matcher::Matcher<State, Request<Body>>,
{
self.or(Self::custom(matcher))
}
/// Create a [`PathMatcher`] matcher to match for a POST request.
pub fn post(path: impl AsRef<str>) -> Self {
Self::method_post().and_path(path)
}
/// Create a [`PathMatcher`] matcher to match for a PUT request.
pub fn put(path: impl AsRef<str>) -> Self {
Self::method_put().and_path(path)
}
/// Create a [`PathMatcher`] matcher to match for a DELETE request.
pub fn delete(path: impl AsRef<str>) -> Self {
Self::method_delete().and_path(path)
}
/// Create a [`PathMatcher`] matcher to match for a PATCH request.
pub fn patch(path: impl AsRef<str>) -> Self {
Self::method_patch().and_path(path)
}
/// Create a [`PathMatcher`] matcher to match for a HEAD request.
pub fn head(path: impl AsRef<str>) -> Self {
Self::method_head().and_path(path)
}
/// Create a [`PathMatcher`] matcher to match for a OPTIONS request.
pub fn options(path: impl AsRef<str>) -> Self {
Self::method_options().and_path(path)
}
/// Create a [`PathMatcher`] matcher to match for a TRACE request.
pub fn trace(path: impl AsRef<str>) -> Self {
Self::method_trace().and_path(path)
}
/// Add a [`HttpMatcher`] to match on top of the existing set of [`HttpMatcher`] matchers.
pub fn and(mut self, matcher: HttpMatcher<State, Body>) -> Self {
match (self.negate, &mut self.kind) {
(false, HttpMatcherKind::All(v)) => {
v.push(matcher);
self
}
_ => HttpMatcher {
kind: HttpMatcherKind::All(vec![self, matcher]),
negate: false,
},
}
}
/// Create a [`HttpMatcher`] matcher to match as an alternative to the existing set of [`HttpMatcher`] matchers.
pub fn or(mut self, matcher: HttpMatcher<State, Body>) -> Self {
match (self.negate, &mut self.kind) {
(false, HttpMatcherKind::Any(v)) => {
v.push(matcher);
self
}
_ => HttpMatcher {
kind: HttpMatcherKind::Any(vec![self, matcher]),
negate: false,
},
}
}
/// Negate the current matcher
pub fn negate(self) -> Self {
Self {
kind: self.kind,
negate: true,
}
}
}
impl<State, Body> rama_core::matcher::Matcher<State, Request<Body>> for HttpMatcher<State, Body>
where
State: Clone + Send + Sync + 'static,
Body: Send + 'static,
{
fn matches(
&self,
ext: Option<&mut Extensions>,
ctx: &Context<State>,
req: &Request<Body>,
) -> bool {
let matches = self.kind.matches(ext, ctx, req);
if self.negate {
!matches
} else {
matches
}
}
}
impl<State, Body> rama_core::matcher::Matcher<State, Request<Body>> for HttpMatcherKind<State, Body>
where
State: Clone + Send + Sync + 'static,
Body: Send + 'static,
{
fn matches(
&self,
ext: Option<&mut Extensions>,
ctx: &Context<State>,
req: &Request<Body>,
) -> bool {
match self {
HttpMatcherKind::All(all) => all.iter().matches_and(ext, ctx, req),
HttpMatcherKind::Method(method) => method.matches(ext, ctx, req),
HttpMatcherKind::Path(path) => path.matches(ext, ctx, req),
HttpMatcherKind::Domain(domain) => domain.matches(ext, ctx, req),
HttpMatcherKind::Version(version) => version.matches(ext, ctx, req),
HttpMatcherKind::Uri(uri) => uri.matches(ext, ctx, req),
HttpMatcherKind::Header(header) => header.matches(ext, ctx, req),
HttpMatcherKind::Socket(socket) => socket.matches(ext, ctx, req),
HttpMatcherKind::Any(all) => all.iter().matches_or(ext, ctx, req),
HttpMatcherKind::Custom(matcher) => matcher.matches(ext, ctx, req),
}
}
}
#[cfg(test)]
mod test {
use itertools::Itertools;
use rama_core::matcher::Matcher;
use super::*;
struct BooleanMatcher(bool);
impl Matcher<(), Request<()>> for BooleanMatcher {
fn matches(
&self,
_ext: Option<&mut Extensions>,
_ctx: &Context<()>,
_req: &Request<()>,
) -> bool {
self.0
}
}
#[test]
fn test_matcher_and_combination() {
for v in [true, false].into_iter().permutations(3) {
let expected = v[0] && v[1] && v[2];
let a = HttpMatcher::custom(BooleanMatcher(v[0]));
let b = HttpMatcher::custom(BooleanMatcher(v[1]));
let c = HttpMatcher::custom(BooleanMatcher(v[2]));
let matcher = a.and(b).and(c);
let req = Request::builder().body(()).unwrap();
assert_eq!(
matcher.matches(None, &Context::default(), &req),
expected,
"({:#?}).matches({:#?})",
matcher,
req
);
}
}
#[test]
fn test_matcher_negation_with_and_combination() {
for v in [true, false].into_iter().permutations(3) {
let expected = !v[0] && v[1] && v[2];
let a = HttpMatcher::custom(BooleanMatcher(v[0]));
let b = HttpMatcher::custom(BooleanMatcher(v[1]));
let c = HttpMatcher::custom(BooleanMatcher(v[2]));
let matcher = a.negate().and(b).and(c);
let req = Request::builder().body(()).unwrap();
assert_eq!(
matcher.matches(None, &Context::default(), &req),
expected,
"({:#?}).matches({:#?})",
matcher,
req
);
}
}
#[test]
fn test_matcher_and_combination_negated() {
for v in [true, false].into_iter().permutations(3) {
let expected = !(v[0] && v[1] && v[2]);
let a = HttpMatcher::custom(BooleanMatcher(v[0]));
let b = HttpMatcher::custom(BooleanMatcher(v[1]));
let c = HttpMatcher::custom(BooleanMatcher(v[2]));
let matcher = a.and(b).and(c).negate();
let req = Request::builder().body(()).unwrap();
assert_eq!(
matcher.matches(None, &Context::default(), &req),
expected,
"({:#?}).matches({:#?})",
matcher,
req
);
}
}
#[test]
fn test_matcher_ors_combination() {
for v in [true, false].into_iter().permutations(3) {
let expected = v[0] || v[1] || v[2];
let a = HttpMatcher::custom(BooleanMatcher(v[0]));
let b = HttpMatcher::custom(BooleanMatcher(v[1]));
let c = HttpMatcher::custom(BooleanMatcher(v[2]));
let matcher = a.or(b).or(c);
let req = Request::builder().body(()).unwrap();
assert_eq!(
matcher.matches(None, &Context::default(), &req),
expected,
"({:#?}).matches({:#?})",
matcher,
req
);
}
}
#[test]
fn test_matcher_negation_with_ors_combination() {
for v in [true, false].into_iter().permutations(3) {
let expected = !v[0] || v[1] || v[2];
let a = HttpMatcher::custom(BooleanMatcher(v[0]));
let b = HttpMatcher::custom(BooleanMatcher(v[1]));
let c = HttpMatcher::custom(BooleanMatcher(v[2]));
let matcher = a.negate().or(b).or(c);
let req = Request::builder().body(()).unwrap();
assert_eq!(
matcher.matches(None, &Context::default(), &req),
expected,
"({:#?}).matches({:#?})",
matcher,
req
);
}
}
#[test]
fn test_matcher_ors_combination_negated() {
for v in [true, false].into_iter().permutations(3) {
let expected = !(v[0] || v[1] || v[2]);
let a = HttpMatcher::custom(BooleanMatcher(v[0]));
let b = HttpMatcher::custom(BooleanMatcher(v[1]));
let c = HttpMatcher::custom(BooleanMatcher(v[2]));
let matcher = a.or(b).or(c).negate();
let req = Request::builder().body(()).unwrap();
assert_eq!(
matcher.matches(None, &Context::default(), &req),
expected,
"({:#?}).matches({:#?})",
matcher,
req
);
}
}
#[test]
fn test_matcher_or_and_or_and_negation() {
for v in [true, false].into_iter().permutations(5) {
let expected = (v[0] || v[1]) && (v[2] || v[3]) && !v[4];
let a = HttpMatcher::custom(BooleanMatcher(v[0]));
let b = HttpMatcher::custom(BooleanMatcher(v[1]));
let c = HttpMatcher::custom(BooleanMatcher(v[2]));
let d = HttpMatcher::custom(BooleanMatcher(v[3]));
let e = HttpMatcher::custom(BooleanMatcher(v[4]));
let matcher = (a.or(b)).and(c.or(d)).and(e.negate());
let req = Request::builder().body(()).unwrap();
assert_eq!(
matcher.matches(None, &Context::default(), &req),
expected,
"({:#?}).matches({:#?})",
matcher,
req
);
}
}
}