feat(request_parser): base code

This commit is contained in:
2024-11-23 00:29:20 +01:00
parent 7ea4e667ed
commit a0a11637f2

View File

@@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::fs; use std::fs;
use std::io::prelude::*; use std::io::prelude::*;
use std::net::TcpStream; use std::net::TcpStream;
@@ -7,6 +8,19 @@ pub struct ProcessedResponse {
status: u8, status: u8,
} }
struct HttpRequestLine {
method: String,
version: f32,
path: String,
params: HashMap<String, String>,
}
struct HttpRequest {
request: HttpRequestLine,
headers: HashMap<String, String>,
body: String,
}
pub fn process_petition(stream: &mut TcpStream) -> std::io::Result<ProcessedResponse> { pub fn process_petition(stream: &mut TcpStream) -> std::io::Result<ProcessedResponse> {
let mut buffer = [0; 1024]; let mut buffer = [0; 1024];
@@ -36,3 +50,23 @@ pub fn process_petition(stream: &mut TcpStream) -> std::io::Result<ProcessedResp
Ok(response) Ok(response)
} }
fn parse_request(request_raw: String) -> Result<HttpRequest, i16> {
// TODO: study if better to use match
if let Some((heading, rest)) = request_raw.split_once("\n") {
// Process heading
// split heading with split_whitespace
// for (i, line) in request_raw.enumerate() {
// }
if let Some((headers, body)) = rest.split_once("\n\n") {
// Process headers and body
// split headers over ":"
}
}
Err(400)
}
fn parse_request_line(request_line: String) -> HttpRequestLine {
if let Some((method, path, version)) = request_line.split_whitespace() {}
}