Commit Graph

340 Commits

Author SHA1 Message Date
Sergio Benitez 764ea06e90 Preserve non-'Config' values in 'Client::debug()'. 2021-04-07 23:09:05 -07:00
Sergio Benitez 8a2592c257 Use multithreaded runtime in blocking client.
The runtime uses a single worker thread. Using a multithreaded runtime
allows futures that require a multithreaded runtime to run.
2021-04-07 23:09:05 -07:00
Sergio Benitez 6ad14dc3f6 Takes URIs as 'TryInto<Origin>' in local client.
In particular, this allows passing in type-safe URIs constructed via the
'uri!' macro, which was not possible before.
2021-04-07 23:09:05 -07:00
Sergio Benitez c0564fa8dc Reexport, implement 'Responder' for 'Either'. 2021-04-07 23:09:04 -07:00
Sergio Benitez 0bdb6b7bc7 Remove 'attach' fairings. Add 'liftoff' fairings.
Launch fairings are now fallible and take the place of attach fairings,
but they are only run, as the name implies, at launch time.

This is is a fundamental shift from eager execution of set-up routines,
including the now defunct attach fairings, to lazy execution,
precipitated by the transition to `async`. The previous functionality,
while simple, caused grave issues:

  1. A instance of 'Rocket' with async attach fairings requires an async
     runtime to be constructed.
  2. The instance is accessible in non-async contexts.
  3. The async attach fairings have no runtime in which to be run.

Here's an example:

```rust
let rocket = rocket::ignite()
    .attach(AttachFairing::from(|rocket| async {
        Ok(rocket.manage(load_from_network::<T>().await))
    }));

let state = rocket.state::<T>();
```

This had no real meaning previously yet was accepted by running the
attach fairing future in an isolated runtime. In isolation, this causes
no issue, but when attach fairing futures share reactor state with other
futures in Rocket, panics ensue.

The new Rocket application lifecycle is this:

  * Build - A Rocket instance is constructed. No fairings are run.
  * Ignition - All launch fairings are run.
  * Liftoff - If all launch fairings succeeded, the server is started.

New 'liftoff' fairings are run in this third phase.
2021-04-07 23:09:00 -07:00
Sergio Benitez 8d4d01106e Fix file name test for Windows. 2021-04-04 00:12:02 -07:00
Sergio Benitez c280a52a51 Fix compilation on Windows. 2021-04-03 19:40:58 -07:00
Sergio Benitez 579508d58f Make 'FileName' a DST. Improve sanitization.
Resolves #1594.
2021-04-03 17:09:00 -07:00
Jonah Brüchert fa1b75ba74 Allow accessing raw file name from 'DataField'. 2021-04-03 17:07:20 -07:00
Sergio Benitez 0654890e3d Add copy methods to 'TempFile'.
Resolves #1600.
2021-04-01 13:27:37 -07:00
Sergio Benitez 2893ce754d Introduce scoped catchers.
Catchers can now be scoped to paths, with preference given to the
longest-prefix, then the status code. This a breaking change for all
applications that register catchers:

  * `Rocket::register()` takes a base path to scope catchers under.
    - The previous behavior is recovered with `::register("/", ...)`.
  * Catchers now fallibly, instead of silently, collide.
  * `ErrorKind::Collision` is now `ErrorKind::Collisions`.

Related changes:

  * `Origin` implements `TryFrom<String>`, `TryFrom<&str>`.
  * All URI variants implement `TryFrom<Uri>`.
  * Added `Segments::prefix_of()`.
  * `Rocket::mount()` takes a  `TryInto<Origin<'_>>` instead of `&str`
    for the base mount point.
  * Extended `errors` example with scoped catchers.
  * Added scoped sections to catchers guide.

Internal changes:

  * Moved router code to `router/router.rs`.
2021-03-28 13:57:33 -07:00
Sergio Benitez c3bad3a287 Fix small typo in lib.rs. 2021-03-27 18:25:46 -07:00
Sergio Benitez 3045e0ac63 Add doc notes on how to depend on '-dev' versions. 2021-03-27 14:20:43 -07:00
Sergio Benitez 78d749b265 Use explicit 'serde-1' 'indexmap' feature. 2021-03-27 14:10:38 -07:00
Sergio Benitez f7b32e0817 Update to latest 'multer'. 2021-03-27 03:37:37 -07:00
Sergio Benitez 4c0d66b6b1 Update 'pin-project-lite' to 0.2. 2021-03-27 01:03:46 -07:00
Sergio Benitez 3119e6f453 Redesign routing benchmarks.
The new benchmarks use routes from real-world project. This is much more
realistic than the previous benchmarks.

