* Fix trailing redirection with query parameters
When the request URI matches a route that need a trailing slash, or has an extra trailing slash, the redirect URI is not generated correctly.
This change adds or removes a trailing slash to the path part of the URI, instead of the full URI, preserving query parameters during redirection.
Signed-off-by: David Calavera <david.calavera@gmail.com>
* Make trailing slash logic safer
Extract parts from Uri and recreate it, so it doesn't bump
into corner cases with string manipulation.
Signed-off-by: David Calavera <david.calavera@gmail.com>
* Remove extra assignment.
Signed-off-by: David Calavera <david.calavera@gmail.com>
* Update axum/src/routing/mod.rs
Co-authored-by: David Pedersen <david.pdrsn@gmail.com>
* changelog
Co-authored-by: David Pedersen <david.pdrsn@gmail.com>
* Added chunk function to multipart field
This fixes not being able to stream data from a multipart directly into a file or other output.
* doc comment for clarification of usage and &mut self
* fixed formatting
* Corrected example to reflex best practices
* Removed unwrap
* clean up docs
* update changelog
Co-authored-by: David Pedersen <david.pdrsn@gmail.com>
* Change `Handler` to have an associated `Future` type
This removes `#[async_trait]` from `Handler` and replaces that with an
associated `Future` type.
As hinted at in #878 I'm working on something with types that need to
implement `Handler`. I'm doing that by wrapping other `Handler` types so
I can implement `Handler` by simply delegating and thus don't need to
allocate another box for `#[async_trait]`. This change makes that
possible.
It does make `Handler` less ergonomic to implement but thats a very
niche feature so I'm fine with that. It wouldn't be appropriate for
`FromRequest` IMO.
* changelog
* Fix status codes for `JsonRejection` rejections
Fixes#865
* Apply suggestions from code review
Co-authored-by: Jonas Platte <jplatte+git@posteo.de>
Co-authored-by: Jonas Platte <jplatte+git@posteo.de>
* Add `IntoResponseParts`
* docs
* Add test
* don't allow overriding body or response
* macroify impls
* re-order things a bit
* Fix tests
* Also allow overriding version
* Move things into separate modules
* docs
* clean up
* fix trybuild test
* remove churn
* simplify buliding response
* fixup test
* fix docs typo
* Use `HeaderValue::from_static`, might be faster
* Bring back `impl IntoResponse` in example
* Remove blanket impl to improve error message
* don't need to set `content-type`
* Apply suggestions from code review
Co-authored-by: Jonas Platte <jplatte@users.noreply.github.com>
* changelog
Co-authored-by: Jonas Platte <jplatte@users.noreply.github.com>
* Introduce IntoResponseHeaders trait
* Implement IntoResponseHeaders for HeaderMap
* Add impl IntoResponse for impl IntoResponseHeaders
… and update IntoResponse impls that use HeaderMap to be generic instead.
* Add impl IntoResponseHeaders for Headers
… and remove IntoResponse impls that use it.
* axum-debug: Fix grammar in docs
* Explain confusing error message in docs
* Remove `RequestParts::take_extensions`
* fix out of date docs
* Remove RequestAlreadyExtracted and replace it with BodyAlreadyExtracted
* fix docs
* fix test
* Update axum-core/src/extract/mod.rs
Co-authored-by: Jonas Platte <jplatte@users.noreply.github.com>
* Remove macro only used once
Co-authored-by: Jonas Platte <jplatte@users.noreply.github.com>
* Change `HeaderMap` extractor to clone the headers
* fix docs
* changelog
* inline variable
* also add changelog item to axum
* don't list types from axum in axum-core's changelog
* document that `HeaderMap::from_request` clones the headers
* fix typo
* a few more typos
- **fixed:** Fix using incorrect path prefix when nesting `Router`s at `/` ([#691])
- **fixed:** Make `nest("", service)` work and mean the same as `nest("/", service)` ([#691])
- **fixed:** Replace response code `301` with `308` for trailing slash redirects. Also deprecates
`Redirect::found` (`302`) in favor of `Redirect::temporary` (`307`) or `Redirect::to` (`303`).
This is to prevent clients from changing non-`GET` requests to `GET` requests ([#682])
[#691]: https://github.com/tokio-rs/axum/pull/691
[#682]: https://github.com/tokio-rs/axum/pull/682
* Use 308 status instead of 301 when redirecting
For redirects resulting from requests to paths with a trailing slash,
use 308 instead of 301 to prevent non-GET requests (POST, PUT, etc) from
being changed to GET.
For example, (assuming a route for /path is defined)...
- Old behavior results in:
POST /path/ -> GET /path
- New behavior results in:
POST /path/ -> POST /path
Fixes#681
* Add deprecation notice to found()
Deprecates found() due to its use of HTTP 302
* rustfmt
* Use dedicated redirect method
Use Redirect::permanent instead of re-implementing its functionality
* Remove deprecated method from example
Replace usages of Redirect:found with Redirect::to and Redirect::temporary as appropriate
* Fix panic in oauth example
Previously the example would panic if a request was made without the
`Cookie` header. Now the user is redirected to the login page as
expected.
* Update CHANGELOG
* Revert pub TypedheaderRejection fields
* Fix clippy lint
* cargo fmt
* Fix CHANGELOG link
* Adhere to implicit line length limit
* Provide more error in `Path` deserialization error
* Rename
* Add error kind for deserializing sequences
* Rename
* Fix wrong docs
* Rename `MissingRouteParams`
* Rename error to have more consistency
* Rename internal error
* Update changelog
* One last renaming, for now
* Add tests
* Tweak changelog a bit
* Move `IntoResponse` to axum-core
* Move `FromRequest` to axum-core
* some clean up
* Remove hyper dependency from axum-core
* Fix docs reference
* Use default
* Update changelog
* Remove mention of default type
This introduces two new possible panics when constructing routers:
- If merging two routers that each have a fallback. Previously that left
side fallback would be silently discarded.
- If nesting a router that has a fallback. Previously it would be
silently discarded.
Overall this should make things more explicit and users shouldn't have
to worry "why isn't my fallback" working.
Fixes#488
* Move `axum-handle-error-extract` into axum
With 0.4 underway we can now nuke `axum-handle-error-extract` and move
its code directly into axum.
So this replaces the old `HandleErrorLayer` with one that supports async
functions and extractors.
* changelog
* fix CI
* Empty crate
* basic setup
* Support `HEAD`
* Add remaining methods
* Impl Debug
* Add `MethodRouter::merge`
* WIP
* Support same route with different methods in different calls
* Update changelog
* Bring back `any` and `any_service`
* Address review feedback
* Implement `IntoResponse` for `http::response::Parts`
Not sure there are many use cases for this but still thinks it makes to
provide since other crates can't.
* Use `Response::from_parts`
- **added:** Add `Router::route_layer` for applying middleware that
will only run on requests that match a route. This is useful for middleware
that return early, such as authorization ([#474])
[#474]: https://github.com/tokio-rs/axum/pull/474
This addresses something thats been bothering me for some time: Most middleware need to run regardless if the request matches a route or not. For example you don't wanna skip logging for unmatched requests.
However middleware such as authorization only make sense to run for matching requests. This previously wasn't possible to express and you'd have to manually apply the middleware to each handler. Consider this:
```rust
Router::new()
.route("/foo", get(|| async {}))
.layer(RequireAuthorizationLayer::bearer("password"));
```
Calling `GET /foo` with an invalid token would receive `401 Unauthorized` as expected however calling some unknown route like `GET /not-found` would also return `401 Unauthorized`. I think this is unexpected and have seen a few users ask questions about it.
It happened because the 404 you'd otherwise see is generated by a fallback service stored on `Router`. When adding a layer to the router the layer would also be applied to the fallback, which in the case of auth means the fallback would never be called for unauthorized requests.
I think what axum does today is the right default however I still think we should support this somehow. Especially since [`extractor_middleware`](https://docs.rs/axum/0.3.1/axum/extract/fn.extractor_middleware.html) is mainly useful for auth but it doesn't work great today due to this gotcha.
This PR proposes adding `Router::layer_on_matching_route` which only applies layers to routes, not the fallback, which fixes the issue. I'm not a big fan of the name `layer_on_matching_route`, would like something shorter, but I think it communicates the purpose decently.
The generics are a bit different since the request body used on the routes and the fallback must match, so layers that changes the request body type are not compatible with `layer_on_matching_route`. Such middleware are very rare so that should be fine.
* Move axum crate into workspace subfolder
Over time I imagine we're gonna have other crates in this repo that
provide utilities or integrations for axum. This prepares for that by
moving the main axum crate into its own folder.
The README situation is a bit annoying because we want `./README.md`
for viewing the repo on github but `axum/README.md` for crates.io. For
now I've just copy/pasted it and added CI step to make sure they're
identical.
* update changelog link
* Add licenses to all examples
* is this how you install `diff`?
* or maybe this is how?
* fix readme links
* like this?
* fix cargo-deny step
* Try making root readme a symlink
* remove compare readme step
not needed since readme in repo root is now a symlink
* Revert "Add licenses to all examples"
This reverts commit ab321b7fb9.