-
Notifications
You must be signed in to change notification settings - Fork 1k
/
call.rs
307 lines (256 loc) · 8.83 KB
/
call.rs
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
use std::error::Error;
use std::pin::Pin;
use std::task::{Context, Poll};
use base64::Engine as _;
use bytes::{Buf, BufMut, Bytes, BytesMut};
use futures_core::{ready, Stream};
use http::{header, HeaderMap, HeaderValue};
use http_body::{Body, SizeHint};
use pin_project::pin_project;
use tonic::Status;
use self::content_types::*;
pub(crate) mod content_types {
use http::{header::CONTENT_TYPE, HeaderMap};
pub(crate) const GRPC_WEB: &str = "application/grpc-web";
pub(crate) const GRPC_WEB_PROTO: &str = "application/grpc-web+proto";
pub(crate) const GRPC_WEB_TEXT: &str = "application/grpc-web-text";
pub(crate) const GRPC_WEB_TEXT_PROTO: &str = "application/grpc-web-text+proto";
pub(crate) fn is_grpc_web(headers: &HeaderMap) -> bool {
matches!(
content_type(headers),
Some(GRPC_WEB) | Some(GRPC_WEB_PROTO) | Some(GRPC_WEB_TEXT) | Some(GRPC_WEB_TEXT_PROTO)
)
}
fn content_type(headers: &HeaderMap) -> Option<&str> {
headers.get(CONTENT_TYPE).and_then(|val| val.to_str().ok())
}
}
const BUFFER_SIZE: usize = 8 * 1024;
const FRAME_HEADER_SIZE: usize = 5;
// 8th (MSB) bit of the 1st gRPC frame byte
// denotes an uncompressed trailer (as part of the body)
const GRPC_WEB_TRAILERS_BIT: u8 = 0b10000000;
#[derive(Copy, Clone, PartialEq, Debug)]
enum Direction {
Request,
Response,
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub(crate) enum Encoding {
Base64,
None,
}
#[pin_project]
pub(crate) struct GrpcWebCall<B> {
#[pin]
inner: B,
buf: BytesMut,
direction: Direction,
encoding: Encoding,
poll_trailers: bool,
}
impl<B> GrpcWebCall<B> {
pub(crate) fn request(inner: B, encoding: Encoding) -> Self {
Self::new(inner, Direction::Request, encoding)
}
pub(crate) fn response(inner: B, encoding: Encoding) -> Self {
Self::new(inner, Direction::Response, encoding)
}
fn new(inner: B, direction: Direction, encoding: Encoding) -> Self {
GrpcWebCall {
inner,
buf: BytesMut::with_capacity(match (direction, encoding) {
(Direction::Response, Encoding::Base64) => BUFFER_SIZE,
_ => 0,
}),
direction,
encoding,
poll_trailers: true,
}
}
// This is to avoid passing a slice of bytes with a length that the base64
// decoder would consider invalid.
#[inline]
fn max_decodable(&self) -> usize {
(self.buf.len() / 4) * 4
}
fn decode_chunk(mut self: Pin<&mut Self>) -> Result<Option<Bytes>, Status> {
// not enough bytes to decode
if self.buf.is_empty() || self.buf.len() < 4 {
return Ok(None);
}
// Split `buf` at the largest index that is multiple of 4. Decode the
// returned `Bytes`, keeping the rest for the next attempt to decode.
let index = self.max_decodable();
crate::util::base64::STANDARD
.decode(self.as_mut().project().buf.split_to(index))
.map(|decoded| Some(Bytes::from(decoded)))
.map_err(internal_error)
}
}
impl<B> GrpcWebCall<B>
where
B: Body<Data = Bytes>,
B::Error: Error,
{
fn poll_decode(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<B::Data, Status>>> {
match self.encoding {
Encoding::Base64 => loop {
if let Some(bytes) = self.as_mut().decode_chunk()? {
return Poll::Ready(Some(Ok(bytes)));
}
let mut this = self.as_mut().project();
match ready!(this.inner.as_mut().poll_data(cx)) {
Some(Ok(data)) => this.buf.put(data),
Some(Err(e)) => return Poll::Ready(Some(Err(internal_error(e)))),
None => {
return if this.buf.has_remaining() {
Poll::Ready(Some(Err(internal_error("malformed base64 request"))))
} else {
Poll::Ready(None)
}
}
}
},
Encoding::None => match ready!(self.project().inner.poll_data(cx)) {
Some(res) => Poll::Ready(Some(res.map_err(internal_error))),
None => Poll::Ready(None),
},
}
}
fn poll_encode(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<B::Data, Status>>> {
let mut this = self.as_mut().project();
if let Some(mut res) = ready!(this.inner.as_mut().poll_data(cx)) {
if *this.encoding == Encoding::Base64 {
res = res.map(|b| crate::util::base64::STANDARD.encode(b).into())
}
return Poll::Ready(Some(res.map_err(internal_error)));
}
// this flag is needed because the inner stream never
// returns Poll::Ready(None) when polled for trailers
if *this.poll_trailers {
return match ready!(this.inner.poll_trailers(cx)) {
Ok(Some(map)) => {
let mut frame = make_trailers_frame(map);
if *this.encoding == Encoding::Base64 {
frame = crate::util::base64::STANDARD.encode(frame).into_bytes();
}
*this.poll_trailers = false;
Poll::Ready(Some(Ok(frame.into())))
}
Ok(None) => Poll::Ready(None),
Err(e) => Poll::Ready(Some(Err(internal_error(e)))),
};
}
Poll::Ready(None)
}
}
impl<B> Body for GrpcWebCall<B>
where
B: Body<Data = Bytes>,
B::Error: Error,
{
type Data = Bytes;
type Error = Status;
fn poll_data(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Self::Data, Self::Error>>> {
match self.direction {
Direction::Request => self.poll_decode(cx),
Direction::Response => self.poll_encode(cx),
}
}
fn poll_trailers(
self: Pin<&mut Self>,
_: &mut Context<'_>,
) -> Poll<Result<Option<HeaderMap<HeaderValue>>, Self::Error>> {
Poll::Ready(Ok(None))
}
fn is_end_stream(&self) -> bool {
self.inner.is_end_stream()
}
fn size_hint(&self) -> SizeHint {
self.inner.size_hint()
}
}
impl<B> Stream for GrpcWebCall<B>
where
B: Body<Data = Bytes>,
B::Error: Error,
{
type Item = Result<Bytes, Status>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Body::poll_data(self, cx)
}
}
impl Encoding {
pub(crate) fn from_content_type(headers: &HeaderMap) -> Encoding {
Self::from_header(headers.get(header::CONTENT_TYPE))
}
pub(crate) fn from_accept(headers: &HeaderMap) -> Encoding {
Self::from_header(headers.get(header::ACCEPT))
}
pub(crate) fn to_content_type(self) -> &'static str {
match self {
Encoding::Base64 => GRPC_WEB_TEXT_PROTO,
Encoding::None => GRPC_WEB_PROTO,
}
}
fn from_header(value: Option<&HeaderValue>) -> Encoding {
match value.and_then(|val| val.to_str().ok()) {
Some(GRPC_WEB_TEXT_PROTO) | Some(GRPC_WEB_TEXT) => Encoding::Base64,
_ => Encoding::None,
}
}
}
fn internal_error(e: impl std::fmt::Display) -> Status {
Status::internal(format!("tonic-web: {}", e))
}
// Key-value pairs encoded as a HTTP/1 headers block (without the terminating newline)
fn encode_trailers(trailers: HeaderMap) -> Vec<u8> {
trailers.iter().fold(Vec::new(), |mut acc, (key, value)| {
acc.put_slice(key.as_ref());
acc.push(b':');
acc.put_slice(value.as_bytes());
acc.put_slice(b"\r\n");
acc
})
}
fn make_trailers_frame(trailers: HeaderMap) -> Vec<u8> {
let trailers = encode_trailers(trailers);
let len = trailers.len();
assert!(len <= u32::MAX as usize);
let mut frame = Vec::with_capacity(len + FRAME_HEADER_SIZE);
frame.push(GRPC_WEB_TRAILERS_BIT);
frame.put_u32(len as u32);
frame.extend(trailers);
frame
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn encoding_constructors() {
let cases = &[
(GRPC_WEB, Encoding::None),
(GRPC_WEB_PROTO, Encoding::None),
(GRPC_WEB_TEXT, Encoding::Base64),
(GRPC_WEB_TEXT_PROTO, Encoding::Base64),
("foo", Encoding::None),
];
let mut headers = HeaderMap::new();
for case in cases {
headers.insert(header::CONTENT_TYPE, case.0.parse().unwrap());
headers.insert(header::ACCEPT, case.0.parse().unwrap());
assert_eq!(Encoding::from_content_type(&headers), case.1, "{}", case.0);
assert_eq!(Encoding::from_accept(&headers), case.1, "{}", case.0);
}
}
}