1use std::{
16 convert::TryInto,
17 future, io,
18 pin::Pin,
19 task::{Context, Poll},
20};
21
22use actix_web::body::BoxBody;
23use actix_web::error::PayloadError;
24use actix_web::{Error, FromRequest, HttpRequest, HttpResponse, dev};
25use bytes::Bytes;
26use futures_util::Stream;
27use pin_project_lite::pin_project;
28
29pub struct DavRequest {
33 pub request: http::Request<DavBody>,
34 prefix: Option<String>,
35}
36
37impl DavRequest {
38 pub fn prefix(&self) -> Option<&str> {
40 self.prefix.as_deref()
41 }
42}
43
44impl FromRequest for DavRequest {
45 type Error = Error;
46 type Future = future::Ready<Result<DavRequest, Error>>;
47
48 fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
49 let mut builder = http::Request::builder()
50 .method(req.method().as_ref())
51 .uri(req.uri().to_string())
52 .version(from_actix_http_version(req.version()));
53 for (name, value) in req.headers().iter() {
54 builder = builder.header(name.as_str(), value.as_ref());
55 }
56 let path = req.path();
57 let tail = req.match_info().unprocessed();
58 let prefix = match &path[..path.len() - tail.len()] {
59 "" | "/" => None,
60 x => Some(x.to_string()),
61 };
62
63 let body = DavBody {
64 body: payload.take(),
65 };
66 let stdreq = DavRequest {
67 request: builder.body(body).unwrap(),
68 prefix,
69 };
70 future::ready(Ok(stdreq))
71 }
72}
73
74pin_project! {
75 pub struct DavBody {
79 #[pin]
80 body: dev::Payload,
81 }
82}
83
84impl http_body::Body for DavBody {
85 type Data = Bytes;
86 type Error = io::Error;
87
88 fn poll_frame(
89 self: Pin<&mut Self>,
90 cx: &mut Context<'_>,
91 ) -> Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
92 self.project()
93 .body
94 .poll_next(cx)
95 .map_ok(http_body::Frame::data)
96 .map_err(|err| match err {
97 PayloadError::Incomplete(Some(err)) => err,
98 PayloadError::Incomplete(None) => io::ErrorKind::BrokenPipe.into(),
99 PayloadError::Io(err) => err,
100 err => io::Error::other(format!("{err:?}")),
101 })
102 }
103}
104
105pub struct DavResponse(pub http::Response<crate::body::Body>);
109
110impl From<http::Response<crate::body::Body>> for DavResponse {
111 fn from(resp: http::Response<crate::body::Body>) -> DavResponse {
112 DavResponse(resp)
113 }
114}
115
116impl actix_web::Responder for DavResponse {
117 type Body = BoxBody;
118
119 fn respond_to(self, _req: &HttpRequest) -> HttpResponse<BoxBody> {
120 use crate::body::{Body, BodyType};
121
122 let (parts, body) = self.0.into_parts();
123 let mut builder = HttpResponse::build(parts.status.as_u16().try_into().unwrap());
124 for (name, value) in parts.headers.into_iter() {
125 builder.append_header((name.unwrap().as_str(), value.as_ref()));
126 }
127 match body.inner {
133 BodyType::Bytes(None) => builder.body(""),
134 BodyType::Bytes(Some(b)) => builder.body(b),
135 BodyType::Empty => builder.body(""),
136 b @ BodyType::AsyncStream(..) => builder.streaming(Body { inner: b }),
137 }
138 }
139}
140
141fn from_actix_http_version(v: actix_web::http::Version) -> http::Version {
144 match v {
145 actix_web::http::Version::HTTP_3 => http::Version::HTTP_3,
146 actix_web::http::Version::HTTP_2 => http::Version::HTTP_2,
147 actix_web::http::Version::HTTP_11 => http::Version::HTTP_11,
148 actix_web::http::Version::HTTP_10 => http::Version::HTTP_10,
149 actix_web::http::Version::HTTP_09 => http::Version::HTTP_09,
150 v => unreachable!("unexpected HTTP version {:?}", v),
151 }
152}