summaryrefslogtreecommitdiff
path: root/src/types
diff options
context:
space:
mode:
authorTyler Murphy <tylermurphy534@gmail.com>2023-02-01 20:34:22 -0500
committerTyler Murphy <tylermurphy534@gmail.com>2023-02-01 20:34:22 -0500
commit2026a8f4579b1db0f6e5e7b11ac33c13969adb6c (patch)
treedaa13419b7227462775e325a4f5f60f2ed33c1da /src/types
parentremove b64 imgs (diff)
downloadxssbook-2026a8f4579b1db0f6e5e7b11ac33c13969adb6c.tar.gz
xssbook-2026a8f4579b1db0f6e5e7b11ac33c13969adb6c.tar.bz2
xssbook-2026a8f4579b1db0f6e5e7b11ac33c13969adb6c.zip
static serve refactor
Diffstat (limited to 'src/types')
-rw-r--r--src/types/extract.rs63
-rw-r--r--src/types/http.rs23
-rw-r--r--src/types/user.rs2
3 files changed, 32 insertions, 56 deletions
diff --git a/src/types/extract.rs b/src/types/extract.rs
index 4d7ac51..1258ef9 100644
--- a/src/types/extract.rs
+++ b/src/types/extract.rs
@@ -1,20 +1,24 @@
-use std::{io::{Read, Cursor}, net::{IpAddr, SocketAddr}};
+use std::{
+ io::{Cursor, Read},
+ net::{IpAddr, SocketAddr},
+};
use axum::{
async_trait,
body::HttpBody,
- extract::{FromRequest, FromRequestParts, ConnectInfo},
+ extract::{ConnectInfo, FromRequest, FromRequestParts},
http::{request::Parts, Request},
response::Response,
BoxError, RequestExt,
};
use bytes::Bytes;
-use image::{io::Reader, ImageFormat, DynamicImage};
+use image::{io::Reader, DynamicImage, ImageFormat};
use serde::de::DeserializeOwned;
use tower_cookies::Cookies;
use crate::{
- admin, console,
+ public::admin,
+ public::console,
types::{
http::{ResponseCode, Result},
session::Session,
@@ -32,41 +36,43 @@ where
type Rejection = Response;
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self> {
-
let headers = &parts.headers;
- let forwardedfor = headers.get("x-forwarded-for")
- .and_then(|h| h.to_str().ok())
- .and_then(|h|
- h.split(',')
- .rev()
- .find_map(|s| s.trim().parse::<IpAddr>().ok())
- );
+ let forwardedfor = headers
+ .get("x-forwarded-for")
+ .and_then(|h| h.to_str().ok())
+ .and_then(|h| {
+ h.split(',')
+ .rev()
+ .find_map(|s| s.trim().parse::<IpAddr>().ok())
+ });
if let Some(forwardedfor) = forwardedfor {
- return Ok(RequestIp(forwardedfor))
+ return Ok(RequestIp(forwardedfor));
}
- let realip = headers.get("x-real-ip")
- .and_then(|hv| hv.to_str().ok())
- .and_then(|s| s.parse::<IpAddr>().ok());
+ let realip = headers
+ .get("x-real-ip")
+ .and_then(|hv| hv.to_str().ok())
+ .and_then(|s| s.parse::<IpAddr>().ok());
if let Some(realip) = realip {
- return Ok(RequestIp(realip))
+ return Ok(RequestIp(realip));
}
- let realip = headers.get("x-real-ip")
- .and_then(|hv| hv.to_str().ok())
- .and_then(|s| s.parse::<IpAddr>().ok());
+ let realip = headers
+ .get("x-real-ip")
+ .and_then(|hv| hv.to_str().ok())
+ .and_then(|s| s.parse::<IpAddr>().ok());
if let Some(realip) = realip {
- return Ok(RequestIp(realip))
+ return Ok(RequestIp(realip));
}
let info = parts.extensions.get::<ConnectInfo<SocketAddr>>();
if let Some(info) = info {
- return Ok(RequestIp(info.0.ip()))
+ return Ok(RequestIp(info.0.ip()));
}
Err(ResponseCode::Forbidden.text("You have no ip"))
@@ -163,7 +169,6 @@ where
type Rejection = Response;
async fn from_request(req: Request<B>, state: &S) -> Result<Self> {
-
let bytes = match read_body(req, state).await {
Ok(body) => body,
Err(err) => return Err(err),
@@ -171,7 +176,7 @@ where
let mut reader = Reader::new(Cursor::new(bytes));
reader.set_format(ImageFormat::Png);
-
+
let Ok(img) = reader.decode() else {
return Err(ResponseCode::BadRequest.text("Failed to decode png image"))
};
@@ -238,7 +243,6 @@ where
B::Error: Into<BoxError>,
S: Send + Sync,
{
-
let Ok(RequestIp(ip)) = req.extract_parts::<RequestIp>().await else {
tracing::error!("Failed to read client ip");
return Err(ResponseCode::InternalServerError.text("Failed to read client ip"));
@@ -255,14 +259,7 @@ where
return Err(ResponseCode::BadRequest.text("Request can be at most 512kb"));
};
- console::log(
- ip,
- method,
- uri,
- Some(path.to_string()),
- None,
- )
- .await;
+ console::log(ip, method, uri, Some(path.to_string()), None).await;
Ok(bytes.bytes().flatten().collect())
}
diff --git a/src/types/http.rs b/src/types/http.rs
index 8524b15..06674ca 100644
--- a/src/types/http.rs
+++ b/src/types/http.rs
@@ -1,11 +1,8 @@
use axum::{
- body::Body,
headers::HeaderName,
- http::{HeaderValue, Request, StatusCode},
+ http::{HeaderValue, StatusCode},
response::{IntoResponse, Response},
};
-use tower::ServiceExt;
-use tower_http::services::ServeFile;
use tracing::instrument;
#[derive(Debug)]
@@ -58,24 +55,6 @@ impl ResponseCode {
);
res
}
-
- #[instrument()]
- pub async fn file(self, path: &str) -> Response {
- if !path.chars().any(|c| c == '.') {
- return Self::BadRequest.text("Folders cannot be served");
- }
- let path = format!("public{path}");
- let svc = ServeFile::new(path);
- let Ok(mut res) = svc.oneshot(Request::new(Body::empty())).await else {
- tracing::error!("Error while fetching file");
- return Self::InternalServerError.text("Error while fetching file");
- };
- if res.status() != StatusCode::OK {
- return Self::NotFound.text("File not found");
- }
- *res.status_mut() = self.code();
- res.into_response()
- }
}
pub type Result<T> = std::result::Result<T, Response>;
diff --git a/src/types/user.rs b/src/types/user.rs
index 2bffa52..835b675 100644
--- a/src/types/user.rs
+++ b/src/types/user.rs
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use tracing::instrument;
-use crate::api::auth::RegistrationRequet;
+use crate::api::RegistrationRequet;
use crate::database;
use crate::types::http::{ResponseCode, Result};