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
use crate::errors::{RemoteError, TimeoutError};
use async_compression::stream::{GzipDecoder, ZlibDecoder};
use bytes::{Buf, Bytes};
use conjure_error::Error;
use futures::task::Context;
use futures::{ready, FutureExt, Stream, StreamExt, TryStreamExt};
use hyper::http::header::CONTENT_ENCODING;
use hyper::{HeaderMap, StatusCode};
use std::io;
use std::mem;
use std::mem::MaybeUninit;
use std::pin::Pin;
use std::task::Poll;
use std::time::Instant;
use tokio::io::{AsyncBufRead, AsyncRead, AsyncReadExt};
use tokio::time::{self, Delay};
use witchcraft_log::info;
use zipkin::{Detached, OpenSpan};
pub struct Response {
status: StatusCode,
headers: HeaderMap,
body: ResponseBody,
}
impl Response {
pub(crate) fn new(
response: hyper::Response<hyper::Body>,
deadline: Instant,
span: OpenSpan<Detached>,
) -> Result<Response, Error> {
let (parts, body) = response.into_parts();
let body = ResponseBody::new(&parts.headers, body, deadline, span)?;
Ok(Response {
status: parts.status,
headers: parts.headers,
body,
})
}
pub(crate) async fn into_error(self, propagate_service_errors: bool) -> Error {
let status = self.status();
let mut buf = vec![];
if let Err(e) = self.into_body().take(10 * 1024).read_to_end(&mut buf).await {
info!(
"error reading response body",
error: Error::internal_safe(e),
);
}
let error = RemoteError {
status,
error: conjure_serde::json::client_from_slice(&buf).ok(),
};
let log_body = error.error.is_none();
let mut error = match &error.error {
Some(e) if propagate_service_errors => {
let e = e.clone();
Error::service_safe(error, e)
}
_ => Error::internal_safe(error),
};
if log_body {
error = error.with_unsafe_param("body", String::from_utf8_lossy(&buf));
}
error
}
pub fn status(&self) -> StatusCode {
self.status
}
pub fn headers(&self) -> &HeaderMap {
&self.headers
}
pub fn into_body(self) -> ResponseBody {
self.body
}
}
pub struct ResponseBody {
stream: Pin<Box<dyn Stream<Item = io::Result<Bytes>> + Sync + Send>>,
cur: Bytes,
}
impl ResponseBody {
#[allow(clippy::borrow_interior_mutable_const)]
fn new(
headers: &HeaderMap,
body: hyper::Body,
deadline: Instant,
span: OpenSpan<Detached>,
) -> Result<ResponseBody, Error> {
let body = IdentityBody {
body,
deadline: time::delay_until(tokio::time::Instant::from_std(deadline)),
_span: span,
};
let stream: Pin<Box<dyn Stream<Item = io::Result<Bytes>> + Sync + Send>> =
match headers.get(&CONTENT_ENCODING) {
Some(v) if v == "gzip" => Box::pin(GzipDecoder::new(body)),
Some(v) if v == "deflate" => Box::pin(ZlibDecoder::new(body)),
Some(v) if v == "identity" => Box::pin(body),
None => Box::pin(body),
Some(v) => {
return Err(Error::internal_safe("unsupported Content-Encoding")
.with_safe_param("encoding", format!("{:?}", v)));
}
};
Ok(ResponseBody {
stream,
cur: Bytes::new(),
})
}
pub async fn read_bytes(&mut self) -> io::Result<Option<Bytes>> {
if self.cur.has_remaining() {
Ok(Some(mem::replace(&mut self.cur, Bytes::new())))
} else {
self.stream.try_next().await
}
}
}
impl AsyncRead for ResponseBody {
unsafe fn prepare_uninitialized_buffer(&self, _: &mut [MaybeUninit<u8>]) -> bool {
false
}
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
let read_buf = ready!(self.as_mut().poll_fill_buf(cx))?;
let nread = usize::min(buf.len(), read_buf.len());
buf[..nread].copy_from_slice(&read_buf[..nread]);
self.consume(nread);
Poll::Ready(Ok(nread))
}
}
impl AsyncBufRead for ResponseBody {
fn poll_fill_buf(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
while !self.cur.has_remaining() {
match ready!(self.stream.poll_next_unpin(cx)).transpose()? {
Some(bytes) => self.cur = bytes,
None => break,
}
}
Poll::Ready(Ok(&self.get_mut().cur))
}
fn consume(mut self: Pin<&mut Self>, amt: usize) {
self.cur.advance(amt);
}
}
struct IdentityBody {
body: hyper::Body,
deadline: Delay,
_span: OpenSpan<Detached>,
}
impl Stream for IdentityBody {
type Item = io::Result<Bytes>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
if let Poll::Ready(()) = self.deadline.poll_unpin(cx) {
return Poll::Ready(Some(Err(io::Error::new(
io::ErrorKind::TimedOut,
TimeoutError(()),
))));
}
match self.body.poll_next_unpin(cx) {
Poll::Ready(Some(Ok(chunk))) => Poll::Ready(Some(Ok(chunk))),
Poll::Ready(Some(Err(e))) => {
Poll::Ready(Some(Err(io::Error::new(io::ErrorKind::Other, e))))
}
Poll::Ready(None) => Poll::Ready(None),
Poll::Pending => Poll::Pending,
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.body.size_hint()
}
}