Skip to content

Commit

Permalink
Fixed remote addr (on Caddy reverse proxy) problem
Browse files Browse the repository at this point in the history
  • Loading branch information
jonirrings committed Dec 4, 2024
1 parent 87a43ff commit f34c0c4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
29 changes: 29 additions & 0 deletions src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::collections::HashMap;
use tokio::net::TcpStream;
use std::fs::File;
use std::io::Read;
use std::borrow::Borrow;
use std::hash::{Hash, Hasher};
use crate::config::{DEF_ASSETS, SERVER_CONFIG};

pub mod http_server;
Expand Down Expand Up @@ -109,3 +111,30 @@ macro_rules! make_route {
}
};
}

#[derive(Eq, Debug, Clone)]
pub struct UniCaseString(String);

impl From<String> for UniCaseString {
fn from(value: String) -> Self {
UniCaseString(value)
}
}

impl Borrow<str> for UniCaseString {
fn borrow(&self) -> &str {
self.0.as_str()
}
}

impl PartialEq for UniCaseString {
fn eq(&self, other: &Self) -> bool {
self.0.to_lowercase() == other.0.to_lowercase()
}
}

impl Hash for UniCaseString {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.to_lowercase().hash(state);
}
}
15 changes: 9 additions & 6 deletions src/http/request.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::borrow::Borrow;
use std::collections::HashMap;
use std::fmt::Debug;
use std::future::Future;
use std::hash::{Hash, Hasher};
use std::pin::Pin;
use log::trace;
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader, BufWriter};
use crate::config::GARBAGE_DATA;
use crate::http::{Method, MethodStr};
use crate::http::{Method, MethodStr, UniCaseString};
use crate::http::response::Response;

#[derive(Debug)]
Expand All @@ -13,7 +16,7 @@ pub struct Request {
pub method: Method,
pub remote_addr : String,
pub query_params: HashMap<String, String>,
pub headers: HashMap<String, String>,
pub headers: HashMap<UniCaseString, String>,
pub form_data : HashMap<String, String>
}

Expand Down Expand Up @@ -189,7 +192,7 @@ fn hex_string_to_int(hex_string: &str) -> Option<u64> {
}
}

pub async fn header_parser<R>(buf_reader: &mut BufReader<R>) -> HashMap<String,String>
pub async fn header_parser<R>(buf_reader: &mut BufReader<R>) -> HashMap<UniCaseString,String>
where
R: AsyncReadExt + Unpin
{
Expand All @@ -201,7 +204,7 @@ where
} else {
let mut header_parts = header_line.splitn(2, ':');
if let (Some(header_key),Some(header_val)) = (header_parts.next(),header_parts.next()) {
headers_out.insert(header_key.trim().to_string(),header_val.trim().to_string());
headers_out.insert(header_key.trim().to_string().into(),header_val.trim().to_string());
}
}
} else {
Expand All @@ -211,7 +214,7 @@ where
headers_out
}

fn check_has_body(headers : &HashMap<String,String>) -> (Option<BodyType>,Option<u64>) {
fn check_has_body(headers : &HashMap<UniCaseString,String>) -> (Option<BodyType>,Option<u64>) {
let content_type_form = if let Some(content_type) = headers.get("Content-Type") {
if content_type.starts_with("multipart/form-data;") {
Some(BodyType::Form)
Expand Down Expand Up @@ -279,7 +282,7 @@ fn clear_path_end_slash(input: &str) -> &str {
}
}

fn trust_addr_proxy(headers : &HashMap<String,String>,remote_addr : &str) -> String {
fn trust_addr_proxy(headers : &HashMap<UniCaseString,String>,remote_addr : &str) -> String {
if let Some(remote_ip) = headers.get("X-Real-IP") {
remote_ip.to_string()
} else {
Expand Down

0 comments on commit f34c0c4

Please sign in to comment.