summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorache <ache@ache.one>2022-12-10 09:01:52 +0100
committerache <ache@ache.one>2022-12-10 09:38:38 +0100
commitf761c6e6e66cc30803929ef01911be3a65c01ef4 (patch)
tree853b868f31e665c7b83912e84b5fe9f1e8aad4a8
parentImplement proxy (diff)
Fix responce
-rw-r--r--src/main.rs27
1 files changed, 24 insertions, 3 deletions
diff --git a/src/main.rs b/src/main.rs
index c7a128f..395b853 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -7,6 +7,7 @@ use std::sync::Arc;
use std::fs;
use hyper::header;
+use hyper::http::response;
use serde_derive::{Serialize, Deserialize};
use toml;
@@ -53,8 +54,25 @@ fn handle(req: Request<Body>, addr: SocketAddr, tx: Sender<VustMessage>, config:
let addr = match req.headers().contains_key("real-ip") {
false => addr,
true => {
- let string_ip = req.headers().get("real-ip").unwrap().to_str().unwrap();
- string_ip.parse().expect("Unable to parse IP address")
+ let real_ip_str = match req.headers().get("real-ip") {
+ Some(ip) => ip.to_str(),
+ None => {
+ return respond(StatusCode::SERVICE_UNAVAILABLE, "Désolé, votre IP est cachée".to_string());
+ },
+ };
+ let real_ip_str = match real_ip_str {
+ Ok(addr) => addr,
+ Err(_) => {
+ return respond(StatusCode::SERVICE_UNAVAILABLE, format!("Désolé, votre IP nous pose problème. ({:?})", real_ip_str).to_string());
+ }
+ };
+ let ip :IpAddr = match real_ip_str.parse() {
+ Ok(addr) => addr,
+ Err(_) => {
+ return respond(StatusCode::SERVICE_UNAVAILABLE, format!("Désolé, votre IP nous pose problème. ({:?})", real_ip_str).to_string());
+ }
+ };
+ SocketAddr::new(ip, 0)
}
};
@@ -121,7 +139,9 @@ fn do_get(tr: &Transaction, ip : SocketAddr, path : String) -> Response<Body> {
match first_row.get(0) {
Ok(nb_likes) => return respond(StatusCode::OK, nb_likes),
// In case of NULL or not a Integer value:
- Err(_) => break,
+ Err(_) => {
+ return respond(StatusCode::OK, "â‹…".to_string())
+ }
}
}
@@ -148,6 +168,7 @@ fn do_like(tr: &Transaction, ip : SocketAddr, path : String) -> Response<Body> {
continue;
}
+ return respond(StatusCode::OK, "Merci ! đź’•".to_string())
},
Ok(Some(t)) => {
let number : u64 = t.get(0).unwrap();