2020-07-03 04:20:28 +00:00
|
|
|
macro_rules! pub_request_impl {
|
|
|
|
($import:literal $($prefix:tt $suffix:tt)?) =>
|
2020-06-28 17:52:52 +00:00
|
|
|
{
|
2021-04-14 00:40:22 +00:00
|
|
|
/// Borrows the inner `Request` as seen by Rocket.
|
2020-06-28 17:52:52 +00:00
|
|
|
///
|
2021-03-05 05:50:37 +00:00
|
|
|
/// Note that no routing has occurred and that there is no remote
|
2021-04-14 00:40:22 +00:00
|
|
|
/// address unless one has been explicitly set with
|
|
|
|
/// [`set_remote()`](Request::set_remote()).
|
2021-03-05 05:50:37 +00:00
|
|
|
///
|
2020-07-03 04:20:28 +00:00
|
|
|
/// # Example
|
2020-06-28 17:52:52 +00:00
|
|
|
///
|
2020-07-03 04:20:28 +00:00
|
|
|
/// ```rust
|
|
|
|
#[doc = $import]
|
2020-06-28 17:52:52 +00:00
|
|
|
///
|
2020-07-03 04:20:28 +00:00
|
|
|
/// # Client::_test(|_, request, _| {
|
|
|
|
/// let request: LocalRequest = request;
|
|
|
|
/// let inner: &rocket::Request = request.inner();
|
|
|
|
/// # });
|
|
|
|
/// ```
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn inner(&self) -> &Request<'c> {
|
|
|
|
self._request()
|
|
|
|
}
|
|
|
|
|
2021-04-14 00:40:22 +00:00
|
|
|
/// Mutably borrows the inner `Request` as seen by Rocket.
|
|
|
|
///
|
|
|
|
/// Note that no routing has occurred and that there is no remote
|
|
|
|
/// address unless one has been explicitly set with
|
|
|
|
/// [`set_remote()`](Request::set_remote()).
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
#[doc = $import]
|
|
|
|
///
|
|
|
|
/// # Client::_test(|_, request, _| {
|
|
|
|
/// let mut request: LocalRequest = request;
|
|
|
|
/// let inner: &mut rocket::Request = request.inner_mut();
|
|
|
|
/// # });
|
|
|
|
/// ```
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn inner_mut(&mut self) -> &mut Request<'c> {
|
|
|
|
self._request_mut()
|
|
|
|
}
|
|
|
|
|
2020-07-03 04:20:28 +00:00
|
|
|
/// Add a header to this request.
|
2020-06-28 17:52:52 +00:00
|
|
|
///
|
2020-07-03 04:20:28 +00:00
|
|
|
/// Any type that implements `Into<Header>` can be used here. Among
|
|
|
|
/// others, this includes [`ContentType`] and [`Accept`].
|
2020-06-28 17:52:52 +00:00
|
|
|
///
|
2020-07-03 04:20:28 +00:00
|
|
|
/// [`ContentType`]: crate::http::ContentType
|
|
|
|
/// [`Accept`]: crate::http::Accept
|
2020-06-28 17:52:52 +00:00
|
|
|
///
|
2020-07-03 04:20:28 +00:00
|
|
|
/// # Examples
|
2020-06-28 17:52:52 +00:00
|
|
|
///
|
2020-07-03 04:20:28 +00:00
|
|
|
/// Add the Content-Type header:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
#[doc = $import]
|
|
|
|
/// use rocket::http::Header;
|
|
|
|
/// use rocket::http::ContentType;
|
|
|
|
///
|
|
|
|
/// # Client::_test(|_, request, _| {
|
|
|
|
/// let request: LocalRequest = request;
|
|
|
|
/// let req = request
|
|
|
|
/// .header(ContentType::JSON)
|
|
|
|
/// .header(Header::new("X-Custom", "custom-value"));
|
|
|
|
/// # });
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn header<H>(mut self, header: H) -> Self
|
|
|
|
where H: Into<crate::http::Header<'static>>
|
|
|
|
{
|
|
|
|
self._request_mut().add_header(header.into());
|
|
|
|
self
|
|
|
|
}
|
Overhaul URI types.
This is fairly large commit with several entangled logical changes.
The primary change in this commit is to completely overhaul how URI
handling in Rocket works. Prior to this commit, the `Uri` type acted as
an origin API. Its parser was minimal and lenient, allowing URIs that
were invalid according to RFC 7230. By contrast, the new `Uri` type
brings with it a strict RFC 7230 compliant parser. The `Uri` type now
represents any kind of valid URI, not simply `Origin` types. Three new
URI types were introduced:
* `Origin` - represents valid origin URIs
* `Absolute` - represents valid absolute URIs
* `Authority` - represents valid authority URIs
The `Origin` type replaces `Uri` in many cases:
* As fields and method inputs of `Route`
* The `&Uri` request guard is now `&Origin`
* The `uri!` macro produces an `Origin` instead of a `Uri`
The strict nature of URI parsing cascaded into the following changes:
* Several `Route` methods now `panic!` on invalid URIs
* The `Rocket::mount()` method is (correctly) stricter with URIs
* The `Redirect` constructors take a `TryInto<Uri>` type
* Dispatching of a `LocalRequest` correctly validates URIs
Overall, URIs are now properly and uniformly handled throughout Rocket's
codebase, resulting in a more reliable and correct system.
In addition to these URI changes, the following changes are also part of
this commit:
* The `LocalRequest::cloned_dispatch()` method was removed in favor of
chaining `.clone().dispatch()`.
* The entire Rocket codebase uses `crate` instead of `pub(crate)` as a
visibility modifier.
* Rocket uses the `crate_visibility_modifier` and `try_from` features.
A note on unsafety: this commit introduces many uses of `unsafe` in the
URI parser. All of these uses are a result of unsafely transforming byte
slices (`&[u8]` or similar) into strings (`&str`). The parser ensures
that these casts are safe, but of course, we must label their use
`unsafe`. The parser was written to be as generic and efficient as
possible and thus can parse directly from byte sources. Rocket, however,
does not make use of this fact and so would be able to remove all uses
of `unsafe` by parsing from an existing `&str`. This should be
considered in the future.
Fixes #443.
Resolves #263.
2018-07-29 01:26:15 +00:00
|
|
|
|
2020-07-03 04:20:28 +00:00
|
|
|
/// Adds a header to this request without consuming `self`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Add the Content-Type header:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
#[doc = $import]
|
|
|
|
/// use rocket::http::ContentType;
|
|
|
|
///
|
|
|
|
/// # Client::_test(|_, mut request, _| {
|
|
|
|
/// let mut request: LocalRequest = request;
|
|
|
|
/// request.add_header(ContentType::JSON);
|
|
|
|
/// # });
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn add_header<H>(&mut self, header: H)
|
|
|
|
where H: Into<crate::http::Header<'static>>
|
|
|
|
{
|
|
|
|
self._request_mut().add_header(header.into());
|
|
|
|
}
|
2018-04-15 02:41:37 +00:00
|
|
|
|
2020-07-03 04:20:28 +00:00
|
|
|
/// Set the remote address of this request.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Set the remote address to "8.8.8.8:80":
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
#[doc = $import]
|
|
|
|
///
|
|
|
|
/// # Client::_test(|_, request, _| {
|
|
|
|
/// let request: LocalRequest = request;
|
|
|
|
/// let address = "8.8.8.8:80".parse().unwrap();
|
|
|
|
/// let req = request.remote(address);
|
|
|
|
/// # });
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn remote(mut self, address: std::net::SocketAddr) -> Self {
|
2021-04-14 00:40:22 +00:00
|
|
|
self.set_remote(address);
|
2020-07-03 04:20:28 +00:00
|
|
|
self
|
|
|
|
}
|
Overhaul URI types.
This is fairly large commit with several entangled logical changes.
The primary change in this commit is to completely overhaul how URI
handling in Rocket works. Prior to this commit, the `Uri` type acted as
an origin API. Its parser was minimal and lenient, allowing URIs that
were invalid according to RFC 7230. By contrast, the new `Uri` type
brings with it a strict RFC 7230 compliant parser. The `Uri` type now
represents any kind of valid URI, not simply `Origin` types. Three new
URI types were introduced:
* `Origin` - represents valid origin URIs
* `Absolute` - represents valid absolute URIs
* `Authority` - represents valid authority URIs
The `Origin` type replaces `Uri` in many cases:
* As fields and method inputs of `Route`
* The `&Uri` request guard is now `&Origin`
* The `uri!` macro produces an `Origin` instead of a `Uri`
The strict nature of URI parsing cascaded into the following changes:
* Several `Route` methods now `panic!` on invalid URIs
* The `Rocket::mount()` method is (correctly) stricter with URIs
* The `Redirect` constructors take a `TryInto<Uri>` type
* Dispatching of a `LocalRequest` correctly validates URIs
Overall, URIs are now properly and uniformly handled throughout Rocket's
codebase, resulting in a more reliable and correct system.
In addition to these URI changes, the following changes are also part of
this commit:
* The `LocalRequest::cloned_dispatch()` method was removed in favor of
chaining `.clone().dispatch()`.
* The entire Rocket codebase uses `crate` instead of `pub(crate)` as a
visibility modifier.
* Rocket uses the `crate_visibility_modifier` and `try_from` features.
A note on unsafety: this commit introduces many uses of `unsafe` in the
URI parser. All of these uses are a result of unsafely transforming byte
slices (`&[u8]` or similar) into strings (`&str`). The parser ensures
that these casts are safe, but of course, we must label their use
`unsafe`. The parser was written to be as generic and efficient as
possible and thus can parse directly from byte sources. Rocket, however,
does not make use of this fact and so would be able to remove all uses
of `unsafe` by parsing from an existing `&str`. This should be
considered in the future.
Fixes #443.
Resolves #263.
2018-07-29 01:26:15 +00:00
|
|
|
|
2020-07-03 04:20:28 +00:00
|
|
|
/// Add a cookie to this request.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Add `user_id` cookie:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
#[doc = $import]
|
|
|
|
/// use rocket::http::Cookie;
|
|
|
|
///
|
|
|
|
/// # Client::_test(|_, request, _| {
|
|
|
|
/// let request: LocalRequest = request;
|
|
|
|
/// let req = request
|
2023-09-29 06:50:29 +00:00
|
|
|
/// .cookie(("username", "sb"))
|
|
|
|
/// .cookie(("user_id", "12"));
|
2020-07-03 04:20:28 +00:00
|
|
|
/// # });
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
2023-09-29 06:50:29 +00:00
|
|
|
pub fn cookie<'a, C>(mut self, cookie: C) -> Self
|
|
|
|
where C: Into<crate::http::Cookie<'a>>
|
|
|
|
{
|
|
|
|
self._request_mut().cookies_mut().add_original(cookie.into().into_owned());
|
2020-07-03 04:20:28 +00:00
|
|
|
self
|
|
|
|
}
|
2019-08-24 17:27:10 +00:00
|
|
|
|
2020-07-03 04:20:28 +00:00
|
|
|
/// Add all of the cookies in `cookies` to this request.
|
|
|
|
///
|
2020-07-22 19:21:19 +00:00
|
|
|
/// # Example
|
2020-07-03 04:20:28 +00:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
#[doc = $import]
|
|
|
|
/// use rocket::http::Cookie;
|
|
|
|
///
|
|
|
|
/// # Client::_test(|_, request, _| {
|
|
|
|
/// let request: LocalRequest = request;
|
2023-09-29 06:50:29 +00:00
|
|
|
/// let cookies = vec![("a", "b"), ("c", "d")];
|
2020-07-03 04:20:28 +00:00
|
|
|
/// let req = request.cookies(cookies);
|
|
|
|
/// # });
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
2023-09-29 06:50:29 +00:00
|
|
|
pub fn cookies<'a, C, I>(mut self, cookies: I) -> Self
|
|
|
|
where C: Into<crate::http::Cookie<'a>>,
|
|
|
|
I: IntoIterator<Item = C>
|
2020-07-22 19:21:19 +00:00
|
|
|
{
|
2020-07-03 04:20:28 +00:00
|
|
|
for cookie in cookies {
|
2023-09-29 06:50:29 +00:00
|
|
|
let cookie: crate::http::Cookie<'_> = cookie.into();
|
2020-10-15 04:37:16 +00:00
|
|
|
self._request_mut().cookies_mut().add_original(cookie.into_owned());
|
2019-08-24 17:27:10 +00:00
|
|
|
}
|
2017-06-06 20:41:04 +00:00
|
|
|
|
2020-07-03 04:20:28 +00:00
|
|
|
self
|
|
|
|
}
|
2017-06-30 09:06:03 +00:00
|
|
|
|
2020-07-03 04:20:28 +00:00
|
|
|
/// Add a [private cookie] to this request.
|
|
|
|
///
|
2020-07-22 19:21:19 +00:00
|
|
|
/// [private cookie]: crate::http::CookieJar::add_private()
|
2020-07-03 04:20:28 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Add `user_id` as a private cookie:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
#[doc = $import]
|
|
|
|
/// use rocket::http::Cookie;
|
|
|
|
///
|
|
|
|
/// # Client::_test(|_, request, _| {
|
|
|
|
/// let request: LocalRequest = request;
|
2023-09-29 06:50:29 +00:00
|
|
|
/// let req = request.private_cookie(("user_id", "sb"));
|
2020-07-03 04:20:28 +00:00
|
|
|
/// # });
|
|
|
|
/// ```
|
2020-07-22 19:21:19 +00:00
|
|
|
#[cfg(feature = "secrets")]
|
|
|
|
#[cfg_attr(nightly, doc(cfg(feature = "secrets")))]
|
2020-07-03 04:20:28 +00:00
|
|
|
#[inline]
|
2023-09-29 06:50:29 +00:00
|
|
|
pub fn private_cookie<C>(mut self, cookie: C) -> Self
|
|
|
|
where C: Into<crate::http::Cookie<'static>>
|
|
|
|
{
|
|
|
|
self._request_mut().cookies_mut().add_original_private(cookie.into());
|
2020-07-03 04:20:28 +00:00
|
|
|
self
|
|
|
|
}
|
2017-06-30 09:06:03 +00:00
|
|
|
|
2023-03-31 18:13:40 +00:00
|
|
|
/// Set mTLS client certificates to send along with the request.
|
|
|
|
///
|
|
|
|
/// If the request already contained certificates, they are replaced with
|
|
|
|
/// thsoe in `reader.`
|
|
|
|
///
|
|
|
|
/// `reader` is expected to be PEM-formatted and contain X509 certificates.
|
|
|
|
/// If it contains more than one certificate, the entire chain is set on the
|
|
|
|
/// request. If it contains items other than certificates, the certificate
|
|
|
|
/// chain up to the first non-certificate item is set on the request. If
|
|
|
|
/// `reader` is syntactically invalid PEM, certificates are cleared on the
|
|
|
|
/// request.
|
2020-07-03 04:20:28 +00:00
|
|
|
///
|
2023-03-31 18:13:40 +00:00
|
|
|
/// The type `C` can be anything that implements [`std::io::Read`]. This
|
|
|
|
/// includes: `&[u8]`, `File`, `&File`, `Stdin`, and so on. To read a file
|
|
|
|
/// in at compile-time, use [`include_bytes!()`].
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
|
|
|
#[doc = $import]
|
|
|
|
/// use rocket::fs::relative;
|
|
|
|
///
|
|
|
|
/// # Client::_test(|_, request, _| {
|
|
|
|
/// let request: LocalRequest = request;
|
|
|
|
/// let path = relative!("../../examples/tls/private/ed25519_cert.pem");
|
|
|
|
/// let req = request.identity(File::open(path).unwrap());
|
|
|
|
/// # });
|
|
|
|
/// ```
|
|
|
|
#[cfg(feature = "mtls")]
|
|
|
|
#[cfg_attr(nightly, doc(cfg(feature = "mtls")))]
|
|
|
|
pub fn identity<C: std::io::Read>(mut self, reader: C) -> Self {
|
|
|
|
use crate::http::{tls::util::load_certs, private::Certificates};
|
|
|
|
|
|
|
|
let mut reader = std::io::BufReader::new(reader);
|
|
|
|
let certs = load_certs(&mut reader).map(Certificates::from);
|
|
|
|
self._request_mut().connection.client_certificates = certs.ok();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the body data of the request.
|
|
|
|
///core/lib/src/local/request.rs
|
2020-07-03 04:20:28 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
#[doc = $import]
|
|
|
|
/// use rocket::http::ContentType;
|
|
|
|
///
|
|
|
|
/// # Client::_test(|_, request, _| {
|
|
|
|
/// let request: LocalRequest = request;
|
|
|
|
/// let req = request
|
2021-04-29 12:19:24 +00:00
|
|
|
/// .header(ContentType::Text)
|
|
|
|
/// .body("Hello, world!");
|
2020-07-03 04:20:28 +00:00
|
|
|
/// # });
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn body<S: AsRef<[u8]>>(mut self, body: S) -> Self {
|
|
|
|
// TODO: For CGI, we want to be able to set the body to be stdin
|
|
|
|
// without actually reading everything into a vector. Can we allow
|
|
|
|
// that here while keeping the simplicity? Looks like it would
|
|
|
|
// require us to reintroduce a NetStream::Local(Box<Read>) or
|
|
|
|
// something like that.
|
|
|
|
*self._body_mut() = body.as_ref().into();
|
|
|
|
self
|
|
|
|
}
|
2017-06-30 09:06:03 +00:00
|
|
|
|
2021-04-29 12:19:24 +00:00
|
|
|
/// Sets the body to `value` serialized as JSON with `Content-Type`
|
|
|
|
/// [`ContentType::JSON`](crate::http::ContentType::JSON).
|
|
|
|
///
|
|
|
|
/// If `value` fails to serialize, the body is set to empty. The
|
|
|
|
/// `Content-Type` header is _always_ set.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
#[doc = $import]
|
|
|
|
/// use rocket::serde::Serialize;
|
|
|
|
/// use rocket::http::ContentType;
|
|
|
|
///
|
|
|
|
/// #[derive(Serialize)]
|
|
|
|
/// struct Task {
|
|
|
|
/// id: usize,
|
|
|
|
/// complete: bool,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// # Client::_test(|_, request, _| {
|
|
|
|
/// let task = Task { id: 10, complete: false };
|
|
|
|
///
|
|
|
|
/// let request: LocalRequest = request;
|
|
|
|
/// let req = request.json(&task);
|
|
|
|
/// assert_eq!(req.content_type(), Some(&ContentType::JSON));
|
|
|
|
/// # });
|
|
|
|
/// ```
|
|
|
|
#[cfg(feature = "json")]
|
|
|
|
#[cfg_attr(nightly, doc(cfg(feature = "json")))]
|
|
|
|
pub fn json<T: crate::serde::Serialize>(self, value: &T) -> Self {
|
|
|
|
let json = serde_json::to_vec(&value).unwrap_or_default();
|
|
|
|
self.header(crate::http::ContentType::JSON).body(json)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the body to `value` serialized as MessagePack with `Content-Type`
|
|
|
|
/// [`ContentType::MsgPack`](crate::http::ContentType::MsgPack).
|
|
|
|
///
|
|
|
|
/// If `value` fails to serialize, the body is set to empty. The
|
|
|
|
/// `Content-Type` header is _always_ set.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
#[doc = $import]
|
|
|
|
/// use rocket::serde::Serialize;
|
|
|
|
/// use rocket::http::ContentType;
|
|
|
|
///
|
|
|
|
/// #[derive(Serialize)]
|
|
|
|
/// struct Task {
|
|
|
|
/// id: usize,
|
|
|
|
/// complete: bool,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// # Client::_test(|_, request, _| {
|
|
|
|
/// let task = Task { id: 10, complete: false };
|
|
|
|
///
|
|
|
|
/// let request: LocalRequest = request;
|
|
|
|
/// let req = request.msgpack(&task);
|
|
|
|
/// assert_eq!(req.content_type(), Some(&ContentType::MsgPack));
|
|
|
|
/// # });
|
|
|
|
/// ```
|
|
|
|
#[cfg(feature = "msgpack")]
|
|
|
|
#[cfg_attr(nightly, doc(cfg(feature = "msgpack")))]
|
|
|
|
pub fn msgpack<T: crate::serde::Serialize>(self, value: &T) -> Self {
|
|
|
|
let msgpack = rmp_serde::to_vec(value).unwrap_or_default();
|
|
|
|
self.header(crate::http::ContentType::MsgPack).body(msgpack)
|
|
|
|
}
|
|
|
|
|
2020-07-03 04:20:28 +00:00
|
|
|
/// Set the body (data) of the request without consuming `self`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Set the body to be a JSON structure; also sets the Content-Type.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
#[doc = $import]
|
|
|
|
/// use rocket::http::ContentType;
|
|
|
|
///
|
|
|
|
/// # Client::_test(|_, request, _| {
|
|
|
|
/// let request: LocalRequest = request;
|
|
|
|
/// let mut request = request.header(ContentType::JSON);
|
2021-01-18 05:33:36 +00:00
|
|
|
/// request.set_body(r#"{ "key": "value", "array": [1, 2, 3] }"#);
|
2020-07-03 04:20:28 +00:00
|
|
|
/// # });
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn set_body<S: AsRef<[u8]>>(&mut self, body: S) {
|
|
|
|
*self._body_mut() = body.as_ref().into();
|
2017-06-06 20:41:04 +00:00
|
|
|
}
|
Overhaul URI types.
This is fairly large commit with several entangled logical changes.
The primary change in this commit is to completely overhaul how URI
handling in Rocket works. Prior to this commit, the `Uri` type acted as
an origin API. Its parser was minimal and lenient, allowing URIs that
were invalid according to RFC 7230. By contrast, the new `Uri` type
brings with it a strict RFC 7230 compliant parser. The `Uri` type now
represents any kind of valid URI, not simply `Origin` types. Three new
URI types were introduced:
* `Origin` - represents valid origin URIs
* `Absolute` - represents valid absolute URIs
* `Authority` - represents valid authority URIs
The `Origin` type replaces `Uri` in many cases:
* As fields and method inputs of `Route`
* The `&Uri` request guard is now `&Origin`
* The `uri!` macro produces an `Origin` instead of a `Uri`
The strict nature of URI parsing cascaded into the following changes:
* Several `Route` methods now `panic!` on invalid URIs
* The `Rocket::mount()` method is (correctly) stricter with URIs
* The `Redirect` constructors take a `TryInto<Uri>` type
* Dispatching of a `LocalRequest` correctly validates URIs
Overall, URIs are now properly and uniformly handled throughout Rocket's
codebase, resulting in a more reliable and correct system.
In addition to these URI changes, the following changes are also part of
this commit:
* The `LocalRequest::cloned_dispatch()` method was removed in favor of
chaining `.clone().dispatch()`.
* The entire Rocket codebase uses `crate` instead of `pub(crate)` as a
visibility modifier.
* Rocket uses the `crate_visibility_modifier` and `try_from` features.
A note on unsafety: this commit introduces many uses of `unsafe` in the
URI parser. All of these uses are a result of unsafely transforming byte
slices (`&[u8]` or similar) into strings (`&str`). The parser ensures
that these casts are safe, but of course, we must label their use
`unsafe`. The parser was written to be as generic and efficient as
possible and thus can parse directly from byte sources. Rocket, however,
does not make use of this fact and so would be able to remove all uses
of `unsafe` by parsing from an existing `&str`. This should be
considered in the future.
Fixes #443.
Resolves #263.
2018-07-29 01:26:15 +00:00
|
|
|
|
2020-07-03 04:20:28 +00:00
|
|
|
/// Dispatches the request, returning the response.
|
|
|
|
///
|
|
|
|
/// This method consumes `self` and is the preferred mechanism for
|
|
|
|
/// dispatching.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
#[doc = $import]
|
|
|
|
///
|
|
|
|
/// # Client::_test(|_, request, _| {
|
|
|
|
/// let request: LocalRequest = request;
|
|
|
|
/// let response = request.dispatch();
|
|
|
|
/// # });
|
|
|
|
/// ```
|
|
|
|
#[inline(always)]
|
|
|
|
pub $($prefix)? fn dispatch(self) -> LocalResponse<'c> {
|
|
|
|
self._dispatch()$(.$suffix)?
|
2020-05-27 08:09:12 +00:00
|
|
|
}
|
2018-06-20 12:02:12 +00:00
|
|
|
|
2020-07-03 04:20:28 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
#[allow(dead_code)]
|
|
|
|
fn _ensure_impls_exist() {
|
|
|
|
fn is_clone_debug<T: Clone + std::fmt::Debug>() {}
|
|
|
|
is_clone_debug::<Self>();
|
2021-03-05 05:50:37 +00:00
|
|
|
|
|
|
|
fn is_deref_req<'a, T: std::ops::Deref<Target = Request<'a>>>() {}
|
|
|
|
is_deref_req::<Self>();
|
2021-04-14 00:40:22 +00:00
|
|
|
|
|
|
|
fn is_deref_mut_req<'a, T: std::ops::DerefMut<Target = Request<'a>>>() {}
|
|
|
|
is_deref_mut_req::<Self>();
|
2020-07-03 04:20:28 +00:00
|
|
|
}
|
2020-06-22 11:54:34 +00:00
|
|
|
}}
|