Skip to content

Commit

Permalink
feat: code refactore & modularize
Browse files Browse the repository at this point in the history
  • Loading branch information
akibahmed229 committed Mar 17, 2024
1 parent 14812dd commit 8b1b416
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 47 deletions.
23 changes: 23 additions & 0 deletions src/controllers/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@
use std::net::{TcpListener, TcpStream}; // listens for incoming TCP connections
use std::io::prelude::*; // brings in the read and write traits
use std::fs;
use std::thread; // thread module
use core::time::Duration; // time module

use crate::router::router::{ HttpMethod, handle_connecton};

// Function to parse HTTP method and path from the request
pub fn parse_request(buffer: &[u8]) -> (HttpMethod, &str) {
// Implement parsing logic here to extract method and path from the buffer

// match the buffer with the request method and path and return tuple of HttpMethod and path
// b'' is a byte string literal wihch is a sequence of bytes
match buffer {
_ if buffer.starts_with(b"GET / HTTP/1.1\r\n") => (HttpMethod::GET, "index.html"),
_ if buffer.starts_with(b"GET /sleep HTTP/1.1\r\n") => {
thread::sleep(Duration::from_secs(5));
(HttpMethod::GET, "index.html")
},
_ if buffer.starts_with(b"POST / HTTP/1.1\r\n") => (HttpMethod::POST, "index.html"),
_ if buffer.starts_with(b"PUT / HTTP/1.1\r\n") => (HttpMethod::PUT, "index.html"),
_ if buffer.starts_with(b"DELETE / HTTP/1.1\r\n") => (HttpMethod::DELETE, "index.html"),
_ => (HttpMethod::INVALID, "404.html")
}
}

// Function to handle GET requests
pub fn handle_get_request(mut stream: TcpStream, path: &str) {
Expand Down
31 changes: 2 additions & 29 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
The handler function then processes the request and sends the response back to the client.
*/

use std::net::{TcpListener, TcpStream}; // listens for incoming TCP connections
use std::io::prelude::*; // brings in the read and write traits
use std::net::TcpListener; // listens for incoming TCP connections

// custom modules
use crate::lib::lib::ThreadPool; // file system module
use router::router::HttpMethod;
use router::router::parse_request;
use crate::controllers::controller::{handle_delete_request, handle_get_request, handle_invalid_request, handle_post_request, handle_put_request};
use router::router::handle_connecton;

// thread pool
mod lib {
Expand Down Expand Up @@ -43,27 +40,3 @@ fn main() {
});
}
}

fn handle_connecton(mut stream: TcpStream) {
let mut buffer = [0; 2048]; // buffer to store the data (e.g., request, response, file content, etc.)

// read the data from the stream and store it in the buffer
stream.read(&mut buffer).unwrap();

// parse the request to get the method (GET,POST,PUT,DELETE) and path
let ( method , path ) = parse_request(&buffer);

// match the request method and call the appropriate handler function
match method {
HttpMethod::GET => handle_get_request(stream, path),
HttpMethod::POST => handle_post_request(stream, path, &buffer),
HttpMethod::PUT => handle_put_request(stream, path, &buffer),
HttpMethod::DELETE => handle_delete_request(stream, path),
_ => handle_invalid_request(stream, path)
}

println!(
"Request: {}",
String::from_utf8_lossy(&buffer[..]) // convert the buffer to a string and print the request
); // print the request
}
43 changes: 25 additions & 18 deletions src/router/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
Router module to parse the HTTP request and return the HTTP method and path
*/

use std::thread; // thread module
use core::time::Duration; // time module
use std::net::TcpStream; // listens for incoming TCP connections
use std::io::prelude::*; // brings in the read and write traits

use crate::controllers::controller::{ parse_request, handle_delete_request, handle_get_request, handle_invalid_request, handle_post_request, handle_put_request};

pub enum HttpMethod {
GET,
Expand All @@ -13,21 +15,26 @@ pub enum HttpMethod {
INVALID,
}

// Function to parse HTTP method and path from the request
pub fn parse_request(buffer: &[u8]) -> (HttpMethod, &str) {
// Implement parsing logic here to extract method and path from the buffer

// match the buffer with the request method and path and return tuple of HttpMethod and path
// b'' is a byte string literal wihch is a sequence of bytes
match buffer {
_ if buffer.starts_with(b"GET / HTTP/1.1\r\n") => (HttpMethod::GET, "index.html"),
_ if buffer.starts_with(b"GET /sleep HTTP/1.1\r\n") => {
thread::sleep(Duration::from_secs(5));
(HttpMethod::GET, "index.html")
},
_ if buffer.starts_with(b"POST / HTTP/1.1\r\n") => (HttpMethod::POST, "index.html"),
_ if buffer.starts_with(b"PUT / HTTP/1.1\r\n") => (HttpMethod::PUT, "index.html"),
_ if buffer.starts_with(b"DELETE / HTTP/1.1\r\n") => (HttpMethod::DELETE, "index.html"),
_ => (HttpMethod::INVALID, "404.html")
pub fn handle_connecton(mut stream: TcpStream) {
let mut buffer = [0; 2048]; // buffer to store the data (e.g., request, response, file content, etc.)

// read the data from the stream and store it in the buffer
stream.read(&mut buffer).unwrap();

// parse the request to get the method (GET,POST,PUT,DELETE) and path
let ( method , path ) = parse_request(&buffer);

// match the request method and call the appropriate handler function
match method {
HttpMethod::GET => handle_get_request(stream, path),
HttpMethod::POST => handle_post_request(stream, path, &buffer),
HttpMethod::PUT => handle_put_request(stream, path, &buffer),
HttpMethod::DELETE => handle_delete_request(stream, path),
_ => handle_invalid_request(stream, path)
}

println!(
"Request: {}",
String::from_utf8_lossy(&buffer[..]) // convert the buffer to a string and print the request
); // print the request
}

0 comments on commit 8b1b416

Please sign in to comment.