2022-10-09 19:43:51 +02:00
|
|
|
use actix_http::header::TryIntoHeaderPair;
|
|
|
|
use bytes::Bytes;
|
2022-10-13 15:35:31 +02:00
|
|
|
use flate2::read::{GzDecoder, ZlibDecoder};
|
2022-10-09 19:43:51 +02:00
|
|
|
use flate2::write::{GzEncoder, ZlibEncoder};
|
|
|
|
use flate2::Compression;
|
2022-10-13 00:56:57 +02:00
|
|
|
use std::io::{Read, Write};
|
2022-10-09 19:43:51 +02:00
|
|
|
|
2022-10-10 11:04:46 +02:00
|
|
|
#[derive(Clone, Copy)]
|
2022-10-09 19:43:51 +02:00
|
|
|
pub enum Encoder {
|
|
|
|
Plain,
|
|
|
|
Gzip,
|
|
|
|
Deflate,
|
|
|
|
Brotli,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Encoder {
|
|
|
|
pub fn encode(self: &Encoder, body: impl Into<Bytes>) -> impl Into<Bytes> {
|
|
|
|
match self {
|
|
|
|
Self::Gzip => {
|
|
|
|
let mut encoder = GzEncoder::new(Vec::new(), Compression::default());
|
2022-10-10 11:04:46 +02:00
|
|
|
encoder
|
|
|
|
.write_all(&body.into())
|
|
|
|
.expect("Failed to encode request body");
|
2022-10-09 19:43:51 +02:00
|
|
|
encoder.finish().expect("Failed to encode request body")
|
|
|
|
}
|
|
|
|
Self::Deflate => {
|
|
|
|
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
|
2022-10-10 11:04:46 +02:00
|
|
|
encoder
|
|
|
|
.write_all(&body.into())
|
|
|
|
.expect("Failed to encode request body");
|
2022-10-09 19:43:51 +02:00
|
|
|
encoder.finish().unwrap()
|
|
|
|
}
|
|
|
|
Self::Plain => Vec::from(body.into()),
|
|
|
|
Self::Brotli => {
|
|
|
|
let mut encoder = brotli::CompressorWriter::new(Vec::new(), 32 * 1024, 3, 22);
|
2022-10-10 11:04:46 +02:00
|
|
|
encoder
|
|
|
|
.write_all(&body.into())
|
|
|
|
.expect("Failed to encode request body");
|
2022-10-09 19:43:51 +02:00
|
|
|
encoder.flush().expect("Failed to encode request body");
|
|
|
|
encoder.into_inner()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-13 00:56:57 +02:00
|
|
|
pub fn decode(self: &Encoder, bytes: impl Into<Bytes>) -> impl Into<Bytes> {
|
|
|
|
let mut buffer = Vec::new();
|
|
|
|
let input = bytes.into();
|
|
|
|
match self {
|
|
|
|
Self::Gzip => {
|
|
|
|
GzDecoder::new(input.as_ref())
|
|
|
|
.read_to_end(&mut buffer)
|
|
|
|
.expect("Invalid gzip stream");
|
2022-10-13 15:35:31 +02:00
|
|
|
}
|
2022-10-13 00:56:57 +02:00
|
|
|
Self::Deflate => {
|
|
|
|
ZlibDecoder::new(input.as_ref())
|
|
|
|
.read_to_end(&mut buffer)
|
|
|
|
.expect("Invalid zlib stream");
|
2022-10-13 15:35:31 +02:00
|
|
|
}
|
2022-10-13 00:56:57 +02:00
|
|
|
Self::Plain => {
|
|
|
|
buffer
|
2022-10-13 15:35:31 +02:00
|
|
|
.write_all(input.as_ref())
|
|
|
|
.expect("Unexpected memory copying issue");
|
|
|
|
}
|
2022-10-13 00:56:57 +02:00
|
|
|
Self::Brotli => {
|
2022-10-13 15:35:31 +02:00
|
|
|
brotli::Decompressor::new(input.as_ref(), 4096)
|
2022-10-13 00:56:57 +02:00
|
|
|
.read_to_end(&mut buffer)
|
|
|
|
.expect("Invalid brotli stream");
|
2022-10-13 15:35:31 +02:00
|
|
|
}
|
2022-10-13 00:56:57 +02:00
|
|
|
};
|
|
|
|
buffer
|
|
|
|
}
|
|
|
|
|
2022-10-09 19:43:51 +02:00
|
|
|
pub fn header(self: &Encoder) -> Option<impl TryIntoHeaderPair> {
|
|
|
|
match self {
|
|
|
|
Self::Plain => None,
|
|
|
|
Self::Gzip => Some(("Content-Encoding", "gzip")),
|
|
|
|
Self::Deflate => Some(("Content-Encoding", "deflate")),
|
|
|
|
Self::Brotli => Some(("Content-Encoding", "br")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn iterator() -> impl Iterator<Item = Self> {
|
2022-10-10 11:04:46 +02:00
|
|
|
[Self::Plain, Self::Gzip, Self::Deflate, Self::Brotli]
|
|
|
|
.iter()
|
|
|
|
.copied()
|
2022-10-09 19:43:51 +02:00
|
|
|
}
|
|
|
|
}
|