Skip to content

Commit 1fdd265

Browse files
committed
Merge branch 'main' into path-error
2 parents 6dacde7 + 254d8fd commit 1fdd265

41 files changed

Lines changed: 1431 additions & 971 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[workspace]
22
members = [
33
"axum",
4+
"axum-core",
45
"axum-debug",
56
"axum-extra",
67
"examples/*",

axum-core/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
# Unreleased
9+
10+
- None.

axum-core/Cargo.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[package]
2+
categories = ["asynchronous", "network-programming", "web-programming"]
3+
description = "Core types and traits for axum"
4+
edition = "2018"
5+
homepage = "https://github.com/tokio-rs/axum"
6+
keywords = ["http", "web", "framework"]
7+
license = "MIT"
8+
name = "axum-core"
9+
readme = "README.md"
10+
repository = "https://github.com/tokio-rs/axum"
11+
version = "0.1.0"
12+
13+
[dependencies]
14+
async-trait = "0.1"
15+
bytes = "1.0"
16+
futures-util = { version = "0.3", default-features = false, features = ["alloc"] }
17+
http = "0.2"
18+
http-body = "0.4"
19+
mime = "0.3.16"
20+
21+
[dev-dependencies]
22+
futures-util = "0.3"
23+
axum = { path = "../axum", version = "0.3" }
24+
hyper = "0.14"

axum-core/LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2021 Axum Contributors
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

axum-core/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# axum-core
2+
3+
[![Build status](https://github.com/tokio-rs/axum/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/tokio-rs/axum-core/actions/workflows/CI.yml)
4+
[![Crates.io](https://img.shields.io/crates/v/axum-core)](https://crates.io/crates/axum-core)
5+
[![Documentation](https://docs.rs/axum-core/badge.svg)](https://docs.rs/axum-core)
6+
7+
Core types and traits for axum.
8+
9+
More information about this crate can be found in the [crate documentation][docs].
10+
11+
## Safety
12+
13+
This crate uses `#![forbid(unsafe_code)]` to ensure everything is implemented in 100% safe Rust.
14+
15+
## Minimum supported Rust version
16+
17+
axum-core's MSRV is 1.54.
18+
19+
## Getting Help
20+
21+
You're also welcome to ask in the [Discord channel][chat] or open an [issue]
22+
with your question.
23+
24+
## Contributing
25+
26+
:balloon: Thanks for your help improving the project! We are so happy to have
27+
you! We have a [contributing guide][contributing] to help you get involved in the
28+
`axum` project.
29+
30+
## License
31+
32+
This project is licensed under the [MIT license][license].
33+
34+
### Contribution
35+
36+
Unless you explicitly state otherwise, any contribution intentionally submitted
37+
for inclusion in `axum` by you, shall be licensed as MIT, without any
38+
additional terms or conditions.
39+
40+
[`axum`]: https://crates.io/crates/axum
41+
[chat]: https://discord.gg/tokio
42+
[contributing]: /CONTRIBUTING.md
43+
[docs]: https://docs.rs/axum-core
44+
[license]: /axum-core/LICENSE
45+
[issue]: https://github.com/tokio-rs/axum/issues/new

axum-core/src/body.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//! HTTP body utilities.
2+
3+
use crate::{BoxError, Error};
4+
use bytes::Bytes;
5+
use bytes::{Buf, BufMut};
6+
use http_body::Body;
7+
8+
/// A boxed [`Body`] trait object.
9+
///
10+
/// This is used in axum as the response body type for applications. It's
11+
/// necessary to unify multiple response bodies types into one.
12+
pub type BoxBody = http_body::combinators::UnsyncBoxBody<Bytes, Error>;
13+
14+
/// Convert a [`http_body::Body`] into a [`BoxBody`].
15+
pub fn boxed<B>(body: B) -> BoxBody
16+
where
17+
B: http_body::Body<Data = Bytes> + Send + 'static,
18+
B::Error: Into<BoxError>,
19+
{
20+
try_downcast(body).unwrap_or_else(|body| body.map_err(Error::new).boxed_unsync())
21+
}
22+
23+
pub(crate) fn try_downcast<T, K>(k: K) -> Result<T, K>
24+
where
25+
T: 'static,
26+
K: Send + 'static,
27+
{
28+
let mut k = Some(k);
29+
if let Some(k) = <dyn std::any::Any>::downcast_mut::<Option<T>>(&mut k) {
30+
Ok(k.take().unwrap())
31+
} else {
32+
Err(k.unwrap())
33+
}
34+
}
35+
36+
// copied from hyper under the following license:
37+
// Copyright (c) 2014-2021 Sean McArthur
38+
39+
// Permission is hereby granted, free of charge, to any person obtaining a copy
40+
// of this software and associated documentation files (the "Software"), to deal
41+
// in the Software without restriction, including without limitation the rights
42+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
43+
// copies of the Software, and to permit persons to whom the Software is
44+
// furnished to do so, subject to the following conditions:
45+
46+
// The above copyright notice and this permission notice shall be included in
47+
// all copies or substantial portions of the Software.
48+
49+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
52+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
54+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
55+
// THE SOFTWARE.
56+
pub(crate) async fn to_bytes<T>(body: T) -> Result<Bytes, T::Error>
57+
where
58+
T: Body,
59+
{
60+
futures_util::pin_mut!(body);
61+
62+
// If there's only 1 chunk, we can just return Buf::to_bytes()
63+
let mut first = if let Some(buf) = body.data().await {
64+
buf?
65+
} else {
66+
return Ok(Bytes::new());
67+
};
68+
69+
let second = if let Some(buf) = body.data().await {
70+
buf?
71+
} else {
72+
return Ok(first.copy_to_bytes(first.remaining()));
73+
};
74+
75+
// With more than 1 buf, we gotta flatten into a Vec first.
76+
let cap = first.remaining() + second.remaining() + body.size_hint().lower() as usize;
77+
let mut vec = Vec::with_capacity(cap);
78+
vec.put(first);
79+
vec.put(second);
80+
81+
while let Some(buf) = body.data().await {
82+
vec.put(buf?);
83+
}
84+
85+
Ok(vec.into())
86+
}
87+
88+
#[test]
89+
fn test_try_downcast() {
90+
assert_eq!(try_downcast::<i32, _>(5_u32), Err(5_u32));
91+
assert_eq!(try_downcast::<i32, _>(5_i32), Ok(5_i32));
92+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ pub struct Error {
88
}
99

1010
impl Error {
11-
pub(crate) fn new(error: impl Into<BoxError>) -> Self {
11+
/// Create a new `Error` from a boxable error.
12+
pub fn new(error: impl Into<BoxError>) -> Self {
1213
Self {
1314
inner: error.into(),
1415
}

0 commit comments

Comments
 (0)