rama-http-backend 0.3.0-alpha.4

error types and utilities for rama
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
use rama_core::{
    Service,
    error::{BoxError, ErrorContext, OpaqueError},
    extensions::{Extensions, ExtensionsMut, ExtensionsRef, InputExtensions},
    telemetry::tracing,
};
use rama_http::{StreamingBody, header::SEC_WEBSOCKET_KEY};
use rama_http_headers::{HeaderMapExt, Host};
use rama_http_types::{
    Method, Request, Response, Version,
    header::{CONNECTION, HOST, KEEP_ALIVE, PROXY_CONNECTION, TRANSFER_ENCODING, UPGRADE},
    uri::PathAndQuery,
};
use rama_net::{address::ProxyAddress, http::RequestContext};
use std::fmt;
use tokio::sync::Mutex;

pub(super) enum SendRequest<Body> {
    Http1(Mutex<rama_http_core::client::conn::http1::SendRequest<Body>>),
    Http2(rama_http_core::client::conn::http2::SendRequest<Body>),
}

impl<Body: fmt::Debug> fmt::Debug for SendRequest<Body> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut f = f.debug_tuple("SendRequest");
        match self {
            Self::Http1(send_request) => f.field(send_request).finish(),
            Self::Http2(send_request) => f.field(send_request).finish(),
        }
    }
}

/// Internal http sender used to send the actual requests.
pub struct HttpClientService<Body> {
    pub(super) sender: SendRequest<Body>,
    pub(super) extensions: Extensions,
}

impl<Body> Service<Request<Body>> for HttpClientService<Body>
where
    Body: StreamingBody<Data: Send + 'static, Error: Into<BoxError>> + Unpin + Send + 'static,
{
    type Output = Response;
    type Error = BoxError;

    async fn serve(&self, req: Request<Body>) -> Result<Self::Output, Self::Error> {
        // Check if this http connection can actually be used for this request version
        match (&self.sender, req.version()) {
            (SendRequest::Http1(_), Version::HTTP_10 | Version::HTTP_11)
            | (SendRequest::Http2(_), Version::HTTP_2) => (),
            (SendRequest::Http1(_), version) => Err(OpaqueError::from_display(format!(
                "Http1 connector cannot send request with version {version:?}"
            ))
            .into_boxed())?,
            (SendRequest::Http2(_), version) => Err(OpaqueError::from_display(format!(
                "Http2 connector cannot send request with version {version:?}"
            ))
            .into_boxed())?,
        }

        // sanitize subject line request uri
        // because Hyper (http) writes the URI as-is
        //
        // Originally reported in and fixed for:
        // <https://github.com/plabayo/rama/issues/250>
        //
        // TODO: fix this in hyper fork (embedded in rama http core)
        // directly instead of here...
        let req = sanitize_client_req_header(req)?;

        let req_extensions = req.extensions().clone();

        let mut resp = match &self.sender {
            SendRequest::Http1(sender) => {
                let mut sender = sender.lock().await;
                sender.ready().await?;
                sender.send_request(req).await
            }
            SendRequest::Http2(sender) => {
                let mut sender = sender.clone();
                sender.ready().await?;
                sender.send_request(req).await
            }
        }?;

        resp.extensions_mut()
            .insert(InputExtensions(req_extensions));

        Ok(resp.map(rama_http_types::Body::new))
    }
}

impl<B> ExtensionsRef for HttpClientService<B> {
    fn extensions(&self) -> &Extensions {
        &self.extensions
    }
}

impl<B> ExtensionsMut for HttpClientService<B> {
    fn extensions_mut(&mut self) -> &mut Extensions {
        &mut self.extensions
    }
}

