diff --git a/src/ear.rs b/src/ear.rs index 1be6571..8afe808 100644 --- a/src/ear.rs +++ b/src/ear.rs @@ -1,3 +1,6 @@ +use std::io::{prelude::*, BufReader}; +use std::net::{TcpListener, TcpStream}; + pub struct Ear { port: u32, ip_addr: String, @@ -11,7 +14,26 @@ impl Ear { } } - pub fn receive_request(&self) -> String { - todo!(); + pub fn listen(&self) { + let listener = TcpListener::bind(format!("{}:{}", self.ip_addr, self.port)).unwrap(); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + + self.receive_request(stream); + } + } + + fn receive_request(&self, mut stream: TcpStream) -> Vec { + let buf_reader = BufReader::new(&mut stream); + let http_request: Vec = buf_reader + .lines() + .map(|result| result.unwrap()) + .take_while(|line| !line.is_empty()) + .collect(); + + println!("Received request: {:#?}", http_request); + + http_request } } diff --git a/src/main.rs b/src/main.rs index f160105..0a793b9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,10 @@ fn main() { let listener = Ear::new(); let sender = Mouth::new(); - let url = listener.receive_request(); + listener.listen(); + + todo!(); + let url: Vec = vec![]; let req: String = parse_url(&url); // TODO: update type let data: String = get_data(&req); // TODO: update type let json_data = transform_to_json(&data); @@ -20,7 +23,7 @@ fn main() { sender.send_data(json_data); } -fn parse_url(url: &str) -> T { +fn parse_url(full_request: &Vec) -> T { todo!(); }