reifydb_network/http/
parser.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
6use super::HttpRequest;
7
8pub fn parse_request(data: &[u8]) -> Result<HttpRequest, String> {
9	let mut headers_buf = [httparse::EMPTY_HEADER; 32];
10	let mut req = httparse::Request::new(&mut headers_buf);
11
12	let status = req.parse(data).map_err(|e| format!("Parse error: {:?}", e))?;
13
14	if status.is_partial() {
15		return Err("Incomplete request".to_string());
16	}
17
18	let method = req.method.ok_or("Missing method")?.to_string();
19	let path = req.path.ok_or("Missing path")?.to_string();
20
21	let mut headers = HashMap::new();
22	for header in req.headers {
23		let key = header.name.to_lowercase();
24		let value = String::from_utf8_lossy(header.value).to_string();
25		headers.insert(key, value);
26	}
27
28	// Extract body if present
29	let header_len = status.unwrap();
30	let body = if data.len() > header_len {
31		data[header_len..].to_vec()
32	} else {
33		Vec::new()
34	};
35
36	Ok(HttpRequest::new(method, path, headers, body))
37}