1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use crate::http_parser::HttpParser;
use crate::request::Request;
use crate::response::Response;
use std::collections::HashMap;
use std::io::Write;
use std::net::TcpStream;
use std::sync::{mpsc, Arc, Mutex};
use std::thread;
use std::time::Duration;

pub struct ThreadPool {
    // A vector containing worker threads equal to the amount specified in new()
    workers: Vec<Worker>,
    // A channel for forwarding jobs to the worker threads
    sender: mpsc::Sender<Message>,
}

enum Message {
    NewJob(TcpStream),
    Terminate,
}

impl ThreadPool {
    /// Create a new ThreadPool.
    ///
    /// The size is the number of threads in the pool.
    ///
    /// # Panics
    ///
    /// The `new` function will panic if the size is zero.
    pub fn new(
        size: usize,
        routes: HashMap<String, fn(Request, Response) -> Response>,
        middleware: Vec<(String, fn(Request, Response) -> (Request, Response, bool))>,
    ) -> ThreadPool {
        assert!(size > 0);

        let (sender, receiver) = mpsc::channel();

        let receiver = Arc::new(Mutex::new(receiver));

        let mut workers = Vec::with_capacity(size);

        for id in 0..size {
            let routes_clone = routes.clone();
            let middleware_clone = middleware.clone();
            workers.push(Worker::new(
                id,
                Arc::clone(&receiver),
                routes_clone,
                middleware_clone,
            ));
        }

        ThreadPool { workers, sender }
    }

    pub fn execute(&self, stream: TcpStream) {
        match self.sender.send(Message::NewJob(stream)) {
            Ok(_) => {}
            Err(error) => {
                println!("{}", error);
            }
        };
    }
}

/// Stops the threads gracefully, meaning that they finish their current tasks and then end themselves. Currently not in use
impl Drop for ThreadPool {
    fn drop(&mut self) {
        println!("Sending terminate message to all workers.");

        for _ in &self.workers {
            match self.sender.send(Message::Terminate) {
                Ok(_) => {}
                Err(error) => println!("{}", error),
            }
        }

        println!("Shutting down all workers.");

        for worker in &mut self.workers {
            println!("Shutting down worker {}", worker.id);

            if let Some(thread) = worker.thread.take() {
                match thread.join() {
                    Ok(_) => {}
                    Err(_error) => println!("Failed to join thread"),
                }
            }
        }
    }
}

struct Worker {
    id: usize,
    thread: Option<thread::JoinHandle<()>>,
}

impl Worker {
    /// Create a new Worker.
    ///
    /// The worker stores clones of the routes and middleware hashmaps for handling requests. The reveiver is used to forward jobs into the thread.
    fn new(
        id: usize,
        receiver: Arc<Mutex<mpsc::Receiver<Message>>>,
        routes: HashMap<String, fn(Request, Response) -> Response>,
        middleware: Vec<(String, fn(Request, Response) -> (Request, Response, bool))>,
    ) -> Worker {
        let thread = thread::spawn(move || 'outer: loop {
            // Receive message from main thread
            let lock = match receiver.lock() {
                Ok(lock) => lock,
                Err(error) => {
                    println!("{}", error);
                    continue 'outer;
                }
            };
            let message = match lock.recv() {
                Ok(message) => message,
                Err(error) => {
                    println!("{}", error);
                    continue 'outer;
                }
            };

            // Handle job
            match message {
                Message::NewJob(stream) => {
                    let mut response = Response::new(404, Vec::new(), HashMap::new());
                    let parse_result = HttpParser::parse(&stream);
                    let mut request = match parse_result {
                        Ok(request) => request,
                        Err(error) => {
                            println!("HTTP Parser Error: {}", error);
                            response.status(400);
                            write_response(stream, response);
                            continue 'outer; // Skip to next iteration
                        }
                    };
                    // Remove params
                    let request_wo_params = match request.url.split("?").next() {
                        Some(url) => url,
                        None => {
                            response.status(400);
                            write_response(stream, response);
                            continue 'outer; // Skip to next iteration
                        }
                    };
                    let mut request_route = String::from(request_wo_params);
                    // Remove trailing / so that pathing is agnostic towards /example/ or /example
                    let last_char = match request_route.pop() {
                        Some(character) => character,
                        None => {
                            response.status(500);
                            response.body("failed to parse http");
                            write_response(stream, response);
                            continue 'outer;
                        }
                    };
                    if last_char != '/' || request_route.len() == 0 {
                        request_route.push(last_char)
                    }
                    if routes.contains_key(&request_route) {
                        // Route through middleware
                        for mid in &middleware {
                            if mid.0.len() > request_route.len() {
                                break;
                            };
                            if mid.0 == request_route[..mid.0.len()] {
                                let answer = mid.1(request, response);
                                response = answer.1;
                                // If the middleware rejects the request we return the response
                                if !answer.2 {
                                    write_response(stream, response);
                                    continue 'outer;
                                }
                                request = answer.0;
                            }
                        }
                        response = routes[&request_route](request, response);
                    }
                    write_response(stream, response);
                }
                Message::Terminate => {
                    println!("Worker {} was told to terminate.", id);
                    break;
                }
            }
        });

        return Worker {
            id,
            thread: Some(thread),
        };
        // Writes a tcp response to the client
        fn write_response(mut stream: TcpStream, response: Response) {
            let five_seconds = Duration::new(5, 0);
            stream
                .set_write_timeout(Some(five_seconds))
                .expect("set_write_timeout call failed");
            match stream.write(&response.to_http()) {
                Ok(_) => {}
                Err(e) => println!("Failed sending response: {}", e),
            }
        }
    }
}