Browse Source

Now the damn thing can fetch arbitrary files

master
Noelle 4 years ago
parent
commit
4552891ecb
4 changed files with 96 additions and 11 deletions
  1. 52
    1
      Cargo.lock
  2. 2
    1
      Cargo.toml
  3. 11
    0
      index.html
  4. 31
    9
      src/main.rs

+ 52
- 1
Cargo.lock View File

@@ -1,5 +1,56 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "aho-corasick"
version = "0.7.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8716408b8bc624ed7f65d223ddb9ac2d044c0547b6fa4b0d554f3a9540496ada"
dependencies = [
"memchr",
]

[[package]]
name = "hello"
version = "0.1.0"
version = "0.2.0"
dependencies = [
"regex",
]

[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"

[[package]]
name = "memchr"
version = "2.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"

[[package]]
name = "regex"
version = "1.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "322cf97724bea3ee221b78fe25ac9c46114ebb51747ad5babd51a2fc6a8235a8"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
"thread_local",
]

[[package]]
name = "regex-syntax"
version = "0.6.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1132f845907680735a84409c3bebc64d1364a5683ffbce899550cd09d5eaefc1"

[[package]]
name = "thread_local"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14"
dependencies = [
"lazy_static",
]

+ 2
- 1
Cargo.toml View File

@@ -1,9 +1,10 @@
[package]
name = "hello"
version = "0.1.0"
version = "0.2.0"
authors = ["noelle"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
regex = "1"

+ 11
- 0
index.html View File

@@ -0,0 +1,11 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello!</title>
</head>
<body>
<h1>Hello!</h1>
<p>Hi from Rust</p>
</body>
</html>

+ 31
- 9
src/main.rs View File

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

use hello::ThreadPool;

extern crate regex;
use regex::Regex;

fn main() {
let listener = TcpListener::bind("127.0.0.1:26382").unwrap();
let pool = ThreadPool::new(4);
@@ -24,14 +29,31 @@ fn handle_connection(mut stream: TcpStream) {
let mut buffer = [0; 512];
stream.read(&mut buffer).unwrap();

let get = b"GET / HTTP/1.1\r\n";
let sleep = b"GET /sleep HTTP/1.1\r\n";
let hdr = Regex::new(r"GET /([^ ]*) HTTP/1.1").unwrap();
let bf = &String::from_utf8_lossy(&buffer[..]);
// let get = b"GET / HTTP/1.1\r\n";
// let sleep = b"GET /sleep HTTP/1.1\r\n";

let (status_line, filename) = if buffer.starts_with(b"GET") {

let (status_line, filename) = if buffer.starts_with(get) {
("HTTP/1.1 200 OK\r\n\r\n", "hello.html")
} else if buffer.starts_with(sleep) {
thread::sleep(Duration::from_secs(5));
("HTTP/1.1 200 OK\r\n\r\n", "hello.html")
let caps = hdr.captures(bf);
match caps {
Some(cap) => {
let c = cap.get(1).unwrap().as_str();
println!("Asked to fetch {}", c);
//assert!(Path::new(c).exists());
if c == "" {
("HTTP/1.1 200 OK\r\n\r\n", "index.html")
} else if Path::new(c).exists() {
("HTTP/1.1 200 OK\r\n\r\n", c)
} else {
("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html")
}
},
None => {
("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html")
}
}
} else {
("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html")
};

Loading…
Cancel
Save