The new benchmarks use `criterion` and exist in their own Cargo project.
2021-03-26 20:02:49 -07:00
Sergio Benitez 20605dac14 Set default route rank using "colorings".
This new system colors paths and queries in one of three ways:

  1. `static`, meaning all components are static
  2. `partial`, meaning at least one component is dynamic
  3. `wild`, meaning all components are dynamic

Static paths carry more weight than static queries. The same is true for
partial and wild paths. This results in the following default rankings:

| path    | query   | rank |
|---------|---------|------|
| static  | static  | -12  |
| static  | partial | -11  |
| static  | wild    | -10  |
| static  | none    | -9   |
| partial | static  | -8   |
| partial | partial | -7   |
| partial | wild    | -6   |
| partial | none    | -5   |
| wild    | static  | -4   |
| wild    | partial | -3   |
| wild    | wild    | -2   |
| wild    | none    | -1   |
2021-03-26 19:41:00 -07:00
Sergio Benitez ec1ccc248c Test 'Limits::{find,get}' with tri-level limits. 2021-03-25 18:52:04 -07:00
Sergio Benitez 630a458417 Expose active 'Rocket' via 'Request::rocket()'. 2021-03-25 18:33:15 -07:00
Sergio Benitez 9cb2552055 Inline a few more 'Request' methods. 2021-03-25 18:19:34 -07:00
Sergio Benitez af598fb872 Use concrete lifetime in 'Request::uri()'. 2021-03-25 18:17:51 -07:00
Sergio Benitez e7934a2a3f Parse inner form 'T' in 'Option<T>' strictly.
This behavior more closely matches the expectation that a missing field
results in 'None'.

Also cleans up forms docs for readability, completeness.
2021-03-24 19:53:13 -07:00
Sergio Benitez 33790254f1 Store full '&Rocket' in '&Request'.
This is instead of storing pieces of the active Rocket instance.
2021-03-24 11:59:23 -07:00
Sergio Benitez 81346e5949 Don't expose hidden 'Route' collider methods. 2021-03-20 03:04:31 -07:00
Sergio Benitez b3021e2acb Fix all broken links. Update some outdated docs. 2021-03-19 18:09:13 -07:00
Sergio Benitez 8749d7293a Simplify and optimize router.
This surfaced a dormant concurrency related issue. Prior to this commit,
the router used `routed_segments()` to retrieve the path segments of the
request. This was okay as there was no route in the request, and matched
segments were retrieved eagerly.

This commit makes segment matching lazy, so no matching occurs if
unnecessary. Between two matches, a `route` is atomically set of
`Request`. This is now visible in `routed_segments()`, which should not
have considered the current route in the first place. This was fixed.
2021-03-19 03:49:58 -07:00
Sergio Benitez 39d7931d6a Expose 'router' module, document 'RouteUri'. 2021-03-19 03:49:58 -07:00
Ben Sully 97acd8d9a9 Update tests and docstrings for 'RouteUri'. 2021-03-19 03:49:58 -07:00
Sergio Benitez 2463637d51 Introduce 'RouteUri'.
Co-authored-by: Ben Sully <ben@bsull.io>
2021-03-19 03:49:54 -07:00
Sergio Benitez f254504dc9 Depend on a single revision of 'state'.
Closes #1577.
2021-03-15 02:46:34 -07:00
Sergio Benitez 1b2edd38b3 Clarify 'TempFile' configuration parameters. 2021-03-15 02:43:01 -07:00
Sergio Benitez f9d4a78fbb Fix spacing in 'Route::default_rank()'. 2021-03-15 02:39:21 -07:00
Sergio Benitez 9c678e606b Track and log catcher names. 2021-03-15 02:27:53 -07:00
Sergio Benitez 304e65ac72 Catch panics that occur before future is returned.
In the course of resolving this issue, double-boxing of handlers was
discovered and removed.
2021-03-15 02:20:48 -07:00
Sergio Benitez 70b42e6f0e Remove second lifetime from 'FromRequest'.
While offering some utility, the lifetime did not carry its weight, and
in practice offered no further ability to borrow. This greatly
simplifies request guard implementations.
2021-03-14 19:57:59 -07:00
Sergio Benitez e532f4e2b3 Disallow defaults in strict forms.
Partially resolves #1536.
2021-03-11 02:03:13 -08:00
Sergio Benitez a0e4c9677c Allow dynamic route names. Log 'StaticFiles' root. 2021-03-10 18:40:38 -08:00
Sergio Benitez 191b93498e Remove boxed futures in server, use async fn. 2021-03-10 16:31:32 -08:00
Sergio Benitez 3c25326917 Log only non-empty fairing classes. 2021-03-10 02:30:13 -08:00
Sergio Benitez 497c4765f2 Fix typo in 'Rocket::ignite()' docs. 2021-03-10 02:16:59 -08:00
Sergio Benitez bc38196f8d Generate a secret key in debug only if it is zero.
This prevents printing a secret key warning if a secret key was
generated, as is done by Rocket itself. This does not change any
behaviors in non-debug profiles.
2021-03-10 02:13:50 -08:00
Sergio Benitez 4e06ee64aa Test 'secret_key' validation, now on pre-launch.
Prior to this commit, it was not possible to test Rocket crates in
production mode without setting a global secret key or bypassing secret
key checking - the testing script did the latter. The consequence is
that it became impossible to test secret key related failures because
the tests passed regardless.

