Add listener for incoming HTTP connections

This commit is contained in:
flyingscorpio@clevo 2022-12-02 22:06:28 +01:00
parent ebfbe46dca
commit 465591215c
2 changed files with 29 additions and 4 deletions

View file

@ -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<String> {
let buf_reader = BufReader::new(&mut stream);
let http_request: Vec<String> = buf_reader
.lines()
.map(|result| result.unwrap())
.take_while(|line| !line.is_empty())
.collect();
println!("Received request: {:#?}", http_request);
http_request
}
}

View file

@ -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<String> = 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<T>(url: &str) -> T {
fn parse_url<T>(full_request: &Vec<String>) -> T {
todo!();
}