rusty_express 0.3.0

A simple http server library written in Rust and provide Express-alike APIs. We know that Rust is hard and daunting, so we will make sure your server can be easy to use without fear!
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
#![allow(unused_variables)]
#![allow(unused_imports)]

use std::collections::HashMap;
use std::io::prelude::*;
use std::io::{BufReader, BufWriter, Error};
use std::net::{Shutdown, TcpStream};
use std::sync::{Arc, mpsc};
use std::time::Duration;

use super::config::ConnMetadata;
use super::http::{Request, RequestWriter, Response, ResponseManager, ResponseStates, ResponseWriter};
use super::router::{Callback, REST, Route, RouteHandler};
use support::{common::write_to_buff, common::flush_buffer, debug, shared_pool, TaskType};

static HEADER_END: [u8; 2] = [13, 10];

#[derive(PartialEq, Eq, Clone, Copy)]
enum ParseError {
    EmptyRequestErr,
    ReadStreamErr,
}

//TODO: still good for implementing middlewear
//pub fn handle_connection_with_states<T: Send + Sync + Clone + StatesProvider>(
//        stream: TcpStream,
//        router: Arc<Route>,
//        metadata: Arc<ConnMetadata>,
//        states: Arc<RwLock<T>>) -> Option<u8> {
//
//    let mut request = Box::new(Request::new());
//    let handler = match handle_request(&stream, &mut request, router) {
//        Err(err) => {
//            debug::print("Error on parsing request", 3);
//            return write_to_stream(stream, &build_err_response(&err, &metadata));
//        },
//        Ok(cb) => cb,
//    };
//
//    match metadata.get_state_interaction() {
//        &StatesInteraction::WithRequest | &StatesInteraction::Both => {
//            let require_updates = match states.read() {
//                Ok(s) => s.on_request(&mut request),
//                _ => false,
//            };
//
//            if require_updates {
//                if let Ok(mut s) = states.write() {
//                    s.update(&request, None);
//                }
//            }
//        },
//        _ => { /* Nothing */ },
//    };
//
//    let mut response = initialize_response(&metadata);
//    let result = handle_response(stream, handler, &request, &mut response, &metadata);
//
//    match metadata.get_state_interaction() {
//        &StatesInteraction::WithRequest | &StatesInteraction::Both => {
//            let require_updates = match states.read() {
//                Ok(s) => s.on_response(&mut response),
//                _ => false,
//            };
//
//            if require_updates {
//                if let Ok(mut s) = states.write() {
//                    s.update(&request, Some(&response));
//                }
//            }
//        },
//        _ => { /* Nothing */ },
//    };
//
//    result
//}

pub fn handle_connection(
        stream: TcpStream,
        router: Arc<Route>,
        metadata: Arc<ConnMetadata>) -> Option<u8> {

    let mut request= Box::new(Request::new());
    let handler = match handle_request(&stream, &mut request, router) {
        Err(err) => {
            debug::print("Error on parsing request", 3);
            return write_to_stream(&stream, &mut build_err_response(&err, &metadata));
        },
        Ok(cb) => cb,
    };

    handle_response(stream, handler, &request,
                    &mut initialize_response(&metadata),  &metadata)
}

fn handle_response(stream: TcpStream, callback: Callback,
                   request: &Box<Request>, response: &mut Box<Response>,
                   metadata: &Arc<ConnMetadata>) -> Option<u8> {

    match request.header("connection") {
        Some(ref val) if val.to_lowercase().eq("close") => response.can_keep_alive(false),
        _ => response.can_keep_alive(true),
    };

    if request.method.eq(&REST::OTHER(String::from("HEAD"))) {
        response.header_only(true);
    }

    Route::handle_request(callback, request, response);
    response.validate_and_update(&metadata.get_status_pages());

    write_to_stream(&stream, response)
}

fn initialize_response(metadata: &Arc<ConnMetadata>) -> Box<Response> {
    let header = metadata.get_default_header();
    match header.is_empty() {
        true => Box::new(Response::new()),
        _ => Box::new(Response::new_with_default_header(header)),
    }
}

