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
use url::Url;
use method::Method;
use method::Method::Options;
use status;
use status::Status::{BadRequest, RequestUriTooLong, HttpVersionNotSupported};
use std::io::{Stream, IoResult};
use std::io::net::ip::SocketAddr;
use std::io::net::tcp::TcpStream;
use std::fmt;
use rfc2616::{CR, LF, SP};
use headers;
use buffer::BufferedStream;
use common::read_http_version;

use headers::HeaderLineErr;
use headers::HeaderLineErr::{EndOfFile, EndOfHeaders, MalformedHeaderSyntax,
                             MalformedHeaderValue};

use self::RequestUri::{Star, AbsoluteUri, AbsolutePath, Authority};

// /// Line/header can't be more than 4KB long (note that with the compacting of LWS the actual source
// /// data could be longer than 4KB)
// const MAX_LINE_LEN: uint = 0x1000;

const MAX_REQUEST_URI_LEN: uint = 1024;
pub const MAX_METHOD_LEN: uint = 64;

pub struct RequestBuffer<'a, S: 'a> {
    /// The socket connection to read from
    pub stream: &'a mut BufferedStream<S>,
}

impl<'a, S: Stream> RequestBuffer<'a, S> {
    pub fn new(stream: &'a mut BufferedStream<S>) -> RequestBuffer<'a, S> {
        RequestBuffer {
            stream: stream,
        }
    }

    pub fn read_request_line(&mut self) -> Result<(Method, RequestUri, (uint, uint)),
                                                  status::Status> {
        let method = match self.read_method() {
            Ok(m) => m,
            // TODO: this is a very common case, if a connection is kept open but then closed or
            // timed out. We should handle that case specially if we can improve perf—check if the
            // peer is still there and just drop the request if it is not
            Err(_) => return Err(BadRequest),
        };

        // Finished reading the method, including consuming a single SP.
        // Before we read the Request-URI, we should consume *SP (it's invalid,
        // but the spec recommends supporting it anyway).
        let mut next_byte;
        loop {
            match self.stream.read_byte() {
                Ok(_b@SP) => continue,
                Ok(b) => {
                    next_byte = b;
                    break;
                },
                _ => return Err(BadRequest),
            };
        }

        // Good, we're now into the Request-URI. Bear in mind that as well as
        // ending in SP, it can for HTTP/0.9 end in CR LF or LF.
        let mut raw_request_uri = String::new();
        loop {
            if next_byte == CR {
                // For CR, we must have an LF immediately afterwards.
                if self.stream.read_byte() != Ok(LF) {
                    return Err(BadRequest);
                } else {
                    // Simplify it by just dealing with the LF possibility
                    next_byte = LF;
                    break;
                }
            } else if next_byte == SP || next_byte == LF {
                break;
            }

            if raw_request_uri.len() == MAX_REQUEST_URI_LEN {
                return Err(RequestUriTooLong)
            }
            raw_request_uri.push(next_byte as char);

            next_byte = match self.stream.read_byte() {
                Ok(b) => b,
                _ => return Err(BadRequest),
            }
        }

        // Now parse it into a RequestUri.
        let request_uri = match RequestUri::from_string(raw_request_uri) {
            Some(r) => r,
            None => return Err(BadRequest),
        };

        // At this point, we need to consider what came immediately after the
        // Request-URI. If it was a SP, then we expect (again after allowing for
        // possible *SP, though illegal) to get an HTTP-Version. If it was CR LF
        // or LF, we consider it to be HTTP/0.9.
        if next_byte == LF {
            // Good, we got CR LF or LF; HTTP/0.9 it is.
            return Ok((method, request_uri, (0, 9)));
        }

        // By this point, next_byte can only be SP. Now we want an HTTP-Version.

        let mut read_b = 0;

        // FIXME: we still have one inconsistency here: this isn't trimming *SP.
        match read_http_version(self.stream, |b| { read_b = b; b == CR || b == LF }) {
            Ok(vv) => {
                if read_b == LF || self.stream.read_byte() == Ok(LF) {
                    Ok((method, request_uri, vv))  // LF or CR LF: valid
                } else {
                    Err(BadRequest)  // CR but no LF: not valid
                }
            },
            Err(_) => Err(BadRequest),  // invalid: ... not valid ;-)
        }
    }

    #[inline]
    fn read_method(&mut self) -> IoResult<Method> {
        mod dummy { include!(concat!(env!("OUT_DIR"), "/read_method.rs")) }

        dummy::dummy::read_method(self.stream)
    }

    /// Read a header (name, value) pair.
    ///
    /// This is not necessarily just a line ending with CRLF; there are much fancier rules at work.
    /// Where appropriate (TODO, it's everywhere at present) linear whitespace is collapsed into a
    /// single space.
    ///
    /// # Error values
    ///
    /// - `EndOfHeaders`: I have no more headers to give; go forth and conquer on the body!
    /// - `EndOfFile`: socket was closed unexpectedly; probable best behavour is to drop the request
    /// - `MalformedHeaderValue`: header's value is invalid; normally, ignore it.
    /// - `MalformedHeaderSyntax`: bad request; you could drop it or try returning 400 Bad Request
    pub fn read_header<T: headers::HeaderEnum>(&mut self) -> Result<T, HeaderLineErr> {
        match headers::header_enum_from_stream(&mut *self.stream) {
        //match headers::HeaderEnum::from_stream(self.stream) {
            (Err(m), None) => Err(m),
            (Err(m), Some(b)) => {
                self.stream.poke_byte(b);
                Err(m)
            },
            (Ok(header), Some(b)) => {
                self.stream.poke_byte(b);
                Ok(header)
            }
            (Ok(header), None) => {
                // This should have read an extra byte, on account of the CR LF SP possibility
                error!("header with no next byte, did reading go wrong?");
                Ok(header)
            }
        }
    }
}

impl<'a, S: Stream> Reader for RequestBuffer<'a, S> {
    fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
        self.stream.read(buf)
    }
}

