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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
// Copyright (c) 2016 The Rouille developers
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.

//! The rouille library is very easy to get started with.
//!
//! Listening to a port is done by calling the [`start_server`](fn.start_server.html) function:
//!
//! ```no_run
//! use rouille::Request;
//! use rouille::Response;
//!
//! rouille::start_server("0.0.0.0:80", move |request| {
//!     Response::text("hello world")
//! });
//! ```
//!
//! Whenever an HTTP request is received on the address passed as first parameter, the closure
//! passed as second parameter is called. This closure must then return a
//! [`Response`](struct.Response.html) that will be sent back to the client.
//!
//! See the documentation of [`start_server`](fn.start_server.html) for more details.
//!
//! # Analyzing the request
//!
//! The parameter that the closure receives is a [`Request`](struct.Request.html) object that
//! represents the request made by the client.
//!
//! The `Request` object itself provides some getters, but most advanced functionalities are
//! provided by other modules of this crate.
//!
//! - In order to dispatch between various code depending on the URL, you can use the
//!   [`router!`](macro.router.html) macro.
//! - In order to analyze the body of the request, like handling JSON input, form input, etc. you
//!   can take a look at [the `input` module](input/index.html).
//!
//! # Returning a response
//!
//! Once you analyzed the request, it is time to return a response by returning a
//! [`Response`](struct.Response.html) object.
//!
//! All the members of `Response` are public, so you can customize it as you want. There are also
//! several constructors that you build a basic `Response` which can then modify.
//!
//! In order to serve static files, take a look at
//! [the `match_assets` function](fn.match_assets.html).
//!
//! In order to apply content encodings (including compression such as gzip or deflate), see
//! the [content_encoding module](content_encoding/index.html), and specifically the
//! [content_encoding::apply](content_encoding/fn.apply.html) function.

#![deny(unsafe_code)]

extern crate base64;
#[cfg(feature = "brotli")]
extern crate brotli;
extern crate chrono;
#[cfg(feature = "gzip")]
extern crate deflate;
extern crate filetime;
extern crate multipart;
extern crate rand;
extern crate serde;
#[macro_use]
extern crate serde_derive;
pub extern crate percent_encoding;
extern crate serde_json;
extern crate sha1_smol;
extern crate threadpool;
extern crate time;
extern crate tiny_http;
pub extern crate url;

// https://github.com/servo/rust-url/blob/e121d8d0aafd50247de5f5310a227ecb1efe6ffe/percent_encoding/lib.rs#L126
pub const DEFAULT_ENCODE_SET: &percent_encoding::AsciiSet = &percent_encoding::CONTROLS
    .add(b' ')
    .add(b'"')
    .add(b'#')
    .add(b'<')
    .add(b'>')
    .add(b'`')
    .add(b'?')
    .add(b'{')
    .add(b'}');

pub use assets::extension_to_mime;
pub use assets::match_assets;
pub use log::{log, log_custom};
pub use response::{Response, ResponseBody};
pub use tiny_http::ReadWrite;

use std::error::Error;
use std::fmt;
use std::io::Cursor;
use std::io::Read;
use std::io::Result as IoResult;
use std::marker::PhantomData;
use std::net::SocketAddr;
use std::net::ToSocketAddrs;
use std::panic;
use std::panic::AssertUnwindSafe;
use std::slice::Iter as SliceIter;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::mpsc;
use std::sync::Arc;
use std::sync::Mutex;
use std::thread;
use std::time::Duration;

pub mod cgi;
pub mod content_encoding;
pub mod input;
pub mod proxy;
pub mod session;
pub mod websocket;

mod assets;
mod find_route;
mod log;
mod response;
mod router;
#[doc(hidden)]
pub mod try_or_400;

/// This macro assumes that the current function returns a `Response` and takes a `Result`.
/// If the expression you pass to the macro is an error, then a 404 response is returned.
#[macro_export]
macro_rules! try_or_404 {
    ($result:expr) => {
        match $result {
            Ok(r) => r,
            Err(_) => return $crate::Response::empty_404(),
        }
    };
}

/// This macro assumes that the current function returns a `Response`. If the condition you pass
/// to the macro is false, then a 400 response is returned.
///
/// # Example
///
/// ```
/// # #[macro_use] extern crate rouille;
/// # fn main() {
/// use rouille::Request;
/// use rouille::Response;
///
/// fn handle_something(request: &Request) -> Response {
///     let data = try_or_400!(post_input!(request, {
///         field1: u32,
///         field2: String,
///     }));
///
///     assert_or_400!(data.field1 >= 2);
///     Response::text("hello")
/// }
/// # }
/// ```
#[macro_export]
macro_rules! assert_or_400 {
    ($cond:expr) => {
        if !$cond {
            return $crate::Response::empty_400();
        }
    };
}