This commit undoes this. As a consequence, all tests are now aware of
the difference between debug and release configurations, the latter of
which validates 'secret_key' by default. New 'Client::debug()' and
'Client::debug_with()' simplify creating an instance of 'Client' with
configuration in debug mode to avoid undesired test failures.

The summary of changes in this commit are:

  * Config 'secret_key' success and failure are now tested.
  * 'secret_key' validation was moved to pre-launch from 'Config:from()'.
  * 'Config::from()' only extracts the config.
  * Added 'Config::try_from()' for non-panicking extraction.
  * 'Config' now knows the profile it was extracted from.
  * The 'Config' provider sets a profile of 'Config.profile'.
  * 'Rocket', 'Client', 'Fairings', implement 'Debug'.
  * 'fairing::Info' implements 'Copy', 'Clone'.
  * 'Fairings' keeps track of, logs attach fairings.
  * 'Rocket::reconfigure()' was added to allow modifying a config.

Internally, the testing script was refactored to properly test the
codebase with the new changes. In particular, it no longer sets a rustc
'cfg' to avoid secret-key checking.

Resolves #1543.
Fixes #1564.
2021-03-09 21:57:26 -08:00
Sergio Benitez 83ffe0f7bc Remove 'Config::profile()'. CFG 'secret_key' field.
This commit makes the `Config.secret_key` conditionally compile on the
`secrets` feature. The net effect is simplified internal code, fewer
corner-cases, and easier to write tests.

This commit removes the `Provider::profile()` implementation of
`Config`. This means that the `Config` provider no longer sets a
profile, a likely confusing behavior. The `Config::figment()` continues
to function as before.
2021-03-09 21:40:53 -08:00
Jeb Rosen d778c2cb10 Drop embedded async 'Client' in async runtime. 2021-03-06 01:40:43 -08:00
Sergio Benitez 68b244ebdc Forward catcher, handler failure to 500 catcher.
This changes core routing so that panics in all handlers are handled by
emitting a long message explaining that panics are bad and invoking the
500 error catcher. If the 500 error catcher fails, Rocket's default 500
catcher is used.
2021-03-06 01:39:31 -08:00
Jeb Rosen a0784b4b15 Catch and gracefully handle panics in routes and catchers. 2021-03-05 22:58:28 -08:00
Sergio Benitez 4d0042c395 Allow '<path..>' to match zero segments.
This changes core routing so that '<path..>' in a route URI matches zero
or more segments. Previously, '<path..>' matched _1_ or more.

  * Routes '$a' and '$b/<p..>' collide if $a and $b previously collided.
  * For example, '/' now collides with '/<p..>'.
  * Request '$a' matches route '$b/<p..>' if $a previously matched $b.
  * For example, request '/' matches route '/<p..>'.

Resolves #985.
2021-03-05 02:01:24 -08:00
Sergio Benitez 08ae0d0b8c Use upstream 'async-trait'. 2021-03-04 22:10:59 -08:00
Sergio Benitez 3bce76f5af Use 'Client::debug()' in more tests. 2021-03-04 21:53:22 -08:00