#[test]
fn test_read_request_line() {
    use method::Method::{Get, Options, Connect, ExtensionMethod};
    use buffer::BufferedStream;
    use memstream::MemReaderFakeStream;

    macro_rules! tt(
        ($value:expr => $expected:expr) => {{
            let expected = $expected;
            let mut stream = BufferedStream::new(
                MemReaderFakeStream::new($value.bytes().collect::<Vec<_>>()));
            assert_eq!(RequestBuffer::new(&mut stream).read_request_line(), expected);
        }}
    )

    tt!("GET / HTTP/1.1\n" => Ok((Get, AbsolutePath(String::from_str("/")), (1, 1))));
    tt!("GET / HTTP/1.1\r\n" => Ok((Get, AbsolutePath(String::from_str("/")), (1, 1))));
    tt!("OPTIONS /foo/bar HTTP/1.1\r\n" => Ok((Options, AbsolutePath(String::from_str("/foo/bar")), (1, 1))));
    tt!("OPTIONS * HTTP/1.1\r\n" => Ok((Options, Star, (1, 1))));
    tt!("CONNECT example.com HTTP/1.1\r\n" => Ok((Connect,
                                                Authority(String::from_str("example.com")),
                                                (1, 1))));
    tt!("FOO /\r\n" => Ok((ExtensionMethod(String::from_str("FOO")), AbsolutePath(String::from_str("/")), (0, 9))));
    tt!("FOO /\n" => Ok((ExtensionMethod(String::from_str("FOO")), AbsolutePath(String::from_str("/")), (0, 9))));
    tt!("get    http://example.com/ HTTP/42.17\r\n"
            => Ok((ExtensionMethod(String::from_str("get")),
                    AbsoluteUri(Url::parse("http://example.com/").unwrap()),
                    (42, 17))));

    // Now for some failing cases.

    // method name is not a token
    tt!("GE,T / HTTP/1.1\r\n" => Err(BadRequest));

    // Request-URI is missing ("HTTP/1.1" isn't a valid Request-URI; I confirmed this by tracing the
    // rule through RFC 2396: the "/" prevents it from being a reg_name authority, and it doesn't
    // satisfy any of the other possibilities for Request-URI either)
    tt!("GET  HTTP/1.1\r\n" => Err(BadRequest));

    // Invalid HTTP-Version
    tt!("GET / HTTX/1.1\r\n" => Err(BadRequest));
}

/// An HTTP request sent to the server.
pub struct Request {
    /// The originating IP address of the request.
    pub remote_addr: Option<SocketAddr>,

    /// The host name and IP address that the request was sent to; this must always be specified for
    /// HTTP/1.1 requests (or the request will be rejected), but for HTTP/1.0 requests the Host
    /// header was not defined, and so this field will probably be None in such cases.
    //host: Option<Host>,  // Now in headers.host

    /// The headers sent with the request.
    pub headers: headers::request::HeaderCollection,

