Skip to content

Commit

Permalink
chore(deps): bump byte-unit from 4.0.19 to 5.0.3 (#192)
Browse files Browse the repository at this point in the history
* 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](magiclen/Byte-Unit@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] <[email protected]>

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

* fix(fixtures): use more precise byte comparison

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Orhun Parmaksız <[email protected]>
  • Loading branch information
dependabot[bot] and orhun authored Dec 5, 2023
1 parent 3c3a4b5 commit a291307
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 17 deletions.
15 changes: 13 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion fixtures/test-server-payload-limit/config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[server]
address = "127.0.0.1:8000"
max_content_length = "10kb"
max_content_length = "10KB"
upload_path = "./upload"

[paste]
Expand Down
4 changes: 1 addition & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
15 changes: 10 additions & 5 deletions src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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 }
}
}
Expand Down Expand Up @@ -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>
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/paste.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
10 changes: 6 additions & 4 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit a291307

Please sign in to comment.