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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
use api_error::ApiError;
use http_parse_error::HttpParseError;
use include_dir::{include_dir, Dir};
use logger::Logger;
use native_tls::{Identity, TlsAcceptor};
use std::borrow::Cow;
use std::collections::HashMap;
use std::fs::{self, File};
use std::io::{self, BufRead, BufReader, Read, Write};
use std::net::{IpAddr, SocketAddr, TcpListener, TcpStream};
use std::path::PathBuf;
use std::sync::Arc;
use termcolor::Color;

mod errors;
mod http_response;
mod logger;
mod router;
mod thread_pool;

pub use errors::*;
pub use http_response::*;
pub use router::*;

pub static STATIC_FILES: Dir<'_> = include_dir!("src/dist");

pub trait ReadWrite: Read + Write + Send + 'static {}

impl<T: Read + Write + Send + 'static> ReadWrite for T {}

struct NetworkStream {
    delegate: Option<Box<dyn ReadWrite>>,
    tls_acceptor: Option<TlsAcceptor>,
}

impl NetworkStream {
    pub fn new(
        cert_path: Option<&PathBuf>,
        cert_pass: Option<&String>,
    ) -> Result<NetworkStream, Box<dyn std::error::Error>> {
        match &cert_path {
            Some(path) => {
                let identity_bytes = fs::read(path)?;

                let identity = Identity::from_pkcs12(&identity_bytes, cert_pass.unwrap())?;

                let tls_acceptor = TlsAcceptor::new(identity)?;

                Ok(NetworkStream {
                    tls_acceptor: Some(tls_acceptor),
                    delegate: None,
                })
            }
            None => Ok(NetworkStream {
                tls_acceptor: None,
                delegate: None,
            }),
        }
    }
    pub fn get_stream(
        &mut self,
        stream: TcpStream,
    ) -> Result<&mut NetworkStream, Box<dyn std::error::Error>> {
        match &self.tls_acceptor {
            Some(acceptor) => {
                let tls_stream = acceptor.accept(stream)?;
                self.delegate = Some(Box::new(tls_stream));
                Ok(self)
            }
            None => {
                self.delegate = Some(Box::new(stream));
                Ok(self)
            }
        }
    }
}

pub struct HttpServer {
    port: u16,
    threads: usize,
    cert_path: Option<PathBuf>,
    cert_pass: Option<String>,
    router: Router,
    logger: Option<Arc<Logger>>,
    bind_address: IpAddr,
}

impl HttpServer {
    pub fn build(
        port: u16,
        threads: usize,
        cert_path: Option<PathBuf>,
        cert_pass: Option<String>,
        bind_address: IpAddr,
    ) -> HttpServer {
        HttpServer {
            port,
            threads,
            cert_path,
            cert_pass,
            router: Router::new(),
            logger: None,
            bind_address,
        }
    }
    pub fn with_logger(mut self) -> Self {
        self.logger = Some(Arc::new(Logger::new()));
        self.router = self
            .router
            .with_logger(Some(Arc::clone(self.logger.as_ref().unwrap())));
        self
    }

    pub fn get_router(&mut self) -> &mut Router {
        &mut self.router
    }

    pub fn add_routes<F>(mut self, routes: F) -> Self
    where
        F: Fn(&mut Router) + Send + Sync + 'static,
    {
        routes(&mut self.router);
        self
    }