    /// The body of the request; empty for such methods as GET.
    pub body: Vec<u8>,

    /// The HTTP method for the request.
    pub method: Method,

    /// The URI that was requested, as found in the Request-URI of the Request-Line.
    /// You will almost never need to use this; you should prefer the `url` field instead.
    pub request_uri: RequestUri,

    /// Whether to close the TCP connection when the request has been served.
    /// The alternative is keeping the connection open and waiting for another request.
    pub close_connection: bool,

    /// The HTTP version number; typically `(1, 1)` or, less commonly, `(1, 0)`.
    pub version: (uint, uint)
}

/// The URI (Request-URI in RFC 2616) as specified in the Status-Line of an HTTP request
#[deriving(PartialEq, Eq)]
pub enum RequestUri {
    /// 'The asterisk "*" means that the request does not apply to a particular resource, but to the
    /// server itself, and is only allowed when the method used does not necessarily apply to a
    /// resource. One example would be "OPTIONS * HTTP/1.1" '
    Star,

    /// 'The absoluteURI form is REQUIRED when the request is being made to a proxy. The proxy is
    /// requested to forward the request or service it from a valid cache, and return the response.
    /// Note that the proxy MAY forward the request on to another proxy or directly to the server
    /// specified by the absoluteURI. In order to avoid request loops, a proxy MUST be able to
    /// recognize all of its server names, including any aliases, local variations, and the numeric
    /// IP address. An example Request-Line would be:
    /// "GET http://www.w3.org/pub/WWW/TheProject.html HTTP/1.1"'
    AbsoluteUri(Url),

    /// 'To allow for transition to absoluteURIs in all requests in future versions of HTTP, all
    /// HTTP/1.1 servers MUST accept the absoluteURI form in requests, even though HTTP/1.1 clients
    /// will only generate them in requests to proxies.'
    ///
    /// TODO: this shouldn't be a string; it should be further parsed. `extra::net::url` has some
    /// stuff which might help, but isn't public.
    AbsolutePath(String),

    /// 'The authority form is only used by the CONNECT method (CONNECT).'
    ///
    /// TODO: this shouldn't be a string; it should be further parsed. `extra::net::url` has some
    /// stuff which might help, but isn't public.
    Authority(String),
}

impl RequestUri {
    /// Interpret a RFC2616 Request-URI
    fn from_string(request_uri: String) -> Option<RequestUri> {
        if request_uri == String::from_str("*") {
            Some(Star)
        } else if request_uri.as_bytes()[0] as char == '/' {
            Some(AbsolutePath(request_uri))
        } else if request_uri[].contains("/") {
            // An authority can't have a slash in it
            match Url::parse(request_uri[]) {
                Ok(url) => Some(AbsoluteUri(url)),
                Err(_) => None,
            }
        } else {
            // TODO: parse authority with extra::net::url
            Some(Authority(request_uri))
        }
    }
}

impl fmt::Show for RequestUri {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Star => f.write(b"*"),
            AbsoluteUri(ref url) => url.fmt(f),
            AbsolutePath(ref str) => f.write(str.as_bytes()),
            Authority(ref str) => f.write(str.as_bytes()),
        }
    }
}

impl Request {

