reifydb_network/http/
request.rs

1// Copyright (c) reifydb.com 2025
2// This file is licensed under the AGPL-3.0-or-later, see license.md file
3
4use std::collections::HashMap;
5
6#[derive(Debug, Clone)]
7pub struct HttpRequest {
8	pub method: String,
9	pub path: String,
10	pub headers: HashMap<String, String>,
11	pub body: Vec<u8>,
12}
13
14impl HttpRequest {
15	pub fn new(method: String, path: String, headers: HashMap<String, String>, body: Vec<u8>) -> Self {
16		Self {
17			method,
18			path,
19			headers,
20			body,
21		}
22	}
23
24	pub fn get_header(&self, name: &str) -> Option<&String> {
25		self.headers.get(&name.to_lowercase())
26	}
27
28	pub fn content_length(&self) -> Option<usize> {
29		self.get_header("content-length").and_then(|v| v.parse().ok())
30	}
31
32	pub fn is_websocket_upgrade(&self) -> bool {
33		self.get_header("upgrade").map(|v| v.to_lowercase() == "websocket").unwrap_or(false)
34			&& self.get_header("connection")
35				.map(|v| {
36					let v_lower = v.to_lowercase();
37					v_lower.contains("upgrade")
38				})
39				.unwrap_or(false)
40	}
41}