    pub fn with_cors_policy(mut self, policy: Cors) -> Self {
        self.router = self.router.with_cors(policy);
        self
    }
    pub fn run(self) -> Result<(), Box<dyn std::error::Error>> {
        self.print_server_info();
        let listener = TcpListener::bind(SocketAddr::from((self.bind_address, self.port)))?;
        let pool = thread_pool::ThreadPool::build(self.threads)?;

        let arc_router = Arc::new(self.router);
        let mut network_stream =
            NetworkStream::new(self.cert_path.as_ref(), self.cert_pass.as_ref())?;
        for stream in listener.incoming() {
            let Ok(stream) = network_stream.get_stream(stream?) else {
                continue;
            };
            let mut stream = stream.delegate.take().unwrap();

            let router_clone = Arc::clone(&arc_router);
            let logger_clone = self.logger.clone();

            pool.execute(move || {
                handle_connection(&mut stream, &router_clone)
                    .unwrap_or_else(|err| {
                        if let (Some(method), Some(path)) = (&err.method, &err.path) {
                            router_clone
                                .log_response(err.error_response.status_code, path, method)
                                .unwrap();
                        }

                        err.error_response
                    })
                    .write_response(&mut stream)
                    .unwrap_or_else(|err| {
                        if let Some(logger) = logger_clone {
                            logger
                                .log_stderr("Error: {}", vec![(err.to_string(), Some(Color::Red))])
                                .unwrap();
                        }
                    });
            })?;
        }
        Ok(())
    }
    fn print_server_info(&self) {
        if let Some(logger) = &self.logger {
            let https = match self.cert_path {
                Some(_) => String::from("Enabled"),
                None => String::from("Disabled"),
            };
            let cors = if self.router.hsa_cors() {
                String::from("Enabled")
            } else {
                String::from("Disabled")
            };

            logger.log_stdout(
                r#"

 ========================================================================================================
 
   _____ _                 _        _    _ _______ _______ _____     _____                          
  / ____(_)               | |      | |  | |__   __|__   __|  __ \   / ____|                         
 | (___  _ _ __ ___  _ __ | | ___  | |__| |  | |     | |  | |__) | | (___   ___ _ ____   _____ _ __ 
  \___ \| | '_ ` _ \| '_ \| |/ _ \ |  __  |  | |     | |  |  ___/   \___ \ / _ \ '__\ \ / / _ \ '__|
  ____) | | | | | | | |_) | |  __/ | |  | |  | |     | |  | |       ____) |  __/ |   \ V /  __/ |   
 |_____/|_|_| |_| |_| .__/|_|\___| |_|  |_|  |_|     |_|  |_|      |_____/ \___|_|    \_/ \___|_|   
                    | |                                                                             
                    |_|                                                                             

=========================================================================================================

Port: {}
Threads: {}
HTTPS: {}
CORS: {}

====================
Logs:"#,
                vec![
                    (self.port.to_string(), Some(Color::Yellow)),
                    (self.threads.to_string(), Some(Color::Yellow)),
                    (https, Some(Color::Yellow)),
                    (cors, Some(Color::Yellow)),
                ],
            )
            .unwrap();
        }
    }
}

fn parse_http<'a>(
    reader: &mut BufReader<&mut Box<dyn ReadWrite>>,
    request_string: &'a mut String,
) -> Result<(&'a str, &'a str, HashMap<&'a str, &'a str>), HttpParseError> {
    loop {
        let mut line = String::new();
        reader.read_line(&mut line)?;
        request_string.push_str(&line);
        if line == "\r\n" {
            break;
        }
    }
    let http_parts: Vec<&str> = request_string.split("\r\n\r\n").collect();
    let request_lines: Vec<&str> = http_parts
        .first()
        .ok_or(HttpParseError::default())?
        .lines()
        .collect();

    let http_method: Vec<&str> = request_lines
        .first()
        .ok_or(HttpParseError::default())?
        .split_whitespace()
        .collect();

    if http_method.len() < 3 {
        return Err(HttpParseError::default());
    }

    let (method, path, _version) = (http_method[0], http_method[1], http_method[2]);

    let mut headers = std::collections::HashMap::new();
    for line in &request_lines[1..] {
        let parts: Vec<&str> = line.splitn(2, ':').collect();
        if parts.len() == 2 {
            headers.insert(
                *parts.first().ok_or(HttpParseError::default())?,
                parts.get(1).ok_or(HttpParseError::default())?.trim(),
            );
        }
    }

    Ok((method, path, headers))
}

