mirror of https://github.com/fafhrd91/actix-web
Compare commits
10 Commits
31349b57c7
...
d30ce2762e
Author | SHA1 | Date |
---|---|---|
|
d30ce2762e | |
|
90c19a835d | |
|
adf57d2b24 | |
|
fcd10fbb5e | |
|
95b6a81f43 | |
|
ab18efe0ac | |
|
903efd382e | |
|
278cac34ae | |
|
5d9e498cdf | |
|
ca51ede874 |
|
@ -0,0 +1,3 @@
|
|||
version: "0.2"
|
||||
words:
|
||||
- actix
|
|
@ -49,7 +49,7 @@ jobs:
|
|||
toolchain: ${{ matrix.version.version }}
|
||||
|
||||
- name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean
|
||||
uses: taiki-e/install-action@v2.49.17
|
||||
uses: taiki-e/install-action@v2.49.33
|
||||
with:
|
||||
tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean
|
||||
|
||||
|
@ -83,7 +83,7 @@ jobs:
|
|||
uses: actions-rust-lang/setup-rust-toolchain@v1.11.0
|
||||
|
||||
- name: Install just, cargo-hack
|
||||
uses: taiki-e/install-action@v2.49.17
|
||||
uses: taiki-e/install-action@v2.49.33
|
||||
with:
|
||||
tool: just,cargo-hack
|
||||
|
||||
|
|
|
@ -64,7 +64,7 @@ jobs:
|
|||
toolchain: ${{ matrix.version.version }}
|
||||
|
||||
- name: Install just, cargo-hack, cargo-nextest, cargo-ci-cache-clean
|
||||
uses: taiki-e/install-action@v2.49.17
|
||||
uses: taiki-e/install-action@v2.49.33
|
||||
with:
|
||||
tool: just,cargo-hack,cargo-nextest,cargo-ci-cache-clean
|
||||
|
||||
|
@ -113,7 +113,7 @@ jobs:
|
|||
toolchain: nightly
|
||||
|
||||
- name: Install just
|
||||
uses: taiki-e/install-action@v2.49.17
|
||||
uses: taiki-e/install-action@v2.49.33
|
||||
with:
|
||||
tool: just
|
||||
|
||||
|
|
|
@ -24,7 +24,7 @@ jobs:
|
|||
components: llvm-tools
|
||||
|
||||
- name: Install just, cargo-llvm-cov, cargo-nextest
|
||||
uses: taiki-e/install-action@v2.49.17
|
||||
uses: taiki-e/install-action@v2.49.33
|
||||
with:
|
||||
tool: just,cargo-llvm-cov,cargo-nextest
|
||||
|
||||
|
|
|
@ -77,7 +77,7 @@ jobs:
|
|||
toolchain: ${{ vars.RUST_VERSION_EXTERNAL_TYPES }}
|
||||
|
||||
- name: Install just
|
||||
uses: taiki-e/install-action@v2.49.17
|
||||
uses: taiki-e/install-action@v2.49.33
|
||||
with:
|
||||
tool: just
|
||||
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
Cargo.lock
|
||||
target/
|
||||
guide/build/
|
||||
/gh-pages
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -18,8 +18,8 @@ all-features = true
|
|||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
bytesize = "2"
|
||||
darling = "0.20"
|
||||
parse-size = "1"
|
||||
proc-macro2 = "1"
|
||||
quote = "1"
|
||||
syn = "2"
|
||||
|
@ -27,7 +27,7 @@ syn = "2"
|
|||
[dev-dependencies]
|
||||
actix-multipart = "0.7"
|
||||
actix-web = "4"
|
||||
rustversion = "1"
|
||||
rustversion-msrv = "0.100"
|
||||
trybuild = "1"
|
||||
|
||||
[lints]
|
||||
|
|
|
@ -9,8 +9,8 @@
|
|||
|
||||
use std::collections::HashSet;
|
||||
|
||||
use bytesize::ByteSize;
|
||||
use darling::{FromDeriveInput, FromField, FromMeta};
|
||||
use parse_size::parse_size;
|
||||
use proc_macro::TokenStream;
|
||||
use proc_macro2::Ident;
|
||||
use quote::quote;
|
||||
|
@ -103,7 +103,7 @@ struct ParsedField<'t> {
|
|||
/// # Field Limits
|
||||
///
|
||||
/// You can use the `#[multipart(limit = "<size>")]` attribute to set field level limits. The limit
|
||||
/// string is parsed using [parse_size].
|
||||
/// string is parsed using [`bytesize`].
|
||||
///
|
||||
/// Note: the form is also subject to the global limits configured using `MultipartFormConfig`.
|
||||
///
|
||||
|
@ -150,7 +150,7 @@ struct ParsedField<'t> {
|
|||
/// struct Form { }
|
||||
/// ```
|
||||
///
|
||||
/// [parse_size]: https://docs.rs/parse-size/1/parse_size
|
||||
/// [`bytesize`]: https://docs.rs/bytesize/2
|
||||
#[proc_macro_derive(MultipartForm, attributes(multipart))]
|
||||
pub fn impl_multipart_form(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let input: syn::DeriveInput = parse_macro_input!(input);
|
||||
|
@ -191,8 +191,8 @@ pub fn impl_multipart_form(input: proc_macro::TokenStream) -> proc_macro::TokenS
|
|||
let attrs = FieldAttrs::from_field(field).map_err(|err| err.write_errors())?;
|
||||
let serialization_name = attrs.rename.unwrap_or_else(|| rust_name.to_string());
|
||||
|
||||
let limit = match attrs.limit.map(|limit| match parse_size(&limit) {
|
||||
Ok(size) => Ok(usize::try_from(size).unwrap()),
|
||||
let limit = match attrs.limit.map(|limit| match limit.parse::<ByteSize>() {
|
||||
Ok(ByteSize(size)) => Ok(usize::try_from(size).unwrap()),
|
||||
Err(err) => Err(syn::Error::new(
|
||||
field.ident.as_ref().unwrap().span(),
|
||||
format!("Could not parse size limit `{}`: {}", limit, err),
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#[rustversion::stable(1.72)] // MSRV
|
||||
#[rustversion_msrv::msrv]
|
||||
#[test]
|
||||
fn compile_macros() {
|
||||
let t = trybuild::TestCases::new();
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
error: Could not parse size limit `2 bytes`: invalid digit found in string
|
||||
error: Could not parse size limit `2 bytes`: couldn't parse "bytes" into a known SI unit, couldn't parse unit of "bytes"
|
||||
--> tests/trybuild/size-limit-parse-fail.rs:6:5
|
||||
|
|
||||
6 | description: Text<String>,
|
||||
| ^^^^^^^^^^^
|
||||
|
||||
error: Could not parse size limit `2 megabytes`: invalid digit found in string
|
||||
error: Could not parse size limit `2 megabytes`: couldn't parse "megabytes" into a known SI unit, couldn't parse unit of "megabytes"
|
||||
--> tests/trybuild/size-limit-parse-fail.rs:12:5
|
||||
|
|
||||
12 | description: Text<String>,
|
||||
| ^^^^^^^^^^^
|
||||
|
||||
error: Could not parse size limit `four meters`: invalid digit found in string
|
||||
error: Could not parse size limit `four meters`: couldn't parse "four meters" into a ByteSize, cannot parse float from empty string
|
||||
--> tests/trybuild/size-limit-parse-fail.rs:18:5
|
||||
|
|
||||
18 | description: Text<String>,
|
||||
|
|
|
@ -34,7 +34,7 @@ actix-web = "4"
|
|||
|
||||
futures-core = { version = "0.3.17", default-features = false, features = ["alloc"] }
|
||||
trybuild = "1"
|
||||
rustversion = "1"
|
||||
rustversion-msrv = "0.100"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#[rustversion::stable(1.72)] // MSRV
|
||||
#[rustversion_msrv::msrv]
|
||||
#[test]
|
||||
fn compile_macros() {
|
||||
let t = trybuild::TestCases::new();
|
||||
|
|
|
@ -34,6 +34,7 @@
|
|||
### Added
|
||||
|
||||
- Add `web::Html` responder.
|
||||
- Add `Logger::statuses` to filter the range of statuses logged.
|
||||
- Add `HttpRequest::full_url()` method to get the complete URL of the request.
|
||||
|
||||
### Fixed
|
||||
|
|
|
@ -189,6 +189,7 @@ tls-openssl = { package = "openssl", version = "0.10.55" }
|
|||
tls-rustls = { package = "rustls", version = "0.23" }
|
||||
tokio = { version = "1.24.2", features = ["rt-multi-thread", "macros"] }
|
||||
zstd = "0.13"
|
||||
capture-logger = "0.1"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
|
|
@ -7,6 +7,7 @@ use std::{
|
|||
fmt::{self, Display as _},
|
||||
future::Future,
|
||||
marker::PhantomData,
|
||||
ops::{Bound, RangeBounds},
|
||||
pin::Pin,
|
||||
rc::Rc,
|
||||
task::{Context, Poll},
|
||||
|
@ -26,7 +27,7 @@ use time::{format_description::well_known::Rfc3339, OffsetDateTime};
|
|||
|
||||
use crate::{
|
||||
body::{BodySize, MessageBody},
|
||||
http::header::HeaderName,
|
||||
http::{header::HeaderName, StatusCode},
|
||||
service::{ServiceRequest, ServiceResponse},
|
||||
Error, Result,
|
||||
};
|
||||
|
@ -92,6 +93,7 @@ struct Inner {
|
|||
exclude: HashSet<String>,
|
||||
exclude_regex: Vec<Regex>,
|
||||
log_target: Cow<'static, str>,
|
||||
status_range: (Bound<StatusCode>, Bound<StatusCode>),
|
||||
}
|
||||
|
||||
impl Logger {
|
||||
|
@ -102,6 +104,10 @@ impl Logger {
|
|||
exclude: HashSet::new(),
|
||||
exclude_regex: Vec::new(),
|
||||
log_target: Cow::Borrowed(module_path!()),
|
||||
status_range: (
|
||||
Bound::Included(StatusCode::from_u16(100).unwrap()),
|
||||
Bound::Included(StatusCode::from_u16(999).unwrap()),
|
||||
),
|
||||
}))
|
||||
}
|
||||
|
||||
|
@ -121,6 +127,23 @@ impl Logger {
|
|||
self
|
||||
}
|
||||
|
||||
/// Set a range of status to include in the logging
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use actix_web::{middleware::Logger, App, http::StatusCode};
|
||||
///
|
||||
/// // Log only the requests with status code higher or equal to BAD_REQUEST(400)
|
||||
/// let app = App::new()
|
||||
/// .wrap(Logger::default().statuses(StatusCode::BAD_REQUEST..));
|
||||
///
|
||||
/// ```
|
||||
pub fn statuses<R: RangeBounds<StatusCode>>(mut self, status: R) -> Self {
|
||||
let inner = Rc::get_mut(&mut self.0).unwrap();
|
||||
inner.status_range = (status.start_bound().cloned(), status.end_bound().cloned());
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the logging target to `target`.
|
||||
///
|
||||
/// By default, the log target is `module_path!()` of the log call location. In our case, that
|
||||
|
@ -242,6 +265,10 @@ impl Default for Logger {
|
|||
exclude: HashSet::new(),
|
||||
exclude_regex: Vec::new(),
|
||||
log_target: Cow::Borrowed(module_path!()),
|
||||
status_range: (
|
||||
Bound::Included(StatusCode::from_u16(100).unwrap()),
|
||||
Bound::Included(StatusCode::from_u16(999).unwrap()),
|
||||
),
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
@ -310,6 +337,7 @@ where
|
|||
LoggerResponse {
|
||||
fut: self.service.call(req),
|
||||
format: None,
|
||||
status_range: self.inner.status_range,
|
||||
time: OffsetDateTime::now_utc(),
|
||||
log_target: Cow::Borrowed(""),
|
||||
_phantom: PhantomData,
|
||||
|
@ -325,6 +353,7 @@ where
|
|||
LoggerResponse {
|
||||
fut: self.service.call(req),
|
||||
format: Some(format),
|
||||
status_range: self.inner.status_range,
|
||||
time: now,
|
||||
log_target: self.inner.log_target.clone(),
|
||||
_phantom: PhantomData,
|
||||
|
@ -343,6 +372,7 @@ pin_project! {
|
|||
fut: S::Future,
|
||||
time: OffsetDateTime,
|
||||
format: Option<Format>,
|
||||
status_range:(Bound<StatusCode>,Bound<StatusCode>),
|
||||
log_target: Cow<'static, str>,
|
||||
_phantom: PhantomData<B>,
|
||||
}
|
||||
|
@ -367,7 +397,13 @@ where
|
|||
debug!("Error in response: {:?}", error);
|
||||
}
|
||||
|
||||
let res = if let Some(ref mut format) = this.format {
|
||||
let mut format = if this.status_range.contains(&res.status()) {
|
||||
this.format.take()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let res = if let Some(ref mut format) = format {
|
||||
// to avoid polluting all the Logger types with the body parameter we swap the body
|
||||
// out temporarily since it's not usable in custom response functions anyway
|
||||
|
||||
|
@ -388,7 +424,6 @@ where
|
|||
};
|
||||
|
||||
let time = *this.time;
|
||||
let format = this.format.take();
|
||||
let log_target = this.log_target.clone();
|
||||
|
||||
Poll::Ready(Ok(res.map_body(move |_, body| StreamLog {
|
||||
|
@ -741,7 +776,13 @@ mod tests {
|
|||
header::HeaderValue::from_static("ACTIX-WEB"),
|
||||
))
|
||||
.to_srv_request();
|
||||
let _res = srv.call(req).await;
|
||||
capture_logger::begin_capture();
|
||||
// The log is executed on drop, so the result need to be dropped
|
||||
let _ = srv.call(req).await;
|
||||
let log = capture_logger::pop_captured().unwrap();
|
||||
assert!(log.message().contains("ttt"));
|
||||
assert!(log.message().contains("ACTIX-WEB"));
|
||||
capture_logger::end_capture();
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
|
@ -767,6 +808,54 @@ mod tests {
|
|||
let _res = srv.call(req).await.unwrap();
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_logger_status_range_include() {
|
||||
let srv = |req: ServiceRequest| {
|
||||
ok(req.into_response(HttpResponse::build(StatusCode::OK).finish()))
|
||||
};
|
||||
let logger = Logger::new("%{User-Agent}i test_included %s").statuses(StatusCode::OK..);
|
||||
|
||||
let srv = logger.new_transform(srv.into_service()).await.unwrap();
|
||||
|
||||
let req = TestRequest::default()
|
||||
.insert_header((
|
||||
header::USER_AGENT,
|
||||
header::HeaderValue::from_static("ACTIX-WEB"),
|
||||
))
|
||||
.to_srv_request();
|
||||
capture_logger::begin_capture();
|
||||
// The log is executed on drop, so the result need to be dropped
|
||||
let _ = srv.call(req).await;
|
||||
let log = capture_logger::pop_captured().unwrap();
|
||||
assert!(log.message().contains("200"));
|
||||
assert!(log.message().contains("ACTIX-WEB"));
|
||||
capture_logger::end_capture();
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_logger_status_range_exclude() {
|
||||
let srv = |req: ServiceRequest| {
|
||||
ok(req.into_response(HttpResponse::build(StatusCode::OK).finish()))
|
||||
};
|
||||
let logger =
|
||||
Logger::new("%{User-Agent}i test_excluded %s").statuses(StatusCode::BAD_REQUEST..);
|
||||
|
||||
let srv = logger.new_transform(srv.into_service()).await.unwrap();
|
||||
|
||||
let req = TestRequest::default()
|
||||
.insert_header((
|
||||
header::USER_AGENT,
|
||||
header::HeaderValue::from_static("ACTIX-WEB"),
|
||||
))
|
||||
.to_srv_request();
|
||||
capture_logger::begin_capture();
|
||||
// The log is executed on drop, so the result need to be dropped
|
||||
let _ = srv.call(req).await;
|
||||
let log = capture_logger::pop_captured();
|
||||
assert!(log.is_none());
|
||||
capture_logger::end_capture();
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_escape_percent() {
|
||||
let mut format = Format::new("%%{r}a");
|
||||
|
|
12
justfile
12
justfile
|
@ -7,14 +7,14 @@ fmt:
|
|||
cargo +nightly fmt
|
||||
fd --hidden --type=file --extension=md --extension=yml --exec-batch npx -y prettier --write
|
||||
|
||||
# Downgrade dev-dependencies necessary to run MSRV checks/tests.
|
||||
# Downgrade dependencies necessary to run MSRV checks/tests.
|
||||
[private]
|
||||
downgrade-for-msrv:
|
||||
cargo update -p=parse-size --precise=1.0.0
|
||||
cargo update -p=clap --precise=4.4.18
|
||||
cargo update -p=divan --precise=0.1.15
|
||||
cargo update -p=litemap --precise=0.7.4
|
||||
cargo update -p=zerofrom --precise=0.1.5
|
||||
cargo update -p=clap --precise=4.4.18 # next ver: 1.74.0
|
||||
cargo update -p=divan --precise=0.1.15 # next ver: 1.80.0
|
||||
cargo update -p=litemap --precise=0.7.4 # next ver: 1.81.0
|
||||
cargo update -p=zerofrom --precise=0.1.5 # next ver: 1.81.0
|
||||
cargo update -p=half --precise=2.4.1 # next ver: 1.81.0
|
||||
|
||||
msrv := ```
|
||||
cargo metadata --format-version=1 \
|
||||
|
|
Loading…
Reference in New Issue