Make incoming HTTP method an enum
This commit is contained in:
parent
2416c47f67
commit
0d9f606960
2 changed files with 34 additions and 18 deletions
|
@ -64,7 +64,7 @@ pub fn save_request_to_history(request: &Request) -> Result<(), DatabaseError> {
|
|||
let connection = &mut crud::establish_connection()?;
|
||||
|
||||
let new_history = InsertHistory {
|
||||
method: &request.method,
|
||||
method: &format!("{}", request.method),
|
||||
section: &request.section,
|
||||
query: &request.requested_data,
|
||||
};
|
||||
|
|
50
src/ear.rs
50
src/ear.rs
|
@ -10,15 +10,9 @@ use urlencoding::decode;
|
|||
use crate::database;
|
||||
use crate::mouth::Mouth;
|
||||
|
||||
/// Corresponds to a listener object
|
||||
pub struct Ear {
|
||||
port: u32,
|
||||
ip_addr: String,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Request {
|
||||
pub method: String,
|
||||
pub method: HttpMethod,
|
||||
pub section: String,
|
||||
pub requested_data: String,
|
||||
}
|
||||
|
@ -32,6 +26,27 @@ impl Display for ReqParseError {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum HttpMethod {
|
||||
Get,
|
||||
Post,
|
||||
}
|
||||
|
||||
impl Display for HttpMethod {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
HttpMethod::Get => write!(f, "GET"),
|
||||
HttpMethod::Post => write!(f, "POST"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Corresponds to a listener object
|
||||
pub struct Ear {
|
||||
port: u32,
|
||||
ip_addr: String,
|
||||
}
|
||||
|
||||
impl Ear {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
|
@ -63,9 +78,7 @@ impl Ear {
|
|||
}
|
||||
}
|
||||
|
||||
/// Handles a given incoming stream.
|
||||
/// A request is supposed to be a REST API call from a client.
|
||||
/// Forwards the received request to `parse_uri`.
|
||||
/// Reads a stream in a buffer and extracts the first line.
|
||||
fn extract_first_line_of_request(&self, mut stream: &TcpStream) -> String {
|
||||
let buf_reader = BufReader::new(&mut stream);
|
||||
let http_request = buf_reader.lines().next().unwrap().unwrap();
|
||||
|
@ -77,8 +90,13 @@ impl Ear {
|
|||
/// Receives and parses a request from `receive_request` to dermine the function to call.
|
||||
/// We are expecting the request to be like 'GET /api/titre/jean'
|
||||
fn parse_uri(full_request: &str) -> Result<Request, ReqParseError> {
|
||||
// TODO: turn method to an enum, and handle cases explicitly instead of unwrapping
|
||||
let method: String = full_request.split_whitespace().nth(0).unwrap().to_string();
|
||||
let method = match full_request.split_whitespace().nth(0) {
|
||||
Some("GET") => HttpMethod::Get,
|
||||
Some("POST") => HttpMethod::Post,
|
||||
Some(method) => return Err(ReqParseError(format!("Unsupport HTTP method: {}", method))),
|
||||
None => return Err(ReqParseError("Malformed URI".to_string())),
|
||||
};
|
||||
|
||||
let uri = full_request.split_whitespace().nth(1).unwrap();
|
||||
let uri = decode(uri).expect("UTF-8").to_string();
|
||||
let uri = uri
|
||||
|
@ -109,11 +127,9 @@ fn parse_uri(full_request: &str) -> Result<Request, ReqParseError> {
|
|||
fn handle_request(request: Request, mut sender: Mouth) {
|
||||
database::save_request_to_history(&request);
|
||||
|
||||
// TODO: turn method to an enum to handle cases explicitly
|
||||
match request.method.as_str() {
|
||||
"GET" => handle_get_request(request, sender),
|
||||
"POST" => handle_post_request(request, sender),
|
||||
_ => sender.send_bad_request(""),
|
||||
match request.method {
|
||||
HttpMethod::Get => handle_get_request(request, sender),
|
||||
HttpMethod::Post => handle_post_request(request, sender),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue