use std::io;
use std::net::TcpListener;
use std::net::TcpStream;
use std::thread;
use rusty_http::Http;
use rusty_http::Request;
use rusty_http::Response;
use rusty_http::StatusCode;
fn add(request: Request, response: Response) -> io::Result<()> {
let Some(a) = request.params.get("a") else {
response.status_code(StatusCode::BadRequest)
.set_header("Content-Type", "text/html").flush()?
.add_to_body("
400 Bad Request
")
.add_to_body("Parameter 'a' not found
").flush()?;
return Ok(());
};
let Some(b) = request.params.get("b") else {
response.status_code(StatusCode::BadRequest)
.set_header("Content-Type", "text/html").flush()?
.add_to_body("400 Bad Request
")
.add_to_body("Parameter 'b' not found
").flush()?;
return Ok(());
};
let Ok(a) = a.parse::() else {
response.status_code(StatusCode::BadRequest)
.set_header("Content-Type", "text/html").flush()?
.add_to_body("400 Bad Request
")
.add_to_body("Parameter 'a' can only be a number from 0 to 255
").flush()?;
return Ok(());
};
let Ok(b) = b.parse::() else {
response.status_code(StatusCode::BadRequest)
.set_header("Content-Type", "text/html").flush()?
.add_to_body("400 Bad Request
")
.add_to_body("Parameter 'b' can only be a number from 0 to 255
").flush()?;
return Ok(());
};
let content = format!("{} + {} = {}
", a, b, a as u16 + b as u16);
response.status_code(StatusCode::Ok)
.set_header("Content-Type", "text/html").flush()?
.add_to_body(content).flush()?;
Ok(())
}
fn sub(request: Request, response: Response) -> io::Result<()> {
let Some(a) = request.params.get("a") else {
response.status_code(StatusCode::BadRequest)
.set_header("Content-Type", "text/html").flush()?
.add_to_body("400 Bad Request
")
.add_to_body("Parameter 'a' not found
").flush()?;
return Ok(());
};
let Some(b) = request.params.get("b") else {
response.status_code(StatusCode::BadRequest)
.set_header("Content-Type", "text/html").flush()?
.add_to_body("400 Bad Request
")
.add_to_body("Parameter 'b' not found
").flush()?;
return Ok(());
};
let Ok(a) = a.parse::() else {
response.status_code(StatusCode::BadRequest)
.set_header("Content-Type", "text/html").flush()?
.add_to_body("400 Bad Request
")
.add_to_body("Parameter 'a' can only be a number from 0 to 255
").flush()?;
return Ok(());
};
let Ok(b) = b.parse::() else {
response.status_code(StatusCode::BadRequest)
.set_header("Content-Type", "text/html").flush()?
.add_to_body("400 Bad Request
")
.add_to_body("Parameter 'b' can only be a number from 0 to 255
").flush()?;
return Ok(());
};
let content = format!("{} - {} = {}
", a, b, a as i16 - b as i16);
response.status_code(StatusCode::Ok)
.set_header("Content-Type", "text/html").flush()?
.add_to_body(content).flush()?;
Ok(())
}
fn not_found(request: Request, response: Response) -> io::Result<()> {
let content = format!("Page '{}' could not been found
", request.uri);
response.status_code(StatusCode::NotFound)
.set_header("Content-Type", "text/html").flush()?
.add_to_body("404 Not Found
")
.add_to_body(content).flush()?;
Ok(())
}
fn init_stream(stream: TcpStream) -> io::Result<()> {
let (request, response) = Http::from(stream)?;
match request.uri.as_str() {
"/add" => add(request, response)?,
"/sub" => sub(request, response)?,
_ => not_found(request, response)?
}
Ok(())
}
fn main() -> io::Result<()> {
let listener = TcpListener::bind("127.0.0.1:8080")?;
for stream in listener.incoming().filter_map(Result::ok) {
thread::spawn(|| init_stream(stream));
}
Ok(())
}