hyperdriver/service/
http.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
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
use std::error::Error as StdError;
use std::future::Future;
use std::task::{Context, Poll};

use http::{Request, Response};
use http_body::Body as HttpBody;

/// An asynchronous function from `Request` to `Response`.
pub trait HttpService<ReqBody> {
    /// The `HttpBody` body of the `http::Response`.
    type ResBody: HttpBody;

    /// The error type that can occur within this `Service`.
    ///
    /// Note: Returning an `Error` to a hyper server will cause the connection
    /// to be abruptly aborted. In most cases, it is better to return a `Response`
    /// with a 4xx or 5xx status code.
    type Error: Into<Box<dyn StdError + Send + Sync>>;

    /// The `Future` returned by this `Service`.
    type Future: Future<Output = Result<Response<Self::ResBody>, Self::Error>>;

    #[doc(hidden)]
    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>;

    #[doc(hidden)]
    fn call(&mut self, req: Request<ReqBody>) -> Self::Future;
}

impl<T, BIn, BOut> HttpService<BIn> for T
where
    T: tower::Service<Request<BIn>, Response = Response<BOut>>,
    BOut: HttpBody,
    T::Error: Into<Box<dyn StdError + Send + Sync>>,
{
    type ResBody = BOut;

    type Error = T::Error;
    type Future = T::Future;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        tower::Service::poll_ready(self, cx)
    }

    fn call(&mut self, req: Request<BIn>) -> Self::Future {
        tower::Service::call(self, req)
    }
}

#[cfg(feature = "client")]
pub(super) mod http1 {

    use std::fmt;
    use std::task::{Context, Poll};

    use ::http;
    use http::uri::Scheme;
    use http::Uri;

    use crate::client::conn::Connection;
    use crate::client::pool::PoolableConnection;
    use crate::client::Error;
    use crate::service::client::ExecuteRequest;
    use crate::service::error::MaybeErrorFuture;
    use crate::service::error::PreprocessService;

    type PreprocessFn<C, B, E> = fn(ExecuteRequest<C, B>) -> Result<ExecuteRequest<C, B>, E>;

    /// A service that checks if the request is HTTP/1.1 compatible.
    #[derive(Debug)]
    pub struct Http1ChecksService<S, C, B>
    where
        S: tower::Service<ExecuteRequest<C, B>, Error = Error>,
        C: Connection<B> + PoolableConnection,
    {
        inner: PreprocessService<S, PreprocessFn<C, B, S::Error>>,
    }

    impl<S, C, B> tower::Service<ExecuteRequest<C, B>> for Http1ChecksService<S, C, B>
    where
        S: tower::Service<ExecuteRequest<C, B>, Error = Error>,
        C: Connection<B> + PoolableConnection,
    {
        type Response = S::Response;

        type Error = S::Error;

        type Future = MaybeErrorFuture<S::Future, S::Response, S::Error>;

        fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
            self.inner.poll_ready(cx)
        }

