use fastly::http::StatusCode;
use fastly::{Error, Request, Response};
use std::ffi::OsStr;
use std::path::Path;

const BLOCK_FILE_EXT_LIST: &[&str] = &["exe", "dll", "zip"];

#[fastly::main]
fn main(req: Request) -> Result<Response, Error> {
    let path = req.get_path();

    // Check if that file extension is in the list of bad file extensions
    if let Some(ext) = Path::new(path).extension().and_then(OsStr::to_str) {
        let ext_lowercase = ext.to_lowercase();
        if BLOCK_FILE_EXT_LIST.contains(&ext_lowercase.as_str()) {
            // Bad file extension found, return a 403 error
            return Ok(Response::from_status(StatusCode::FORBIDDEN));
        }
    }

    // If not blocked, let the request pass through to the origin
    Ok(req.send("origin_0")?)
}