~mort/coffeepaste

ffefeb6de68dbc8d9dba91c614e93a100b9453fd — Martin Dørum 3 months ago 78e7447
add allow_upload key
2 files changed, 17 insertions(+), 1 deletions(-)

M src/main.rs
A web/401.html
M src/main.rs => src/main.rs +16 -1
@@ 36,6 36,7 @@ static FAVICON_ICO: &'static [u8] = include_bytes!("../web/favicon.ico");
static VENDOR_HIGHLIGHT_JS: &'static [u8] = include_bytes!("../web/vendor/highlight.min.js");
static VENDOR_HIGHLIGHT_CSS: &'static [u8] = include_bytes!("../web/vendor/highlight.min.css");
static ERR_400_HTML: &'static [u8] = include_bytes!("../web/400.html");
static ERR_401_HTML: &'static [u8] = include_bytes!("../web/401.html");
static ERR_404_HTML: &'static [u8] = include_bytes!("../web/404.html");
static ERR_413_HTML: &'static [u8] = include_bytes!("../web/413.html");
static ERR_500_HTML: &'static [u8] = include_bytes!("../web/500.html");


@@ 45,6 46,7 @@ static CONFIG: OnceCell<Config> = OnceCell::new();
#[derive(Deserialize, Debug)]
struct UserConfig {
    token: String,
    allow_upload: Option<bool>,
    max_file_size: Option<i64>,
}



@@ 55,6 57,7 @@ struct Config {
    listen: String,
    max_file_size: i64,
    url: String,
    allow_upload: bool,
    users: Vec<UserConfig>,
}



@@ 66,6 69,7 @@ impl std::default::Default for Config {
            listen: "127.0.0.1:8080".into(),
            max_file_size: 10000000,
            url: "http://localhost:8080".into(),
            allow_upload: true,
            users: Vec::new(),
        }
    }


@@ 312,6 316,7 @@ async fn on_put(config: &Config, req: Request<Body>) -> Result<Response<Body>, I
    let headers = req.headers();

    let mut max_file_size = config.max_file_size;
    let mut allow_upload = config.allow_upload;

    if let Some(auth) = headers.get(header::AUTHORIZATION) {
        let mut parts = match auth.to_str() {


@@ 352,7 357,7 @@ async fn on_put(config: &Config, req: Request<Body>) -> Result<Response<Body>, I
        if user_config.is_none() {
            error!("Invalid authorization token");
            return Ok(Response::builder()
                .status(400).body(Body::from(ERR_400_HTML)).unwrap());
                .status(401).body(Body::from(ERR_401_HTML)).unwrap());
        }

        let user_config = user_config.unwrap();


@@ 360,6 365,16 @@ async fn on_put(config: &Config, req: Request<Body>) -> Result<Response<Body>, I
        if let Some(user_max_file_size) = user_config.max_file_size {
            max_file_size = user_max_file_size;
        }

        if let Some(user_allow_upload) = user_config.allow_upload {
            allow_upload = user_allow_upload;
        }
    }

    if !allow_upload {
        error!("Uploads not allowed");
        return Ok(Response::builder()
            .status(401).body(Body::from(ERR_401_HTML)).unwrap());
    }

    let (parts, mut reqbody) = req.into_parts();

A web/401.html => web/401.html +1 -0
@@ 0,0 1,1 @@
401: Unauthorized