/// Starts a server and uses the given requests handler.
///
/// The request handler takes a `&Request` and must return a `Response` to send to the user.
///
/// > **Note**: `start_server` is meant to be an easy-to-use function. If you want more control,
/// > see [the `Server` struct](struct.Server.html).
///
/// # Common mistakes
///
/// The handler must capture its environment by value and not by reference (`'static`). If you
/// use closure, don't forget to put `move` in front of the closure.
///
/// The handler must also be thread-safe (`Send` and `Sync`).
/// For example this handler isn't thread-safe:
///
/// ```should_fail
/// let mut requests_counter = 0;
///
/// rouille::start_server("localhost:80", move |request| {
///     requests_counter += 1;
///
///     // ... rest of the handler ...
/// # panic!()
/// })
/// ```
///
/// Multiple requests can be processed simultaneously, therefore you can't mutably access
/// variables from the outside.
///
/// Instead you must use a `Mutex`:
///
/// ```no_run
/// use std::sync::Mutex;
/// let requests_counter = Mutex::new(0);
///
/// rouille::start_server("localhost:80", move |request| {
///     *requests_counter.lock().unwrap() += 1;
///
///     // rest of the handler
/// # panic!()
/// })
/// ```
///
/// # Panic handling in the handler
///
/// If your request handler panics, a 500 error will automatically be sent to the client.
///
/// # Panic
///
/// This function will panic if the server starts to fail (for example if you use a port that is
/// already occupied) or if the socket is force-closed by the operating system.
///
/// If you need to handle these situations, please see `Server`.
pub fn start_server<A, F>(addr: A, handler: F) -> !
where
    A: ToSocketAddrs,
    F: Send + Sync + 'static + Fn(&Request) -> Response,
{
    Server::new(addr, handler)
        .expect("Failed to start server")
        .run();
    panic!("The server socket closed unexpectedly")
}

/// Identical to `start_server` but uses a `ThreadPool` of the given size.
///
/// When `pool_size` is `None`, the thread pool size will default to `8 * num-cpus`.
/// `pool_size` must be greater than zero or this function will panic.
pub fn start_server_with_pool<A, F>(addr: A, pool_size: Option<usize>, handler: F) -> !
where
    A: ToSocketAddrs,
    F: Send + Sync + 'static + Fn(&Request) -> Response,
{
    let pool_size = pool_size.unwrap_or_else(|| {
        8 * thread::available_parallelism()
            .map(|n| n.get())
            .unwrap_or(1)
    });

    Server::new(addr, handler)
        .expect("Failed to start server")
        .pool_size(pool_size)
        .run();
    panic!("The server socket closed unexpectedly")
}

struct AtomicCounter(Arc<AtomicUsize>);

impl AtomicCounter {
    fn new(count: &Arc<AtomicUsize>) -> Self {
        count.fetch_add(1, Ordering::Relaxed);
        AtomicCounter(Arc::clone(count))
    }
}

impl Drop for AtomicCounter {
    fn drop(&mut self) {
        self.0.fetch_sub(1, Ordering::Release);
    }
}

/// Executes a function in either a thread of a thread pool
enum Executor {
    Threaded { count: Arc<AtomicUsize> },
    Pooled { pool: threadpool::ThreadPool },
}
impl Executor {
    /// `size` must be greater than zero or the call to `ThreadPool::new` will panic.
    fn with_size(size: usize) -> Self {
        let pool = threadpool::ThreadPool::new(size);
        Executor::Pooled { pool }
    }

    #[inline]
    fn execute<F: FnOnce() + Send + 'static>(&self, f: F) {
        match *self {
            Executor::Threaded { ref count } => {
                let counter = AtomicCounter::new(count);
                thread::spawn(move || {
                    let _counter = counter;
                    f()
                });
            }
            Executor::Pooled { ref pool } => {
                pool.execute(f);
            }
        }
    }

    fn join(&self) {
        match *self {
            Executor::Threaded { ref count } => {
                while count.load(Ordering::Acquire) > 0 {
                    thread::sleep(Duration::from_millis(100));
                }
            }
            Executor::Pooled { ref pool } => {
                pool.join();
            }
        }
    }
}

