aws-smithy-http-client 1.5.0

HTTP client abstractions for generated smithy clients
Documentation
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

//! HTTP proxy and tunneled-origin support for integration tests.

#[cfg(any(
    feature = "rustls-aws-lc",
    feature = "rustls-aws-lc-fips",
    feature = "rustls-ring",
    feature = "s2n-tls"
))]
use aws_smithy_runtime_api::client::dns::{DnsFuture, ResolveDns, ResolveDnsError};
use base64::Engine;
use http_1x::{Request, Response, StatusCode};
use hyper::body::Incoming;
use hyper::service::service_fn;
use hyper_util::rt::TokioIo;
use std::collections::HashMap;
use std::convert::Infallible;
#[cfg(any(
    feature = "rustls-aws-lc",
    feature = "rustls-aws-lc-fips",
    feature = "rustls-ring",
    feature = "s2n-tls"
))]
use std::net::IpAddr;
use std::net::SocketAddr;
use std::sync::{Arc, Mutex};
use tokio::net::TcpListener;
use tokio::sync::oneshot;

#[cfg(any(feature = "rustls-ring", feature = "s2n-tls"))]
use http_body_util::Full;
#[cfg(any(feature = "rustls-ring", feature = "s2n-tls"))]
use hyper::body::Bytes;
#[cfg(any(feature = "rustls-ring", feature = "s2n-tls"))]
use tokio::io::copy_bidirectional;
#[cfg(any(feature = "rustls-ring", feature = "s2n-tls"))]
use tokio::net::TcpStream;

/// One request observed by a test server.
#[derive(Clone, Debug)]
pub(crate) struct RecordedRequest {
    pub(crate) method: String,
    pub(crate) uri: String,
    pub(crate) headers: HashMap<String, String>,
}

impl RecordedRequest {
    fn from_request(request: &Request<Incoming>) -> Self {
        Self {
            method: request.method().to_string(),
            uri: request.uri().to_string(),
            headers: request
                .headers()
                .iter()
                .map(|(name, value)| {
                    (
                        name.to_string(),
                        value.to_str().unwrap_or_default().to_string(),
                    )
                })
                .collect(),
        }
    }
}

/// DNS resolver that maps one expected hostname to a test server.
#[cfg(any(
    feature = "rustls-aws-lc",
    feature = "rustls-aws-lc-fips",
    feature = "rustls-ring",
    feature = "s2n-tls"
))]
#[derive(Clone, Debug)]
pub(crate) struct RecordingDnsResolver {
    hostname: String,
    address: IpAddr,
    lookups: Arc<Mutex<Vec<String>>>,
}

#[cfg(any(
    feature = "rustls-aws-lc",
    feature = "rustls-aws-lc-fips",
    feature = "rustls-ring",
    feature = "s2n-tls"
))]
impl RecordingDnsResolver {
    fn new(hostname: impl Into<String>, address: IpAddr) -> Self {
        Self {
            hostname: hostname.into(),
            address,
            lookups: Arc::new(Mutex::new(Vec::new())),
        }
    }

    pub(crate) fn lookups(&self) -> Vec<String> {
        self.lookups
            .lock()
            .expect("DNS lookup log is not poisoned")
            .clone()
    }
}

#[cfg(any(
    feature = "rustls-aws-lc",
    feature = "rustls-aws-lc-fips",
    feature = "rustls-ring",
    feature = "s2n-tls"
))]
impl ResolveDns for RecordingDnsResolver {
    fn resolve_dns<'a>(&'a self, name: &'a str) -> DnsFuture<'a> {
        self.lookups
            .lock()
            .expect("DNS lookup log is not poisoned")
            .push(name.to_string());
        if name == self.hostname {
            DnsFuture::ready(Ok(vec![self.address]))
        } else {
            DnsFuture::ready(Err(ResolveDnsError::new(std::io::Error::other(format!(
                "unexpected DNS lookup for {name}"
            )))))
        }
    }
}

/// HTTP/1 server used as either a forward proxy or a direct origin.
#[derive(Debug)]
pub(crate) struct MockHttpServer {
    connections: Arc<()>,
    addr: SocketAddr,
    shutdown: Option<oneshot::Sender<()>>,
    requests: Arc<Mutex<Vec<RecordedRequest>>>,
}

