glote 0.2.4

A fast Rust web library
Documentation
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
use std::collections::HashMap;
use std::io::{ BufRead, BufReader, Error, ErrorKind, Read };
use std::net::TcpListener;
use std::os::fd::{ AsRawFd, RawFd };
use std::ptr;
use std::sync::{ Arc, RwLock };
use std::time::Instant;
use libc::*;

use crate::request::{ parse_path_params, Request };
use crate::response::Response;
use crate::workerpool::WorkerPool;

// Atomically reference counter with safely travale though thread with own lifetime/ownership
pub type Next<'a> = &'a mut dyn FnMut();

pub type Handler = dyn Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>) + Send + Sync + 'static;
pub type Middleware = dyn Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>, Next) +
    Send +
    Sync +
    'static;

// Metadata of routes
#[derive(Clone)]
struct Route {
    method: String,
    path: String,
    middleware: Vec<Arc<Middleware>>,
    handler: Arc<Handler>,
}

pub struct Glote {
    routes: Arc<RwLock<Vec<Route>>>,
    middleware: Arc<RwLock<Vec<Arc<Middleware>>>>,
    pool: WorkerPool,
    static_path: Option<String>,
}

impl Glote {
    // Returns Arc self
    pub fn new() -> Arc<Self> {
        // Number of core in our cpu
        let num_cores = std::thread
            ::available_parallelism()
            .map(|n| n.get())
            .unwrap_or(1);
        // Total worker Defualt (total core * 4) or 4
        Arc::new(Self {
            routes: Arc::new(RwLock::new(Vec::new())),
            middleware: Arc::new(RwLock::new(Vec::new())),
            pool: WorkerPool::new(num_cores * 4),
            static_path: None,
        })
    }
    // Manually set number of workers
    pub fn set_warkers(&mut self, size: usize) {
        self.pool = WorkerPool::new(size);
    }

    // Runs Global+route middleware and final handler
    fn run_handlers(
        &self,
        req: Arc<RwLock<Request>>,
        res: Arc<RwLock<Response>>,
        middlewares: &[Arc<Middleware>],
        final_handler: impl FnMut() + Send + 'static
    ) {
        let req = Arc::clone(&req);
        let res = Arc::clone(&res);
        let middlewares = middlewares.to_vec();

        // Store final handler into box for linklist fashion with multiple middlewares
        let mut final_handler = Some(Box::new(final_handler) as Box<dyn FnMut()>);
        // makes chain in reverse way
        for mw in middlewares.into_iter().rev() {
            let req = req.clone();
            let res = res.clone();
            let mut next = final_handler.take().unwrap();
            final_handler = Some(
                Box::new(move || {
                    // case we send response and don't want to go deeper
                    if !res.read().unwrap().is_stopped() {
                        mw(req.clone(), res.clone(), &mut next);
                    }
                })
            );
        }

        if let Some(mut chain) = final_handler {
            // case we send response and don't want to go deeper
            if !res.read().unwrap().is_stopped() {
                chain();
            }
        }
    }

