chore(deps): bump byte-unit from 4.0.19 to 5.0.3 (#192)

* chore(deps): bump byte-unit from 4.0.19 to 5.0.3

Bumps [byte-unit](https://github.com/magiclen/byte-unit) from 4.0.19 to 5.0.3.
- [Commits](https://github.com/magiclen/byte-unit/compare/v4.0.19...v5.0.3)

---
updated-dependencies:
- dependency-name: byte-unit
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* fix(deps): update codebase accordingly to the new version of byte-unit

* fix(fixtures): use more precise byte comparison

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com>
This commit is contained in:
dependabot[bot] 2023-12-05 13:08:22 +01:00 committed by GitHub
parent 3c3a4b58ac
commit a291307bec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 33 additions and 17 deletions

15
Cargo.lock generated
View File

@ -546,10 +546,11 @@ checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec"
[[package]]
name = "byte-unit"
version = "4.0.19"
version = "5.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da78b32057b8fdfc352504708feeba7216dcd65a2c9ab02978cbd288d1279b6c"
checksum = "bc40af92e0f7f964b7ab1ebc81315cce78fc484802d534143321c956f58d7be3"
dependencies = [
"rust_decimal",
"serde",
"utf8-width",
]
@ -2110,6 +2111,16 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "rust_decimal"
version = "1.33.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06676aec5ccb8fc1da723cc8c0f9a46549f21ebb8753d3915c6c41db1e7f1dc4"
dependencies = [
"arrayvec",
"num-traits",
]
[[package]]
name = "rustc-demangle"
version = "0.1.23"

View File

@ -54,7 +54,7 @@ default-features = false
features = ["toml", "yaml"]
[dependencies.byte-unit]
version = "4.0.19"
version = "5.0.3"
features = ["serde"]
[dependencies.infer]

View File

@ -1,6 +1,6 @@
[server]
address = "127.0.0.1:8000"
max_content_length = "10kb"
max_content_length = "10KB"
upload_path = "./upload"
[paste]

View File

@ -173,9 +173,7 @@ async fn main() -> IoResult<()> {
.wrap(Logger::new(
"%{r}a \"%r\" %s %b \"%{Referer}i\" \"%{User-Agent}i\" %T",
))
.wrap(ContentLengthLimiter::new(
server_config.max_content_length.get_bytes(),
))
.wrap(ContentLengthLimiter::new(server_config.max_content_length))
.configure(server::configure_routes)
})
.bind(&server_config.address)?;

View File

@ -3,6 +3,7 @@ use actix_web::http::header::CONTENT_LENGTH;
use actix_web::http::StatusCode;
use actix_web::{body::EitherBody, Error};
use actix_web::{HttpMessage, HttpResponseBuilder};
use byte_unit::Byte;
use futures_util::{Future, TryStreamExt};
use std::{
future::{ready, Ready},
@ -14,12 +15,12 @@ use std::{
#[derive(Debug)]
pub struct ContentLengthLimiter {
// Maximum amount of bytes to allow.
max_bytes: u128,
max_bytes: Byte,
}
impl ContentLengthLimiter {
/// Constructs a new instance.
pub fn new(max_bytes: u128) -> Self {
pub fn new(max_bytes: Byte) -> Self {
Self { max_bytes }
}
}
@ -47,7 +48,7 @@ where
#[derive(Debug)]
pub struct ContentLengthLimiterMiddleware<S> {
service: Rc<S>,
max_bytes: u128,
max_bytes: Byte,
}
impl<S, B> Service<ServiceRequest> for ContentLengthLimiterMiddleware<S>
@ -66,10 +67,14 @@ where
.headers()
.get(CONTENT_LENGTH)
.and_then(|v| v.to_str().ok())
.and_then(|v| v.parse::<u128>().ok())
.and_then(|v| v.parse::<Byte>().ok())
{
if content_length > self.max_bytes {
tracing::warn!("Upload rejected due to exceeded limit.");
tracing::warn!(
"Upload rejected due to exceeded limit. ({:-#} > {:-#})",
content_length,
self.max_bytes
);
return Box::pin(async move {
// drain the body due to https://github.com/actix/actix-web/issues/2695
let mut payload = request.take_payload();

View File

@ -210,7 +210,6 @@ impl Paste {
.map_err(|_| error::ErrorInternalServerError("cannot acquire config"))?
.server
.max_content_length
.get_bytes()
.try_into()
.map_err(error::ErrorInternalServerError)?;
let bytes = response
@ -277,6 +276,7 @@ mod tests {
use awc::ClientBuilder;
use byte_unit::Byte;
use std::env;
use std::str::FromStr;
use std::time::Duration;
#[actix_rt::test]

View File

@ -9,7 +9,7 @@ use actix_files::NamedFile;
use actix_multipart::Multipart;
use actix_web::{delete, error, get, post, web, Error, HttpRequest, HttpResponse};
use awc::Client;
use byte_unit::Byte;
use byte_unit::{Byte, UnitType};
use futures_util::stream::StreamExt;
use mime::TEXT_PLAIN_UTF_8;
use serde::{Deserialize, Serialize};
@ -307,7 +307,9 @@ async fn upload(
tracing::info!(
"{} ({}) is uploaded from {}",
file_name,
Byte::from_bytes(paste.data.len() as u128).get_appropriate_unit(false),
Byte::from_u128(paste.data.len() as u128)
.unwrap_or_default()
.get_appropriate_unit(UnitType::Decimal),
host
);
let config = config
@ -743,7 +745,7 @@ mod tests {
App::new()
.app_data(Data::new(RwLock::new(Config::default())))
.app_data(Data::new(Client::default()))
.wrap(ContentLengthLimiter::new(1))
.wrap(ContentLengthLimiter::new(Byte::from_u64(1)))
.configure(configure_routes),
)
.await;
@ -975,7 +977,7 @@ mod tests {
async fn test_upload_remote_file() -> Result<(), Error> {
let mut config = Config::default();
config.server.upload_path = env::current_dir()?;
config.server.max_content_length = Byte::from_bytes(30000);
config.server.max_content_length = Byte::from_u128(30000).unwrap_or_default();
let app = test::init_service(
App::new()