pub struct MiddlewareConfig { /* private fields */ }Expand description
The application’s middleware. The global middleware runs on every request before routing, and
the web and api groups run on the routes in src/routes/web.rs and src/routes/api.rs.
Before any middleware runs, the kernel applies the trusted proxies. The default middleware are:
- The global middleware, in order: [
SecurityHeaders],TrustHosts, [HandleCors], [SpoofMethods], [TrimStrings], and [ConvertEmptyStringsToNull]. - The
webgroup, in order: [EncryptCookies], [StartSession], and [PreventRequestForgery]. - The
apigroup, which is empty.
Unless middleware sets other limits, every route also has a LimitBody of
2 MB for a body read into memory, a LimitUploads of 16 MB for a
multipart form, and a Timeout of 30 seconds.
To customize a default, call replace. To drop one, call
remove:
use laravel::http::MiddlewareConfig;
use laravel::http::middleware::{PreventRequestForgery, TrimStrings};
fn configure(middleware: &mut MiddlewareConfig) {
middleware.trust_proxies(["10.0.0.0/8"]);
middleware.replace(PreventRequestForgery::new().except(["webhooks/*"]));
middleware.remove::<TrimStrings>();
}Implementations§
Source§impl MiddlewareConfig
impl MiddlewareConfig
Sourcepub fn trust_proxies<I, S>(&mut self, proxies: I) -> &mut MiddlewareConfig
pub fn trust_proxies<I, S>(&mut self, proxies: I) -> &mut MiddlewareConfig
Trust the X-Forwarded-* and Forwarded headers from the given proxies. Each proxy is an
IP address, a network such as 10.0.0.0/8, or * for any address.
The kernel removes those headers from requests that come from any other address. It reads
the client’s address from the nearest X-Forwarded-For entry that isn’t a trusted proxy.
An application that never calls trust_proxies trusts no proxies, except on Laravel
Cloud, where it trusts every proxy.
§Panics
Panics when a proxy isn’t an IP address or network. For a list you don’t control, such
as one read from the environment, use try_trust_proxies.
Sourcepub fn try_trust_proxies<I, S>(
&mut self,
proxies: I,
) -> Result<&mut MiddlewareConfig, InvalidProxy>
pub fn try_trust_proxies<I, S>( &mut self, proxies: I, ) -> Result<&mut MiddlewareConfig, InvalidProxy>
Trust proxies as trust_proxies does, but return an error instead
of panicking when a proxy isn’t an IP address or network. After an error, the trusted
proxies stay as they were.
use laravel::http::MiddlewareConfig;
fn configure(middleware: &mut MiddlewareConfig, proxies: &str) {
if let Err(error) = middleware.try_trust_proxies(proxies.split(',')) {
tracing::warn!("trusting no proxies: {error}");
}
}§Errors
Fails with the first proxy that isn’t an IP address or network.
Sourcepub fn trust_hosts<I, S>(&mut self, hosts: I) -> &mut MiddlewareConfig
pub fn trust_hosts<I, S>(&mut self, hosts: I) -> &mut MiddlewareConfig
Trust requests for only these hosts, instead of the host in APP_URL and its subdomains.
Each host is a name such as example.com, or *.example.com for every subdomain. To learn
how Laravel checks hosts, check out TrustHosts.
Sourcepub fn replace(
&mut self,
middleware: impl IntoMiddleware,
) -> &mut MiddlewareConfig
pub fn replace( &mut self, middleware: impl IntoMiddleware, ) -> &mut MiddlewareConfig
Replace the middleware of the same type, keeping its position. replace looks through the
global middleware and the web and api groups.
Sourcepub fn remove<M>(&mut self) -> &mut MiddlewareConfigwhere
M: 'static,
pub fn remove<M>(&mut self) -> &mut MiddlewareConfigwhere
M: 'static,
Remove middleware of the given type from the global middleware and the web and api
groups.
Sourcepub fn append(
&mut self,
middleware: impl IntoMiddleware,
) -> &mut MiddlewareConfig
pub fn append( &mut self, middleware: impl IntoMiddleware, ) -> &mut MiddlewareConfig
Add middleware to the end of the global middleware, so it runs after the others.
Sourcepub fn prepend(
&mut self,
middleware: impl IntoMiddleware,
) -> &mut MiddlewareConfig
pub fn prepend( &mut self, middleware: impl IntoMiddleware, ) -> &mut MiddlewareConfig
Add middleware to the start of the global middleware, so it runs before the others.
Sourcepub fn web(
&mut self,
configure: impl FnOnce(&mut MiddlewareGroup),
) -> &mut MiddlewareConfig
pub fn web( &mut self, configure: impl FnOnce(&mut MiddlewareGroup), ) -> &mut MiddlewareConfig
Configure the web group, which runs on routes that use sessions, cookies, and pages.
Sourcepub fn api(
&mut self,
configure: impl FnOnce(&mut MiddlewareGroup),
) -> &mut MiddlewareConfig
pub fn api( &mut self, configure: impl FnOnce(&mut MiddlewareGroup), ) -> &mut MiddlewareConfig
Configure the api group, which runs on stateless API routes.
Trait Implementations§
Source§impl Clone for MiddlewareConfig
impl Clone for MiddlewareConfig
Source§fn clone(&self) -> MiddlewareConfig
fn clone(&self) -> MiddlewareConfig
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for MiddlewareConfig
impl Debug for MiddlewareConfig
Source§impl Default for MiddlewareConfig
impl Default for MiddlewareConfig
Source§fn default() -> MiddlewareConfig
fn default() -> MiddlewareConfig
Auto Trait Implementations§
impl !RefUnwindSafe for MiddlewareConfig
impl !UnwindSafe for MiddlewareConfig
impl Forget for MiddlewareConfig
impl Freeze for MiddlewareConfig
impl Move for MiddlewareConfig
impl Send for MiddlewareConfig
impl Sync for MiddlewareConfig
impl Unpin for MiddlewareConfig
impl UnsafeUnpin for MiddlewareConfigwhere
*const u8 is !null: UnsafeUnpin,
PhantomData<Entry>: UnsafeUnpin,
PhantomData<IpNet>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<K> ForeignKey<K> for K
impl<K> ForeignKey<K> for K
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more