        fn call(&mut self, req: ExecuteRequest<C, B>) -> Self::Future {
            self.inner.call(req)
        }
    }

    impl<S, C, B> Clone for Http1ChecksService<S, C, B>
    where
        S: tower::Service<ExecuteRequest<C, B>, Error = Error> + Clone,
        C: Connection<B> + PoolableConnection,
    {
        fn clone(&self) -> Self {
            Self {
                inner: self.inner.clone(),
            }
        }
    }

    impl<S, C, B> Http1ChecksService<S, C, B>
    where
        S: tower::Service<ExecuteRequest<C, B>, Error = Error>,
        C: Connection<B> + PoolableConnection,
    {
        /// Create a new `Http1ChecksService`.
        pub fn new(service: S) -> Self {
            Self {
                inner: PreprocessService::new(service, check_http1_request),
            }
        }
    }

    /// A layer that checks if the request is HTTP/1.1 compatible.
    pub struct Http1ChecksLayer<C, B> {
        processor: std::marker::PhantomData<fn(C, B)>,
    }

    impl<C, B> Http1ChecksLayer<C, B> {
        /// Create a new `Http1ChecksLayer`.
        pub fn new() -> Self {
            Self {
                processor: std::marker::PhantomData,
            }
        }
    }

    impl<C, B> Default for Http1ChecksLayer<C, B> {
        fn default() -> Self {
            Self::new()
        }
    }

    impl<C, B> Clone for Http1ChecksLayer<C, B> {
        fn clone(&self) -> Self {
            Self::new()
        }
    }

    impl<C, B> fmt::Debug for Http1ChecksLayer<C, B> {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            f.debug_struct("Http1ChecksLayer").finish()
        }
    }

    impl<C, B, S> tower::layer::Layer<S> for Http1ChecksLayer<C, B>
    where
        S: tower::Service<ExecuteRequest<C, B>, Error = Error>,
        C: Connection<B> + PoolableConnection,
    {
        type Service = Http1ChecksService<S, C, B>;

        fn layer(&self, service: S) -> Self::Service {
            Http1ChecksService::new(service)
        }
    }

    fn check_http1_request<C, B>(
        mut req: ExecuteRequest<C, B>,
    ) -> Result<ExecuteRequest<C, B>, Error>
    where
        C: Connection<B> + PoolableConnection,
    {
        if req.connection().version() >= http::Version::HTTP_2 {
            return Ok(req);
        }

        if req.request().method() == http::Method::CONNECT {
            authority_form(req.request_mut().uri_mut());

            // If the URI is to HTTPS, and the connector claimed to be a proxy,
            // then it *should* have tunneled, and so we don't want to send
            // absolute-form in that case.
            if req.request().uri().scheme() == Some(&Scheme::HTTPS) {
                origin_form(req.request_mut().uri_mut());
            }
        } else if req.request().uri().scheme().is_none()
            || req.request().uri().authority().is_none()
        {
            absolute_form(req.request_mut().uri_mut());
        } else {
            origin_form(req.request_mut().uri_mut());
        }

        Ok(req)
    }

    /// Convert the URI to authority-form, if it is not already.
    ///
    /// This is the form of the URI with just the authority and a default
    /// path and scheme. This is used in HTTP/1 CONNECT requests.
    fn authority_form(uri: &mut Uri) {
        *uri = match uri.authority() {
            Some(auth) => {
                let mut parts = ::http::uri::Parts::default();
                parts.authority = Some(auth.clone());
                Uri::from_parts(parts).expect("authority is valid")
            }
            None => {
                unreachable!("authority_form with relative uri");
            }
        };
    }

    fn absolute_form(uri: &mut Uri) {
        debug_assert!(uri.scheme().is_some(), "absolute_form needs a scheme");
        debug_assert!(
            uri.authority().is_some(),
            "absolute_form needs an authority"
        );
    }

    /// Convert the URI to origin-form, if it is not already.
    ///
    /// This form of the URI has no scheme or authority, and contains just
    /// the path, usually used in HTTP/1 requests.
    fn origin_form(uri: &mut Uri) {
        let path = match uri.path_and_query() {
            Some(path) if path.as_str() != "/" => {
                let mut parts = ::http::uri::Parts::default();
                parts.path_and_query = Some(path.clone());
                Uri::from_parts(parts).expect("path is valid uri")
            }
            _none_or_just_slash => {
                debug_assert!(Uri::default() == "/");
                Uri::default()
            }
        };
        *uri = path
    }

    #[cfg(test)]
    mod tests {

        use super::*;

        #[test]
        fn test_origin_form() {
            let mut uri = "http://example.com".parse().unwrap();
            origin_form(&mut uri);
            assert_eq!(uri, "/");

            let mut uri = "/some/path/here".parse().unwrap();
            origin_form(&mut uri);
            assert_eq!(uri, "/some/path/here");

            let mut uri = "http://example.com:8080/some/path?query#fragment"
                .parse()
                .unwrap();
            origin_form(&mut uri);
            assert_eq!(uri, "/some/path?query");

            let mut uri = "/".parse().unwrap();
            origin_form(&mut uri);
            assert_eq!(uri, "/");
        }

        #[test]
        fn test_absolute_form() {
            let mut uri = "http://example.com".parse().unwrap();
            absolute_form(&mut uri);
            assert_eq!(uri, "http://example.com");

            let mut uri = "http://example.com:8080".parse().unwrap();
            absolute_form(&mut uri);
            assert_eq!(uri, "http://example.com:8080");

            let mut uri = "https://example.com/some/path?query".parse().unwrap();
            absolute_form(&mut uri);
            assert_eq!(uri, "https://example.com/some/path?query");

            let mut uri = "https://example.com:8443".parse().unwrap();
            absolute_form(&mut uri);
            assert_eq!(uri, "https://example.com:8443");

            let mut uri = "http://example.com:443".parse().unwrap();
            absolute_form(&mut uri);
            assert_eq!(uri, "http://example.com:443");

            let mut uri = "https://example.com:80".parse().unwrap();
            absolute_form(&mut uri);
            assert_eq!(uri, "https://example.com:80");
        }
    }
}

#[cfg(feature = "client")]
pub(super) mod http2 {
    use std::fmt;
    use std::task::{Context, Poll};

    use ::http;

    use crate::client::conn::Connection;
    use crate::client::pool::PoolableConnection;
    use crate::client::Error;
    use crate::service::client::ExecuteRequest;
    use crate::service::error::{MaybeErrorFuture, PreprocessService};

    const CONNECTION_HEADERS: [http::HeaderName; 5] = [
        http::header::CONNECTION,
        http::HeaderName::from_static("proxy-connection"),
        http::HeaderName::from_static("keep-alive"),
        http::header::TRANSFER_ENCODING,
        http::header::UPGRADE,
    ];

    type PreprocessFn<C, B, E> = fn(ExecuteRequest<C, B>) -> Result<ExecuteRequest<C, B>, E>;

    /// A service that checks if the request is HTTP/2 compatible.
    #[derive(Debug)]
    pub struct Http2ChecksService<S, C, B>
    where
        S: tower::Service<ExecuteRequest<C, B>, Error = Error>,
        C: Connection<B> + PoolableConnection,
    {
        inner: PreprocessService<S, PreprocessFn<C, B, S::Error>>,
    }

