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
//! Defines a BinsServer, which can serve requests against bins.
//!
//! BinsServer holds a database and wraps Hyper's Http server.
use std::sync::{Mutex, Arc};
use std::io::Write;
use std::collections::HashMap;
use std::io::Read;

use hyper::server::{Server, Handler, Request, Response};
use hyper::uri::RequestUri::AbsolutePath;
use hyper::header::ContentLength;
use hyper::header::Headers;
use hyper::server::Listening;
use hyper::{Get, Post, Delete};
use hyper::status::StatusCode;

use storage::*;
use models;
use models::{Id, IdExtractor};

use errors::*;

use regex::Regex;

use rustc_serialize::json;
use rustc_serialize::Encodable;

use time;

use url::Url;

const BIN_SUMMARY_PATH_REGEXP: &'static str =
    r"/rusqbins/((?i)[A-F0-9]{8}\-[A-F0-9]{4}\-4[A-F0-9]{3}\-[89AB][A-F0-9]{3}\-[A-F0-9]{12})$";
const BIN_REQUESTS_PATH_REGEXP: &'static str =
    r"/rusqbins/((?i)[A-F0-9]{8}\-[A-F0-9]{4}\-4[A-F0-9]{3}\-[89AB][A-F0-9]{3}\-[A-F0-9]{12})/requests/?$";

/// Holds details about the current running server
pub struct BinsServer<T>
    where T: Bins + Send + 'static
{
    pub address: String,
    pub port: usize,
    pub storage: Arc<Mutex<T>>,
}

/// A Worker handles requests on the server and holds on to some
/// state, which enables it to do its work efficiently.
struct Worker<T>
    where T: Bins
{
    id_extractor: IdExtractor,
    bin_summary_path_regexp: Regex,
    bin_requests_path_regexp: Regex,
    bins: Arc<Mutex<T>>,
}

header! { (ContentType, "Content-Type") => [String] }
header! { (XRusqBinId, "X-Rusqbin-Id") => [String] }

impl<T> Handler for Worker<T>
    where T: Bins + Send
{
    // Req is a mutable reference because we need to mutate it for reading
    fn handle(&self, mut req: Request, res: Response) {
        // From https://github.com/hyperium/hyper/blob/0.9.x/examples/server.rs
        let handling_result: Result<(), Error> =
            {
                let req_uri = req.uri.clone();
                match req_uri {
                    AbsolutePath(ref path) => {
                        match (&req.method, &path[..]) {
                            (&Get, _) if self.extract_id_from_bin_summary_path(&path).is_some() => {
                                self.find_bin_summary(&path, res)
                            }
                            (&Delete, _) if self.extract_id_from_bin_summary_path(&path)
                                                .is_some() => self.delete_bin(&path, res),
                            (&Get, _) if self.extract_id_from_bin_requests_path(&path)
                                             .is_some() => self.find_bin_requests(&path, res),
                            (&Get, "/rusqbins") |
                            (&Get, "/rusqbins/") => self.list_bins(res),
                            (&Post, "/rusqbins") |
                            (&Post, "/rusqbins/") => self.create_bin(res),
                            _ if self.extract_id_from_header(&req.headers).is_some() => {
                                self.insert_request(&mut req, res)
                            }
                            _ => bad_request(res),
                        }
                    }
                    _ => bad_request(res),
                }
            };
        match handling_result {
            Err(Error::PoisonedLock) => panic!("Yo. Mutex got poisoned. Now wut?"),
            Err(e) => error!("Something really messed up bad: {:?}", e),
            _ => debug!("Request handled fine"),
        }
    }
}