fn write_to_stream(stream: &TcpStream, response: &mut Box<Response>) -> Option<u8> {
    let mut writer = BufWriter::new(stream);

    // Serialize the header to the stream
    response.serialize_header(&mut writer);

    // Blank line to indicate the end of the response header
    write_to_buff(&mut writer, &HEADER_END);

    // If header only, we're done
    if response.is_header_only() {
        return flush_buffer(&mut writer);
    }

    if !response.to_keep_alive() {
        // else, write the body to the stream
        response.serialize_body(&mut writer);

        // flush the buffer and shutdown the connection: we're done; no need for explicit shutdown
        // the stream as it's dropped automatically on out-of-the-scope.
        return flush_buffer(&mut writer);
    }

    if let Ok(clone) = stream.try_clone() {
        // serialize_trunked_body will block until all the keep-alive i/o are done
        response.serialize_trunked_body(clone, &mut writer);
    }

    // trunked keep-alive i/o is done, shut down the stream for good since copies
    // can be listening on read/write
    if let Err(err) = stream.shutdown(Shutdown::Both) {
        debug::print(&format!("Encountered errors while shutting down the trunked body stream: {}", err), 1);
        return Some(1);
    }

    // Otherwise we're good to leave.
    Some(0)
}

fn handle_request(mut stream: &TcpStream, request: &mut Box<Request>, router: Arc<Route>) -> Result<Callback, ParseError> {
    let mut buffer = [0; 1024];

    if let Err(e) = stream.read(&mut buffer) {
        debug::print(&format!("Reading stream error -- {}", e), 3);
        Err(ParseError::ReadStreamErr)
    } else {
        let request_raw = String::from_utf8_lossy(&buffer[..]);
        if request_raw.is_empty() {
            return Err(ParseError::EmptyRequestErr);
        }

        let callback = parse_request(&request_raw, request, router);
        if let Some(callback) = callback {
            Ok(callback)
        } else {
            Err(ParseError::EmptyRequestErr)
        }
    }
}

fn parse_request(request: &str, store: &mut Box<Request>, router: Arc<Route>) -> Option<Callback> {
    if request.is_empty() {
        return None;
    }

    debug::print(&format!("\r\nPrint request: \r\n{}", request), 2);

    let mut lines = request.trim().lines();
    let base_line = match lines.nth(0) {
        Some(line) => line.trim(),
        _ => return None,
    };

    let rx = parse_request_base(base_line, store, router);
    if rx.is_none() { return None; }

    let mut is_body = false;
    for line in lines {
        if line.is_empty() && !is_body {
            // meeting the empty line dividing header and body
            is_body = true;
            continue;
        }

        parse_request_body(store, line, is_body);
    }

    if let Some(receiver) = rx {
        if let Ok(received) = receiver.recv_timeout(Duration::from_millis(128)) {
            if received.0.is_some() {
                store.create_param(received.1);
            }

            return received.0;
        }
    }

    None
}

fn parse_request_body(store: &mut Box<Request>, line: &str, is_body: bool) {
    //TODO: Better support for content-dispositions?

    if !is_body {
        let header_info: Vec<&str> = line.trim().splitn(2, ':').collect();

        if header_info.len() == 2 {
            let header_key = &header_info[0].trim().to_lowercase()[..];
            if header_key.eq("cookie") {
                cookie_parser(store, header_info[1].trim());
            } else {
                store.write_header(header_key, header_info[1].trim(), true);
            }
        }
    } else {
        store.extend_body(line);
    }
}