    impl<S, C, B> Clone for Http2ChecksService<S, C, B>
    where
        S: tower::Service<ExecuteRequest<C, B>, Error = Error> + Clone,
        C: Connection<B> + PoolableConnection,
    {
        fn clone(&self) -> Self {
            Self::new(self.inner.service().clone())
        }
    }

    impl<S, C, B> Http2ChecksService<S, C, B>
    where
        S: tower::Service<ExecuteRequest<C, B>, Error = Error>,
        C: Connection<B> + PoolableConnection,
    {
        /// Create a new `Http2ChecksService`.
        pub fn new(inner: S) -> Self {
            Self {
                inner: PreprocessService::new(inner, check_http2_request),
            }
        }
    }

    fn check_http2_request<C, B>(
        mut req: ExecuteRequest<C, B>,
    ) -> Result<ExecuteRequest<C, B>, Error>
    where
        C: Connection<B> + PoolableConnection,
    {
        if req.connection().version() == http::Version::HTTP_2 {
            if req.request().method() == http::Method::CONNECT {
                return Err(Error::InvalidMethod(http::Method::CONNECT));
            }

            *req.request_mut().version_mut() = http::Version::HTTP_2;

            for connection_header in &CONNECTION_HEADERS {
                if req
                    .request_mut()
                    .headers_mut()
                    .remove(connection_header)
                    .is_some()
                {
                    tracing::warn!(
                        "removed illegal connection header {:?} from HTTP/2 request",
                        connection_header
                    );
                };
            }

            if req
                .request_mut()
                .headers_mut()
                .remove(http::header::HOST)
                .is_some()
            {
                tracing::warn!("removed illegal header `host` from HTTP/2 request");
            }
        }
        Ok(req)
    }

    impl<S, C, B> tower::Service<ExecuteRequest<C, B>> for Http2ChecksService<S, C, B>
    where
        S: tower::Service<ExecuteRequest<C, B>, Error = Error>,
        C: Connection<B> + PoolableConnection,
    {
        type Response = S::Response;

        type Error = S::Error;

        type Future = MaybeErrorFuture<S::Future, S::Response, S::Error>;

        #[inline]
        fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
            self.inner.poll_ready(cx)
        }

        #[inline]
        fn call(&mut self, req: ExecuteRequest<C, B>) -> Self::Future {
            self.inner.call(req)
        }
    }

    /// A `Layer` that applies HTTP/2 checks to requests.
    pub struct Http2ChecksLayer<C, B> {
        _marker: std::marker::PhantomData<fn(C, B)>,
    }

    impl<C, B> Http2ChecksLayer<C, B> {
        /// Create a new `Http2ChecksLayer`.
        pub fn new() -> Self {
            Self {
                _marker: std::marker::PhantomData,
            }
        }
    }

    impl<C, B> Default for Http2ChecksLayer<C, B> {
        fn default() -> Self {
            Self::new()
        }
    }

    impl<C, B> fmt::Debug for Http2ChecksLayer<C, B> {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            f.debug_struct("Http2ChecksLayer").finish()
        }
    }

    impl<C, B> Clone for Http2ChecksLayer<C, B> {
        fn clone(&self) -> Self {
            Self::new()
        }
    }

    impl<S, C, B> tower::layer::Layer<S> for Http2ChecksLayer<C, B>
    where
        S: tower::Service<ExecuteRequest<C, B>, Error = Error>,
        C: Connection<B> + PoolableConnection,
    {
        type Service = Http2ChecksService<S, C, B>;

        fn layer(&self, inner: S) -> Self::Service {
            Http2ChecksService::new(inner)
        }
    }
}

#[cfg(test)]
#[allow(dead_code)]
mod tests {
    use super::*;
    use bytes::Bytes;
    use http_body_util::Empty;
    use std::{convert::Infallible, future::Ready};

    struct Svc;

    impl tower::Service<http::Request<Empty<Bytes>>> for Svc {
        type Response = http::Response<Empty<Bytes>>;
        type Error = Infallible;
        type Future = Ready<Result<Self::Response, Self::Error>>;

        fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
            Poll::Ready(Ok(()))
        }

        fn call(&mut self, req: http::Request<Empty<Bytes>>) -> Self::Future {
            assert_eq!(req.version(), http::Version::HTTP_11);
            std::future::ready(Ok(http::Response::new(Empty::new())))
        }
    }

    static_assertions::assert_impl_all!(Svc: HttpService<Empty<Bytes>, ResBody=Empty<Bytes>, Error=Infallible>);

    struct NotASvc;

    impl tower::Service<http::Request<()>> for Svc {
        type Response = http::Response<()>;
        type Error = Infallible;
        type Future = Ready<Result<Self::Response, Self::Error>>;

        fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
            Poll::Ready(Ok(()))
        }

        fn call(&mut self, req: http::Request<()>) -> Self::Future {
            assert_eq!(req.version(), http::Version::HTTP_11);
            std::future::ready(Ok(http::Response::new(())))
        }
    }

    static_assertions::assert_not_impl_all!(NotASvc: HttpService<(), ResBody=(), Error=Infallible>);
}