impl<T> Worker<T>
    where T: Bins
{
    // <-- Routing-related helper functions
    fn extract_id_from_bin_summary_path<'a>(&'a self, s: &'a str) -> Option<Id> {
        let caps = self.bin_summary_path_regexp.captures(&*s);
        caps.and_then(|c| c.get(1).and_then(|r| self.id_extractor.parse(r.as_str())))
    }

    fn extract_id_from_bin_requests_path<'a>(&'a self, s: &'a str) -> Option<Id> {
        let caps = self.bin_requests_path_regexp.captures(&*s);
        caps.and_then(|c| c.get(1).and_then(|r| self.id_extractor.parse(r.as_str())))
    }

    fn extract_id_from_header<'a>(&'a self, headers: &'a Headers) -> Option<Id> {
        headers.get::<XRusqBinId>().and_then(|s| self.id_extractor.parse(s))
    }
    // Routing-related helper functions -->

    // <-- "Controller" methods.

    fn create_bin(&self, res: Response) -> Result<(), Error> {
        let mut cont = self.bins.lock()?;
        let new_bin = cont.create_bin();
        info!("Created a new bin {:?}", new_bin);
        write_json(&new_bin, res)
    }

    fn delete_bin(&self, path: &String, res: Response) -> Result<(), Error> {
        if let Some(id) = self.extract_id_from_bin_summary_path(path) {
            debug!("Trying to delete a bin with id: {}", id);
            let mut cont = self.bins.lock()?;
            match cont.delete_bin(&id) {
                DeleteBinStatus::Ok => {
                    info!("Deleted bin with id: {}", id);
                    ok(res)
                }
                DeleteBinStatus::NoSuchBin => {
                    info!("No bin with id: {}", id);
                    not_found(res)
                }
            }
        } else {
            // this methods should not be invoked if extraction isn't successful
            Err(Error::UnforeseenError)
        }
    }

    fn list_bins(&self, res: Response) -> Result<(), Error> {
        let cont = self.bins.lock()?;
        let all = &cont.get_bin_summaries();
        info!("Retrieved all bins: {:?}", all);
        write_json(all, res)
    }

    fn find_bin_summary(&self, path: &String, res: Response) -> Result<(), Error> {
        if let Some(id) = self.extract_id_from_bin_summary_path(path) {
            debug!("Trying to find a bin with id: {}", id);
            let cont = self.bins.lock()?;
            match cont.get_bin_summary(&id) {
                Some(ref bin) => {
                    info!("Retrieved bin summary: {:?}", bin);
                    write_json(bin, res)
                }
                None => {
                    info!("No bin with that id: {}", id);
                    not_found(res)
                }
            }
        } else {
            // this methods should not be invoked if extraction isn't successful
            Err(Error::UnforeseenError)
        }
    }

    fn find_bin_requests(&self, path: &String, res: Response) -> Result<(), Error> {
        if let Some(id) = self.extract_id_from_bin_requests_path(path) {
            debug!("Trying to find a bin with id: {} ", id);
            let cont = self.bins.lock()?;
            match cont.get_bin(&id) {
                Some(ref bin) => {
                    info!("Retrieved bin: {:?}", bin);
                    write_json(bin, res)
                }
                None => {
                    info!("No bin with that id: {}", id);
                    not_found(res)
                }
            }
        } else {
            // this methods should not be invoked if extraction isn't successful
            Err(Error::UnforeseenError)
        }
    }

    fn insert_request(&self, req: &mut Request, res: Response) -> Result<(), Error> {
        let now = time::get_time();
        debug!("Insert time: {:?}", now);
        let now_millis = (now.sec as i64 * 1000) + (now.nsec as i64 / 1000 / 1000);
        debug!("Insert time in Epoch millis: {:?}", now_millis);
        if let Some(id) = self.extract_id_from_header(&req.headers.clone()) {
            let mut cont = self.bins.lock()?;
            match cont.insert_request(&id, build_models_request(now_millis, req)?) {
                InsertRequestStatus::Ok => {
                    info!("Successfully inserted a request into bin with id: {}", id);
                    ok(res)
                }
                _ => {
                    info!("No bin with that id: {}", id);
                    not_found(res)
                }
            }
        } else {
            // this methods should not be invoked if extraction isn't successful
            Err(Error::UnforeseenError)
        }
    }
}

fn write_json<T: Encodable>(t: &T, mut res: Response) -> Result<(), Error> {
    let pretty = json::as_pretty_json(t);
    let encoded = format!("{}", pretty);
    res.headers_mut().set(ContentLength(encoded.len() as u64));
    res.headers_mut().set(ContentType("application/json".to_owned()));
    let mut res_start = res.start()?;
    Ok(res_start.write_all(encoded.as_bytes())?)
}

fn not_found(mut res: Response) -> Result<(), Error> {
    *res.status_mut() = StatusCode::NotFound;
    Ok(())
}

fn bad_request(mut res: Response) -> Result<(), Error> {
    *res.status_mut() = StatusCode::BadRequest;
    Ok(())
}

fn ok(mut res: Response) -> Result<(), Error> {
    *res.status_mut() = StatusCode::Ok;
    Ok(())
}

fn build_models_request(req_time: i64, req: &mut Request) -> Result<models::Request, Error> {
    let req_headers: Headers = req.headers.clone(); // to escape immutable req borrow..
    let content_length = req_headers.get::<ContentLength>().map(|l| l.0);
    let content_type = req_headers.get::<ContentType>().map(|t| t.0.clone());
    let method = req.method.as_ref().to_owned();
    let path = format!("{}", req.uri);

    let body = match content_length {
        Some(l) if l > 0 => {
            let mut buffer = String::new();
            let _ = req.read_to_string(&mut buffer)?;
            Some(buffer)
        }
        _ => None,
    };

    let mut headers: HashMap<String, Vec<String>> = HashMap::new();
    for header in req_headers.iter() {
        let k = header.name();
        let v = header.value_string();
        headers.entry(k.to_owned()).or_insert(vec![]).push(v);
    }

    // our req is at this point guaranteed to be an AbsolutePath by the time it comes here.
    let parsed_url: Url = Url::parse(&*format!("http://b.com{}", path))?;
    let mut query_map: HashMap<String, Vec<String>> = HashMap::new();
    for (k, v) in parsed_url.query_pairs() {
        query_map.entry(k.into_owned()).or_insert(vec![]).push(v.into_owned());
    }

    Ok(models::Request {
           content_length: content_length,
           content_type: content_type,
           time: req_time,
           method: method,
           path: path,
           body: body,
           headers: headers,
           query_string: query_map,
       })
}

impl<T> BinsServer<T>
    where T: Bins + Send + 'static
{
    pub fn new(port: usize, bins: T) -> BinsServer<T> {
        let address = format!("127.0.0.1:{}", port);
        BinsServer {
            address: address,
            port: port,
            storage: Arc::new(Mutex::new(bins)),
        }
    }

    /// Starts a BinsServer.
    pub fn start(&self) -> Result<Listening, Error> {
        let bin_summary_path_regexp = Regex::new(BIN_SUMMARY_PATH_REGEXP)?;
        let bin_requests_path_regexp = Regex::new(BIN_REQUESTS_PATH_REGEXP)?;
        let worker = Worker {
            id_extractor: IdExtractor::new(),
            bin_summary_path_regexp: bin_summary_path_regexp,
            bin_requests_path_regexp: bin_requests_path_regexp,
            bins: self.storage.clone(),
        };
        let listening: Listening = Server::http(&*self.address)?.handle(worker)?;
        Ok(listening)
    }
}