impl Default for Executor {
    fn default() -> Self {
        Executor::Threaded {
            count: Arc::new(AtomicUsize::new(0)),
        }
    }
}

/// A listening server.
///
/// This struct is the more manual server creation API of rouille and can be used as an alternative
/// to the `start_server` function.
///
/// The `start_server` function is just a shortcut for `Server::new` followed with `run`. See the
/// documentation of the `start_server` function for more details about the handler.
///
/// # Example
///
/// ```no_run
/// use rouille::Server;
/// use rouille::Response;
///
/// let server = Server::new("localhost:0", |request| {
///     Response::text("hello world")
/// }).unwrap();
/// println!("Listening on {:?}", server.server_addr());
/// server.run();
/// ```
pub struct Server<F> {
    server: tiny_http::Server,
    handler: Arc<AssertUnwindSafe<F>>,
    executor: Executor,
}

impl<F> Server<F>
where
    F: Send + Sync + 'static + Fn(&Request) -> Response,
{
    /// Builds a new `Server` object.
    ///
    /// After this function returns, the HTTP server is listening.
    ///
    /// Returns an error if there was an error while creating the listening socket, for example if
    /// the port is already in use.
    pub fn new<A>(addr: A, handler: F) -> Result<Server<F>, Box<dyn Error + Send + Sync + 'static>>
    where
        A: ToSocketAddrs,
    {
        let server = tiny_http::Server::http(addr)?;
        Ok(Server {
            server,
            executor: Executor::default(),
            handler: Arc::new(AssertUnwindSafe(handler)), // TODO: using AssertUnwindSafe here is wrong, but unwind safety has some usability problems in Rust in general
        })
    }

    /// Builds a new `Server` object with SSL support.
    ///
    /// After this function returns, the HTTPS server is listening.
    ///
    /// Returns an error if there was an error while creating the listening socket, for example if
    /// the port is already in use.
    #[cfg(any(feature = "ssl", feature = "rustls"))]
    pub fn new_ssl<A>(
        addr: A,
        handler: F,
        certificate: Vec<u8>,
        private_key: Vec<u8>,
    ) -> Result<Server<F>, Box<dyn Error + Send + Sync + 'static>>
    where
        A: ToSocketAddrs,
    {
        let ssl_config = tiny_http::SslConfig {
            certificate,
            private_key,
        };
        let server = tiny_http::Server::https(addr, ssl_config)?;
        Ok(Server {
            server,
            executor: Executor::default(),
            handler: Arc::new(AssertUnwindSafe(handler)), // TODO: using AssertUnwindSafe here is wrong, but unwind safety has some usability problems in Rust in general
        })
    }

    /// Use a `ThreadPool` of the given size to process requests
    ///
    /// `pool_size` must be greater than zero or this function will panic.
    pub fn pool_size(mut self, pool_size: usize) -> Self {
        self.executor = Executor::with_size(pool_size);
        self
    }

    /// Returns the address of the listening socket.
    #[inline]
    pub fn server_addr(&self) -> SocketAddr {
        self.server
            .server_addr()
            .to_ip()
            .expect("Unexpected Unix socket listener")
    }

    /// Runs the server forever, or until the listening socket is somehow force-closed by the
    /// operating system.
    #[inline]
    pub fn run(self) {
        for request in self.server.incoming_requests() {
            self.process(request);
        }
    }

    /// Processes all the client requests waiting to be processed, then returns.
    ///
    /// This function executes very quickly, as each client requests that needs to be processed
    /// is processed in a separate thread.
    #[inline]
    pub fn poll(&self) {
        while let Ok(Some(request)) = self.server.try_recv() {
            self.process(request);
        }
    }

    /// Creates a new thread for the server that can be gracefully stopped later.
    ///
    /// This function returns a tuple of a `JoinHandle` and a `Sender`.
    /// You must call `JoinHandle::join()` otherwise the server will not run until completion.
    /// The server can be stopped at will by sending it an empty `()` message from another thread.
    /// There may be a maximum of a 1 second delay between sending the stop message and the server
    /// stopping. This delay may be shortened in future.
    ///
    /// ```no_run
    /// use std::thread;
    /// use std::time::Duration;
    /// use rouille::Server;
    /// use rouille::Response;
    ///
    /// let server = Server::new("localhost:0", |request| {
    ///     Response::text("hello world")
    /// }).unwrap();
    /// println!("Listening on {:?}", server.server_addr());
    /// let (handle, sender) = server.stoppable();
    ///
    /// // Stop the server in 3 seconds
    /// thread::spawn(move || {
    ///     thread::sleep(Duration::from_secs(3));
    ///     sender.send(()).unwrap();
    /// });
    ///
    /// // Block the main thread until the server is stopped
    /// handle.join().unwrap();
    /// ```
    #[inline]
    pub fn stoppable(self) -> (thread::JoinHandle<()>, mpsc::Sender<()>) {
        let (tx, rx) = mpsc::channel();
        let handle = thread::spawn(move || {
            while rx.try_recv().is_err() {
                // In order to reduce CPU load wait 1s for a recv before looping again
                while let Ok(Some(request)) = self.server.recv_timeout(Duration::from_secs(1)) {
                    self.process(request);
                }
            }
        });

        (handle, tx)
    }

    /// Same as `poll()` but blocks for at most `duration` before returning.
    ///
    /// This function can be used implement a custom server loop in a more CPU-efficient manner
    /// than calling `poll`.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use rouille::Server;
    /// use rouille::Response;
    ///
    /// let server = Server::new("localhost:0", |request| {
    ///     Response::text("hello world")
    /// }).unwrap();
    /// println!("Listening on {:?}", server.server_addr());
    ///
    /// loop {
    ///     server.poll_timeout(std::time::Duration::from_millis(100));
    /// }
    /// ```
    #[inline]
    pub fn poll_timeout(&self, dur: std::time::Duration) {
        while let Ok(Some(request)) = self.server.recv_timeout(dur) {
            self.process(request);
        }
    }

    /// Waits for all in-flight requests to be processed. This is useful for implementing a graceful
    /// shutdown.
    ///
    /// Note: new connections may still be accepted while we wait, and this function does not guarantee
    /// to wait for those new requests. To implement a graceful shutdown or a clean rolling-update,
    /// the following approach should be used:
    ///
    /// 1) Stop routing requests to this server. For a rolling update, requests should be routed
    ///    to the new instance. This logic typically sits outside of your application.
    ///
    /// 2) Drain the queue of incoming connections by calling `poll_timeout` with a short timeout.
    ///
    /// 3) Wait for in-flight requests to be processed by using this method.
    ///
    /// # Example
    /// ```no_run
    /// # use std::time::Duration;
    /// # use rouille::Server;
    /// #
    /// # let server = Server::new("", |_| unimplemented!()).unwrap();
    /// # fn is_stopping() -> bool { unimplemented!() }
    ///
    /// // Accept connections until we receive a SIGTERM
    /// while !is_stopping() {
    ///     server.poll_timeout(Duration::from_millis(100));
    /// }
    ///
    /// // We received a SIGTERM, but there still may be some queued connections,
    /// // so wait for them to be accepted.
    /// println!("Shutting down gracefully...");
    /// server.poll_timeout(Duration::from_millis(100));
    ///
    /// // We can expect there to be no more queued connections now, but slow requests
    /// // may still be in-flight, so wait for them to finish.
    /// server.join();
    /// ```
    pub fn join(&self) {
        self.executor.join();
    }

    // Internal function, called when we got a request from tiny-http that needs to be processed.
    fn process(&self, request: tiny_http::Request) {
        // We spawn a thread so that requests are processed in parallel.
        let handler = self.handler.clone();
        self.executor.execute(|| {
            // Small helper struct that makes it possible to put
            // a `tiny_http::Request` inside a `Box<Read>`.
            struct RequestRead(Arc<Mutex<Option<tiny_http::Request>>>);
            impl Read for RequestRead {
                #[inline]
                fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
                    self.0
                        .lock()
                        .unwrap()
                        .as_mut()
                        .unwrap()
                        .as_reader()
                        .read(buf)
                }
            }

            // Building the `Request` object.
            let tiny_http_request;
            let rouille_request = {
                let url = request.url().to_owned();
                let method = request.method().as_str().to_owned();
                let headers = request
                    .headers()
                    .iter()
                    .map(|h| (h.field.to_string(), h.value.clone().into()))
                    .collect();
                let remote_addr = request.remote_addr().copied();

                tiny_http_request = Arc::new(Mutex::new(Some(request)));
                let data = Arc::new(Mutex::new(Some(
                    Box::new(RequestRead(tiny_http_request.clone())) as Box<_>,
                )));

                Request {
                    url,
                    method,
                    headers,
                    https: false,
                    data,
                    remote_addr,
                }
            };

            // Calling the handler ; this most likely takes a lot of time.
            // If the handler panics, we build a dummy response.
            let mut rouille_response = {
                // We don't use the `rouille_request` anymore after the panic, so it's ok to assert
                // it's unwind safe.
                let rouille_request = AssertUnwindSafe(rouille_request);
                let res = panic::catch_unwind(move || {
                    let rouille_request = rouille_request;
                    handler(&rouille_request)
                });

                match res {
                    Ok(r) => r,
                    Err(_) => Response::html(
                        "<h1>Internal Server Error</h1>\
                                        <p>An internal error has occurred on the server.</p>",
                    )
                    .with_status_code(500),
                }
            };

            // writing the response
            let (res_data, res_len) = rouille_response.data.into_reader_and_size();
            let mut response = tiny_http::Response::empty(rouille_response.status_code)
                .with_data(res_data, res_len);

            let mut upgrade_header = "".into();

            for (key, value) in rouille_response.headers {
                if key.eq_ignore_ascii_case("Content-Length") {
                    continue;
                }

                if key.eq_ignore_ascii_case("Upgrade") {
                    upgrade_header = value;
                    continue;
                }

                if let Ok(header) = tiny_http::Header::from_bytes(key.as_bytes(), value.as_bytes())
                {
                    response.add_header(header);
                } else {
                    // TODO: ?
                }
            }

            if let Some(ref mut upgrade) = rouille_response.upgrade {
                let trq = tiny_http_request.lock().unwrap().take().unwrap();
                let socket = trq.upgrade(&upgrade_header, response);
                upgrade.build(socket);
            } else {
                // We don't really care if we fail to send the response to the client, as there's
                // nothing we can do anyway.
                let _ = tiny_http_request
                    .lock()
                    .unwrap()
                    .take()
                    .unwrap()
                    .respond(response);
            }
        });
    }
}

