Bladeren bron

Change package name

master
Noelle 4 jaren geleden
bovenliggende
commit
9aa92c709f
3 gewijzigde bestanden met toevoegingen van 36 en 13 verwijderingen
  1. 1
    1
      Cargo.toml
  2. 27
    2
      src/lib.rs
  3. 8
    10
      src/main.rs

+ 1
- 1
Cargo.toml Bestand weergeven

@@ -1,5 +1,5 @@
[package]
name = "hello"
name = "webserv"
version = "0.2.0"
authors = ["noelle"]
edition = "2018"

+ 27
- 2
src/lib.rs Bestand weergeven

@@ -138,8 +138,8 @@ impl Response {
&self.content
}
pub fn add_file_content(&mut self, filename: &str) {
self.set_content(fs::read_to_string(p).unwrap().as_str());
pub fn add_file_contents(&mut self, filename: &str) {
self.set_content(fs::read_to_string(filename).unwrap().as_str());
}
}

@@ -173,3 +173,28 @@ impl Route {
self.action
}
}

// #[derive(Copy, Clone)]
// pub struct RoutesList {
// routes: Vec<Route>,
// }

// impl RoutesList {
// pub fn new() -> RoutesList {
// RoutesList {
// routes: Vec::new(),
// }
// }

// pub fn add_route(&mut self, r: &Route) {
// self.routes.push(r)
// }

// pub fn get_routes(&self) -> Vec<Route> {
// &self.routes
// }

// pub fn get_copy(&self) -> RoutesList {
// self.clone()
// }
// }

+ 8
- 10
src/main.rs Bestand weergeven

@@ -1,12 +1,12 @@
use std::net::{TcpStream, TcpListener};
use std::io::prelude::*;
use std::fs;
// use std::fs;
// use std::thread;
// use std::time::Duration;
// use std::str;
use std::path::Path;

use hello::{ThreadPool, Response, Route};
use webserv::{ThreadPool, Response, Route};

extern crate regex;
use regex::Regex;
@@ -47,7 +47,7 @@ fn handle_connection(mut stream: TcpStream) {
None => {
println!("This wasn't even a well-formed header");
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n");
r.add_file_content("404.html");
r.add_file_contents("404.html");
}
}
@@ -55,7 +55,7 @@ fn handle_connection(mut stream: TcpStream) {
} else {
println!("It didn't start with GET!");
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n");
r.add_file_content("404.html");
r.add_file_contents("404.html");
}
// println!("Sending {}: {}", r.header, r.path);
@@ -74,15 +74,13 @@ fn route(c: &str, mut r: &mut Response) {
let mut routes = vec![];
routes.push(Route::new("api/hi", hi));
routes.push(Route::new("api/bye", bye));


// println!("Asked to fetch {}", c);
//assert!(Path::new(c).exists());
if c == "" {
println!("Asked to send root!");
r.set_header("HTTP/1.1 200 OK\r\n\r\n");
r.add_file_content("index.html");
r.add_file_contents("index.html");
} else if c.starts_with("api/") {
let mut iter_routes = routes.into_iter();
let valid_route = iter_routes.find(|x| x.get_path() == c);
@@ -91,7 +89,7 @@ fn route(c: &str, mut r: &mut Response) {
(rt.get_action())(&c, &mut r);
},
None => {
none_api(&c, &mut r);
api_none(&c, &mut r);
}
}
} else if Path::new(c).exists() {
@@ -104,7 +102,7 @@ fn route(c: &str, mut r: &mut Response) {

}

fn route_basic(c: &&str, mut r: &mut Response) {
fn route_basic(c: &&str, r: &mut Response) {
if Path::new(c).is_dir() {
let mut cs = c.to_string();
if cs.chars().last().unwrap().to_string() != "/" {
@@ -127,7 +125,7 @@ fn route_basic(c: &&str, mut r: &mut Response) {
}
}

fn none_api(_c: &&str, r: &mut Response) {
fn api_none(_c: &&str, r: &mut Response) {
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n");
r.set_content("There is currently no API endpoint at that address.");
}

Laden…
Annuleren
Opslaan