Skip to content

Commit 4eedffd

Browse files
committed
Bundle max upload and max unpack size into one struct
To get rid of a clippy too many arguments warning; also nice to extract the knowledge of how these get decided to one place
1 parent ff8b5ba commit 4eedffd

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

src/controllers/krate/publish.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Functionality related to publishing a new crate or version of a crate.
22
3-
use std::cmp;
43
use std::collections::HashMap;
54
use std::sync::Arc;
65

@@ -9,7 +8,7 @@ use serde_json;
98

109
use git;
1110
use render;
12-
use util::{internal, ChainError};
11+
use util::{internal, ChainError, Maximums};
1312
use util::{read_fill, read_le_u32};
1413

1514
use controllers::prelude::*;
@@ -111,12 +110,18 @@ pub fn publish(req: &mut dyn Request) -> CargoResult<Response> {
111110
let content_length = req
112111
.content_length()
113112
.chain_error(|| human("missing header: Content-Length"))?;
114-
let max = krate
115-
.max_upload_size
116-
.map(|m| m as u64)
117-
.unwrap_or(app.config.max_upload_size);
118-
if content_length > max {
119-
return Err(human(&format_args!("max upload size is: {}", max)));
113+
114+
let maximums = Maximums::new(
115+
krate.max_upload_size,
116+
app.config.max_upload_size,
117+
app.config.max_unpack_size,
118+
);
119+
120+
if content_length > maximums.max_upload_size {
121+
return Err(human(&format_args!(
122+
"max upload size is: {}",
123+
maximums.max_upload_size
124+
)));
120125
}
121126

122127
// This is only redundant for now. Eventually the duplication will be removed.
@@ -162,16 +167,10 @@ pub fn publish(req: &mut dyn Request) -> CargoResult<Response> {
162167
// Upload the crate, return way to delete the crate from the server
163168
// If the git commands fail below, we shouldn't keep the crate on the
164169
// server.
165-
let max_unpack = cmp::max(app.config.max_unpack_size, max);
166-
let (cksum, mut crate_bomb, mut readme_bomb) = app.config.uploader.upload_crate(
167-
req,
168-
&krate,
169-
readme,
170-
file_length,
171-
max,
172-
max_unpack,
173-
vers,
174-
)?;
170+
let (cksum, mut crate_bomb, mut readme_bomb) =
171+
app.config
172+
.uploader
173+
.upload_crate(req, &krate, readme, file_length, maximums, vers)?;
175174
version.record_readme_rendering(&conn)?;
176175

177176
let mut hex_cksum = String::new();

src/uploaders.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use semver;
77
use tar;
88

99
use util::LimitErrorReader;
10-
use util::{human, internal, CargoResult, ChainError};
10+
use util::{human, internal, CargoResult, ChainError, Maximums};
1111

1212
use std::env;
1313
use std::fs::{self, File};
@@ -162,16 +162,15 @@ impl Uploader {
162162
krate: &Crate,
163163
readme: Option<String>,
164164
file_length: u32,
165-
max: u64,
166-
max_unpack: u64,
165+
maximums: Maximums,
167166
vers: &semver::Version,
168167
) -> CargoResult<(Vec<u8>, Bomb, Bomb)> {
169168
let app = Arc::clone(req.app());
170169
let (crate_path, checksum) = {
171170
let path = Uploader::crate_path(&krate.name, &vers.to_string());
172171
let mut body = Vec::new();
173-
LimitErrorReader::new(req.body(), max).read_to_end(&mut body)?;
174-
verify_tarball(krate, vers, &body, max_unpack)?;
172+
LimitErrorReader::new(req.body(), maximums.max_upload_size).read_to_end(&mut body)?;
173+
verify_tarball(krate, vers, &body, maximums.max_unpack_size)?;
175174
self.upload(
176175
app.handle(),
177176
&path,

src/util/mod.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::cmp;
12
use std::collections::HashMap;
23
use std::io::Cursor;
34

@@ -32,3 +33,24 @@ pub fn json_response<T: Serialize>(t: &T) -> Response {
3233
body: Box::new(Cursor::new(json.into_bytes())),
3334
}
3435
}
36+
37+
#[derive(Debug, Copy, Clone)]
38+
pub struct Maximums {
39+
pub max_upload_size: u64,
40+
pub max_unpack_size: u64,
41+
}
42+
43+
impl Maximums {
44+
pub fn new(
45+
krate_max_upload: Option<i32>,
46+
app_max_upload: u64,
47+
app_max_unpack: u64,
48+
) -> Maximums {
49+
let max_upload_size = krate_max_upload.map(|m| m as u64).unwrap_or(app_max_upload);
50+
let max_unpack_size = cmp::max(app_max_unpack, max_upload_size);
51+
Maximums {
52+
max_upload_size,
53+
max_unpack_size,
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)