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
use super::encode::BUFFER_SIZE;
use crate::{metadata::MetadataValue, Status};
use bytes::{Buf, BufMut, BytesMut};
use flate2::read::{GzDecoder, GzEncoder};
use std::fmt;
pub(crate) const ENCODING_HEADER: &str = "grpc-encoding";
pub(crate) const ACCEPT_ENCODING_HEADER: &str = "grpc-accept-encoding";
#[derive(Debug, Default, Clone, Copy)]
pub struct EnabledCompressionEncodings {
pub(crate) gzip: bool,
}
impl EnabledCompressionEncodings {
pub fn gzip(self) -> bool {
self.gzip
}
pub fn enable_gzip(&mut self) {
self.gzip = true;
}
pub(crate) fn into_accept_encoding_header_value(self) -> Option<http::HeaderValue> {
let Self { gzip } = self;
if gzip {
Some(http::HeaderValue::from_static("gzip,identity"))
} else {
None
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum CompressionEncoding {
#[allow(missing_docs)]
Gzip,
}
impl CompressionEncoding {
pub(crate) fn from_accept_encoding_header(
map: &http::HeaderMap,
enabled_encodings: EnabledCompressionEncodings,
) -> Option<Self> {
let header_value = map.get(ACCEPT_ENCODING_HEADER)?;
let header_value_str = header_value.to_str().ok()?;
let EnabledCompressionEncodings { gzip } = enabled_encodings;
split_by_comma(header_value_str).find_map(|value| match value {
"gzip" if gzip => Some(CompressionEncoding::Gzip),
_ => None,
})
}
pub(crate) fn from_encoding_header(
map: &http::HeaderMap,
enabled_encodings: EnabledCompressionEncodings,
) -> Result<Option<Self>, Status> {
let header_value = if let Some(value) = map.get(ENCODING_HEADER) {
value
} else {
return Ok(None);
};
let header_value_str = if let Ok(value) = header_value.to_str() {
value
} else {
return Ok(None);
};
let EnabledCompressionEncodings { gzip } = enabled_encodings;
match header_value_str {
"gzip" if gzip => Ok(Some(CompressionEncoding::Gzip)),
other => {
let mut status = Status::unimplemented(format!(
"Content is compressed with `{}` which isn't supported",
other
));
let header_value = enabled_encodings
.into_accept_encoding_header_value()
.map(MetadataValue::unchecked_from_header_value)
.unwrap_or_else(|| MetadataValue::from_static("identity"));
status
.metadata_mut()
.insert(ACCEPT_ENCODING_HEADER, header_value);
Err(status)
}
}
}
pub(crate) fn into_header_value(self) -> http::HeaderValue {
match self {
CompressionEncoding::Gzip => http::HeaderValue::from_static("gzip"),
}
}
}
impl fmt::Display for CompressionEncoding {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CompressionEncoding::Gzip => write!(f, "gzip"),
}
}
}
fn split_by_comma(s: &str) -> impl Iterator<Item = &str> {
s.trim().split(',').map(|s| s.trim())
}
pub(crate) fn compress(
encoding: CompressionEncoding,
decompressed_buf: &mut BytesMut,
out_buf: &mut BytesMut,
len: usize,
) -> Result<(), std::io::Error> {
let capacity = ((len / BUFFER_SIZE) + 1) * BUFFER_SIZE;
out_buf.reserve(capacity);
match encoding {
CompressionEncoding::Gzip => {
let mut gzip_encoder = GzEncoder::new(
&decompressed_buf[0..len],
flate2::Compression::new(6),
);
let mut out_writer = out_buf.writer();
std::io::copy(&mut gzip_encoder, &mut out_writer)?;
}
}
decompressed_buf.advance(len);
Ok(())
}
pub(crate) fn decompress(
encoding: CompressionEncoding,
compressed_buf: &mut BytesMut,
out_buf: &mut BytesMut,
len: usize,
) -> Result<(), std::io::Error> {
let estimate_decompressed_len = len * 2;
let capacity = ((estimate_decompressed_len / BUFFER_SIZE) + 1) * BUFFER_SIZE;
out_buf.reserve(capacity);
match encoding {
CompressionEncoding::Gzip => {
let mut gzip_decoder = GzDecoder::new(&compressed_buf[0..len]);
let mut out_writer = out_buf.writer();
std::io::copy(&mut gzip_decoder, &mut out_writer)?;
}
}
compressed_buf.advance(len);
Ok(())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum SingleMessageCompressionOverride {
Inherit,
Disable,
}
impl Default for SingleMessageCompressionOverride {
fn default() -> Self {
Self::Inherit
}
}