/// Trait for objects that can take ownership of a raw connection to the client data.
///
/// The purpose of this trait is to be used with the `Connection: Upgrade` header, hence its name.
pub trait Upgrade {
    /// Initializes the object with the given socket.
    fn build(&mut self, socket: Box<dyn ReadWrite + Send>);
}

/// Represents a request that your handler must answer to.
///
/// This can be either a real request (received by the HTTP server) or a mock object created with
/// one of the `fake_*` constructors.
pub struct Request {
    method: String,
    url: String,
    headers: Vec<(String, String)>,
    https: bool,
    data: Arc<Mutex<Option<Box<dyn Read + Send>>>>,
    remote_addr: Option<SocketAddr>,
}

impl fmt::Debug for Request {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Request")
            .field("method", &self.method)
            .field("url", &self.url)
            .field("headers", &self.headers)
            .field("https", &self.https)
            .field("remote_addr", &self.remote_addr)
            .finish()
    }
}

impl Request {
    /// Builds a fake HTTP request to be used during tests.
    ///
    /// The remote address of the client will be `127.0.0.1:12345`. Use `fake_http_from` to
    /// specify what the client's address should be.
    pub fn fake_http<U, M>(
        method: M,
        url: U,
        headers: Vec<(String, String)>,
        data: Vec<u8>,
    ) -> Request
    where
        U: Into<String>,
        M: Into<String>,
    {
        let data = Arc::new(Mutex::new(Some(Box::new(Cursor::new(data)) as Box<_>)));
        let remote_addr = Some("127.0.0.1:12345".parse().unwrap());

        Request {
            url: url.into(),
            method: method.into(),
            https: false,
            data,
            headers,
            remote_addr,
        }
    }