    /// Get a response from an open socket.
    pub fn load(stream: &mut BufferedStream<TcpStream>) -> (Request, Result<(), status::Status>) {
        let mut buffer = RequestBuffer::new(stream);

        // Start out with dummy values
        let mut request = Request {
            remote_addr: buffer.stream.wrapped.peer_name().ok(),
            headers: headers::request::HeaderCollection::new(),
            body: Vec::new(),
            method: Options,
            request_uri: Star,
            close_connection: true,
            version: (0, 0),
        };

        let (method, request_uri, version) = match buffer.read_request_line() {
            Ok(vals) => vals,
            Err(err) => return (request, Err(err)),
        };
        request.method = method;
        request.request_uri = request_uri;
        request.version = version;

        // request.close_connection is deliberately left set to true so that in case of a bad
        // request we can close the connection
        let close_connection = match version {
            (1, 0) => true,
            (1, 1) => false,
            _ => return (request, Err(HttpVersionNotSupported)),
        };

        loop {
            match buffer.read_header() {
                Err(EndOfFile) => panic!("client disconnected, nowhere to send response"),
                Err(EndOfHeaders) => break,
                Err(MalformedHeaderSyntax) => {
                    println!("BAD REQUEST: malformed header (TODO: is this right?)");
                    return (request, Err(BadRequest));
                },
                Err(MalformedHeaderValue) => {
                    println!("Bad header encountered. TODO: handle this better.");
                    // Now just ignore the header
                },
                Ok(header) => {
                    request.headers.insert(header);
                },
            }
        }

        // HTTP/1.0 doesn't have Host, but HTTP/1.1 requires it
        if request.version == (1, 1) && request.headers.host.is_none() {
            println!("BAD REQUEST: host is none for HTTP/1.1 request");
            return (request, Err(BadRequest));
        }

        request.close_connection = close_connection;
        match request.headers.connection {
            Some(ref h) => for v in h.iter() {
                match *v {
                    headers::connection::Connection::Close => {
                        request.close_connection = true;
                        break;
                    },
                    headers::connection::Connection::Token(ref s) if s[] == "keep-alive" => {
                        request.close_connection = false;
                        // No break; let it be overridden by close should some weird person do that
                    },
                    headers::connection::Connection::Token(_) => (),
                }
            },
            None => (),
        }

        // Read body if its length is specified
        match request.headers.content_length {
            Some(length) => {
                match buffer.read_exact(length) {
                    Ok(body) => request.body = body,
                    Err(_) => return (request, Err(BadRequest))
                }
            },
            None => ()
        }

        (request, Ok(()))
    }
}



/* What follows is most of Go's net/http module's definition of Request.

pub struct Request {
    // GET, POST, etc.
    method: ~Method,

    // The URL requested, constructed from the request line and (if available)
    // the Host header.
    url: ~Url,

    // The HTTP protocol version used; typically (1, 1)
    protocol: (uint, uint),

    // Request headers, all nicely and correctly parsed.
    headers: ~Headers,

    // The message body.
    body: Reader,

    // ContentLength records the length of the associated content.
    // The value -1 indicates that the length is unknown.
    // Values >= 0 indicate that the given number of bytes may
    // be read from Body.
    // For outgoing requests, a value of 0 means unknown if Body is not nil.
    content_length: i64,

    // TransferEncoding lists the transfer encodings from outermost to
    // innermost. An empty list denotes the "identity" encoding.
    // TransferEncoding can usually be ignored; chunked encoding is
    // automatically added and removed as necessary when sending and
    // receiving requests.
    transfer_encoding: ~[~str],

    // Close indicates whether to close the connection after
    // replying to this request.
    close: bool,

    // The host on which the URL is sought.
    // Per RFC 2616, this is either the value of the Host: header
    // or the host name given in the URL itself.
    // It may be of the form "host:port".
    host: ~str,

    // Form contains the parsed form data, including both the URL
    // field's query parameters and the POST or PUT form data.
    // This field is only available after ParseForm is called.
    // The HTTP client ignores Form and uses Body instead.
    form: url.Values,

    // PostForm contains the parsed form data from POST or PUT
    // body parameters.
    // This field is only available after ParseForm is called.
    // The HTTP client ignores PostForm and uses Body instead.
    post_form: url.Values,

    // MultipartForm is the parsed multipart form, including file uploads.
    // This field is only available after ParseMultipartForm is called.
    // The HTTP client ignores MultipartForm and uses Body instead.
    multipart_form: *multipart.Form,

    // Trailer maps trailer keys to values.  Like for Header, if the
    // response has multiple trailer lines with the same key, they will be
    // concatenated, delimited by commas.
    // For server requests, Trailer is only populated after Body has been
    // closed or fully consumed.
    // Trailer support is only partially complete.
    trailer: ~Headers,

    // RemoteAddr allows HTTP servers and other software to record
    // the network address that sent the request, usually for
    // logging. This field is not filled in by ReadRequest and
    // has no defined format. The HTTP server in this package
    // sets RemoteAddr to an "IP:port" address before invoking a
    // handler.
    // This field is ignored by the HTTP client.
    remote_addr: string,

    // RequestURI is the unmodified Request-URI of the
    // Request-Line (RFC 2616, Section 5.1) as sent by the client
    // to a server. Usually the URL field should be used instead.
    // It is an error to set this field in an HTTP client request.
    request_uri: string,

    // TLS allows HTTP servers and other software to record
    // information about the TLS connection on which the request
    // was received. This field is not filled in by ReadRequest.
    // The HTTP server in this package sets the field for
    // TLS-enabled connections before invoking a handler;
    // otherwise it leaves the field nil.
    // This field is ignored by the HTTP client.
    tls: *tls.ConnectionState,
}*/