feat(query): basic query parsing done
This commit is contained in:
67
src/http.rs
67
src/http.rs
@@ -11,35 +11,34 @@ pub struct ProcessedResponse {
|
|||||||
type QueryParams = HashMap<String, String>;
|
type QueryParams = HashMap<String, String>;
|
||||||
type Headers = HashMap<String, String>;
|
type Headers = HashMap<String, String>;
|
||||||
|
|
||||||
struct HttpRequestQuery {
|
#[derive(Debug)]
|
||||||
path: String,
|
struct HttpRequestQuery<'a> {
|
||||||
|
path: &'a str,
|
||||||
params: QueryParams,
|
params: QueryParams,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct HttpRequestLine {
|
#[derive(Debug)]
|
||||||
method: String,
|
struct HttpRequestLine<'a> {
|
||||||
version: f32,
|
method: &'a str,
|
||||||
query: HttpRequestQuery,
|
version: &'a str,
|
||||||
|
query: HttpRequestQuery<'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct HttpRequest {
|
#[derive(Debug)]
|
||||||
request: HttpRequestLine,
|
struct HttpRequest<'a> {
|
||||||
|
request: HttpRequestLine<'a>,
|
||||||
headers: Headers,
|
headers: Headers,
|
||||||
body: String,
|
body: Option<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]; // TODO: manage this size
|
||||||
|
|
||||||
let _amount = stream.read(&mut buffer)?;
|
let _amount = stream.read(&mut buffer)?;
|
||||||
|
|
||||||
let petition = String::from_utf8_lossy(&buffer[..]);
|
let petition = String::from_utf8_lossy(&buffer[..]);
|
||||||
|
|
||||||
let petition = petition.split("\n");
|
let petition = parse_request(&petition);
|
||||||
|
|
||||||
for line in petition {
|
|
||||||
println!("PART: {}", line)
|
|
||||||
}
|
|
||||||
|
|
||||||
let response_status = "200 OK";
|
let response_status = "200 OK";
|
||||||
|
|
||||||
@@ -58,13 +57,15 @@ pub fn process_petition(stream: &mut TcpStream) -> std::io::Result<ProcessedResp
|
|||||||
Ok(response)
|
Ok(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_request(request_raw: String) -> Result<HttpRequest, i16> {
|
fn parse_request(request_raw: &str) -> Result<HttpRequest, i16> {
|
||||||
// TODO: study if better to use match
|
// TODO: study if better to use match
|
||||||
if let Some((heading, rest)) = request_raw.split_once("\n") {
|
if let Some((heading, rest)) = request_raw.split_once("\n") {
|
||||||
// Process heading
|
// Process heading
|
||||||
// split heading with split_whitespace
|
// split heading with split_whitespace
|
||||||
// for (i, line) in request_raw.enumerate() {
|
// for (i, line) in request_raw.enumerate() {
|
||||||
// }
|
// }
|
||||||
|
let request = parse_request_block(heading);
|
||||||
|
println!("This is a raw request: {:?}", request);
|
||||||
if let Some((headers, body)) = rest.split_once("\n\n") {
|
if let Some((headers, body)) = rest.split_once("\n\n") {
|
||||||
// Process headers and body
|
// Process headers and body
|
||||||
// split headers over ":"
|
// split headers over ":"
|
||||||
@@ -75,9 +76,12 @@ fn parse_request(request_raw: String) -> Result<HttpRequest, i16> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn parse_request_block(request_block: &str) -> Result<HttpRequestLine, i16> {
|
fn parse_request_block(request_block: &str) -> Result<HttpRequestLine, i16> {
|
||||||
let request_components: Vec<&str> = request_block.split(" ").collect();
|
let [method, query, version]: [&str; 3] = request_block
|
||||||
|
.split_whitespace()
|
||||||
|
.collect::<Vec<&str>>()
|
||||||
|
.try_into()
|
||||||
|
.unwrap(); // FIXME: check this out
|
||||||
|
|
||||||
if let Ok(array) = request_components.try_into() {
|
|
||||||
if let Ok(query) = parse_query(query) {
|
if let Ok(query) = parse_query(query) {
|
||||||
return Ok(HttpRequestLine {
|
return Ok(HttpRequestLine {
|
||||||
method,
|
method,
|
||||||
@@ -85,18 +89,33 @@ fn parse_request_block(request_block: &str) -> Result<HttpRequestLine, i16> {
|
|||||||
query,
|
query,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Err(400)
|
Err(400)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_query(query: &str) -> Result<HttpRequestQuery, i16> {
|
fn parse_query(query: &str) -> Result<HttpRequestQuery, i16> {
|
||||||
if let Some((path, params)) = query.split_once("?") {
|
if let Some((path, params)) = query.split_once("?") {
|
||||||
return Ok(HttpRequestQuery {
|
if let Ok(params) = parse_query_params(params) {
|
||||||
path: path.to_string(),
|
return Ok(HttpRequestQuery { path, params });
|
||||||
params: parse_query_params(params),
|
}
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
match query.split_once("?") {
|
||||||
|
Some((path, params)) => {
|
||||||
|
if let Ok(params) = parse_query_params(params) {
|
||||||
|
return Ok(HttpRequestQuery { path, params });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
return Ok(HttpRequestQuery {
|
||||||
|
path: query,
|
||||||
|
params: HashMap::new(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Err(400)
|
Err(400)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_query_params(query: &str) -> QueryParams {}
|
fn parse_query_params(query: &str) -> Result<QueryParams, i16> {
|
||||||
|
Ok(HashMap::new())
|
||||||
|
// Err(400)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user