Incoming HTTP request updates

This commit is contained in:
Will Galebach 2024-02-15 15:59:40 +00:00
parent 7a4e774dff
commit db50846909
5 changed files with 22 additions and 4 deletions

1
Cargo.lock generated
View File

@ -3139,6 +3139,7 @@ dependencies = [
"open", "open",
"public-ip", "public-ip",
"rand 0.8.5", "rand 0.8.5",
"regex",
"reqwest", "reqwest",
"ring 0.16.20", "ring 0.16.20",
"rmp-serde", "rmp-serde",

View File

@ -82,3 +82,4 @@ warp = "0.3.5"
wasmtime = "15.0.1" wasmtime = "15.0.1"
wasmtime-wasi = "15.0.1" wasmtime-wasi = "15.0.1"
zip = "0.6" zip = "0.6"
regex = "1.10"

View File

@ -41,6 +41,7 @@ type WsPathBindings = Arc<RwLock<Router<BoundWsPath>>>;
struct BoundPath { struct BoundPath {
pub app: ProcessId, pub app: ProcessId,
pub path: String,
pub secure_subdomain: Option<String>, pub secure_subdomain: Option<String>,
pub authenticated: bool, pub authenticated: bool,
pub local_only: bool, pub local_only: bool,
@ -185,17 +186,19 @@ pub async fn http_server(
let jwt_secret_bytes = Arc::new(jwt_secret_bytes); let jwt_secret_bytes = Arc::new(jwt_secret_bytes);
let http_response_senders: HttpResponseSenders = Arc::new(DashMap::new()); let http_response_senders: HttpResponseSenders = Arc::new(DashMap::new());
let ws_senders: WebSocketSenders = Arc::new(DashMap::new()); let ws_senders: WebSocketSenders = Arc::new(DashMap::new());
let path = format!("/rpc:distro:sys/message");
// add RPC path // add RPC path
let mut bindings_map: Router<BoundPath> = Router::new(); let mut bindings_map: Router<BoundPath> = Router::new();
let rpc_bound_path = BoundPath { let rpc_bound_path = BoundPath {
app: ProcessId::new(Some("rpc"), "distro", "sys"), app: ProcessId::new(Some("rpc"), "distro", "sys"),
path: path.clone(),
secure_subdomain: None, // TODO maybe RPC should have subdomain? secure_subdomain: None, // TODO maybe RPC should have subdomain?
authenticated: false, authenticated: false,
local_only: true, local_only: true,
static_content: None, static_content: None,
}; };
bindings_map.add("/rpc:distro:sys/message", rpc_bound_path); bindings_map.add(&path, rpc_bound_path);
let path_bindings: PathBindings = Arc::new(RwLock::new(bindings_map)); let path_bindings: PathBindings = Arc::new(RwLock::new(bindings_map));
// ws path bindings // ws path bindings
@ -584,6 +587,7 @@ async fn http_handler(
} }
} else { } else {
// otherwise, make a message to the correct app // otherwise, make a message to the correct app
let url_params: HashMap<String, String> = route.params().into_iter().map(|(key, value)| (key.to_string(), value.to_string())).collect();
( (
KernelMessage { KernelMessage {
id, id,
@ -607,7 +611,9 @@ async fn http_handler(
host.unwrap_or(Authority::from_static("localhost")), host.unwrap_or(Authority::from_static("localhost")),
original_path original_path
), ),
bound_path: bound_path.path.clone(),
headers: serialized_headers, headers: serialized_headers,
url_params,
query_params, query_params,
})) }))
.unwrap(), .unwrap(),
@ -1138,6 +1144,7 @@ async fn handle_app_message(
&normalize_path(&path), &normalize_path(&path),
BoundPath { BoundPath {
app: km.source.process.clone(), app: km.source.process.clone(),
path: path.clone(),
secure_subdomain: None, secure_subdomain: None,
authenticated, authenticated,
local_only, local_only,
@ -1160,6 +1167,7 @@ async fn handle_app_message(
&normalize_path(&path), &normalize_path(&path),
BoundPath { BoundPath {
app: km.source.process.clone(), app: km.source.process.clone(),
path: path.clone(),
secure_subdomain: None, secure_subdomain: None,
authenticated, authenticated,
local_only, local_only,
@ -1183,6 +1191,7 @@ async fn handle_app_message(
&normalize_path(&path), &normalize_path(&path),
BoundPath { BoundPath {
app: km.source.process.clone(), app: km.source.process.clone(),
path: path.clone(),
secure_subdomain: Some(subdomain), secure_subdomain: Some(subdomain),
authenticated: true, authenticated: true,
local_only: false, local_only: false,
@ -1205,6 +1214,7 @@ async fn handle_app_message(
&normalize_path(&path), &normalize_path(&path),
BoundPath { BoundPath {
app: km.source.process.clone(), app: km.source.process.clone(),
path: path.clone(),
secure_subdomain: Some(subdomain), secure_subdomain: Some(subdomain),
authenticated: true, authenticated: true,
local_only: false, local_only: false,

View File

@ -69,6 +69,10 @@ pub fn auth_cookie_valid(our_node: &str, cookie: &str, jwt_secret: &[u8]) -> boo
} }
pub fn normalize_path(path: &str) -> String { pub fn normalize_path(path: &str) -> String {
if path.starts_with("regex") {
return path.strip_prefix("regex").unwrap_or("").to_string();
}
match path.strip_suffix('/') { match path.strip_suffix('/') {
Some(new) => new.to_string(), Some(new) => new.to_string(),
None => path.to_string(), None => path.to_string(),

View File

@ -29,10 +29,12 @@ pub enum HttpServerRequest {
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct IncomingHttpRequest { pub struct IncomingHttpRequest {
pub source_socket_addr: Option<String>, // will parse to SocketAddr pub source_socket_addr: Option<String>, // will parse to SocketAddr
pub method: String, // will parse to http::Method pub method: String, // will parse to http::Method
pub url: String, // will parse to url::Url pub url: String, // will parse to url::Url
pub bound_path: String, // the path that was originally bound
pub headers: HashMap<String, String>, pub headers: HashMap<String, String>,
pub url_params: HashMap<String, String>, // comes from route-recognizer
pub query_params: HashMap<String, String>, pub query_params: HashMap<String, String>,
// BODY is stored in the lazy_load_blob, as bytes // BODY is stored in the lazy_load_blob, as bytes
} }