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
//! HTTP implementation
use fastcgi;
use fastcgi::{Readable, Writable};

use std::io;
use std::io::{Read, Write};
use std::collections::HashMap;
use std::net::TcpStream;

extern crate byteorder;
use self::byteorder::{ByteOrder, BigEndian};

#[derive(Debug)]
pub struct Request<'sr>
{
    id: u16,
    role: u16,
    flags: u8,
    headers: HashMap<Vec<u8>, Vec<u8>>,
    buf: Vec<u8>,
    stream: &'sr TcpStream,
    pending: bool,
}

impl<'sr> Request<'sr>
{

    /// Constructor
    pub fn new(stream: &'sr TcpStream, id: u16) -> Request
    {
        Request {
            id: id,
            role: 0,
            flags: 0,
            headers: HashMap::new(),
            buf: Vec::new(),
            stream: stream,
            pending: true,
        }
    }

    /// Add request options
    pub fn add_options(&mut self, data: Vec<u8>)
    {
        let begin_request = fastcgi::BeginRequestBody::read(&data[..]);
        self.role = begin_request.role;
        self.flags = begin_request.flags;
    }

    /// Add param pairs
    pub fn add_param(&mut self, data: Vec<u8>)
    {
        self.headers.extend(ParamFetcher::new(data).parse_param());
    }

    /// FastCGI requestId
    pub fn get_id(&self) -> u16
    {
        self.id
    }

    /// List all headers in bytes
    pub fn headers(&self) -> &HashMap<Vec<u8>, Vec<u8>>
    {
        &self.headers
    }

    /// List all headers in utf8
    pub fn headers_utf8(&self) -> HashMap<String, String>
    {
        self.headers.iter()
            .map(|(k, v)| (
                String::from_utf8_lossy(k).into_owned(),
                String::from_utf8_lossy(v).into_owned(),
            ))
            .collect::<HashMap<_, _>>()
    }

    /// Header by key in bytes
    /// Key are case-sensitive
    pub fn header(&self, key: &[u8]) -> Option<&Vec<u8>>
    {
        self.headers.get(key)
    }

    /// Header by key in utf8
    /// Key are case-sensitive
    pub fn header_utf8(&self, key: &[u8]) -> Option<String>
    {
        self.headers.get(key).map(|v| String::from_utf8_lossy(v).into_owned())
    }

    /// A vector with multiple header in utf8
    /// Key are case-sensitive
    pub fn header_multiple_utf8(&self, key: &[u8]) -> Option<Vec<String>>
    {
        self.header_utf8(key).map(|v| {
                v.split(',')
                .map(|h| h.trim().to_string())
                .collect()
        })
    }

    /// Read FastCGI header
    pub fn fcgi_header(mut stream: &TcpStream) -> fastcgi::Header
    {
        let mut buf: [u8; fastcgi::HEADER_LEN] = [0; fastcgi::HEADER_LEN];
        stream.read(&mut buf).expect("Read fcgi header");
        fastcgi::Header::read(&buf)
    }

    pub fn fcgi_body(mut stream: &TcpStream, length: usize) -> Vec<u8>
    {
        let mut body: Vec<u8> = Vec::with_capacity(length);
        unsafe {
            body.set_len(length);
        }

        match stream.read(&mut body) {
            Ok(_len) if _len == length => body,
            Ok(_len) => panic!("{} bytes readed, expected {}", _len, length),
            Err(e)  => panic!("{}", e),
        }
    }

    pub fn fcgi_record(&mut self, h: fastcgi::Header, body: Vec<u8>)
    {
        match h.type_ {
            fastcgi::BEGIN_REQUEST => self.add_options(body),
            fastcgi::PARAMS => self.add_param(body),
            fastcgi::STDIN => self.buf.extend(body),
            _ => panic!("Wron FastCGI request header"),
        }
    }
}

impl<'sr> io::Read for Request<'sr>
{
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize>
    {
        while self.buf.len() < buf.len() && self.pending {
            let h = Self::fcgi_header(self.stream);
            if h.content_length == 0 {
                self.pending = false;
                break;
            }
            let body = Self::fcgi_body(self.stream, h.content_length as usize);
            self.buf.extend(body);
        }

        let end = if buf.len() > self.buf.len() {
            self.buf.len()
        } else {
            buf.len()
        };

        // TODO: how avoid it?
        for (k, v) in self.buf.drain(..end).enumerate() {
            buf[k] = v;
        }

        Ok(end)
    }
}


/// Helper for split key-value param pairs
struct ParamFetcher
{
    data: Vec<u8>,
    pos: usize,
}

impl ParamFetcher
{
    /// Constructor
    fn new(data: Vec<u8>) -> Self
    {
        ParamFetcher {
            data: data,
            pos: 0,
        }
    }