    /// Builds a fake HTTP request to be used during tests.
    pub fn fake_http_from<U, M>(
        from: SocketAddr,
        method: M,
        url: U,
        headers: Vec<(String, String)>,
        data: Vec<u8>,
    ) -> Request
    where
        U: Into<String>,
        M: Into<String>,
    {
        let data = Arc::new(Mutex::new(Some(Box::new(Cursor::new(data)) as Box<_>)));

        Request {
            url: url.into(),
            method: method.into(),
            https: false,
            data,
            headers,
            remote_addr: Some(from),
        }
    }

    /// Builds a fake HTTPS request to be used during tests.
    ///
    /// The remote address of the client will be `127.0.0.1:12345`. Use `fake_https_from` to
    /// specify what the client's address should be.
    pub fn fake_https<U, M>(
        method: M,
        url: U,
        headers: Vec<(String, String)>,
        data: Vec<u8>,
    ) -> Request
    where
        U: Into<String>,
        M: Into<String>,
    {
        let data = Arc::new(Mutex::new(Some(Box::new(Cursor::new(data)) as Box<_>)));
        let remote_addr = Some("127.0.0.1:12345".parse().unwrap());

        Request {
            url: url.into(),
            method: method.into(),
            https: true,
            data,
            headers,
            remote_addr,
        }
    }