fn sanitize_client_req_header<B>(req: Request<B>) -> Result<Request<B>, BoxError> {
    // logic specific to this method
    if req.method() == Method::CONNECT && req.uri().host().is_none() {
        return Err(OpaqueError::from_display("missing host in CONNECT request").into());
    }

    let uses_http_proxy = req
        .extensions()
        .get::<ProxyAddress>()
        .and_then(|proxy| proxy.protocol.as_ref())
        .map(|protocol| protocol.is_http())
        .unwrap_or_default();

    let request_ctx = RequestContext::try_from(&req).context("fetch request context")?;

    let is_insecure_request_over_http_proxy = !request_ctx.protocol.is_secure() && uses_http_proxy;

    // logic specific to http versions
    Ok(match req.version() {
        Version::HTTP_09 | Version::HTTP_10 | Version::HTTP_11 => {
            // remove authority and scheme for non-connect requests
            // cfr: <https://datatracker.ietf.org/doc/html/rfc2616#section-5.1.2>
            // Unless we are sending an insecure request over a http(s) proxy
            if req.method() != Method::CONNECT
                && !is_insecure_request_over_http_proxy
                && req.uri().host().is_some()
            {
                tracing::trace!(
                    "remove authority and scheme from non-connect direct http(~1) request"
                );
                let (mut parts, body) = req.into_parts();
                let mut uri_parts = parts.uri.into_parts();
                uri_parts.scheme = None;
                uri_parts.authority = None;

                // NOTE: in case the requested resource was the root ("/") it is possible
                // that the path is now empty. Hyper (currently used) has h1 built-in and
                // has a difference between the header encoding and the `as_str` method. The
                // encoding will be empty, which is invalid according to
                // <https://datatracker.ietf.org/doc/html/rfc2616#section-5.1.2> and will fail.
                // As such we force it here to `/` (the path) incase it is empty,
                // as there is no way if this required or no... Sad sad sad...
                //
                // NOTE: once we fork hyper we can just handle it there, as there
                // is no valid reason for that encoding every to be empty... *sigh*
                if uri_parts.path_and_query.as_ref().map(|pq| pq.as_str()) == Some("/") {
                    uri_parts.path_and_query = Some(PathAndQuery::from_static("/"));
                }

                // add required host header if not defined
                if !parts.headers.contains_key(HOST) {
                    if request_ctx.authority_has_default_port() {
                        let host = request_ctx.authority.host;
                        tracing::trace!("add missing host {host} from authority as host header");
                        parts.headers.typed_insert(Host::from(host));
                    } else {
                        let authority = request_ctx.authority;
                        tracing::trace!("add missing authority {authority} as host header");
                        parts.headers.typed_insert(Host::from(authority));
                    }
                }

                parts.uri = rama_http_types::Uri::from_parts(uri_parts)?;
                Request::from_parts(parts, body)
            } else if !req.headers().contains_key(HOST) {
                let mut req = req;

                if request_ctx.authority_has_default_port() {
                    let authority = request_ctx.authority;
                    tracing::trace!(
                        url.full = %req.uri(),
                        server.address = %authority.host,
                        server.port = authority.port,
                        "add host from authority as HOST header to req (was missing it)",
                    );
                    req.headers_mut().typed_insert(Host::from(authority));
                } else {
                    let host = request_ctx.authority.host;
                    tracing::trace!(
                        url.full = %req.uri(),
                        "add {host} as HOST header to req (was missing it)",
                    );
                    req.headers_mut().typed_insert(Host::from(host));
                }

                req
            } else {
                req
            }
        }
        Version::HTTP_2 => {
            // set scheme/host if not defined as otherwise pseudo
            // headers won't be possible to be set in the h2 crate
            let mut req = if req.uri().host().is_none() {
                let request_ctx = RequestContext::try_from(&req)
                    .context("[h2+] add scheme/host: missing RequestCtx")?;

                tracing::trace!(
                    network.protocol.name = "http",
                    network.protocol.version = ?req.version(),
                    "defining authority and scheme to non-connect direct http request"
                );

                let (mut parts, body) = req.into_parts();
                let mut uri_parts = parts.uri.into_parts();
                uri_parts.scheme = Some(
                    request_ctx
                        .protocol
                        .as_str()
                        .try_into()
                        .context("use RequestContext.protocol as http scheme")?,
                );
                // NOTE: in a green future we might not need to stringify
                // this entire thing first... maybe something someone at some
                // point can take a look at this mess

                // Default port is stripped in browsers. It's important that we also do this
                // as some reverse proxies such as nginx respond 404 if authority is not an exact match
                let authority = if request_ctx.authority_has_default_port() {
                    request_ctx.authority.host.to_string()
                } else {
                    request_ctx.authority.to_string()
                };

                uri_parts.authority = Some(
                    authority
                        .try_into()
                        .context("use RequestContext.authority as http authority")?,
                );

                parts.uri = rama_http_types::Uri::from_parts(uri_parts)
                    .context("create http uri from parts")?;

                Request::from_parts(parts, body)
            } else {
                req
            };

            // remove illegal headers
            for illegal_h2_header in [
                &CONNECTION,
                &TRANSFER_ENCODING,
                &PROXY_CONNECTION,
                &UPGRADE,
                &SEC_WEBSOCKET_KEY,
                &KEEP_ALIVE,
                &HOST,
            ] {
                if let Some(header) = req.headers_mut().remove(illegal_h2_header) {
                    tracing::trace!(
                        http.header.name = ?header,
                        "removed illegal (~http1) header from h2 request",
                    );
                }
            }

            req
        }
        Version::HTTP_3 => {
            tracing::debug!(
                url.full = %req.uri(),
                "h3 request detected, but sanitize_client_req_header does not yet support this",
            );
            req
        }
        _ => {
            tracing::debug!(
                url.full = %req.uri(),
                http.method = ?req.method(),
                "request with unknown version detected, sanitize_client_req_header cannot support this",
            );
            req
        }
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use rama_http::{Scheme, Uri, uri::Authority};
    use rama_net::Protocol;

    #[test]
    fn should_sanitize_http1_except_connect() {
        for method in [
            Method::DELETE,
            Method::GET,
            Method::HEAD,
            Method::OPTIONS,
            Method::PATCH,
            Method::POST,
            Method::PUT,
            Method::TRACE,
        ]
        .into_iter()
        {
            let uri = Uri::builder()
                .authority("example.com")
                .scheme(Scheme::HTTPS)
                .path_and_query("/test")
                .build()
                .unwrap();

            let req = Request::builder().uri(uri).method(method).body(()).unwrap();
            let req = sanitize_client_req_header(req).unwrap();

            let (parts, _) = req.into_parts();
            let uri = parts.uri.into_parts();

            assert_eq!(uri.scheme, None);
            assert_eq!(uri.authority, None);
        }
    }

    #[test]
    fn should_not_sanitize_http1_connect() {
        let uri = Uri::builder()
            .authority("example.com")
            .scheme("https")
            .path_and_query("/test")
            .build()
            .unwrap();

        let req = Request::builder()
            .method(Method::CONNECT)
            .uri(uri)
            .body(())
            .unwrap();
        let req = sanitize_client_req_header(req).unwrap();

        let (parts, _) = req.into_parts();
        let uri = parts.uri.into_parts();

        assert_eq!(uri.scheme, Some(Scheme::HTTPS));
        assert_eq!(uri.authority, Some(Authority::from_static("example.com")));
    }

    #[test]
    fn should_not_sanitize_insecure_http1_request_over_http_proxy() {
        let uri = Uri::builder()
            .authority("example.com")
            .scheme(Scheme::HTTP)
            .path_and_query("/test")
            .build()
            .unwrap();

        let mut req = Request::builder().uri(uri).body(()).unwrap();

        req.extensions_mut().insert(ProxyAddress {
            address: rama_net::address::HostWithPort::example_domain_http(),
            credential: None,
            protocol: Some(Protocol::HTTP),
        });

        let req = sanitize_client_req_header(req).unwrap();

        let (parts, _) = req.into_parts();
        let uri = parts.uri.into_parts();

        assert_eq!(uri.scheme, Some(Scheme::HTTP));
        assert_eq!(uri.authority, Some(Authority::from_static("example.com")));
    }

    #[test]
    fn should_sanitize_secure_http1_request_over_http_proxy() {
        let uri = Uri::builder()
            .authority("example.com")
            .scheme(Scheme::HTTPS)
            .path_and_query("/test")
            .build()
            .unwrap();

        let mut req = Request::builder().uri(uri).body(()).unwrap();

        req.extensions_mut().insert(ProxyAddress {
            address: rama_net::address::HostWithPort::example_domain_http(),
            credential: None,
            protocol: Some(Protocol::HTTP),
        });

        let req = sanitize_client_req_header(req).unwrap();

        let (parts, _) = req.into_parts();
        let uri = parts.uri.into_parts();

        assert_eq!(uri.scheme, None);
        assert_eq!(uri.authority, None);
    }

    #[test]
    fn should_sanitize_insecure_http1_request_over_socks_proxy() {
        let uri = Uri::builder()
            .authority("example.com")
            .scheme(Scheme::HTTP)
            .path_and_query("/test")
            .build()
            .unwrap();

        let mut req = Request::builder().uri(uri).body(()).unwrap();

        req.extensions_mut().insert(ProxyAddress {
            address: rama_net::address::HostWithPort::example_domain_http(),
            credential: None,
            protocol: Some(Protocol::SOCKS5),
        });

        let req = sanitize_client_req_header(req).unwrap();

        let (parts, _) = req.into_parts();
        let uri = parts.uri.into_parts();

        assert_eq!(uri.scheme, None);
        assert_eq!(uri.authority, None);
    }
}