forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.rs
More file actions
363 lines (321 loc) · 8.54 KB
/
http.rs
File metadata and controls
363 lines (321 loc) · 8.54 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
use crate::{
constants::get_default_user_agent,
log,
util::errors::{self, WrappedError},
};
use async_trait::async_trait;
use core::panic;
use futures::stream::TryStreamExt;
use hyper::{
header::{HeaderName, CONTENT_LENGTH},
http::HeaderValue,
HeaderMap, StatusCode,
};
use serde::de::DeserializeOwned;
use std::{io, pin::Pin, str::FromStr, task::Poll};
use tokio::{
fs,
io::{AsyncRead, AsyncReadExt},
sync::mpsc,
};
use tokio_util::compat::FuturesAsyncReadCompatExt;
use super::{
errors::{wrap, AnyError, StatusError},
io::{copy_async_progress, ReadBuffer, ReportCopyProgress},
};
pub async fn download_into_file<T>(
filename: &std::path::Path,
progress: T,
mut res: SimpleResponse,
) -> Result<fs::File, WrappedError>
where
T: ReportCopyProgress,
{
let mut file = fs::File::create(filename)
.await
.map_err(|e| errors::wrap(e, "failed to create file"))?;
let content_length = res
.headers
.get(CONTENT_LENGTH)
.and_then(|h| h.to_str().ok())
.and_then(|s| s.parse::<u64>().ok())
.unwrap_or(0);
copy_async_progress(progress, &mut res.read, &mut file, content_length)
.await
.map_err(|e| errors::wrap(e, "failed to download file"))?;
Ok(file)
}
pub struct SimpleResponse {
pub status_code: StatusCode,
pub headers: HeaderMap,
pub read: Pin<Box<dyn Send + AsyncRead + 'static>>,
pub url: String,
}
impl SimpleResponse {
pub fn generic_error(url: String) -> Self {
let (_, rx) = mpsc::unbounded_channel();
SimpleResponse {
url,
status_code: StatusCode::INTERNAL_SERVER_ERROR,
headers: HeaderMap::new(),
read: Box::pin(DelegatedReader::new(rx)),
}
}
/// Converts the response into a StatusError
pub async fn into_err(mut self) -> StatusError {
let mut body = String::new();
self.read.read_to_string(&mut body).await.ok();
StatusError {
url: self.url,
status_code: self.status_code.as_u16(),
body,
}
}
/// Deserializes the response body as JSON
pub async fn json<T: DeserializeOwned>(&mut self) -> Result<T, AnyError> {
let mut buf = vec![];
// ideally serde would deserialize a stream, but it does not appear that
// is supported. reqwest itself reads and decodes separately like we do here:
self.read
.read_to_end(&mut buf)
.await
.map_err(|e| wrap(e, "error reading response"))?;
let t = serde_json::from_slice(&buf)
.map_err(|e| wrap(e, format!("error decoding json from {}", self.url)))?;
Ok(t)
}
}
/// *Very* simple HTTP implementation. In most cases, this will just delegate to
/// the request library on the server (i.e. `reqwest`) but it can also be used
/// to make update/download requests on the client rather than the server,
/// similar to SSH's `remote.SSH.localServerDownload` setting.
#[async_trait]
pub trait SimpleHttp {
async fn make_request(
&self,
method: &'static str,
url: String,
) -> Result<SimpleResponse, AnyError>;
}
// Implementation of SimpleHttp that uses a reqwest client.
#[derive(Clone)]
pub struct ReqwestSimpleHttp {
client: reqwest::Client,
}
impl ReqwestSimpleHttp {
pub fn new() -> Self {
Self {
client: reqwest::ClientBuilder::new()
.user_agent(get_default_user_agent())
.build()
.unwrap(),
}
}
pub fn with_client(client: reqwest::Client) -> Self {
Self { client }
}
}
impl Default for ReqwestSimpleHttp {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl SimpleHttp for ReqwestSimpleHttp {
async fn make_request(
&self,
method: &'static str,
url: String,
) -> Result<SimpleResponse, AnyError> {
let res = self
.client
.request(reqwest::Method::try_from(method).unwrap(), &url)
.send()
.await?;
Ok(SimpleResponse {
status_code: res.status(),
headers: res.headers().clone(),
url,
read: Box::pin(
res.bytes_stream()
.map_err(|e| futures::io::Error::new(futures::io::ErrorKind::Other, e))
.into_async_read()
.compat(),
),
})
}
}
enum DelegatedHttpEvent {
InitResponse {
status_code: u16,
headers: Vec<(String, String)>,
},
Body(Vec<u8>),
End,
}
// Handle for a delegated request that allows manually issuing and response.
pub struct DelegatedHttpRequest {
pub method: &'static str,
pub url: String,
ch: mpsc::UnboundedSender<DelegatedHttpEvent>,
}
impl DelegatedHttpRequest {
pub fn initial_response(&self, status_code: u16, headers: Vec<(String, String)>) {
self.ch
.send(DelegatedHttpEvent::InitResponse {
status_code,
headers,
})
.ok();
}
pub fn body(&self, chunk: Vec<u8>) {
self.ch.send(DelegatedHttpEvent::Body(chunk)).ok();
}
pub fn end(self) {}
}
impl Drop for DelegatedHttpRequest {
fn drop(&mut self) {
self.ch.send(DelegatedHttpEvent::End).ok();
}
}
/// Implementation of SimpleHttp that allows manually controlling responses.
#[derive(Clone)]
pub struct DelegatedSimpleHttp {
start_request: mpsc::Sender<DelegatedHttpRequest>,
log: log::Logger,
}
impl DelegatedSimpleHttp {
pub fn new(log: log::Logger) -> (Self, mpsc::Receiver<DelegatedHttpRequest>) {
let (tx, rx) = mpsc::channel(4);
(
DelegatedSimpleHttp {
log,
start_request: tx,
},
rx,
)
}
}
#[async_trait]
impl SimpleHttp for DelegatedSimpleHttp {
async fn make_request(
&self,
method: &'static str,
url: String,
) -> Result<SimpleResponse, AnyError> {
trace!(self.log, "making delegated request to {}", url);
let (tx, mut rx) = mpsc::unbounded_channel();
let sent = self
.start_request
.send(DelegatedHttpRequest {
method,
url: url.clone(),
ch: tx,
})
.await;
if sent.is_err() {
return Ok(SimpleResponse::generic_error(url)); // sender shut down
}
match rx.recv().await {
Some(DelegatedHttpEvent::InitResponse {
status_code,
headers,
}) => {
trace!(
self.log,
"delegated request to {} resulted in status = {}",
url,
status_code
);
let mut headers_map = HeaderMap::with_capacity(headers.len());
for (k, v) in &headers {
if let (Ok(key), Ok(value)) = (
HeaderName::from_str(&k.to_lowercase()),
HeaderValue::from_str(v),
) {
headers_map.insert(key, value);
}
}
Ok(SimpleResponse {
url,
status_code: StatusCode::from_u16(status_code)
.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR),
headers: headers_map,
read: Box::pin(DelegatedReader::new(rx)),
})
}
Some(DelegatedHttpEvent::End) => Ok(SimpleResponse::generic_error(url)),
Some(_) => panic!("expected initresponse as first message from delegated http"),
None => Ok(SimpleResponse::generic_error(url)), // sender shut down
}
}
}
struct DelegatedReader {
receiver: mpsc::UnboundedReceiver<DelegatedHttpEvent>,
readbuf: ReadBuffer,
}
impl DelegatedReader {
pub fn new(rx: mpsc::UnboundedReceiver<DelegatedHttpEvent>) -> Self {
DelegatedReader {
readbuf: ReadBuffer::default(),
receiver: rx,
}
}
}
impl AsyncRead for DelegatedReader {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
buf: &mut tokio::io::ReadBuf<'_>,
) -> std::task::Poll<std::io::Result<()>> {
if let Some((v, s)) = self.readbuf.take_data() {
return self.readbuf.put_data(buf, v, s);
}
match self.receiver.poll_recv(cx) {
Poll::Ready(Some(DelegatedHttpEvent::Body(msg))) => self.readbuf.put_data(buf, msg, 0),
Poll::Ready(Some(_)) => Poll::Ready(Ok(())), // EOF
Poll::Ready(None) => {
Poll::Ready(Err(io::Error::new(io::ErrorKind::UnexpectedEof, "EOF")))
}
Poll::Pending => Poll::Pending,
}
}
}
/// Simple http implementation that falls back to delegated http if
/// making a direct reqwest fails.
#[derive(Clone)]
pub struct FallbackSimpleHttp {
native: ReqwestSimpleHttp,
delegated: DelegatedSimpleHttp,
}
impl FallbackSimpleHttp {
pub fn new(native: ReqwestSimpleHttp, delegated: DelegatedSimpleHttp) -> Self {
FallbackSimpleHttp { native, delegated }
}
pub fn native(&self) -> ReqwestSimpleHttp {
self.native.clone()
}
pub fn delegated(&self) -> DelegatedSimpleHttp {
self.delegated.clone()
}
}
#[async_trait]
impl SimpleHttp for FallbackSimpleHttp {
async fn make_request(
&self,
method: &'static str,
url: String,
) -> Result<SimpleResponse, AnyError> {
let r1 = self.native.make_request(method, url.clone()).await;
if let Ok(res) = r1 {
if !res.status_code.is_server_error() {
return Ok(res);
}
}
self.delegated.make_request(method, url).await
}
}