2020-05-03 08:37:40 +00:00
|
|
|
use std::io;
|
2019-04-10 19:43:31 +00:00
|
|
|
|
2017-12-26 03:42:55 +00:00
|
|
|
use bytes::{BufMut, BytesMut};
|
2017-12-25 21:40:06 +00:00
|
|
|
use http::Version;
|
2017-12-14 00:44:35 +00:00
|
|
|
|
2019-06-28 08:34:26 +00:00
|
|
|
use crate::extensions::Extensions;
|
|
|
|
|
2020-05-03 08:37:40 +00:00
|
|
|
const DIGITS_START: u8 = b'0';
|
2018-08-07 14:33:49 +00:00
|
|
|
|
2020-05-03 08:37:40 +00:00
|
|
|
pub(crate) fn write_status_line(version: Version, n: u16, bytes: &mut BytesMut) {
|
2017-12-25 21:40:06 +00:00
|
|
|
match version {
|
2020-05-03 08:37:40 +00:00
|
|
|
Version::HTTP_11 => bytes.put_slice(b"HTTP/1.1 "),
|
|
|
|
Version::HTTP_10 => bytes.put_slice(b"HTTP/1.0 "),
|
|
|
|
Version::HTTP_09 => bytes.put_slice(b"HTTP/0.9 "),
|
|
|
|
_ => {
|
|
|
|
// other HTTP version handlers do not use this method
|
2018-04-13 23:02:01 +00:00
|
|
|
}
|
2017-12-25 21:40:06 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 08:37:40 +00:00
|
|
|
let d100 = (n / 100) as u8;
|
|
|
|
let d10 = ((n / 10) % 10) as u8;
|
|
|
|
let d1 = (n % 10) as u8;
|
2017-12-14 00:44:35 +00:00
|
|
|
|
2020-05-03 08:37:40 +00:00
|
|
|
bytes.put_u8(DIGITS_START + d100);
|
|
|
|
bytes.put_u8(DIGITS_START + d10);
|
|
|
|
bytes.put_u8(DIGITS_START + d1);
|
2017-12-14 00:44:35 +00:00
|
|
|
|
2020-05-03 08:37:40 +00:00
|
|
|
// trailing space before reason
|
|
|
|
bytes.put_u8(b' ');
|
2017-12-26 03:42:55 +00:00
|
|
|
}
|
|
|
|
|
2018-03-24 16:22:34 +00:00
|
|
|
/// NOTE: bytes object has to contain enough space
|
2020-05-17 23:42:51 +00:00
|
|
|
pub fn write_content_length(n: u64, bytes: &mut BytesMut) {
|
2020-02-06 23:51:02 +00:00
|
|
|
bytes.put_slice(b"\r\ncontent-length: ");
|
|
|
|
|
2017-12-26 03:42:55 +00:00
|
|
|
if n < 10 {
|
2020-02-06 23:51:02 +00:00
|
|
|
bytes.put_u8(DIGITS_START + (n as u8));
|
2017-12-26 03:42:55 +00:00
|
|
|
} else if n < 100 {
|
2020-02-06 23:51:02 +00:00
|
|
|
let n = n as u8;
|
2017-12-26 03:42:55 +00:00
|
|
|
|
2020-02-06 23:51:02 +00:00
|
|
|
let d10 = n / 10;
|
|
|
|
let d1 = n % 10;
|
2017-12-26 03:42:55 +00:00
|
|
|
|
2020-02-06 23:51:02 +00:00
|
|
|
bytes.put_u8(DIGITS_START + d10);
|
|
|
|
bytes.put_u8(DIGITS_START + d1);
|
|
|
|
} else if n < 1000 {
|
|
|
|
let n = n as u16;
|
|
|
|
|
|
|
|
let d100 = (n / 100) as u8;
|
|
|
|
let d10 = ((n / 10) % 10) as u8;
|
|
|
|
let d1 = (n % 10) as u8;
|
|
|
|
|
|
|
|
bytes.put_u8(DIGITS_START + d100);
|
|
|
|
bytes.put_u8(DIGITS_START + d10);
|
|
|
|
bytes.put_u8(DIGITS_START + d1);
|
2020-02-24 20:58:41 +00:00
|
|
|
} else if n < 10_000 {
|
2020-02-06 23:51:02 +00:00
|
|
|
let n = n as u16;
|
|
|
|
|
|
|
|
let d1000 = (n / 1000) as u8;
|
|
|
|
let d100 = ((n / 100) % 10) as u8;
|
|
|
|
let d10 = ((n / 10) % 10) as u8;
|
|
|
|
let d1 = (n % 10) as u8;
|
|
|
|
|
2020-02-24 20:58:41 +00:00
|
|
|
bytes.put_u8(DIGITS_START + d1000);
|
|
|
|
bytes.put_u8(DIGITS_START + d100);
|
|
|
|
bytes.put_u8(DIGITS_START + d10);
|
|
|
|
bytes.put_u8(DIGITS_START + d1);
|
|
|
|
} else if n < 100_000 {
|
|
|
|
let n = n as u32;
|
|
|
|
|
|
|
|
let d10000 = (n / 10000) as u8;
|
|
|
|
let d1000 = ((n / 1000) % 10) as u8;
|
|
|
|
let d100 = ((n / 100) % 10) as u8;
|
|
|
|
let d10 = ((n / 10) % 10) as u8;
|
|
|
|
let d1 = (n % 10) as u8;
|
|
|
|
|
|
|
|
bytes.put_u8(DIGITS_START + d10000);
|
|
|
|
bytes.put_u8(DIGITS_START + d1000);
|
|
|
|
bytes.put_u8(DIGITS_START + d100);
|
|
|
|
bytes.put_u8(DIGITS_START + d10);
|
|
|
|
bytes.put_u8(DIGITS_START + d1);
|
|
|
|
} else if n < 1_000_000 {
|
|
|
|
let n = n as u32;
|
|
|
|
|
2020-02-27 02:20:30 +00:00
|
|
|
let d100000 = (n / 100_000) as u8;
|
2020-02-24 20:58:41 +00:00
|
|
|
let d10000 = ((n / 10000) % 10) as u8;
|
|
|
|
let d1000 = ((n / 1000) % 10) as u8;
|
|
|
|
let d100 = ((n / 100) % 10) as u8;
|
|
|
|
let d10 = ((n / 10) % 10) as u8;
|
|
|
|
let d1 = (n % 10) as u8;
|
|
|
|
|
|
|
|
bytes.put_u8(DIGITS_START + d100000);
|
|
|
|
bytes.put_u8(DIGITS_START + d10000);
|
2020-02-06 23:51:02 +00:00
|
|
|
bytes.put_u8(DIGITS_START + d1000);
|
|
|
|
bytes.put_u8(DIGITS_START + d100);
|
|
|
|
bytes.put_u8(DIGITS_START + d10);
|
|
|
|
bytes.put_u8(DIGITS_START + d1);
|
2017-12-26 03:42:55 +00:00
|
|
|
} else {
|
2020-05-17 23:42:51 +00:00
|
|
|
write_u64(n, bytes);
|
2017-12-26 03:42:55 +00:00
|
|
|
}
|
2020-02-06 23:51:02 +00:00
|
|
|
|
|
|
|
bytes.put_slice(b"\r\n");
|
2017-12-14 00:44:35 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 23:42:51 +00:00
|
|
|
pub(crate) fn write_u64(n: u64, bytes: &mut BytesMut) {
|
2020-02-16 15:20:25 +00:00
|
|
|
let mut n = n;
|
2020-02-24 20:58:41 +00:00
|
|
|
|
2020-05-17 23:42:51 +00:00
|
|
|
// 20 chars is max length of a u64 (2^64)
|
2020-02-16 15:20:25 +00:00
|
|
|
// digits will be added to the buffer from lsd to msd
|
|
|
|
let mut buf = BytesMut::with_capacity(20);
|
2020-02-06 23:51:02 +00:00
|
|
|
|
2020-02-16 15:20:25 +00:00
|
|
|
while n > 9 {
|
|
|
|
// "pop" the least-significant digit
|
|
|
|
let lsd = (n % 10) as u8;
|
2018-09-26 08:56:34 +00:00
|
|
|
|
2020-02-16 15:20:25 +00:00
|
|
|
// remove the lsd from n
|
2020-02-27 02:20:30 +00:00
|
|
|
n /= 10;
|
2018-09-26 08:56:34 +00:00
|
|
|
|
2020-02-16 15:20:25 +00:00
|
|
|
buf.put_u8(DIGITS_START + lsd);
|
2018-09-26 08:56:34 +00:00
|
|
|
}
|
2017-12-14 00:44:35 +00:00
|
|
|
|
2020-02-16 15:20:25 +00:00
|
|
|
// put msd to result buffer
|
|
|
|
bytes.put_u8(DIGITS_START + (n as u8));
|
2020-02-24 20:58:41 +00:00
|
|
|
|
2020-02-16 15:20:25 +00:00
|
|
|
// put, in reverse (msd to lsd), remaining digits to buffer
|
|
|
|
for i in (0..buf.len()).rev() {
|
|
|
|
bytes.put_u8(buf[i]);
|
2017-12-14 00:44:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 19:43:31 +00:00
|
|
|
pub(crate) struct Writer<'a>(pub &'a mut BytesMut);
|
|
|
|
|
|
|
|
impl<'a> io::Write for Writer<'a> {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
self.0.extend_from_slice(buf);
|
|
|
|
Ok(buf.len())
|
|
|
|
}
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-28 08:34:26 +00:00
|
|
|
pub(crate) trait DataFactory {
|
|
|
|
fn set(&self, ext: &mut Extensions);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct Data<T>(pub(crate) T);
|
|
|
|
|
|
|
|
impl<T: Clone + 'static> DataFactory for Data<T> {
|
|
|
|
fn set(&self, ext: &mut Extensions) {
|
|
|
|
ext.insert(self.0.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-26 03:42:55 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-05-03 08:37:40 +00:00
|
|
|
use std::str::from_utf8;
|
|
|
|
|
2017-12-26 03:42:55 +00:00
|
|
|
use super::*;
|
|
|
|
|
2020-05-03 08:37:40 +00:00
|
|
|
#[test]
|
|
|
|
fn test_status_line() {
|
|
|
|
let mut bytes = BytesMut::new();
|
|
|
|
bytes.reserve(50);
|
|
|
|
write_status_line(Version::HTTP_11, 200, &mut bytes);
|
|
|
|
assert_eq!(from_utf8(&bytes.split().freeze()).unwrap(), "HTTP/1.1 200 ");
|
|
|
|
|
|
|
|
let mut bytes = BytesMut::new();
|
|
|
|
bytes.reserve(50);
|
|
|
|
write_status_line(Version::HTTP_09, 404, &mut bytes);
|
|
|
|
assert_eq!(from_utf8(&bytes.split().freeze()).unwrap(), "HTTP/0.9 404 ");
|
|
|
|
|
|
|
|
let mut bytes = BytesMut::new();
|
|
|
|
bytes.reserve(50);
|
|
|
|
write_status_line(Version::HTTP_09, 515, &mut bytes);
|
|
|
|
assert_eq!(from_utf8(&bytes.split().freeze()).unwrap(), "HTTP/0.9 515 ");
|
|
|
|
}
|
|
|
|
|
2018-01-02 21:39:32 +00:00
|
|
|
#[test]
|
|
|
|
fn test_write_content_length() {
|
|
|
|
let mut bytes = BytesMut::new();
|
2018-03-24 16:22:34 +00:00
|
|
|
bytes.reserve(50);
|
2018-01-02 21:39:32 +00:00
|
|
|
write_content_length(0, &mut bytes);
|
2019-12-05 17:35:43 +00:00
|
|
|
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 0\r\n"[..]);
|
2018-03-24 16:22:34 +00:00
|
|
|
bytes.reserve(50);
|
2018-01-02 21:39:32 +00:00
|
|
|
write_content_length(9, &mut bytes);
|
2019-12-05 17:35:43 +00:00
|
|
|
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 9\r\n"[..]);
|
2018-03-24 16:22:34 +00:00
|
|
|
bytes.reserve(50);
|
2018-01-02 21:39:32 +00:00
|
|
|
write_content_length(10, &mut bytes);
|
2019-12-05 17:35:43 +00:00
|
|
|
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 10\r\n"[..]);
|
2018-03-24 16:22:34 +00:00
|
|
|
bytes.reserve(50);
|
2018-01-02 21:39:32 +00:00
|
|
|
write_content_length(99, &mut bytes);
|
2019-12-05 17:35:43 +00:00
|
|
|
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 99\r\n"[..]);
|
2018-03-24 16:22:34 +00:00
|
|
|
bytes.reserve(50);
|
2018-01-02 21:39:32 +00:00
|
|
|
write_content_length(100, &mut bytes);
|
2019-12-05 17:35:43 +00:00
|
|
|
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 100\r\n"[..]);
|
2018-03-24 16:22:34 +00:00
|
|
|
bytes.reserve(50);
|
2018-01-02 21:39:32 +00:00
|
|
|
write_content_length(101, &mut bytes);
|
2019-12-05 17:35:43 +00:00
|
|
|
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 101\r\n"[..]);
|
2018-03-24 16:22:34 +00:00
|
|
|
bytes.reserve(50);
|
2018-01-02 21:39:32 +00:00
|
|
|
write_content_length(998, &mut bytes);
|
2019-12-05 17:35:43 +00:00
|
|
|
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 998\r\n"[..]);
|
2018-03-24 16:22:34 +00:00
|
|
|
bytes.reserve(50);
|
2018-01-02 21:39:32 +00:00
|
|
|
write_content_length(1000, &mut bytes);
|
2019-12-05 17:35:43 +00:00
|
|
|
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 1000\r\n"[..]);
|
2018-03-24 16:22:34 +00:00
|
|
|
bytes.reserve(50);
|
2018-01-02 21:39:32 +00:00
|
|
|
write_content_length(1001, &mut bytes);
|
2019-12-05 17:35:43 +00:00
|
|
|
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 1001\r\n"[..]);
|
2018-03-24 16:22:34 +00:00
|
|
|
bytes.reserve(50);
|
2018-01-02 21:39:32 +00:00
|
|
|
write_content_length(5909, &mut bytes);
|
2019-12-05 17:35:43 +00:00
|
|
|
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 5909\r\n"[..]);
|
2020-02-06 23:51:02 +00:00
|
|
|
bytes.reserve(50);
|
2020-02-24 20:58:41 +00:00
|
|
|
write_content_length(9999, &mut bytes);
|
|
|
|
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 9999\r\n"[..]);
|
|
|
|
bytes.reserve(50);
|
2020-02-06 23:51:02 +00:00
|
|
|
write_content_length(10001, &mut bytes);
|
|
|
|
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 10001\r\n"[..]);
|
|
|
|
bytes.reserve(50);
|
|
|
|
write_content_length(59094, &mut bytes);
|
|
|
|
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 59094\r\n"[..]);
|
2020-02-24 20:58:41 +00:00
|
|
|
bytes.reserve(50);
|
|
|
|
write_content_length(99999, &mut bytes);
|
|
|
|
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 99999\r\n"[..]);
|
2020-02-16 15:20:25 +00:00
|
|
|
|
|
|
|
bytes.reserve(50);
|
|
|
|
write_content_length(590947, &mut bytes);
|
|
|
|
assert_eq!(
|
|
|
|
bytes.split().freeze(),
|
|
|
|
b"\r\ncontent-length: 590947\r\n"[..]
|
|
|
|
);
|
|
|
|
bytes.reserve(50);
|
2020-02-24 20:58:41 +00:00
|
|
|
write_content_length(999999, &mut bytes);
|
|
|
|
assert_eq!(
|
|
|
|
bytes.split().freeze(),
|
|
|
|
b"\r\ncontent-length: 999999\r\n"[..]
|
|
|
|
);
|
|
|
|
bytes.reserve(50);
|
2020-02-16 15:20:25 +00:00
|
|
|
write_content_length(5909471, &mut bytes);
|
|
|
|
assert_eq!(
|
|
|
|
bytes.split().freeze(),
|
|
|
|
b"\r\ncontent-length: 5909471\r\n"[..]
|
|
|
|
);
|
|
|
|
bytes.reserve(50);
|
|
|
|
write_content_length(59094718, &mut bytes);
|
|
|
|
assert_eq!(
|
|
|
|
bytes.split().freeze(),
|
|
|
|
b"\r\ncontent-length: 59094718\r\n"[..]
|
|
|
|
);
|
2020-02-24 20:58:41 +00:00
|
|
|
bytes.reserve(50);
|
|
|
|
write_content_length(4294973728, &mut bytes);
|
|
|
|
assert_eq!(
|
|
|
|
bytes.split().freeze(),
|
|
|
|
b"\r\ncontent-length: 4294973728\r\n"[..]
|
|
|
|
);
|
2018-01-02 21:39:32 +00:00
|
|
|
}
|
2017-12-14 00:44:35 +00:00
|
|
|
}
|