    /// Builds a fake HTTPS request to be used during tests.
    pub fn fake_https_from<U, M>(
        from: SocketAddr,
        method: M,
        url: U,
        headers: Vec<(String, String)>,
        data: Vec<u8>,
    ) -> Request
    where
        U: Into<String>,
        M: Into<String>,
    {
        let data = Arc::new(Mutex::new(Some(Box::new(Cursor::new(data)) as Box<_>)));

        Request {
            url: url.into(),
            method: method.into(),
            https: true,
            data,
            headers,
            remote_addr: Some(from),
        }
    }

    /// If the decoded URL of the request starts with `prefix`, builds a new `Request` that is
    /// the same as the original but without that prefix.
    ///
    /// # Example
    ///
    /// ```
    /// # use rouille::Request;
    /// # use rouille::Response;
    /// fn handle(request: &Request) -> Response {
    ///     if let Some(request) = request.remove_prefix("/static") {
    ///         return rouille::match_assets(&request, "/static");
    ///     }
    ///
    ///     // ...
    ///     # panic!()
    /// }
    /// ```
    pub fn remove_prefix(&self, prefix: &str) -> Option<Request> {
        if !self.url().starts_with(prefix) {
            return None;
        }

        // TODO: url-encoded characters in the prefix are not implemented
        assert!(self.url.starts_with(prefix));
        Some(Request {
            method: self.method.clone(),
            url: self.url[prefix.len()..].to_owned(),
            headers: self.headers.clone(), // TODO: expensive
            https: self.https,
            data: self.data.clone(),
            remote_addr: self.remote_addr,
        })
    }

    /// Returns `true` if the request uses HTTPS, and `false` if it uses HTTP.
    ///
    /// # Example
    ///
    /// ```
    /// use rouille::{Request, Response};
    ///
    /// fn handle(request: &Request) -> Response {
    ///     if !request.is_secure() {
    ///         return Response::redirect_303(format!("https://example.com"));
    ///     }
    ///
    ///     // ...
    /// # panic!()
    /// }
    /// ```
    #[inline]
    pub fn is_secure(&self) -> bool {
        self.https
    }

    /// Returns the method of the request (`GET`, `POST`, etc.).
    #[inline]
    pub fn method(&self) -> &str {
        &self.method
    }

    /// Returns the raw URL requested by the client. It is not decoded and thus can contain strings
    /// such as `%20`, and the query parameters such as `?p=hello`.
    ///
    /// See also `url()`.
    ///
    /// # Example
    ///
    /// ```
    /// use rouille::Request;
    ///
    /// let request = Request::fake_http("GET", "/hello%20world?foo=bar", vec![], vec![]);
    /// assert_eq!(request.raw_url(), "/hello%20world?foo=bar");
    /// ```
    #[inline]
    pub fn raw_url(&self) -> &str {
        &self.url
    }

    /// Returns the raw query string requested by the client. In other words, everything after the
    /// first `?` in the raw url.
    ///
    /// Returns the empty string if no query string.
    #[inline]
    pub fn raw_query_string(&self) -> &str {
        if let Some(pos) = self.url.bytes().position(|c| c == b'?') {
            self.url.split_at(pos + 1).1
        } else {
            ""
        }
    }

    /// Returns the URL requested by the client.
    ///
    /// Contrary to `raw_url`, special characters have been decoded and the query string
    /// (eg `?p=hello`) has been removed.
    ///
    /// If there is any non-unicode character in the URL, it will be replaced with `U+FFFD`.
    ///
    /// > **Note**: This function will decode the token `%2F` will be decoded as `/`. However the
    /// > official specifications say that such a token must not count as a delimiter for URL paths.
    /// > In other words, `/hello/world` is not the same as `/hello%2Fworld`.
    ///
    /// # Example
    ///
    /// ```
    /// use rouille::Request;
    ///
    /// let request = Request::fake_http("GET", "/hello%20world?foo=bar", vec![], vec![]);
    /// assert_eq!(request.url(), "/hello world");
    /// ```
    pub fn url(&self) -> String {
        let url = self.url.as_bytes();
        let url = if let Some(pos) = url.iter().position(|&c| c == b'?') {
            &url[..pos]
        } else {
            url
        };

        percent_encoding::percent_decode(url)
            .decode_utf8_lossy()
            .into_owned()
    }

