Move error handling into utils

main
Elnu 1 year ago
parent 7c043e8357
commit ef562fe656

9
Cargo.lock generated

@ -698,6 +698,13 @@ dependencies = [
[[package]] [[package]]
name = "images" name = "images"
version = "0.1.0" version = "0.1.0"
dependencies = [
"actix-web",
"derive_more",
"mime",
"reqwest",
"utils",
]
[[package]] [[package]]
name = "indexmap" name = "indexmap"
@ -1458,6 +1465,8 @@ name = "utils"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"actix-web", "actix-web",
"derive_more",
"reqwest",
] ]
[[package]] [[package]]

@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
utils = { path = "../utils" } utils = { path = "../utils", features = [ "nothuman" ] }
actix-web = "4.3.1" actix-web = "4.3.1"
derive_more = "0.99.17" derive_more = "0.99.17"
mime = "0.3.17" mime = "0.3.17"

@ -1,7 +1,4 @@
mod error; use utils::{error::{Error, Result}, is_human};
pub use error::{Error, Result};
use utils::is_human;
use actix_web::{get, http::header, App, HttpRequest, HttpResponse, HttpServer, Responder}; use actix_web::{get, http::header, App, HttpRequest, HttpResponse, HttpServer, Responder};

@ -7,3 +7,8 @@ edition = "2021"
[dependencies] [dependencies]
actix-web = "4.3.1" actix-web = "4.3.1"
derive_more = "0.99.17"
reqwest = "0.11.18"
[features]
nothuman = []

@ -1,4 +1,7 @@
use actix_web::{HttpResponse, ResponseError}; #[cfg(feature = "nothuman")]
use actix_web::HttpResponse;
use actix_web::ResponseError;
use derive_more::From; use derive_more::From;
use std::fmt::{self, Display}; use std::fmt::{self, Display};
@ -6,7 +9,8 @@ pub type Result<T> = std::result::Result<T, Error>;
#[derive(From, Debug)] #[derive(From, Debug)]
pub enum Error { pub enum Error {
TatoebaApi(reqwest::Error), Reqwest(reqwest::Error),
#[cfg(feature = "nothuman")]
NotHuman { target: String }, NotHuman { target: String },
} }
@ -22,12 +26,14 @@ impl ResponseError for Error {
use Error::*; use Error::*;
match self { match self {
// 503 Service Unavailable // 503 Service Unavailable
TatoebaApi(error) => error.status().unwrap_or(StatusCode::SERVICE_UNAVAILABLE), Reqwest(error) => error.status().unwrap_or(StatusCode::SERVICE_UNAVAILABLE),
// 403 Forbidden // 403 Forbidden
#[cfg(feature = "nothuman")]
NotHuman { .. } => StatusCode::FORBIDDEN, NotHuman { .. } => StatusCode::FORBIDDEN,
} }
} }
#[cfg(feature = "nothuman")]
fn error_response(&self) -> HttpResponse<actix_web::body::BoxBody> { fn error_response(&self) -> HttpResponse<actix_web::body::BoxBody> {
HttpResponse::build(self.status_code()).body(match self { HttpResponse::build(self.status_code()).body(match self {
Self::NotHuman { target } => format!( Self::NotHuman { target } => format!(
@ -40,3 +46,4 @@ impl ResponseError for Error {
}) })
} }
} }

@ -0,0 +1,21 @@
use actix_web::{http::header, HttpRequest};
pub fn is_human(request: &HttpRequest) -> bool {
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent
const HUMANS: &[&str] = &[
"Firefox", "Chrome", // Chrome and Chromium browsers
"Opera", // Old Presto-based Opera
"Mobile", // Safari
"Trident", // Internet Explorer
];
request
.headers()
.get(header::USER_AGENT)
.and_then(|header| header.to_str().ok())
.map(|ua| {
HUMANS
.iter()
.any(|&human| ua.contains(human))
})
.unwrap_or(false)
}

@ -1,21 +1,6 @@
use actix_web::{http::header, HttpRequest}; pub mod error;
pub fn is_human(request: &HttpRequest) -> bool { #[cfg(feature = "nothuman")]
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent mod is_human;
const HUMANS: &[&str] = &[ #[cfg(feature = "nothuman")]
"Firefox", "Chrome", // Chrome and Chromium browsers pub use is_human::is_human;
"Opera", // Old Presto-based Opera
"Mobile", // Safari
"Trident", // Internet Explorer
];
request
.headers()
.get(header::USER_AGENT)
.and_then(|header| header.to_str().ok())
.map(|ua| {
HUMANS
.iter()
.any(|&human| ua.contains(human))
})
.unwrap_or(false)
}

Loading…
Cancel
Save