From 626e11a725934b64260e17e68d273727be76c57e Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 14 Feb 2021 11:54:20 +0400 Subject: Divided package into library and binary --- Cargo.lock | 9 +++- Cargo.toml | 13 ++--- blog/Cargo.toml | 8 +++ blog/src/main.rs | 14 ++++++ spider/Cargo.toml | 9 ++++ spider/src/lib.rs | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 148 ------------------------------------------------------ 7 files changed, 182 insertions(+), 157 deletions(-) create mode 100644 blog/Cargo.toml create mode 100644 blog/src/main.rs create mode 100644 spider/Cargo.toml create mode 100644 spider/src/lib.rs delete mode 100644 src/main.rs diff --git a/Cargo.lock b/Cargo.lock index cf76cf3..af691e8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,12 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. [[package]] -name = "rust-web" +name = "blog" +version = "0.1.0" +dependencies = [ + "spider", +] + +[[package]] +name = "spider" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 6e5c485..1183527 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,6 @@ -[package] -name = "rust-web" -version = "0.1.0" -authors = ["Andrew "] -edition = "2018" +[workspace] -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] +members = [ + "spider", + "blog" +] diff --git a/blog/Cargo.toml b/blog/Cargo.toml new file mode 100644 index 0000000..1b8587f --- /dev/null +++ b/blog/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "blog" +version = "0.1.0" +authors = ["Andrew "] +edition = "2018" + +[dependencies] +spider = { path = "../spider" } diff --git a/blog/src/main.rs b/blog/src/main.rs new file mode 100644 index 0000000..0dba17a --- /dev/null +++ b/blog/src/main.rs @@ -0,0 +1,14 @@ +use std::net::TcpListener; + +use spider::handle_client; + +fn main() -> std::io::Result<()> { + let listener = TcpListener::bind("localhost:3000")?; + + // accept connections and process them serially + for stream in listener.incoming() { + handle_client(stream?); + } + + Ok(()) +} diff --git a/spider/Cargo.toml b/spider/Cargo.toml new file mode 100644 index 0000000..84e5702 --- /dev/null +++ b/spider/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "spider" +version = "0.1.0" +authors = ["Andrew "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/spider/src/lib.rs b/spider/src/lib.rs new file mode 100644 index 0000000..66947cf --- /dev/null +++ b/spider/src/lib.rs @@ -0,0 +1,138 @@ +use std::net::{TcpListener, TcpStream}; +use std::str; +use std::collections::HashMap; +use std::fmt; + +enum HttpMethod { + GET, POST +} + +impl fmt::Display for HttpMethod { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + HttpMethod::GET => write!(f, "GET"), + HttpMethod::POST => write!(f, "POST") + } + } +} + +impl HttpMethod { + pub fn parse(s: String) -> Option { + if s == "GET" { + return Some(HttpMethod::GET); + } + else if s == "POST" { + return Some(HttpMethod::POST); + } + else { + return None; + } + } +} + +struct Request { + method: HttpMethod, + path: String, + http_version: String, + headers: HashMap, + // body: &'a [u8] +} + +impl Request { + pub fn new( method: HttpMethod + , path: String + , http_version: String + , headers: HashMap) -> Request { + return Request { + method, + path, + http_version, + headers + }; + } + + pub fn print(&self) { + println!("Request({}, {})", self.method, self.path); + } +} + +struct Response { + status: i32, + headers: HashMap, + body: String +} + +impl Response { + pub fn new(body: &str) -> Response { + let headers: HashMap = HashMap::new(); + return Response { + status: 200, + headers: headers, + body: String::from(body) + }; + } +} + +fn do_get(request: Request) -> Response { + return Response::new("hey"); +} + +fn do_post(request: Request) -> Response { + return Response::new("hey"); +} + +// TODO(andrew): Add some error handling. +fn parse_request(data: &str) -> Option { + let lines = data.split("\r\n").collect::>(); + + let mut headers: HashMap = HashMap::new(); + for line in &lines[1..] { + let line = line.split(": ").collect::>(); + headers.insert(String::from(line[0]), line[1..].join(" ")); + } + + let first_line = lines[0].split(" ").collect::>(); + + let method = String::from(first_line[0]); + match HttpMethod::parse(method) { + Some(method) => + return Some(Request::new( + method, + String::from(first_line[1]), + String::from(first_line[2]), + headers )), + None => return None + }; +} + +fn format_response<'a>(response: Response) -> &'a [u8] { + let buf: &[u8]; + buf = &[0; 1024]; + + + return buf; +} + +pub fn handle_client(stream: TcpStream) { + let mut buf: [u8; 1024] = [0; 1024]; + stream.peek(&mut buf).expect("Couldn't read from socket"); + + let s = match str::from_utf8(&buf) { + Ok(v) => v, + Err(_e) => panic!("Couldn't convert u8 to character") + }; + + let request = parse_request(&s); + // TODO(andrew): remove panic!. + let request = match request { + Some(r) => r, + None => panic!("Request parsed with errors") + }; + + let response = match request.method { + HttpMethod::GET => do_get(request), + HttpMethod::POST => do_post(request) + }; + let response = format_response(response); +} + diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index c8a9fec..0000000 --- a/src/main.rs +++ /dev/null @@ -1,148 +0,0 @@ -use std::net::{TcpListener, TcpStream}; -use std::str; -use std::collections::HashMap; -use std::fmt; - -enum HttpMethod { - GET, POST -} - -impl fmt::Display for HttpMethod { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - HttpMethod::GET => write!(f, "GET"), - HttpMethod::POST => write!(f, "POST") - } - } -} - -impl HttpMethod { - pub fn parse(s: String) -> Option { - if s == "GET" { - return Some(HttpMethod::GET); - } - else if s == "POST" { - return Some(HttpMethod::POST); - } - else { - return None; - } - } -} - -struct Request { - method: HttpMethod, - path: String, - http_version: String, - headers: HashMap, - // body: &'a [u8] -} - -impl Request { - pub fn new( method: HttpMethod - , path: String - , http_version: String - , headers: HashMap) -> Request { - return Request { - method, - path, - http_version, - headers - }; - } - - pub fn print(&self) { - println!("Request({}, {})", self.method, self.path); - } -} - -struct Response { - status: i32, - headers: HashMap, - body: String -} - -impl Response { - pub fn new(body: &str) -> Response { - let headers: HashMap = HashMap::new(); - return Response { - status: 200, - headers: headers, - body: String::from(body) - }; - } -} - -fn do_get(request: Request) -> Response { - return Response::new("hey"); -} - -fn do_post(request: Request) -> Response { - return Response::new("hey"); -} - -// TODO(andrew): Add some error handling. -fn parse_request(data: &str) -> Option { - let lines = data.split("\r\n").collect::>(); - - let mut headers: HashMap = HashMap::new(); - for line in &lines[1..] { - let line = line.split(": ").collect::>(); - headers.insert(String::from(line[0]), line[1..].join(" ")); - } - - let first_line = lines[0].split(" ").collect::>(); - - let method = String::from(first_line[0]); - match HttpMethod::parse(method) { - Some(method) => - return Some(Request::new( - method, - String::from(first_line[1]), - String::from(first_line[2]), - headers )), - None => return None - }; -} - -fn format_response<'a>(response: Response) -> &'a [u8] { - let buf: &[u8]; - buf = &[0; 1024]; - - - return buf; -} - -fn handle_client(stream: TcpStream) { - let mut buf: [u8; 1024] = [0; 1024]; - stream.peek(&mut buf).expect("Couldn't read from socket"); - - let s = match str::from_utf8(&buf) { - Ok(v) => v, - Err(_e) => panic!("Couldn't convert u8 to character") - }; - - let request = parse_request(&s); - // TODO(andrew): remove panic!. - let request = match request { - Some(r) => r, - None => panic!("Request parsed with errors") - }; - - let response = match request.method { - HttpMethod::GET => do_get(request), - HttpMethod::POST => do_post(request) - }; - let response = format_response(response); -} - -fn main() -> std::io::Result<()> { - let listener = TcpListener::bind("localhost:3000")?; - - // accept connections and process them serially - for stream in listener.incoming() { - handle_client(stream?); - } - - Ok(()) -} -- cgit v1.2.3