Skip to main content

roas_http_validator/adapters/
actix.rs

1//! `actix_web::HttpRequest`.
2//!
3//! actix-web is the reason this crate does not simply take
4//! `http::Request`: actix-http still declares `http = "0.2"`, so its
5//! `Method`, `Uri` and `HeaderMap` are different types from the ones
6//! hyper 1 and axum 0.8 use, and no single signature accepts both.
7//!
8//! Nothing from either `http` version appears below — `path()` and
9//! `query_string()` hand back plain `&str` — so this adapter is
10//! indifferent to which one actix-web is built against.
11
12use std::borrow::Cow;
13
14use crate::request::{RequestView, ToRequestView};
15
16impl ToRequestView for actix_web::HttpRequest {
17    fn request_view(&self) -> RequestView<'_> {
18        let view = RequestView::new(self.method().as_str(), self.path()).with_headers(
19            self.headers().iter().map(|(name, value)| {
20                let value = match value.to_str() {
21                    Ok(text) => Cow::Borrowed(text),
22                    Err(_) => Cow::Owned(String::from_utf8_lossy(value.as_bytes()).into_owned()),
23                };
24                (Cow::Borrowed(name.as_str()), value)
25            }),
26        );
27        let query = self.query_string();
28        if query.is_empty() {
29            view
30        } else {
31            view.with_query(query)
32        }
33    }
34}