1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 08:22:59 +01:00
actix-web/actix-http/src/helpers.rs

224 lines
6.4 KiB
Rust
Raw Normal View History

use std::{io, ptr};
2017-12-26 04:42:55 +01:00
use bytes::{BufMut, BytesMut};
2017-12-25 22:40:06 +01:00
use http::Version;
2017-12-14 01:44:35 +01:00
2019-06-28 10:34:26 +02:00
use crate::extensions::Extensions;
2018-04-14 01:02:01 +02:00
const DEC_DIGITS_LUT: &[u8] = b"0001020304050607080910111213141516171819\
2017-12-14 01:44:35 +01:00
2021222324252627282930313233343536373839\
4041424344454647484950515253545556575859\
6061626364656667686970717273747576777879\
8081828384858687888990919293949596979899";
pub(crate) const STATUS_LINE_BUF_SIZE: usize = 13;
2017-12-25 22:40:06 +01:00
pub(crate) fn write_status_line(version: Version, mut n: u16, bytes: &mut BytesMut) {
let mut buf: [u8; STATUS_LINE_BUF_SIZE] = *b"HTTP/1.1 ";
2017-12-25 22:40:06 +01:00
match version {
Version::HTTP_2 => buf[5] = b'2',
Version::HTTP_10 => buf[7] = b'0',
2018-04-14 01:02:01 +02:00
Version::HTTP_09 => {
buf[5] = b'0';
buf[7] = b'9';
}
2017-12-25 22:40:06 +01:00
_ => (),
}
2017-12-26 04:42:55 +01:00
let mut curr: isize = 12;
2017-12-14 01:44:35 +01:00
let buf_ptr = buf.as_mut_ptr();
let lut_ptr = DEC_DIGITS_LUT.as_ptr();
2017-12-26 04:42:55 +01:00
let four = n > 999;
2017-12-14 01:44:35 +01:00
2018-09-26 10:56:34 +02:00
// decode 2 more chars, if > 2 chars
let d1 = (n % 100) << 1;
n /= 100;
curr -= 2;
2017-12-14 01:44:35 +01:00
unsafe {
2017-12-26 04:42:55 +01:00
ptr::copy_nonoverlapping(lut_ptr.offset(d1 as isize), buf_ptr.offset(curr), 2);
2018-09-26 10:56:34 +02:00
}
2017-12-14 01:44:35 +01:00
2018-09-26 10:56:34 +02:00
// decode last 1 or 2 chars
if n < 10 {
curr -= 1;
unsafe {
2017-12-14 01:44:35 +01:00
*buf_ptr.offset(curr) = (n as u8) + b'0';
2018-09-26 10:56:34 +02:00
}
} else {
let d1 = n << 1;
curr -= 2;
unsafe {
2018-04-14 01:02:01 +02:00
ptr::copy_nonoverlapping(
lut_ptr.offset(d1 as isize),
buf_ptr.offset(curr),
2,
);
2017-12-14 01:44:35 +01:00
}
}
2018-03-18 19:05:44 +01:00
bytes.put_slice(&buf);
2017-12-26 04:42:55 +01:00
if four {
2019-12-05 18:35:43 +01:00
bytes.put_u8(b' ');
2017-12-26 04:42:55 +01:00
}
}
const DIGITS_START: u8 = b'0';
/// NOTE: bytes object has to contain enough space
pub fn write_content_length(n: usize, bytes: &mut BytesMut) {
bytes.put_slice(b"\r\ncontent-length: ");
2017-12-26 04:42:55 +01:00
if n < 10 {
bytes.put_u8(DIGITS_START + (n as u8));
2017-12-26 04:42:55 +01:00
} else if n < 100 {
let n = n as u8;
2017-12-26 04:42:55 +01:00
let d10 = n / 10;
let d1 = n % 10;
2017-12-26 04:42:55 +01: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);
} else if n < 10000 {
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;
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 04:42:55 +01:00
} else {
write_usize(n, bytes);
2017-12-26 04:42:55 +01:00
}
bytes.put_slice(b"\r\n");
2017-12-14 01:44:35 +01:00
}
pub(crate) fn write_usize(n: usize, bytes: &mut BytesMut) {
let mut n = n;
// 20 chars is max length of a usize (2^64)
// digits will be added to the buffer from lsd to msd
let mut buf = BytesMut::with_capacity(20);
while n > 9 {
// "pop" the least-significant digit
let lsd = (n % 10) as u8;
2018-09-26 10:56:34 +02:00
// remove the lsd from n
n = n / 10;
2018-09-26 10:56:34 +02:00
buf.put_u8(DIGITS_START + lsd);
2018-09-26 10:56:34 +02:00
}
2017-12-14 01:44:35 +01:00
// put msd to result buffer
bytes.put_u8(DIGITS_START + (n as u8));
// 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 01:44:35 +01: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 10:34:26 +02: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 04:42:55 +01:00
#[cfg(test)]
mod tests {
use super::*;
2018-01-02 22:39:32 +01:00
#[test]
fn test_write_content_length() {
let mut bytes = BytesMut::new();
bytes.reserve(50);
2018-01-02 22:39:32 +01:00
write_content_length(0, &mut bytes);
2019-12-05 18:35:43 +01:00
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 0\r\n"[..]);
bytes.reserve(50);
2018-01-02 22:39:32 +01:00
write_content_length(9, &mut bytes);
2019-12-05 18:35:43 +01:00
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 9\r\n"[..]);
bytes.reserve(50);
2018-01-02 22:39:32 +01:00
write_content_length(10, &mut bytes);
2019-12-05 18:35:43 +01:00
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 10\r\n"[..]);
bytes.reserve(50);
2018-01-02 22:39:32 +01:00
write_content_length(99, &mut bytes);
2019-12-05 18:35:43 +01:00
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 99\r\n"[..]);
bytes.reserve(50);
2018-01-02 22:39:32 +01:00
write_content_length(100, &mut bytes);
2019-12-05 18:35:43 +01:00
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 100\r\n"[..]);
bytes.reserve(50);
2018-01-02 22:39:32 +01:00
write_content_length(101, &mut bytes);
2019-12-05 18:35:43 +01:00
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 101\r\n"[..]);
bytes.reserve(50);
2018-01-02 22:39:32 +01:00
write_content_length(998, &mut bytes);
2019-12-05 18:35:43 +01:00
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 998\r\n"[..]);
bytes.reserve(50);
2018-01-02 22:39:32 +01:00
write_content_length(1000, &mut bytes);
2019-12-05 18:35:43 +01:00
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 1000\r\n"[..]);
bytes.reserve(50);
2018-01-02 22:39:32 +01:00
write_content_length(1001, &mut bytes);
2019-12-05 18:35:43 +01:00
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 1001\r\n"[..]);
bytes.reserve(50);
2018-01-02 22:39:32 +01:00
write_content_length(5909, &mut bytes);
2019-12-05 18:35:43 +01:00
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 5909\r\n"[..]);
bytes.reserve(50);
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"[..]);
bytes.reserve(50);
write_content_length(590947, &mut bytes);
assert_eq!(
bytes.split().freeze(),
b"\r\ncontent-length: 590947\r\n"[..]
);
bytes.reserve(50);
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"[..]
);
2018-01-02 22:39:32 +01:00
}
2017-12-14 01:44:35 +01:00
}