    /// Returns the value of a GET parameter or None if it doesn't exist.
    pub fn get_param(&self, param_name: &str) -> Option<String> {
        let name_pattern = &format!("{}=", param_name);
        let param_pairs = self.raw_query_string().split('&');
        param_pairs
            .filter(|pair| pair.starts_with(name_pattern) || pair == &param_name)
            .map(|pair| pair.split('=').nth(1).unwrap_or(""))
            .next()
            .map(|value| {
                percent_encoding::percent_decode(value.replace('+', " ").as_bytes())
                    .decode_utf8_lossy()
                    .into_owned()
            })
    }

    /// Returns the value of a header of the request.
    ///
    /// Returns `None` if no such header could be found.
    #[inline]
    pub fn header(&self, key: &str) -> Option<&str> {
        self.headers
            .iter()
            .find(|&&(ref k, _)| k.eq_ignore_ascii_case(key))
            .map(|&(_, ref v)| &v[..])
    }

    /// Returns a list of all the headers of the request.
    #[inline]
    pub fn headers(&self) -> HeadersIter {
        HeadersIter {
            iter: self.headers.iter(),
        }
    }

    /// Returns the state of the `DNT` (Do Not Track) header.
    ///
    /// If the header is missing or is malformed, `None` is returned. If the header exists,
    /// `Some(true)` is returned if `DNT` is `1` and `Some(false)` is returned if `DNT` is `0`.
    ///
    /// # Example
    ///
    /// ```
    /// use rouille::{Request, Response};
    ///
    /// # fn track_user(request: &Request) {}
    /// fn handle(request: &Request) -> Response {
    ///     if !request.do_not_track().unwrap_or(false) {
    ///         track_user(&request);
    ///     }
    ///
    ///     // ...
    /// # panic!()
    /// }
    /// ```
    pub fn do_not_track(&self) -> Option<bool> {
        match self.header("DNT") {
            Some(h) if h == "1" => Some(true),
            Some(h) if h == "0" => Some(false),
            _ => None,
        }
    }

    /// Returns the body of the request.
    ///
    /// The body can only be retrieved once. Returns `None` is the body has already been retrieved
    /// before.
    ///
    /// # Example
    ///
    /// ```
    /// use std::io::Read;
    /// use rouille::{Request, Response, ResponseBody};
    ///
    /// fn echo(request: &Request) -> Response {
    ///     let mut data = request.data().expect("Oops, body already retrieved, problem \
    ///                                           in the server");
    ///
    ///     let mut buf = Vec::new();
    ///     match data.read_to_end(&mut buf) {
    ///         Ok(_) => (),
    ///         Err(_) => return Response::text("Failed to read body")
    ///     };
    ///
    ///     Response {
    ///         data: ResponseBody::from_data(buf),
    ///         .. Response::text("")
    ///     }
    /// }
    /// ```
    pub fn data(&self) -> Option<RequestBody> {
        let reader = self.data.lock().unwrap().take();
        reader.map(|r| RequestBody {
            body: r,
            marker: PhantomData,
        })
    }

    /// Returns the address of the client that made this request.
    ///
    /// # Example
    ///
    /// ```
    /// use rouille::{Request, Response};
    ///
    /// fn handle(request: &Request) -> Response {
    ///     Response::text(format!("Your IP is: {:?}", request.remote_addr()))
    /// }
    /// ```
    #[inline]
    pub fn remote_addr(&self) -> &SocketAddr {
        self.remote_addr
            .as_ref()
            .expect("Unexpected Unix socket for request")
    }
}

/// Iterator to the list of headers in a request.
#[derive(Debug, Clone)]
pub struct HeadersIter<'a> {
    iter: SliceIter<'a, (String, String)>,
}

impl<'a> Iterator for HeadersIter<'a> {
    type Item = (&'a str, &'a str);

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next().map(|&(ref k, ref v)| (&k[..], &v[..]))
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.iter.size_hint()
    }
}

impl<'a> ExactSizeIterator for HeadersIter<'a> {}

/// Gives access to the body of a request.
///
/// In order to obtain this object, call `request.data()`.
pub struct RequestBody<'a> {
    body: Box<dyn Read + Send>,
    marker: PhantomData<&'a ()>,
}

impl<'a> Read for RequestBody<'a> {
    #[inline]
    fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
        self.body.read(buf)
    }
}