    /// Parse pairs
    fn parse_param(&mut self) -> HashMap<Vec<u8>, Vec<u8>>
    {
        let mut param: HashMap<Vec<u8>, Vec<u8>> = HashMap::new();

        let data_length: usize = self.data.len();

        while data_length > self.pos {

            let key_length = self.param_length();
            let value_length = self.param_length();

            let key = Vec::from(&self.data[self.pos..self.pos + key_length]);
            self.pos += key_length;

            let value = Vec::from(&self.data[self.pos..self.pos + value_length]);
            self.pos += value_length;

            param.insert(key, value);
        }

        param
    }

    /// Read param length and move interlal cursor
    fn param_length(&mut self) -> usize
    {
        let mut length: usize = self.data[self.pos] as usize;

        if (length >> 7) == 1 {

            self.data[self.pos] = self.data[self.pos] & 0x7F;
            length = BigEndian::read_u32(&self.data[self.pos..(self.pos + 4)]) as usize;

            self.pos += 4;
        } else {
            self.pos += 1;
        }

        length
    }
}



/// HTTP status header
const HTTP_STATUS: &'static str = "Status";
/// HTTP line delimiter
const HTTP_LINE: &'static str = "\r\n";

#[derive(Debug)]
/// HTTP implementation of response
pub struct Response<'sw>
{
    id: u16,
    header: HashMap<Vec<u8>, Vec<u8>>,
    buf: Vec<u8>,
    stream: &'sw TcpStream,
    pending: bool,
}

impl<'sw> Response<'sw>
{
    /// Constructor
    pub fn new(stream: &'sw TcpStream, id: u16) -> Self
    {
        let mut header = HashMap::new();
        header.insert(Vec::from(HTTP_STATUS.as_bytes()),
                      Vec::from("200".as_bytes()));

        Response {
            id: id,
            header: header,
            buf: Vec::new(),
            stream: stream,
            pending: false,
        }
    }

    /// Get as raw bytes
    pub fn http_headers(&self) -> Vec<u8>
    {
        let mut data: Vec<u8> = Vec::new();

        for (name, value) in &self.header {
            data.extend_from_slice(&name[..]);
            data.push(b':');
            data.extend_from_slice(&value[..]);
            data.extend_from_slice(HTTP_LINE.as_bytes());
        }

        // http headers delimiter
        data.extend_from_slice(HTTP_LINE.as_bytes());

        data
    }

    /// End request record
    fn end_request(&self) -> Vec<u8>
    {
        let data = fastcgi::EndRequestBody {
                       app_status: 0,
                       protocol_status: fastcgi::REQUEST_COMPLETE,
                       reserved: [0; 3],
                   }
                   .write();

        let mut result: Vec<u8> = self.record_header(fastcgi::END_REQUEST, data.len() as u16);
        result.extend(data);

        result
    }

    /// Get raw header bytes
    fn record_header(&self, type_: u8, length: u16) -> Vec<u8>
    {
        let header = fastcgi::Header {
            version: fastcgi::VERSION_1,
            type_: type_,
            request_id: self.id,
            content_length: length,
            padding_length: 0,
            reserved: [0; 1],
        };

        header.write()
    }

    /// Add some HTTP header
    pub fn header(&mut self, key: &[u8], value: &[u8])
    {
        self.header.insert(Vec::from(key), Vec::from(value));
    }

    /// Add some HTTP header from utf8
    pub fn header_utf8(&mut self, key: &str, value: &str)
    {
        self.header.insert(key.as_bytes().to_vec(), value.as_bytes().to_vec());
    }

    /// Set custom HTTP status
    pub fn status(&mut self, code: u16)
    {
        self.header.insert(Vec::from(HTTP_STATUS.as_bytes()),
                           Vec::from(code.to_string().as_bytes())
        );
    }

    fn send_header(&mut self)
    {
        if !self.pending {
            for part in self.http_headers().chunks(fastcgi::MAX_LENGTH) {
                let header = self.record_header(fastcgi::STDOUT, part.len() as u16);
                self.stream.write(&header).expect("Send response headers");
                self.stream.write(&part).expect("Send response headers");
            }

            self.pending = true;
        }
    }

    fn send_chunk(&mut self, end: usize)
    {
        let h = self.record_header(fastcgi::STDOUT, end as u16);
        self.stream.write(&h).expect("Send response body");
        self.stream.write(&self.buf.drain(..end).collect::<Vec<_>>()).expect("Send response body");
    }
}

impl<'sw> io::Write for Response<'sw>
{

    fn write(&mut self, buf: &[u8]) -> io::Result<usize>
    {
        self.send_header();
        self.buf.extend_from_slice(buf);
        while self.buf.len() > fastcgi::MAX_LENGTH {
            self.send_chunk(fastcgi::MAX_LENGTH);
        }

        Ok(buf.len())
    }

    fn flush(&mut self) -> io::Result<()>
    {
        let mut data: Vec<u8> = Vec::new();

        self.send_header();

        // self a rest
        let end = self.buf.len();
        self.send_chunk(end);

        // terminate record
        data.extend_from_slice(&self.record_header(fastcgi::STDOUT, 0));
        data.extend_from_slice(&self.end_request());

        self.stream.write(&data).expect("Send end");

        Ok(())
    }
}