summaryrefslogtreecommitdiff
path: root/spider/src/lib.rs
diff options
context:
space:
mode:
authorAndrew <saintruler@gmail.com>2021-02-14 13:43:29 +0400
committerAndrew <saintruler@gmail.com>2021-02-14 13:43:29 +0400
commit679f1c88ea9c56110f8f66f6f253fce30704e45e (patch)
treec8eba9915a827c9dd667a1e8c553dda4f98365cc /spider/src/lib.rs
parent626e11a725934b64260e17e68d273727be76c57e (diff)
Changed structure of library
Diffstat (limited to 'spider/src/lib.rs')
-rw-r--r--spider/src/lib.rs142
1 files changed, 4 insertions, 138 deletions
diff --git a/spider/src/lib.rs b/spider/src/lib.rs
index 66947cf..baa0220 100644
--- a/spider/src/lib.rs
+++ b/spider/src/lib.rs
@@ -1,138 +1,4 @@
-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<HttpMethod> {
- 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<String, String>,
- // body: &'a [u8]
-}
-
-impl Request {
- pub fn new( method: HttpMethod
- , path: String
- , http_version: String
- , headers: HashMap<String, String>) -> Request {
- return Request {
- method,
- path,
- http_version,
- headers
- };
- }
-
- pub fn print(&self) {
- println!("Request({}, {})", self.method, self.path);
- }
-}
-
-struct Response {
- status: i32,
- headers: HashMap<String, String>,
- body: String
-}
-
-impl Response {
- pub fn new(body: &str) -> Response {
- let headers: HashMap<String, String> = 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<Request> {
- let lines = data.split("\r\n").collect::<Vec<&str>>();
-
- let mut headers: HashMap<String, String> = HashMap::new();
- for line in &lines[1..] {
- let line = line.split(": ").collect::<Vec<&str>>();
- headers.insert(String::from(line[0]), line[1..].join(" "));
- }
-
- let first_line = lines[0].split(" ").collect::<Vec<&str>>();
-
- 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);
-}
-
+pub mod request;
+pub mod response;
+pub mod http_method;
+pub mod http_server;