2018-11-06 02:59:44 +00:00
|
|
|
use std::sync::RwLock;
|
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
|
|
|
use std::borrow::Cow;
|
|
|
|
|
2019-06-13 01:48:02 +00:00
|
|
|
use crate::Rocket;
|
|
|
|
use crate::local::LocalRequest;
|
|
|
|
use crate::http::{Method, private::CookieJar};
|
|
|
|
use crate::error::LaunchError;
|
2017-06-06 20:41:04 +00:00
|
|
|
|
2017-06-30 09:06:03 +00:00
|
|
|
/// A structure to construct requests for local dispatching.
|
|
|
|
///
|
|
|
|
/// # Usage
|
|
|
|
///
|
2018-11-09 05:38:44 +00:00
|
|
|
/// A `Client` is constructed via the [`new()`] or [`untracked()`] methods from
|
|
|
|
/// an already constructed `Rocket` instance. Once a value of `Client` has been
|
|
|
|
/// constructed, the [`LocalRequest`] constructor methods ([`get()`], [`put()`],
|
|
|
|
/// [`post()`], and so on) can be used to create a `LocalRequest` for
|
|
|
|
/// dispatching.
|
2017-06-30 09:06:03 +00:00
|
|
|
///
|
2019-06-13 01:48:02 +00:00
|
|
|
/// See the [top-level documentation](crate::local) for more usage information.
|
2017-06-30 09:06:03 +00:00
|
|
|
///
|
2018-04-15 02:41:37 +00:00
|
|
|
/// ## Cookie Tracking
|
|
|
|
///
|
2018-11-09 05:38:44 +00:00
|
|
|
/// A `Client` constructed using [`new()`] propagates cookie changes made by
|
2018-04-15 02:41:37 +00:00
|
|
|
/// responses to previously dispatched requests. In other words, if a previously
|
|
|
|
/// dispatched request resulted in a response that adds a cookie, any future
|
|
|
|
/// requests will contain that cookie. Similarly, cookies removed by a response
|
2018-07-12 03:44:09 +00:00
|
|
|
/// won't be propagated further.
|
2018-04-15 02:41:37 +00:00
|
|
|
///
|
|
|
|
/// This is typically the desired mode of operation for a `Client` as it removes
|
2018-07-12 03:44:09 +00:00
|
|
|
/// the burden of manually tracking cookies. Under some circumstances, however,
|
2018-04-15 02:41:37 +00:00
|
|
|
/// disabling this tracking may be desired. In these cases, use the
|
2018-11-09 05:38:44 +00:00
|
|
|
/// [`untracked()`](Client::untracked()) constructor to create a `Client` that
|
2018-04-15 02:41:37 +00:00
|
|
|
/// _will not_ track cookies.
|
|
|
|
///
|
2018-11-09 05:38:44 +00:00
|
|
|
/// ### Synchronization
|
|
|
|
///
|
|
|
|
/// While `Client` implements `Sync`, using it in a multithreaded environment
|
|
|
|
/// while tracking cookies can result in surprising, non-deterministic behavior.
|
|
|
|
/// This is because while cookie modifications are serialized, the exact
|
|
|
|
/// ordering depends on when requests are dispatched. Specifically, when cookie
|
|
|
|
/// tracking is enabled, all request dispatches are serialized, which in-turn
|
|
|
|
/// serializes modifications to the internally tracked cookies.
|
|
|
|
///
|
|
|
|
/// If possible, refrain from sharing a single instance of `Client` across
|
|
|
|
/// multiple threads. Instead, prefer to create a unique instance of `Client`
|
|
|
|
/// per thread. If it's not possible, ensure that either you are not depending
|
|
|
|
/// on cookies, the ordering of their modifications, or both, or have arranged
|
|
|
|
/// for dispatches to occur in a deterministic ordering.
|
|
|
|
///
|
2017-06-30 09:06:03 +00:00
|
|
|
/// ## Example
|
|
|
|
///
|
|
|
|
/// The following snippet creates a `Client` from a `Rocket` instance and
|
2018-07-12 03:44:09 +00:00
|
|
|
/// dispatches a local request to `POST /` with a body of `Hello, world!`.
|
2017-06-30 09:06:03 +00:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use rocket::local::Client;
|
|
|
|
///
|
2019-08-24 17:27:10 +00:00
|
|
|
/// # let _ = async {
|
2017-06-30 09:06:03 +00:00
|
|
|
/// let rocket = rocket::ignite();
|
|
|
|
/// let client = Client::new(rocket).expect("valid rocket");
|
|
|
|
/// let response = client.post("/")
|
|
|
|
/// .body("Hello, world!")
|
2019-08-24 17:27:10 +00:00
|
|
|
/// .dispatch().await;
|
|
|
|
/// # };
|
2017-06-30 09:06:03 +00:00
|
|
|
/// ```
|
|
|
|
///
|
2018-11-09 05:38:44 +00:00
|
|
|
/// [`new()`]: #method.new
|
|
|
|
/// [`untracked()`]: #method.untracked
|
|
|
|
/// [`get()`]: #method.get
|
|
|
|
/// [`put()`]: #method.put
|
|
|
|
/// [`post()`]: #method.post
|
2017-06-06 20:41:04 +00:00
|
|
|
pub struct Client {
|
|
|
|
rocket: Rocket,
|
2019-09-20 20:43:05 +00:00
|
|
|
pub(crate) cookies: Option<RwLock<CookieJar>>,
|
2017-06-06 20:41:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Client {
|
2018-04-15 02:41:37 +00:00
|
|
|
/// Constructs a new `Client`. If `tracked` is `true`, an empty `CookieJar`
|
|
|
|
/// is created for cookie tracking. Otherwise, the internal `CookieJar` is
|
|
|
|
/// set to `None`.
|
|
|
|
fn _new(rocket: Rocket, tracked: bool) -> Result<Client, LaunchError> {
|
|
|
|
let cookies = match tracked {
|
2018-11-06 02:59:44 +00:00
|
|
|
true => Some(RwLock::new(CookieJar::new())),
|
2018-04-15 02:41:37 +00:00
|
|
|
false => None
|
|
|
|
};
|
|
|
|
|
2017-09-28 21:22:03 +00:00
|
|
|
Ok(Client { rocket: rocket.prelaunch_check()?, cookies })
|
2018-04-15 02:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct a new `Client` from an instance of `Rocket` with cookie
|
|
|
|
/// tracking.
|
|
|
|
///
|
|
|
|
/// # Cookie Tracking
|
|
|
|
///
|
2018-07-12 03:44:09 +00:00
|
|
|
/// By default, a `Client` propagates cookie changes made by responses to
|
2018-04-15 02:41:37 +00:00
|
|
|
/// previously dispatched requests. In other words, if a previously
|
|
|
|
/// dispatched request resulted in a response that adds a cookie, any future
|
|
|
|
/// requests will contain the new cookies. Similarly, cookies removed by a
|
2018-07-12 03:44:09 +00:00
|
|
|
/// response won't be propagated further.
|
2018-04-15 02:41:37 +00:00
|
|
|
///
|
|
|
|
/// This is typically the desired mode of operation for a `Client` as it
|
2018-07-12 03:44:09 +00:00
|
|
|
/// removes the burden of manually tracking cookies. Under some
|
2018-04-15 02:41:37 +00:00
|
|
|
/// circumstances, however, disabling this tracking may be desired. The
|
|
|
|
/// [`untracked()`](Client::untracked()) method creates a `Client` that
|
|
|
|
/// _will not_ track cookies.
|
2017-06-30 09:06:03 +00:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// If launching the `Rocket` instance would fail, excepting network errors,
|
|
|
|
/// the `LaunchError` is returned.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use rocket::local::Client;
|
|
|
|
///
|
|
|
|
/// let client = Client::new(rocket::ignite()).expect("valid rocket");
|
|
|
|
/// ```
|
2018-04-15 02:41:37 +00:00
|
|
|
#[inline(always)]
|
2017-06-06 20:41:04 +00:00
|
|
|
pub fn new(rocket: Rocket) -> Result<Client, LaunchError> {
|
2018-04-15 02:41:37 +00:00
|
|
|
Client::_new(rocket, true)
|
|
|
|
}
|
2017-06-06 20:41:04 +00:00
|
|
|
|
2018-04-15 02:41:37 +00:00
|
|
|
/// Construct a new `Client` from an instance of `Rocket` _without_ cookie
|
|
|
|
/// tracking.
|
|
|
|
///
|
|
|
|
/// # Cookie Tracking
|
|
|
|
///
|
|
|
|
/// Unlike the [`new()`](Client::new()) constructor, a `Client` returned
|
2018-07-12 03:44:09 +00:00
|
|
|
/// from this method _does not_ automatically propagate cookie changes.
|
2018-04-15 02:41:37 +00:00
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// If launching the `Rocket` instance would fail, excepting network errors,
|
|
|
|
/// the `LaunchError` is returned.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use rocket::local::Client;
|
|
|
|
///
|
|
|
|
/// let client = Client::untracked(rocket::ignite()).expect("valid rocket");
|
|
|
|
/// ```
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn untracked(rocket: Rocket) -> Result<Client, LaunchError> {
|
|
|
|
Client::_new(rocket, false)
|
2017-06-06 20:41:04 +00:00
|
|
|
}
|
|
|
|
|
2017-06-30 09:06:03 +00:00
|
|
|
/// Returns the instance of `Rocket` this client is creating requests for.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use rocket::local::Client;
|
|
|
|
///
|
|
|
|
/// let my_rocket = rocket::ignite();
|
|
|
|
/// let client = Client::new(my_rocket).expect("valid rocket");
|
|
|
|
///
|
|
|
|
/// // get the instance of `my_rocket` within `client`
|
|
|
|
/// let my_rocket = client.rocket();
|
|
|
|
/// ```
|
2017-06-06 20:41:04 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn rocket(&self) -> &Rocket {
|
|
|
|
&self.rocket
|
|
|
|
}
|
|
|
|
|
2017-06-30 09:06:03 +00:00
|
|
|
/// Create a local `GET` request to the URI `uri`.
|
|
|
|
///
|
|
|
|
/// When dispatched, the request will be served by the instance of Rocket
|
|
|
|
/// within `self`. The request is not dispatched automatically. To actually
|
2018-10-06 13:25:17 +00:00
|
|
|
/// dispatch the request, call [`LocalRequest::dispatch()`] on the returned
|
|
|
|
/// request.
|
2017-06-30 09:06:03 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use rocket::local::Client;
|
|
|
|
///
|
|
|
|
/// let client = Client::new(rocket::ignite()).expect("valid rocket");
|
|
|
|
/// let req = client.get("/hello");
|
|
|
|
/// ```
|
2017-06-06 20:41:04 +00:00
|
|
|
#[inline(always)]
|
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
|
|
|
pub fn get<'c, 'u: 'c, U: Into<Cow<'u, str>>>(&'c self, uri: U) -> LocalRequest<'c> {
|
2017-06-06 20:41:04 +00:00
|
|
|
self.req(Method::Get, uri)
|
|
|
|
}
|
|
|
|
|
2017-06-30 09:06:03 +00:00
|
|
|
/// Create a local `PUT` request to the URI `uri`.
|
|
|
|
///
|
|
|
|
/// When dispatched, the request will be served by the instance of Rocket
|
|
|
|
/// within `self`. The request is not dispatched automatically. To actually
|
2018-10-06 13:25:17 +00:00
|
|
|
/// dispatch the request, call [`LocalRequest::dispatch()`] on the returned
|
|
|
|
/// request.
|
2017-06-30 09:06:03 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use rocket::local::Client;
|
|
|
|
///
|
|
|
|
/// let client = Client::new(rocket::ignite()).expect("valid rocket");
|
|
|
|
/// let req = client.put("/hello");
|
|
|
|
/// ```
|
2017-06-06 20:41:04 +00:00
|
|
|
#[inline(always)]
|
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
|
|
|
pub fn put<'c, 'u: 'c, U: Into<Cow<'u, str>>>(&'c self, uri: U) -> LocalRequest<'c> {
|
2017-06-06 20:41:04 +00:00
|
|
|
self.req(Method::Put, uri)
|
|
|
|
}
|
|
|
|
|
2017-06-30 09:06:03 +00:00
|
|
|
/// Create a local `POST` request to the URI `uri`.
|
|
|
|
///
|
|
|
|
/// When dispatched, the request will be served by the instance of Rocket
|
|
|
|
/// within `self`. The request is not dispatched automatically. To actually
|
2018-10-06 13:25:17 +00:00
|
|
|
/// dispatch the request, call [`LocalRequest::dispatch()`] on the returned
|
|
|
|
/// request.
|
2017-06-30 09:06:03 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use rocket::local::Client;
|
|
|
|
/// use rocket::http::ContentType;
|
|
|
|
///
|
|
|
|
/// let client = Client::new(rocket::ignite()).expect("valid rocket");
|
|
|
|
///
|
|
|
|
/// let req = client.post("/hello")
|
|
|
|
/// .body("field=value&otherField=123")
|
|
|
|
/// .header(ContentType::Form);
|
|
|
|
/// ```
|
2017-06-06 20:41:04 +00:00
|
|
|
#[inline(always)]
|
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
|
|
|
pub fn post<'c, 'u: 'c, U: Into<Cow<'u, str>>>(&'c self, uri: U) -> LocalRequest<'c> {
|
2017-06-06 20:41:04 +00:00
|
|
|
self.req(Method::Post, uri)
|
|
|
|
}
|
|
|
|
|
2017-06-30 09:06:03 +00:00
|
|
|
/// Create a local `DELETE` request to the URI `uri`.
|
|
|
|
///
|
|
|
|
/// When dispatched, the request will be served by the instance of Rocket
|
|
|
|
/// within `self`. The request is not dispatched automatically. To actually
|
2018-10-06 13:25:17 +00:00
|
|
|
/// dispatch the request, call [`LocalRequest::dispatch()`] on the returned
|
|
|
|
/// request.
|
2017-06-30 09:06:03 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use rocket::local::Client;
|
|
|
|
///
|
|
|
|
/// let client = Client::new(rocket::ignite()).expect("valid rocket");
|
|
|
|
/// let req = client.delete("/hello");
|
|
|
|
/// ```
|
2017-06-06 20:41:04 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn delete<'c, 'u: 'c, U>(&'c self, uri: U) -> LocalRequest<'c>
|
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
|
|
|
where U: Into<Cow<'u, str>>
|
2017-06-06 20:41:04 +00:00
|
|
|
{
|
|
|
|
self.req(Method::Delete, uri)
|
|
|
|
}
|
|
|
|
|
2017-06-30 09:06:03 +00:00
|
|
|
/// Create a local `OPTIONS` request to the URI `uri`.
|
|
|
|
///
|
|
|
|
/// When dispatched, the request will be served by the instance of Rocket
|
|
|
|
/// within `self`. The request is not dispatched automatically. To actually
|
2018-10-06 13:25:17 +00:00
|
|
|
/// dispatch the request, call [`LocalRequest::dispatch()`] on the returned
|
|
|
|
/// request.
|
2017-06-30 09:06:03 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use rocket::local::Client;
|
|
|
|
///
|
|
|
|
/// let client = Client::new(rocket::ignite()).expect("valid rocket");
|
|
|
|
/// let req = client.options("/hello");
|
|
|
|
/// ```
|
2017-06-06 20:41:04 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn options<'c, 'u: 'c, U>(&'c self, uri: U) -> LocalRequest<'c>
|
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
|
|
|
where U: Into<Cow<'u, str>>
|
2017-06-06 20:41:04 +00:00
|
|
|
{
|
|
|
|
self.req(Method::Options, uri)
|
|
|
|
}
|
|
|
|
|
2017-06-30 09:06:03 +00:00
|
|
|
/// Create a local `HEAD` request to the URI `uri`.
|
|
|
|
///
|
|
|
|
/// When dispatched, the request will be served by the instance of Rocket
|
|
|
|
/// within `self`. The request is not dispatched automatically. To actually
|
2018-10-06 13:25:17 +00:00
|
|
|
/// dispatch the request, call [`LocalRequest::dispatch()`] on the returned
|
|
|
|
/// request.
|
2017-06-30 09:06:03 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use rocket::local::Client;
|
|
|
|
///
|
|
|
|
/// let client = Client::new(rocket::ignite()).expect("valid rocket");
|
|
|
|
/// let req = client.head("/hello");
|
|
|
|
/// ```
|
2017-06-06 20:41:04 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn head<'c, 'u: 'c, U>(&'c self, uri: U) -> LocalRequest<'c>
|
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
|
|
|
where U: Into<Cow<'u, str>>
|
2017-06-06 20:41:04 +00:00
|
|
|
{
|
|
|
|
self.req(Method::Head, uri)
|
|
|
|
}
|
|
|
|
|
2017-06-30 09:06:03 +00:00
|
|
|
/// Create a local `PATCH` request to the URI `uri`.
|
|
|
|
///
|
|
|
|
/// When dispatched, the request will be served by the instance of Rocket
|
|
|
|
/// within `self`. The request is not dispatched automatically. To actually
|
2018-10-06 13:25:17 +00:00
|
|
|
/// dispatch the request, call [`LocalRequest::dispatch()`] on the returned
|
|
|
|
/// request.
|
2017-06-30 09:06:03 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use rocket::local::Client;
|
|
|
|
///
|
|
|
|
/// let client = Client::new(rocket::ignite()).expect("valid rocket");
|
|
|
|
/// let req = client.patch("/hello");
|
|
|
|
/// ```
|
2017-06-06 20:41:04 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn patch<'c, 'u: 'c, U>(&'c self, uri: U) -> LocalRequest<'c>
|
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
|
|
|
where U: Into<Cow<'u, str>>
|
2017-06-06 20:41:04 +00:00
|
|
|
{
|
|
|
|
self.req(Method::Patch, uri)
|
|
|
|
}
|
2017-06-30 09:06:03 +00:00
|
|
|
|
|
|
|
/// Create a local request with method `method` to the URI `uri`.
|
|
|
|
///
|
|
|
|
/// When dispatched, the request will be served by the instance of Rocket
|
|
|
|
/// within `self`. The request is not dispatched automatically. To actually
|
2018-10-06 13:25:17 +00:00
|
|
|
/// dispatch the request, call [`LocalRequest::dispatch()`] on the returned
|
|
|
|
/// request.
|
2017-06-30 09:06:03 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use rocket::local::Client;
|
|
|
|
/// use rocket::http::Method;
|
|
|
|
///
|
|
|
|
/// let client = Client::new(rocket::ignite()).expect("valid rocket");
|
|
|
|
/// let req = client.req(Method::Get, "/hello");
|
|
|
|
/// ```
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn req<'c, 'u: 'c, U>(&'c self, method: Method, uri: U) -> LocalRequest<'c>
|
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
|
|
|
where U: Into<Cow<'u, str>>
|
2017-06-30 09:06:03 +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
|
|
|
LocalRequest::new(self, method, uri.into())
|
2017-06-30 09:06:03 +00:00
|
|
|
}
|
2017-06-06 20:41:04 +00:00
|
|
|
}
|
2018-11-06 02:59:44 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::Client;
|
|
|
|
|
|
|
|
fn assert_sync<T: Sync>() {}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_local_client_impl_sync() {
|
|
|
|
assert_sync::<Client>();
|
|
|
|
}
|
|
|
|
}
|