Browse Source

Change package name

master
Noelle 4 years ago
parent
commit
9aa92c709f
3 changed files with 36 additions and 13 deletions
  1. 1
    1
      Cargo.toml
  2. 27
    2
      src/lib.rs
  3. 8
    10
      src/main.rs

+ 1
- 1
Cargo.toml View File

[package] [package]
name = "hello"
name = "webserv"
version = "0.2.0" version = "0.2.0"
authors = ["noelle"] authors = ["noelle"]
edition = "2018" edition = "2018"

+ 27
- 2
src/lib.rs View File

&self.content &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());
} }
} }


self.action 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 View File

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


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


extern crate regex; extern crate regex;
use regex::Regex; use regex::Regex;
None => { None => {
println!("This wasn't even a well-formed header"); println!("This wasn't even a well-formed header");
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n"); 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");
} }
} }
} else { } else {
println!("It didn't start with GET!"); println!("It didn't start with GET!");
r.set_header("HTTP/1.1 404 NOT FOUND\r\n\r\n"); 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); // println!("Sending {}: {}", r.header, r.path);
let mut routes = vec![]; let mut routes = vec![];
routes.push(Route::new("api/hi", hi)); routes.push(Route::new("api/hi", hi));
routes.push(Route::new("api/bye", bye)); routes.push(Route::new("api/bye", bye));



// println!("Asked to fetch {}", c); // println!("Asked to fetch {}", c);
//assert!(Path::new(c).exists()); //assert!(Path::new(c).exists());
if c == "" { if c == "" {
println!("Asked to send root!"); println!("Asked to send root!");
r.set_header("HTTP/1.1 200 OK\r\n\r\n"); 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/") { } else if c.starts_with("api/") {
let mut iter_routes = routes.into_iter(); let mut iter_routes = routes.into_iter();
let valid_route = iter_routes.find(|x| x.get_path() == c); let valid_route = iter_routes.find(|x| x.get_path() == c);
(rt.get_action())(&c, &mut r); (rt.get_action())(&c, &mut r);
}, },
None => { None => {
none_api(&c, &mut r);
api_none(&c, &mut r);
} }
} }
} else if Path::new(c).exists() { } else if Path::new(c).exists() {


} }


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


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_header("HTTP/1.1 404 NOT FOUND\r\n\r\n");
r.set_content("There is currently no API endpoint at that address."); r.set_content("There is currently no API endpoint at that address.");
} }

Loading…
Cancel
Save