From d4c3b70d6290c52f903e674e9957b979abd4ce07 Mon Sep 17 00:00:00 2001 From: lamp Date: Sun, 5 Mar 2023 21:37:45 +0000 Subject: init --- src/http/flatten.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/http/flatten.rs (limited to 'src/http/flatten.rs') diff --git a/src/http/flatten.rs b/src/http/flatten.rs new file mode 100644 index 0000000..51ead0c --- /dev/null +++ b/src/http/flatten.rs @@ -0,0 +1,37 @@ +use std::collections::HashMap; + +use super::rule::{HTTPMessage, Method}; + +#[derive(Debug)] +pub enum Version { + Http0_9, + Http1_0, + Http1_1, + // HTTP/2 won't parse, anyway. +} + +#[derive(Debug)] +pub struct HTTPRequest { + pub method: Method, + pub version: Version, + pub requested_path: Vec, + pub headers: HashMap>, +} + +pub fn flatten(message: HTTPMessage) -> Option { + let method = message.request_line.method; + let version = match (message.request_line.http_version.major, message.request_line.http_version.minor) { + (0, 9) => Version::Http0_9, + (1, 0) => Version::Http1_0, + (1, 1) => Version::Http1_1, + _ => return None, + }; + let requested_path = message.request_line.request_target.absolute_path.segments.into_iter().map(|segment| segment.lexeme).collect(); + let headers = message.header_fields.into_iter().map(|field| (field.name.lexeme, field.value.content)).collect(); + Some(HTTPRequest{ + method, + version, + requested_path, + headers, + }) +} -- cgit v1.2.3