#[cfg(test)]
mod tests {
    use Request;

    #[test]
    fn header() {
        let request = Request::fake_http(
            "GET",
            "/",
            vec![("Host".to_owned(), "localhost".to_owned())],
            vec![],
        );
        assert_eq!(request.header("Host"), Some("localhost"));
        assert_eq!(request.header("host"), Some("localhost"));
    }

    #[test]
    fn get_param() {
        let request = Request::fake_http("GET", "/?p=hello", vec![], vec![]);
        assert_eq!(request.get_param("p"), Some("hello".to_owned()));
    }

    #[test]
    fn get_param_multiple_param() {
        let request = Request::fake_http("GET", "/?foo=bar&message=hello", vec![], vec![]);
        assert_eq!(request.get_param("message"), Some("hello".to_owned()));
    }
    #[test]
    fn get_param_no_match() {
        let request = Request::fake_http("GET", "/?hello=world", vec![], vec![]);
        assert_eq!(request.get_param("foo"), None);
    }

    #[test]
    fn get_param_partial_suffix_match() {
        let request = Request::fake_http("GET", "/?hello=world", vec![], vec![]);
        assert_eq!(request.get_param("lo"), None);
    }

    #[test]
    fn get_param_partial_prefix_match() {
        let request = Request::fake_http("GET", "/?hello=world", vec![], vec![]);
        assert_eq!(request.get_param("he"), None);
    }

    #[test]
    fn get_param_superstring_match() {
        let request = Request::fake_http("GET", "/?jan=01", vec![], vec![]);
        assert_eq!(request.get_param("january"), None);
    }

    #[test]
    fn get_param_flag_with_equals() {
        let request = Request::fake_http("GET", "/?flag=", vec![], vec![]);
        assert_eq!(request.get_param("flag"), Some("".to_owned()));
    }

    #[test]
    fn get_param_flag_without_equals() {
        let request = Request::fake_http("GET", "/?flag", vec![], vec![]);
        assert_eq!(request.get_param("flag"), Some("".to_owned()));
    }

    #[test]
    fn get_param_flag_with_multiple_params() {
        let request = Request::fake_http("GET", "/?flag&foo=bar", vec![], vec![]);
        assert_eq!(request.get_param("flag"), Some("".to_owned()));
    }

    #[test]
    fn body_twice() {
        let request = Request::fake_http("GET", "/", vec![], vec![62, 62, 62]);
        assert!(request.data().is_some());
        assert!(request.data().is_none());
    }

    #[test]
    fn url_strips_get_query() {
        let request = Request::fake_http("GET", "/?p=hello", vec![], vec![]);
        assert_eq!(request.url(), "/");
    }

    #[test]
    fn urlencode_query_string() {
        let request = Request::fake_http("GET", "/?p=hello%20world", vec![], vec![]);
        assert_eq!(request.get_param("p"), Some("hello world".to_owned()));
    }

    #[test]
    fn plus_in_query_string() {
        let request = Request::fake_http("GET", "/?p=hello+world", vec![], vec![]);
        assert_eq!(request.get_param("p"), Some("hello world".to_owned()));
    }

    #[test]
    fn encoded_plus_in_query_string() {
        let request = Request::fake_http("GET", "/?p=hello%2Bworld", vec![], vec![]);
        assert_eq!(request.get_param("p"), Some("hello+world".to_owned()));
    }

    #[test]
    fn url_encode() {
        let request = Request::fake_http("GET", "/hello%20world", vec![], vec![]);
        assert_eq!(request.url(), "/hello world");
    }

    #[test]
    fn plus_in_url() {
        let request = Request::fake_http("GET", "/hello+world", vec![], vec![]);
        assert_eq!(request.url(), "/hello+world");
    }

    #[test]
    fn dnt() {
        let request =
            Request::fake_http("GET", "/", vec![("DNT".to_owned(), "1".to_owned())], vec![]);
        assert_eq!(request.do_not_track(), Some(true));

        let request =
            Request::fake_http("GET", "/", vec![("DNT".to_owned(), "0".to_owned())], vec![]);
        assert_eq!(request.do_not_track(), Some(false));

        let request = Request::fake_http("GET", "/", vec![], vec![]);
        assert_eq!(request.do_not_track(), None);

        let request = Request::fake_http(
            "GET",
            "/",
            vec![("DNT".to_owned(), "malformed".to_owned())],
            vec![],
        );
        assert_eq!(request.do_not_track(), None);
    }
}