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
#![allow(dead_code)]
//! This crate provieds FastCGI client with supporting multithreaded socket listener and HTTP-instances multiplexed onto a single connection.

// object
mod fastcgi;
mod http;

pub use http::{Request, Response};

// Data struct
use std::collections::HashMap;
use std::iter::Iterator;

// net / io
use std::net::{TcpListener, TcpStream, ToSocketAddrs};
use std::io::Write;

// Thread
#[cfg(feature="spawn")]
use std::thread;

pub struct Client
{
    listener: TcpListener,
}

/// TcpListener wrapper
impl Client
{
    pub fn new<A: ToSocketAddrs>(addr: A) -> Client
    {
        Client {
            listener: TcpListener::bind(addr).expect("Bind address"),
        }
    }

    /// Run thread
    /// Accept `Handler` as callback
    #[cfg(feature="spawn")]
    pub fn run<T: Handler + Send + Clone + 'static>(&self, handler: T)
    {
        let listener = self.listener.try_clone().expect("Clone listener");
        let handler = handler.clone();

        thread::spawn(move || {

            for stream in listener.incoming() {
                match stream {
                    Ok(stream) => {
                        let reader = StreamSyntax::new(&stream);
                        for mut pair in reader {
                            // call handler
                            handler.process(&mut pair);
                            pair.response().flush().unwrap();
                        }
                    }
                    Err(e) => panic!("{}", e),
                }
            }
        });
    }

    /// Accept `Handler` as callback
    #[cfg(not(feature="spawn"))]
    pub fn run<T: Handler>(&self, handler: T)
    {
        for stream in self.listener.incoming() {
            match stream {
                Ok(stream) => {
                    let reader = StreamSyntax::new(&stream);
                    for mut pair in reader {
                        // call handler
                        handler.process(&mut pair);
                        pair.response().flush().unwrap();
                    }
                }
                Err(e) => panic!("{}", e),
            }
        }
    }
}

/// HTTP request / response pairs
pub struct HttpPair<'s>(http::Request<'s>, http::Response<'s>);

impl<'s> HttpPair<'s>
{
    pub fn request(&mut self) -> &mut http::Request<'s>
    {
        &mut self.0
    }

    pub fn response(&mut self) -> &mut http::Response<'s>
    {
        &mut self.1
    }
}

/// FasctCGI request parser
pub struct StreamSyntax<'s>
{
    born: bool,
    pair: HashMap<u16, HttpPair<'s>>,
    stream: &'s TcpStream,
}

impl<'s> StreamSyntax<'s>
{
    fn new(stream: &'s TcpStream) -> StreamSyntax
    {
        StreamSyntax {
            born: true,
            pair: HashMap::new(),
            stream: stream,
        }
    }
}

/// Iterator implementation
impl<'s> Iterator for StreamSyntax<'s>
{
    type Item = HttpPair<'s>;

    /// Yield HTTP request / response
    fn next(&mut self) -> Option<Self::Item>
    {
        while !self.pair.is_empty() || self.born {
            let h = http::Request::fcgi_header(self.stream);
            let body = http::Request::fcgi_body(self.stream, &h);

            self.pair.entry(h.request_id)
                .or_insert(HttpPair(
                    http::Request::new(self.stream, h.request_id),
                    http::Response::new(self.stream, h.request_id),
                ));

            match h.type_ {
                fastcgi::ABORT_REQUEST => {
                    self.pair.remove(&h.request_id).unwrap()
                        .response()
                        .flush()
                        .expect("Send end request on abort")
                }
                fastcgi::PARAMS if h.content_length == 0 => {
                    self.born = false;
                    return self.pair.remove(&h.request_id);
                }
                _ => {
                    self.pair.get_mut(&h.request_id).unwrap()
                        .request().fcgi_record(h, body)
                }
            }
        }

        None
    }
}

/// Callback trait
pub trait Handler
{
    /// Run HTTP-request handling
    fn process(&self, &mut HttpPair);
}