fn handle_connection(
    stream: &mut Box<dyn ReadWrite>,
    router: &Arc<Router>,
) -> Result<HttpResponse, ApiError> {
    let mut reader = BufReader::new(&mut *stream);

    let mut request = String::new();
    let (method, path, headers) = parse_http(&mut reader, &mut request)?;

    let mut buffer = Vec::new();
    let body;

    match headers.get("Content-Type") {
        Some(content_type) => {
            if content_type.contains("multipart/form-data") {
                let path = headers.get("Path").unwrap();
                let response =
                    handle_multipart_file_upload(content_type, &headers, &mut reader, path)
                        .map_err(|err| {
                            ApiError::new_with_html(400, format!("File upload error: {}", err))
                        })?;
                return Ok(response);
            } else {
                body = parse_body(&headers, reader, &mut buffer)?;
            }
        }
        None => {
            body = parse_body(&headers, reader, &mut buffer)?;
        }
    }

    let response = router.route(path, method, body.as_deref())?;
    Ok(response)
}

fn parse_body<'a>(
    headers: &'a HashMap<&'a str, &'a str>,
    reader: BufReader<&mut Box<dyn ReadWrite>>,
    buffer: &'a mut Vec<u8>,
) -> Result<Option<Cow<'a, str>>, Box<dyn std::error::Error>> {
    match headers.get("Content-Length") {
        Some(content_length) => {
            let content_length = content_length.parse::<usize>()?;
            let mut body_reader = reader.take(content_length.try_into()?);
            body_reader.read_to_end(buffer)?;
            let body = String::from_utf8_lossy(&buffer[..]);
            Ok(Some(body))
        }
        None => Ok(None),
    }
}

fn handle_multipart_file_upload(
    content_type: &str,
    headers: &HashMap<&str, &str>,
    reader: &mut BufReader<&mut Box<dyn ReadWrite>>,
    path: &str,
) -> Result<HttpResponse, Box<dyn std::error::Error>> {
    let idx = content_type
        .find("boundary=")
        .ok_or("Missing multipart boundary")?;
    let boundary = format!("--{}", &content_type[(idx + "boundary=".len())..]);
    let mut multipart_headers = HashMap::new();
    let mut header_size = 0;

    //read headers
    loop {
        let mut line = String::new();
        header_size += reader.read_line(&mut line)?;
        if line.trim() == boundary {
            continue;
        }
        if line == "\r\n" {
            break;
        }

        let parts: Vec<&str> = line.trim().split(':').map(|s| s.trim()).collect();
        if parts.len() < 2 {
            return Err("Error parsing multipart request".into());
        }
        multipart_headers.insert(parts[0].to_owned(), parts[1].to_owned());
    }

    //get file name from content disposition and form target path
    let content_disposition = multipart_headers
        .get("Content-Disposition")
        .ok_or("Missing content disposition")?;
    let filename = content_disposition
        .split("filename=\"")
        .nth(1)
        .and_then(|s| s.split('\"').next())
        .ok_or("Error parsing file name")?;
    let mut target_path = PathBuf::from("./").canonicalize()?.join(path);
    target_path.push(filename);

    let current_dir = std::env::current_dir()?;
    if !target_path.starts_with(current_dir) {
        return Err("Only paths relative to the current directory are allowed".into());
    }

    //calculate file size based on whole content length so that reading the stream can be stopped
    let mut file = File::create(target_path)?;
    let content_length = headers
        .get("Content-Length")
        .ok_or("Missing content length")?
        .parse::<usize>()?;
    let file_bytes = content_length - boundary.len() - header_size - 6;

    //take only the file length from the main buf reader
    let mut limited_reader = reader.take(file_bytes.try_into()?);

    //copy streams
    io::copy(&mut limited_reader, &mut file)?;

    let response = HttpResponse::new(
        Some(Body::Text(format!(
            "File {} uploaded successfully.",
            filename
        ))),
        Some(String::from("text/plain")),
        200,
    );
    Ok(response)
}