Commit graph

171 commits

Author SHA1 Message Date
David Pedersen
afcefb4a70
Add AppendHeaders (#927)
* Add `AppendHeaders`

* axum changelog
2022-04-17 23:14:04 +02:00
David Calavera
83e1a15040
Fix trailing redirection with query parameters (#936)
* 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>
2022-04-17 21:12:52 +00:00
Grachev Mikhail
4c936dbb52
Fix a typo in CHANGELOG.md (#906) 2022-04-04 09:26:39 +02:00
Tristan Bouchard
d417910a16
Add Field::chunk (#901)
* 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>
2022-04-04 09:26:11 +02:00
David Pedersen
3e716f303f axum: Version 0.5.1 2022-04-03 20:23:18 +02:00
Jonas Platte
1191b58083
Add RequestParts::extract (#897) 2022-04-01 09:25:15 +02:00
David Pedersen
fec64bc1b6 axum: Version 0.5.0 2022-03-31 20:37:45 +02:00
David Pedersen
d7860ea9e2
Copy over changelogs from v0.4.x branch (#893)
* Copy over changelogs from v0.4.x branch

Preparing to release 0.5

* bump crate versions
2022-03-31 20:21:55 +02:00
David Pedersen
21552fe434
Remove bound from into_make_service_with_connect_info (#892)
Fixes #859
2022-03-31 16:49:49 +00:00
Evan Schwartz
3084dc10ca
Accept &str for Redirect (#889)
* Accept &str for Redirect

* Fix doc example

* fix more docs examples

* update changelog

* fix changelog label

Co-authored-by: David Pedersen <david.pdrsn@gmail.com>
2022-03-31 10:03:06 +02:00
David Pedersen
56e2d57320
Change Handler to have an associated Future type (#879)
* 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
2022-03-21 13:32:06 +00:00
David Pedersen
437fe5b931
Make status codes for JsonRejection more precise (#868)
* 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>
2022-03-18 16:53:39 +01:00
David Pedersen
88974f4299
Add missing changelog items (#845)
* Add missing changelog items

* Update axum/CHANGELOG.md

Co-authored-by: Jonas Platte <jplatte+git@posteo.de>

Co-authored-by: Jonas Platte <jplatte+git@posteo.de>
2022-03-09 15:07:41 +00:00
David Pedersen
14960e9a11 fix changelog links 2022-03-09 15:43:54 +01:00
David Pedersen
401c7a324a
Fix routing for opaque nested services (#842)
* Fix routing for opaque nested services

* Also test `MatchedPath`

* clean up and more comments
2022-03-09 11:25:06 +01:00
Trent
843437b501
Add Host extractor (#827) 2022-03-06 16:29:10 +00:00
David Pedersen
1614ef7a22
Fix inconsistent double slash when nesting routes (#824) 2022-03-04 09:28:44 +01:00
David Pedersen
90e74f12c4
Requires paths start with a / (#823)
* Requires routes to start with `/`

* Also check routes in `TypedPath`

* changelog

* changelog links
2022-03-03 23:24:27 +00:00
Jonas Platte
ab486198e6
Fix typo in CHANGELOG.md (#822) 2022-03-04 00:18:37 +01:00
David Pedersen
d943ba6d81
Replace HasRoutes with Into<Router> (#819)
* Move `HasRoutes` into axum

* fix doc test

* Just use `Into<Router>`
2022-03-03 21:50:31 +00:00
David Pedersen
2428d99081
Export AddExtension from middleware (#811)
* Export `AddExtension` from `middleware`

* Move `AddExtension` into `extension` module
2022-03-01 13:59:33 +00:00
David Pedersen
2de202da40
Remove AddExtensionLayer (#807)
* Remove `AddExtensionLayer`

Its deprecated on 0.4.x so we can remove it from `main`.

* changelog
2022-03-01 13:59:43 +01:00
David Pedersen
a2b568c7c1
Implement tower::Layer for Extension (#801)
* Implement `tower::Layer` for `Extension`

* changelog
2022-03-01 00:39:22 +01:00
David Pedersen
0d05b5e31f
Remove deprecated APIs (#800)
* Remove deprecations APIs

* changelog
2022-02-28 23:22:21 +00:00
David Pedersen
f12ab072c5
Add IntoResponseParts (#797)
* 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>
2022-02-28 23:04:33 +00:00
David Pedersen
4ccc4bea71
Update to tokio-tungstenite 0.17 (#791)
Fixes https://github.com/tokio-rs/axum/issues/781
2022-02-25 10:42:09 +00:00
David Pedersen
113a15a713
Document sharing state between handler and middleware (#783)
* Fix heading levels

* Document passing state from middleware to handlers
2022-02-22 16:14:18 +01:00
David Pedersen
c135436cc9
Fix setting content-length: 0 for HEAD responses with compression middleware (#755)
* Fix getting `content-length` for `chunked` responses

Fixes #747

* changelog

* Fix `cargo deny bans`

https://github.com/tokio-rs/axum/pull/753 will fix things properly
2022-02-14 09:56:08 +00:00
David Pedersen
515495d957
Set content-length for responses to HEAD requests (#734)
* Set `content-length` internally in `Router`

* changelog

* require `B: HttpBody` at the `impl`

* add changelog refs
2022-01-31 13:48:51 +01:00
David Pedersen
513766b9e7
Set Allow header when returning 405 Method Not Allowed (#733) 2022-01-30 21:41:34 +01:00
Alex
b1283e9708
Fix year for the 0.4.4 release (#727) 2022-01-26 08:17:39 +01:00
David Pedersen
9004a14302
Move middleware::from_fn into axum (#719)
* Move `middleware::from_fn` into axum

* changelog

* fix feature

* Rephrase changelog a bit
2022-01-25 10:19:06 +01:00
David Pedersen
4f6e86f026 Fixup changelogs 2022-01-25 09:46:26 +01:00
Jonas Platte
bf83f34617 Add trait IntoResponseHeaders (#649)
* 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
2022-01-23 18:01:52 +01:00
David Pedersen
b1ef0be1a7 Remove RequestParts::take_extensions (#699)
* 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>
2022-01-23 18:01:52 +01:00
David Pedersen
184ea656c0 Change HeaderMap extractor to clone the headers (#698)
* 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
2022-01-23 18:01:52 +01:00
Sabrina Jewson
d5694f0d0d Make sse::Event build event as a BytesMut (#647)
Co-authored-by: David Pedersen <david.pdrsn@gmail.com>
2022-01-23 18:01:52 +01:00
David Pedersen
d97f2aeae6 Make TypedHeaderRejectionReason #[non_exhaustive] (#665)
* Make `TypedHeaderRejectionReason` `#[non_exhaustive]`

Fixes https://github.com/tokio-rs/axum/issues/660

* add changelog pr link
2022-01-23 18:01:52 +01:00
David Pedersen
5d79c638f4 Require Output = () on WebSocketStream::on_upgrade (#646)
Fixes https://github.com/tokio-rs/axum/issues/636
2022-01-23 18:01:52 +01:00
David Pedersen
5fe981a1da
axum: Version 0.4.4 (#706)
- **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
2022-01-13 10:37:33 +01:00
Nick Ashley
007a0e85f2
Use 308 instead of 301 for trailing slash redirects (#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
2022-01-12 15:14:06 +01:00
David Pedersen
d181867355
Fix nesting Router at / leading to double slash in the path (#691)
* Fix nesting router at `/`

Fixes https://github.com/tokio-rs/axum/issues/690

* Allow nesting at `""`

* changelog

* remove outdated test
2022-01-06 12:12:24 +01:00
David Pedersen
4c48efc861
axum: Version 0.4.3 (#650) 2021-12-21 15:41:03 +01:00
David Pedersen
c50b55384c
Add ExtensionsAlreadyExtracted to PathRejection (#619)
* Add `ExtensionsAlreadyExtracted` to `PathRejection`

* format imports

* changelog
2021-12-12 23:51:18 +01:00
Kai Jewson
d9bf100216
Don't drop leading spaces in SSE responses (#600)
* Don't drop leading spaces in SSE responses

* Mention leading space fix in changelog

Co-authored-by: David Pedersen <david.pdrsn@gmail.com>
2021-12-12 16:11:15 +00:00
Kai Jewson
6feea82d61
Enforce stronger validation of SSE values (#599)
* Enforce stronger validation of SSE values

* Mention `sse::Event` strictness in changelog

Co-authored-by: David Pedersen <david.pdrsn@gmail.com>
2021-12-12 16:03:20 +00:00
Jonas Platte
b1623ce7f2
Add AddExtension::layer (#607) 2021-12-10 22:56:56 +01:00
David Pedersen
4b85b227e1 Update changelog with v0.3.4
This was shipped from the `v0.3.x` branch so wasn't moved to `main`.
2021-12-08 18:38:14 +01:00
David Pedersen
9f6be24ecd
axum: Version 0.4.2 (#592)
* axum: Version 0.4.2

This time depending on the correct version of axum-core.

* add missing changelog links
2021-12-06 12:03:13 +00:00
David Pedersen
8e1341d1f6
Release axum, axum-core, and axum-debug (#591) 2021-12-06 10:30:09 +01:00
Kai Jewson
dfb06e721c
Introduce Response type alias as a shorthand for Response<BoxBody> (#590)
* Introduce `Response` type alias as a shorthand

* Don't re-export `Response` at the crate root
2021-12-05 19:16:46 +01:00
David Pedersen
a63843d6c5
Release axum, axum-core, and axum-extra (#582) 2021-12-02 20:02:38 +01:00
David Pedersen
3ec680cce7
Provide more data in Path deserialization error (#574)
* 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
2021-12-02 08:51:29 +01:00
David Pedersen
254d8fde17
Move FromRequest and IntoResponse into new axum-core crate (#564)
* 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
2021-11-30 13:46:13 +00:00
Kai Jewson
2b6dba49cb
Remove the associated Body type on IntoResponse (#571) 2021-11-28 18:52:18 +01:00
Pure White
5a5800c1ae
feat: default to charset=utf-8 for text content type (#554)
* feat: default to charset=utf-8 for text content type

* added changelog && fix comment

* fix workflow
2021-11-25 08:31:30 +00:00
David Pedersen
1d94d75c57
Panic instead of silently discarding fallbacks (#529)
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
2021-11-18 22:19:06 +01:00
David Pedersen
9a410371a6
Move axum-handle-error-extract into axum (#534)
* 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
2021-11-17 19:09:58 +00:00
David Pedersen
a317072467
Rename box_body to body (#533)
Probably would have been easier to merge this change directly into
`main` first and the backport it to `v0.3.x` but here we are :P

Fixes #528
2021-11-17 12:59:29 +00:00
David Pedersen
939995e80e
Make the B type parameter in Handler default to axum::body::Body (#527)
* Swap type params in `Handler`

* Default `B` type param in `Handler`
2021-11-16 23:00:39 +01:00
David Pedersen
c4081ec40e
Update tokio-tungstenite (#525) 2021-11-16 20:22:00 +00:00
David Pedersen
2bd0310463
New method router (#521)
* 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
2021-11-16 20:49:07 +01:00
David Pedersen
b9dfea6360
Version 0.3.3 (#510)
- Implement `FromRequest` for [`http::request::Parts`] so it can be used an
  extractor ([#489])
- Implement `IntoResponse` for `http::response::Parts` ([#490])

[#489]: https://github.com/tokio-rs/axum/pull/489
[#490]: https://github.com/tokio-rs/axum/pull/490
[`http::request::Parts`]: https://docs.rs/http/latest/http/request/struct.Parts.html
2021-11-13 20:41:47 +01:00
David Pedersen
fdd889525d
Implement FromRequest for http::request::Parts (#489)
Its a convenient way to extract everything from a request except the
body. Also makes sense for axum to provide this since other crates
can't.
2021-11-09 20:37:24 +00:00
David Pedersen
62be8aaa18
Implement IntoResponse for http::response::Parts (#490)
* 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`
2021-11-09 20:50:29 +01:00
David Pedersen
3d78711e47
Version 0.3.2 (#483)
- **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
2021-11-08 18:01:32 +00:00
David Pedersen
7eb2c40b24
Add Router::route_layer (#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.
2021-11-08 18:48:02 +01:00
David Pedersen
09bcd8862d
Version 0.3.1 (#473)
- **fixed:** Implement `Clone` for `IntoMakeServiceWithConnectInfo` ([#471])

[#471]: https://github.com/tokio-rs/axum/pull/471
2021-11-06 13:26:23 +00:00
David Pedersen
b3b377d467
Implement Clone for IntoMakeServiceWithConnectInfo (#471)
Fixes https://github.com/tokio-rs/axum/issues/470
2021-11-06 13:06:20 +00:00
david-perez
071a1b26e6
Fix typos (#461) 2021-11-03 19:03:46 +00:00
David Pedersen
ba4d8a2357
Move axum crate into workspace subfolder (#458)
* 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.
2021-11-03 12:38:48 +01:00