impl MockHttpServer {
    pub(crate) async fn new<F>(handler: F) -> Self
    where
        F: Fn(RecordedRequest) -> Response<String> + Send + Sync + 'static,
    {
        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("test server should bind");
        let addr = listener.local_addr().expect("listener has an address");
        let (shutdown, mut shutdown_rx) = oneshot::channel();
        let requests = Arc::new(Mutex::new(Vec::new()));
        let server_requests = requests.clone();
        let connections = Arc::new(());
        let server_connections = connections.clone();
        let handler = Arc::new(handler);

        tokio::spawn(async move {
            loop {
                tokio::select! {
                    accepted = listener.accept() => {
                        let Ok((stream, _)) = accepted else {
                            break;
                        };
                        let handler = handler.clone();
                        let requests = server_requests.clone();
                        let connection = server_connections.clone();
                        tokio::spawn(async move {
                            let _connection = connection;
                            let service = service_fn(move |request: Request<Incoming>| {
                                let handler = handler.clone();
                                let requests = requests.clone();
                                async move {
                                    let request = RecordedRequest::from_request(&request);
                                    requests
                                        .lock()
                                        .expect("request log is not poisoned")
                                        .push(request.clone());
                                    Ok::<_, Infallible>(handler(request))
                                }
                            });
                            if let Err(error) = hyper::server::conn::http1::Builder::new()
                                .serve_connection(TokioIo::new(stream), service)
                                .await
                            {
                                tracing::debug!(%error, "test HTTP server connection ended");
                            }
                        });
                    }
                    _ = &mut shutdown_rx => break,
                }
            }
        });

        Self {
            connections,
            addr,
            shutdown: Some(shutdown),
            requests,
        }
    }

    pub(crate) async fn with_response(status: StatusCode, body: &str) -> Self {
        let body = body.to_string();
        Self::new(move |_| {
            Response::builder()
                .status(status)
                .body(body.clone())
                .expect("valid response")
        })
        .await
    }

    pub(crate) async fn with_auth_validation(user: &str, password: &str) -> Self {
        let expected = basic_authorization(user, password);
        Self::new(move |request| {
            if request.headers.get("proxy-authorization") == Some(&expected) {
                Response::builder()
                    .status(StatusCode::OK)
                    .body("authenticated".to_string())
                    .expect("valid response")
            } else {
                Response::builder()
                    .status(StatusCode::PROXY_AUTHENTICATION_REQUIRED)
                    .header("proxy-authenticate", "Basic realm=\"proxy\"")
                    .body("authentication required".to_string())
                    .expect("valid response")
            }
        })
        .await
    }

    pub(crate) fn addr(&self) -> SocketAddr {
        self.addr
    }

    #[cfg(any(
        feature = "rustls-aws-lc",
        feature = "rustls-aws-lc-fips",
        feature = "rustls-ring",
        feature = "s2n-tls"
    ))]
    pub(crate) fn dns_resolver(&self, hostname: impl Into<String>) -> RecordingDnsResolver {
        RecordingDnsResolver::new(hostname, self.addr.ip())
    }

    pub(crate) fn requests(&self) -> Vec<RecordedRequest> {
        self.requests
            .lock()
            .expect("request log is not poisoned")
            .clone()
    }

    pub(crate) fn connection_count(&self) -> usize {
        // The server and this handle each retain one reference.
        Arc::strong_count(&self.connections)
            .checked_sub(2)
            .expect("server references remain")
    }
}

impl Drop for MockHttpServer {
    fn drop(&mut self) {
        if let Some(shutdown) = self.shutdown.take() {
            let _ = shutdown.send(());
        }
    }
}

#[cfg(any(feature = "rustls-ring", feature = "s2n-tls"))]
#[derive(Debug)]
pub(crate) struct MockConnectProxy {
    addr: SocketAddr,
    shutdown: Option<oneshot::Sender<()>>,
    requests: Arc<Mutex<Vec<RecordedRequest>>>,
}

#[cfg(any(feature = "rustls-ring", feature = "s2n-tls"))]
impl MockConnectProxy {
    /// Starts a proxy that accepts CONNECT and relays bytes to one fixed origin.
    pub(crate) async fn relay_to(
        origin: SocketAddr,
        expected_authorization: Option<String>,
    ) -> Self {
        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("CONNECT proxy should bind");
        let addr = listener.local_addr().expect("listener has an address");
        let (shutdown, mut shutdown_rx) = oneshot::channel();
        let requests = Arc::new(Mutex::new(Vec::new()));
        let server_requests = requests.clone();

        tokio::spawn(async move {
            loop {
                tokio::select! {
                    accepted = listener.accept() => {
                        let Ok((stream, _)) = accepted else {
                            break;
                        };
                        let requests = server_requests.clone();
                        let expected_authorization = expected_authorization.clone();
                        tokio::spawn(async move {
                            let service = service_fn(move |mut request: Request<Incoming>| {
                                let requests = requests.clone();
                                let expected_authorization = expected_authorization.clone();
                                async move {
                                    let recorded = RecordedRequest::from_request(&request);
                                    requests
                                        .lock()
                                        .expect("request log is not poisoned")
                                        .push(recorded.clone());

                                    if recorded.method != "CONNECT" {
                                        return Ok::<_, Infallible>(empty_response(
                                            StatusCode::METHOD_NOT_ALLOWED,
                                        ));
                                    }
                                    if expected_authorization.as_ref()
                                        != recorded.headers.get("proxy-authorization")
                                    {
                                        return Ok(empty_response(
                                            StatusCode::PROXY_AUTHENTICATION_REQUIRED,
                                        ));
                                    }

                                    let upgraded = hyper::upgrade::on(&mut request);
                                    tokio::spawn(async move {
                                        let Ok(upgraded) = upgraded.await else {
                                            return;
                                        };
                                        let Ok(mut upstream) = TcpStream::connect(origin).await else {
                                            return;
                                        };
                                        let mut downstream = TokioIo::new(upgraded);
                                        let _ = copy_bidirectional(&mut downstream, &mut upstream).await;
                                    });

                                    Ok(empty_response(StatusCode::OK))
                                }
                            });
                            let connection = hyper::server::conn::http1::Builder::new()
                                .serve_connection(TokioIo::new(stream), service)
                                .with_upgrades();
                            if let Err(error) = connection.await {
                                tracing::debug!(%error, "CONNECT proxy connection ended");
                            }
                        });
                    }
                    _ = &mut shutdown_rx => break,
                }
            }
        });

        Self {
            addr,
            shutdown: Some(shutdown),
            requests,
        }
    }

