1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-01-19 22:24:40 +01:00
actix-web/actix-http/src/helpers.rs

165 lines
5.3 KiB
Rust
Raw Normal View History

use std::io;
use bytes::BufMut;
2017-12-25 13:40:06 -08:00
use http::Version;
2017-12-13 16:44:35 -08:00
const DIGITS_START: u8 = b'0';
pub(crate) fn write_status_line<B: BufMut>(version: Version, n: u16, buf: &mut B) {
2017-12-25 13:40:06 -08:00
match version {
Version::HTTP_11 => buf.put_slice(b"HTTP/1.1 "),
Version::HTTP_10 => buf.put_slice(b"HTTP/1.0 "),
Version::HTTP_09 => buf.put_slice(b"HTTP/0.9 "),
_ => {
// other HTTP version handlers do not use this method
2018-04-13 16:02:01 -07:00
}
2017-12-25 13:40:06 -08:00
}
let d100 = (n / 100) as u8;
let d10 = ((n / 10) % 10) as u8;
let d1 = (n % 10) as u8;
2017-12-13 16:44:35 -08:00
buf.put_u8(DIGITS_START + d100);
buf.put_u8(DIGITS_START + d10);
buf.put_u8(DIGITS_START + d1);
2017-12-13 16:44:35 -08:00
// trailing space before reason
buf.put_u8(b' ');
2017-12-25 19:42:55 -08:00
}
/// NOTE: bytes object has to contain enough space
pub fn write_content_length<B: BufMut>(n: u64, buf: &mut B) {
if n == 0 {
buf.put_slice(b"\r\ncontent-length: 0\r\n");
return;
2017-12-25 19:42:55 -08:00
}
let mut buffer = itoa::Buffer::new();
2018-09-26 20:56:34 +12:00
buf.put_slice(b"\r\ncontent-length: ");
buf.put_slice(buffer.format(n).as_bytes());
buf.put_slice(b"\r\n");
2017-12-13 16:44:35 -08:00
}
2021-04-19 21:12:52 +01:00
// TODO: bench why this is needed
pub(crate) struct Writer<'a, B>(pub &'a mut B);
impl<'a, B> io::Write for Writer<'a, B>
where
B: BufMut,
{
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.put_slice(buf);
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
2017-12-25 19:42:55 -08:00
#[cfg(test)]
mod tests {
use std::str::from_utf8;
use bytes::BytesMut;
2017-12-25 19:42:55 -08:00
use super::*;
#[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 13:39:32 -08:00
#[test]
fn test_write_content_length() {
let mut bytes = BytesMut::new();
bytes.reserve(50);
2018-01-02 13:39:32 -08:00
write_content_length(0, &mut bytes);
2019-12-05 23:35:43 +06:00
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 0\r\n"[..]);
bytes.reserve(50);
2018-01-02 13:39:32 -08:00
write_content_length(9, &mut bytes);
2019-12-05 23:35:43 +06:00
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 9\r\n"[..]);
bytes.reserve(50);
2018-01-02 13:39:32 -08:00
write_content_length(10, &mut bytes);
2019-12-05 23:35:43 +06:00
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 10\r\n"[..]);
bytes.reserve(50);
2018-01-02 13:39:32 -08:00
write_content_length(99, &mut bytes);
2019-12-05 23:35:43 +06:00
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 99\r\n"[..]);
bytes.reserve(50);
2018-01-02 13:39:32 -08:00
write_content_length(100, &mut bytes);
2019-12-05 23:35:43 +06:00
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 100\r\n"[..]);
bytes.reserve(50);
2018-01-02 13:39:32 -08:00
write_content_length(101, &mut bytes);
2019-12-05 23:35:43 +06:00
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 101\r\n"[..]);
bytes.reserve(50);
2018-01-02 13:39:32 -08:00
write_content_length(998, &mut bytes);
2019-12-05 23:35:43 +06:00
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 998\r\n"[..]);
bytes.reserve(50);
2018-01-02 13:39:32 -08:00
write_content_length(1000, &mut bytes);
2019-12-05 23:35:43 +06:00
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 1000\r\n"[..]);
bytes.reserve(50);
2018-01-02 13:39:32 -08:00
write_content_length(1001, &mut bytes);
2019-12-05 23:35:43 +06:00
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 1001\r\n"[..]);
bytes.reserve(50);
2018-01-02 13:39:32 -08:00
write_content_length(5909, &mut bytes);
2019-12-05 23:35:43 +06:00
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 5909\r\n"[..]);
bytes.reserve(50);
write_content_length(9999, &mut bytes);
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 9999\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(99999, &mut bytes);
assert_eq!(bytes.split().freeze(), b"\r\ncontent-length: 99999\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(999999, &mut bytes);
assert_eq!(
bytes.split().freeze(),
b"\r\ncontent-length: 999999\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"[..]
);
bytes.reserve(50);
write_content_length(4294973728, &mut bytes);
assert_eq!(
bytes.split().freeze(),
b"\r\ncontent-length: 4294973728\r\n"[..]
);
2018-01-02 13:39:32 -08:00
}
2017-12-13 16:44:35 -08:00
}