fn parse_request_base(line: &str, req: &mut Box<Request>, router: Arc<Route>)
    -> Option<mpsc::Receiver<(Option<Callback>, HashMap<String, String>)>> {

    let mut header_only = false;
    let mut raw_scheme = String::new();
    let mut raw_fragment = String::new();

    for (index, info) in line.split_whitespace().enumerate() {
        if index < 2 && info.is_empty() { return None; }

        match index {
            0 => {
                let base_method = match &info.to_uppercase()[..] {
                    "GET" => REST::GET,
                    "PUT" => REST::PUT,
                    "POST" => REST::POST,
                    "DELETE" => REST::DELETE,
                    "OPTIONS" => REST::OPTIONS,
                    _ => {
                        let others = info.to_uppercase();
                        if others.eq("HEADER") {
                            header_only = true;
                        }

                        REST::OTHER(others)
                    },
                };

                req.method = base_method;
            },
            1 => split_path(info, &mut req.uri, &mut raw_scheme, &mut raw_fragment),
            2 => req.write_header("HTTP_VERSION", info, true),
            _ => { break; },
        };
    }

    if !req.uri.is_empty() {
        let uri = req.uri.to_owned();
        let req_method = req.method.clone();

        let (tx, rx) = mpsc::channel();
        shared_pool::run(move || {
            router.seek_handler(&req_method, &uri, header_only, tx);
        }, TaskType::Request);

        // now do more work on non-essential parsing
        if !raw_fragment.is_empty() {

        }

        if !raw_scheme.is_empty() {
            req.create_scheme(scheme_parser(raw_scheme));
        }

        return Some(rx);
    }

    None
}

fn split_path(full_uri: &str, final_uri: &mut String, final_scheme: &mut String, final_frag: &mut String) {
    let uri = full_uri.trim();
    if uri.is_empty() {
        final_uri.push_str("/");
        return;
    }

    let mut uri_parts: Vec<&str> = uri.rsplitn(2, "/").collect();

    // parse fragment out
    if let Some(pos) = uri_parts[0].find("#") {
        let (remains, frag) = uri_parts[0].split_at(pos);
        uri_parts[0] = remains;

        if !frag.is_empty() {
            final_frag.push_str(frag);
        }
    }

    // parse scheme out
    if let Some(pos) = uri_parts[0].find("?") {
        let (remains, scheme) = uri_parts[0].split_at(pos);
        uri_parts[0] = remains;

        if uri_parts[1].is_empty() {
            final_uri.push_str(&format!("/{}", uri_parts[0])[..]);
        } else {
            final_uri.push_str(&format!("{}/{}", uri_parts[1], uri_parts[0])[..]);
        };

        final_scheme.push_str(scheme.trim());
    } else {
        let uri_len = uri.len();
        if uri_len > 1 && uri.ends_with("/") {
            final_uri.push_str(&uri[..uri_len-1]);
        } else {
            final_uri.push_str(uri)
        };
    }
}

/// Cookie parser will parse the request header's cookie field into a hash-map, where the
/// field is the key of the map, which map to a single value of the key from the Cookie
/// header field. Assuming no duplicate cookie keys, or the first cookie key-value pair
/// will be stored.
fn cookie_parser(store: &mut Box<Request>, cookie_body: &str) {
    if cookie_body.is_empty() { return; }

    for set in cookie_body.trim().split(";").into_iter() {
        let pair: Vec<&str> = set.trim().splitn(2, "=").collect();
        if pair.len() == 2 {
            store.set_cookie(pair[0].trim(), pair[1].trim(), false);
        } else if pair.len() > 0 {
            store.set_cookie(pair[0].trim(), "", false);
        }
    }
}

fn scheme_parser(scheme: String) -> HashMap<String, Vec<String>> {
    let mut scheme_result: HashMap<String, Vec<String>> = HashMap::new();
    for (_, kv_pair) in scheme.trim().split("&").enumerate() {
        let store: Vec<&str> = kv_pair.trim().splitn(2, "=").collect();

        if store.len() > 0 {
            let key = store[0].trim();
            let val =
                if store.len() == 2 {
                    store[1].trim().to_owned()
                } else {
                    String::new()
                };

            if scheme_result.contains_key(key) {
                if let Some(val_vec) = scheme_result.get_mut(key) {
                    val_vec.push(val);
                }
            } else {
                scheme_result.insert(key.to_owned(), vec![val]);
            }
        }
    }

    scheme_result
}

fn build_err_response(err: &ParseError, metadata: &Arc<ConnMetadata>) -> Box<Response> {
    let mut resp = Box::new(Response::new());
    let status: u16 = match err {
        &ParseError::ReadStreamErr => 500,
        _ => 404,
    };

    resp.status(status);
    resp.validate_and_update(&metadata.get_status_pages());
    resp.keep_alive(false);

    if resp.get_content_type().is_empty() {
        resp.set_content_type("text/html");
    }

    resp
}