    pub(crate) fn addr(&self) -> SocketAddr {
        self.addr
    }

    pub(crate) fn requests(&self) -> Vec<RecordedRequest> {
        self.requests
            .lock()
            .expect("request log is not poisoned")
            .clone()
    }
}

#[cfg(any(feature = "rustls-ring", feature = "s2n-tls"))]
impl Drop for MockConnectProxy {
    fn drop(&mut self) {
        if let Some(shutdown) = self.shutdown.take() {
            let _ = shutdown.send(());
        }
    }
}

#[cfg(any(feature = "rustls-ring", feature = "s2n-tls"))]
#[derive(Debug)]
pub(crate) struct MockTlsOrigin {
    addr: SocketAddr,
    shutdown: Option<oneshot::Sender<()>>,
    requests: Arc<Mutex<Vec<RecordedRequest>>>,
}

#[cfg(any(feature = "rustls-ring", feature = "s2n-tls"))]
impl MockTlsOrigin {
    pub(crate) async fn new(body: &str) -> Self {
        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("TLS origin should bind");
        let addr = listener.local_addr().expect("listener has an address");
        let acceptor = super::tls::SERVER_IDENTITY
            .acceptor(&[b"http/1.1"])
            .expect("test TLS configuration should load");
        let (shutdown, mut shutdown_rx) = oneshot::channel();
        let requests = Arc::new(Mutex::new(Vec::new()));
        let server_requests = requests.clone();
        let body = body.to_string();

        tokio::spawn(async move {
            loop {
                tokio::select! {
                    accepted = listener.accept() => {
                        let Ok((stream, _)) = accepted else {
                            break;
                        };
                        let acceptor = acceptor.clone();
                        let requests = server_requests.clone();
                        let body = body.clone();
                        tokio::spawn(async move {
                            let Ok(stream) = acceptor.accept(stream).await else {
                                return;
                            };
                            let service = service_fn(move |request: Request<Incoming>| {
                                let requests = requests.clone();
                                let body = body.clone();
                                async move {
                                    requests
                                        .lock()
                                        .expect("request log is not poisoned")
                                        .push(RecordedRequest::from_request(&request));
                                    Ok::<_, Infallible>(Response::new(Full::new(Bytes::from(body))))
                                }
                            });
                            if let Err(error) = hyper::server::conn::http1::Builder::new()
                                .serve_connection(TokioIo::new(stream), service)
                                .await
                            {
                                tracing::debug!(%error, "TLS origin connection ended");
                            }
                        });
                    }
                    _ = &mut shutdown_rx => break,
                }
            }
        });

        Self {
            addr,
            shutdown: Some(shutdown),
            requests,
        }
    }

    pub(crate) fn addr(&self) -> SocketAddr {
        self.addr
    }

    pub(crate) fn requests(&self) -> Vec<RecordedRequest> {
        self.requests
            .lock()
            .expect("request log is not poisoned")
            .clone()
    }
}

#[cfg(any(feature = "rustls-ring", feature = "s2n-tls"))]
impl Drop for MockTlsOrigin {
    fn drop(&mut self) {
        if let Some(shutdown) = self.shutdown.take() {
            let _ = shutdown.send(());
        }
    }
}

pub(crate) fn basic_authorization(user: &str, password: &str) -> String {
    format!(
        "Basic {}",
        base64::prelude::BASE64_STANDARD.encode(format!("{user}:{password}"))
    )
}

#[cfg(any(feature = "rustls-ring", feature = "s2n-tls"))]
fn empty_response(status: StatusCode) -> Response<Full<Bytes>> {
    Response::builder()
        .status(status)
        .body(Full::default())
        .expect("valid response")
}