    // Set Global Middleware
    pub fn use_middleware<F>(&self, middleware: F)
        where F: Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>, Next) + Send + Sync + 'static
    {
        let mut middlewares = self.middleware.write().unwrap();
        middlewares.push(Arc::new(middleware));
    }

    pub fn static_file(&mut self, path: &str) {
        self.static_path = Some(path.into());
    }

    /**
     * Start our server at specific port
     */
    pub fn listen(self: Arc<Self>, port: u16) {
        let listener = TcpListener::bind(("127.0.0.1", port)).unwrap();
        listener.set_nonblocking(true).unwrap();

        // Create epoll
        let epfd = unsafe { epoll_create1(EPOLL_CLOEXEC) };
        if epfd == -1 {
            panic!("epoll_create1 failed");
        }
        // Tcp_listener FD
        let listener_fd = listener.as_raw_fd();
        // Create event
        let mut ev = epoll_event {
            events: EPOLLIN as u32,
            u64: listener_fd as u64,
        };
        unsafe {
            epoll_ctl(epfd, EPOLL_CTL_ADD, listener_fd, &mut ev);
        }

        let mut clients: HashMap<RawFd, std::net::TcpStream> = HashMap::new();
        let mut buffers: HashMap<RawFd, Vec<u8>> = HashMap::new();

        let num_cores = std::thread
            ::available_parallelism()
            .map(|n| n.get())
            .unwrap_or(1);

        // Events size cpu_cores * 1024
        let mut events = vec![epoll_event { events: 0, u64: 0 }; num_cores*1024];

        println!("\n---------------------\nServer running on port {}", port);

        loop {
            // Wait for Notify FD
            let nfds = unsafe { epoll_wait(epfd, events.as_mut_ptr(), events.len() as i32, -1) };

            if nfds < 0 {
                continue;
            }

            // Loop into all sockets we are reciveing
            for i in 0..nfds as usize {
                // Get the raw fd
                let fd = events[i].u64 as RawFd;
                // If it incoming request
                if fd == listener_fd {
                    // Listening incoming request
                    loop {
                        match listener.accept() {
                            Ok((stream, _addr)) => {
                                // Stream non_blocking
                                stream.set_nonblocking(true).unwrap();
                                // Get stread fd
                                let cfd = stream.as_raw_fd();
                                // Notify when its ready
                                let mut ev = epoll_event {
                                    events: EPOLLIN as u32,
                                    u64: cfd as u64,
                                };

                                // epoll_wait will return an event whene this client data ready
                                unsafe {
                                    epoll_ctl(epfd, EPOLL_CTL_ADD, cfd, &mut ev);
                                }

                                // Store the client in clients hashmap
                                clients.insert(cfd, stream);
                                // Empty buffer for the client
                                buffers.insert(cfd, Vec::new());
                            }
                            Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => {
                                break;
                            }
                            Err(e) => {
                                eprintln!("accept error: {}", e);
                                break;
                            }
                        }
                    }
                    continue;
                }

                // client socket ready
                let mut remove = false;

                if let Some(stream) = clients.get_mut(&fd) {
                    // Clinet buffer
                    let mut tmp = [0u8; 4096];
                    match stream.read(&mut tmp) {
                        Ok(0) => {
                            // No data client closed
                            remove = true;
                        }
                        Ok(n) => {
                            // Store data into client buffer Hashmaps
                            let entry = buffers.get_mut(&fd).unwrap();
                            entry.extend_from_slice(&tmp[..n]);

                            // check if we have full headers
                            if
                                let Some(headers_end_pos) = entry
                                    .windows(4)
                                    .position(|w| w == b"\r\n\r\n")
                            {
                                // Stroe contents
                                let headers = &entry[..headers_end_pos + 4];
                                let headers_str = String::from_utf8_lossy(headers);
                                let mut content_length: usize = 0;
                                for line in headers_str.lines() {
                                    if line.to_ascii_lowercase().starts_with("content-length:") {
                                        if let Some(v) = line.split(':').nth(1) {
                                            if let Ok(len) = v.trim().parse::<usize>() {
                                                content_length = len;
                                            }
                                        }
                                    }
                                }

                                let total_len = headers_end_pos + 4 + content_length; // Request total length
                                // Check raw data greater or equal to total length
                                if entry.len() >= total_len {
                                    // Request in rwa form
                                    let request_bytes = entry
                                        .drain(..total_len)
                                        .collect::<Vec<u8>>();

                                    // remove client from epoll and our maps
                                    unsafe {
                                        epoll_ctl(epfd, EPOLL_CTL_DEL, fd, ptr::null_mut());
                                    }
                                    // Delete client and get it
                                    let stream = clients.remove(&fd).unwrap();
                                    buffers.remove(&fd);

                                    // parse request headers only
                                    let headers_only = &request_bytes[..headers_end_pos + 4];
                                    let mut lines: Vec<String> = Vec::new();
                                    for line in headers_only.split(|&b| b == b'\n') {
                                        // trim trailing \r and whitespace
                                        let s = String::from_utf8_lossy(line)
                                            .trim_end_matches('\r')
                                            .to_string();
                                        lines.push(s);
                                    }
                                    // Add body lines
                                    if content_length > 0 {
                                        // Raw body
                                        let body_bytes =
                                            &request_bytes
                                                [
                                                    headers_end_pos + 4..headers_end_pos +
                                                        4 +
                                                        content_length
                                                ];
                                        // Parse into string
                                        let body_str =
                                            String::from_utf8_lossy(body_bytes).to_string();
                                        // Adds a empty string to seprate header and body
                                        lines.push(String::new());
                                        lines.extend(body_str.lines().map(|s| s.to_string()));
                                    } else {
                                        // There is no body just push empty string
                                        lines.push(String::new());
                                    }

                                    // clone routes/middleware once for worker
                                    let routers_clone = {
                                        let guard = self.routes.read().unwrap();
                                        guard.clone()
                                    };
                                    let middleware_clone = {
                                        let guard = self.middleware.read().unwrap();
                                        guard.clone()
                                    };
                                    let this = self.clone();

                                    // Spawn worker with parsed request (no further reading)
                                    self.pool.execute(move || {
                                        let now = Instant::now();
                                        // Parse metadata into Request struct
                                        let req = Request::new(&lines);
                                        // Parse stream into Response struct
                                        let mut res_opt = Some(
                                            Arc::new(RwLock::new(Response::new(stream)))
                                        );

                                        // Check is Route have or not
                                        let mut matched = false;
                                        // Iterate in Routes
                                        for route in routers_clone.into_iter() {
                                            // Case method same
                                            if route.method == req.method {
                                                // Parse params
                                                if
                                                    let Some(params) = parse_path_params(
                                                        &route.path,
                                                        &req.path
                                                    )
                                                {
                                                    // CLone req inside have params
                                                    let mut req_with_params = req.clone();
                                                    req_with_params.path_params = params;
                                                    let req_with_params = Arc::new(
                                                        RwLock::new(req_with_params)
                                                    );
                                                    // Combined Global Middleware and Routes Middleware
                                                    let combined_middleware: Vec<_> =
                                                        middleware_clone
                                                            .iter()
                                                            .chain(route.middleware.iter())
                                                            .cloned()
                                                            .collect();

                                                    if let Some(res_actual) = res_opt.take() {
                                                        // Take arc clone
                                                        let req_for_handler = Arc::clone(
                                                            &req_with_params
                                                        );
                                                        let res_for_handler = Arc::clone(
                                                            &res_actual
                                                        );
                                                        // Call run_handler
                                                        this.run_handlers(
                                                            Arc::clone(&req_for_handler),
                                                            Arc::clone(&res_for_handler),
                                                            &combined_middleware,
                                                            {
                                                                let req_inner =
                                                                    req_for_handler.clone();
                                                                let res_inner =
                                                                    res_for_handler.clone();
                                                                move || {
                                                                    (route.handler)(
                                                                        req_inner.clone(),
                                                                        res_inner.clone()
                                                                    );
                                                                }
                                                            }
                                                        );
                                                        // Route matched
                                                        matched = true;
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                        // Duration to fullfill the request
                                        let duration = now.elapsed();
                                        if !matched {
                                            if let Some(res) = res_opt {
                                                let mut res = res.write().unwrap();
                                                res.status(404);
                                                res.send("404 Not Found");
                                            }
                                            println!(
                                                "\x1b[31m{} {}: {:?}\x1b[0m ",
                                                req.method,
                                                req.path,
                                                duration
                                            );
                                        } else {
                                            println!(
                                                "\x1b[32m{} {}: {:?}\x1b[0m ",
                                                req.method,
                                                req.path,
                                                duration
                                            );
                                        }
                                    });
                                }
                            }
                        }
                        Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => {
                            // nothing to do
                        }
                        Err(e) => {
                            eprintln!("read error fd {}: {}", fd, e);
                            remove = true;
                        }
                    }
                }
                // Remove it from event also client and buffer
                if remove {
                    unsafe {
                        epoll_ctl(epfd, EPOLL_CTL_DEL, fd, ptr::null_mut());
                    }
                    clients.remove(&fd);
                    buffers.remove(&fd);
                }
            }
        }
    }

    // ========== Get Method ============

    // Get routes without middleware
    pub fn get<F>(&self, path: &str, handler: F)
        where F: Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>) + Send + Sync + 'static
    {
        // Make empty middleware
        let empty_middleware: Vec<fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>, Next)> = vec![];

        self.get_with_middleware(path, empty_middleware, handler);
    }

    // Get routes with middleware
    pub fn get_with_middleware<F>(
        &self,
        path: &str,
        middleware: Vec<fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>, Next)>,
        handler: F
    )
        where F: Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>) + Send + Sync + 'static
    {
        // Warp all middleware with Arc
        let wrapped: Vec<Arc<Middleware>> = middleware
            .into_iter()
            .map(|m| Arc::new(m) as Arc<Middleware>)
            .collect();

        self.get_with_middleware_run(path, wrapped, handler);
    }

    // Responsible for runing both Get without middleware or with middleware
    fn get_with_middleware_run<F>(&self, path: &str, middleware: Vec<Arc<Middleware>>, handler: F)
        where F: Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>) + Send + Sync + 'static
    {
        // Sets route Metadata
        let route = Route {
            method: "GET".to_string(),
            path: path.to_string(),
            middleware,
            handler: Arc::new(handler),
        };
        // Push with Global routes
        self.routes.write().unwrap().push(route);
    }

    // ========== Post Method ============
    // Post routes without middleware
    pub fn post<F>(&self, path: &str, handler: F)
        where F: Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>) + Send + Sync + 'static
    {
        // Make empty middleware
        let empty_middleware: Vec<fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>, Next)> = vec![];

        self.post_with_middleware(path, empty_middleware, handler);
    }

    // Post routes with middleware
    pub fn post_with_middleware<F>(
        &self,
        path: &str,
        middleware: Vec<fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>, Next)>,
        handler: F
    )
        where F: Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>) + Send + Sync + 'static
    {
        // Warp all middleware with Arc
        let wrapped: Vec<Arc<Middleware>> = middleware
            .into_iter()
            .map(|m| Arc::new(m) as Arc<Middleware>)
            .collect();

        self.post_with_middleware_run(path, wrapped, handler);
    }

    // Responsible for running both Post without middleware or with middleware
    fn post_with_middleware_run<F>(&self, path: &str, middleware: Vec<Arc<Middleware>>, handler: F)
        where F: Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>) + Send + Sync + 'static
    {
        let route = Route {
            method: "POST".to_string(),
            path: path.to_string(),
            middleware,
            handler: Arc::new(handler),
        };
        self.routes.write().unwrap().push(route);
    }

    // ========== Put Method ============
    // Put routes without middleware
    pub fn put<F>(&self, path: &str, handler: F)
        where F: Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>) + Send + Sync + 'static
    {
        // Make empty middleware
        let empty_middleware: Vec<fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>, Next)> = vec![];

        self.put_with_middleware(path, empty_middleware, handler);
    }

    // Put routes with middleware
    pub fn put_with_middleware<F>(
        &self,
        path: &str,
        middleware: Vec<fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>, Next)>,
        handler: F
    )
        where F: Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>) + Send + Sync + 'static
    {
        // Warp all middleware with Arc
        let wrapped: Vec<Arc<Middleware>> = middleware
            .into_iter()
            .map(|m| Arc::new(m) as Arc<Middleware>)
            .collect();

        self.put_with_middleware_run(path, wrapped, handler);
    }

    // Responsible for running both Put without middleware or with middleware
    fn put_with_middleware_run<F>(&self, path: &str, middleware: Vec<Arc<Middleware>>, handler: F)
        where F: Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>) + Send + Sync + 'static
    {
        let route = Route {
            method: "PUT".to_string(),
            path: path.to_string(),
            middleware,
            handler: Arc::new(handler),
        };
        self.routes.write().unwrap().push(route);
    }

    // ========== Delete Method ============

    // Delete routes without middleware
    pub fn delete<F>(&self, path: &str, handler: F)
        where F: Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>) + Send + Sync + 'static
    {
        // Make empty middleware
        let empty_middleware: Vec<fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>, Next)> = vec![];

        self.delete_with_middleware(path, empty_middleware, handler);
    }

    // Delete routes with middleware
    pub fn delete_with_middleware<F>(
        &self,
        path: &str,
        middleware: Vec<fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>, Next)>,
        handler: F
    )
        where F: Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>) + Send + Sync + 'static
    {
        // Warp all middleware with Arc
        let wrapped: Vec<Arc<Middleware>> = middleware
            .into_iter()
            .map(|m| Arc::new(m) as Arc<Middleware>)
            .collect();

        self.delete_with_middleware_run(path, wrapped, handler);
    }

    // Responsible for running both Delete without middleware or with middleware
    fn delete_with_middleware_run<F>(
        &self,
        path: &str,
        middleware: Vec<Arc<Middleware>>,
        handler: F
    )
        where F: Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>) + Send + Sync + 'static
    {
        let route = Route {
            method: "DELETE".to_string(),
            path: path.to_string(),
            middleware,
            handler: Arc::new(handler),
        };
        self.routes.write().unwrap().push(route);
    }
}