Skip to content

Commit a952bb1

Browse files
committed
Fix the actix example while the actix_web remains on the old version
of "http" crate actix/actix-web#3384
1 parent 8b1e7f3 commit a952bb1

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

src/actix.rs

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
//! }
1313
//! ```
1414
//!
15-
use std::io;
16-
17-
use std::pin::Pin;
18-
use std::task::{Context, Poll};
15+
use std::{
16+
convert::TryInto,
17+
io,
18+
pin::Pin,
19+
task::{Context, Poll},
20+
};
1921

2022
use actix_web::body::BoxBody;
2123
use actix_web::error::PayloadError;
@@ -45,11 +47,11 @@ impl FromRequest for DavRequest {
4547

4648
fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
4749
let mut builder = http::Request::builder()
48-
.method(req.method().to_owned())
49-
.uri(req.uri().to_owned())
50-
.version(req.version().to_owned());
50+
.method(req.method().as_ref())
51+
.uri(req.uri().to_string())
52+
.version(from_actix_http_version(req.version()));
5153
for (name, value) in req.headers().iter() {
52-
builder = builder.header(name, value);
54+
builder = builder.header(name.as_str(), value.as_ref());
5355
}
5456
let path = req.match_info().unprocessed();
5557
let tail = req.match_info().unprocessed();
@@ -82,29 +84,20 @@ impl http_body::Body for DavBody {
8284
type Data = Bytes;
8385
type Error = io::Error;
8486

85-
fn poll_data(
87+
fn poll_frame(
8688
self: Pin<&mut Self>,
8789
cx: &mut Context<'_>,
88-
) -> Poll<Option<Result<Self::Data, Self::Error>>> {
89-
let this = self.project();
90-
match this.body.poll_next(cx) {
91-
Poll::Ready(Some(Ok(data))) => Poll::Ready(Some(Ok(data))),
92-
Poll::Ready(Some(Err(err))) => Poll::Ready(Some(Err(match err {
90+
) -> Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
91+
self.project()
92+
.body
93+
.poll_next(cx)
94+
.map_ok(http_body::Frame::data)
95+
.map_err(|err| match err {
9396
PayloadError::Incomplete(Some(err)) => err,
9497
PayloadError::Incomplete(None) => io::ErrorKind::BrokenPipe.into(),
9598
PayloadError::Io(err) => err,
96-
other => io::Error::new(io::ErrorKind::Other, format!("{:?}", other)),
97-
}))),
98-
Poll::Ready(None) => Poll::Ready(None),
99-
Poll::Pending => Poll::Pending,
100-
}
101-
}
102-
103-
fn poll_trailers(
104-
self: Pin<&mut Self>,
105-
_cx: &mut Context,
106-
) -> Poll<Result<Option<http::HeaderMap>, Self::Error>> {
107-
Poll::Ready(Ok(None))
99+
err => io::Error::new(io::ErrorKind::Other, format!("{err:?}")),
100+
})
108101
}
109102
}
110103

@@ -126,9 +119,9 @@ impl actix_web::Responder for DavResponse {
126119
use crate::body::{Body, BodyType};
127120

128121
let (parts, body) = self.0.into_parts();
129-
let mut builder = HttpResponse::build(parts.status);
122+
let mut builder = HttpResponse::build(parts.status.as_u16().try_into().unwrap());
130123
for (name, value) in parts.headers.into_iter() {
131-
builder.append_header((name.unwrap(), value));
124+
builder.append_header((name.unwrap().as_str(), value.as_ref()));
132125
}
133126
// I noticed that actix-web returns an empty chunked body
134127
// (\r\n0\r\n\r\n) and _no_ Transfer-Encoding header on
@@ -144,3 +137,16 @@ impl actix_web::Responder for DavResponse {
144137
resp
145138
}
146139
}
140+
141+
/// Converts HTTP version from `actix_web` version of `http` crate while `actix_web` remains on old version.
142+
/// https://github.com/actix/actix-web/issues/3384
143+
fn 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+
}

0 commit comments

Comments
 (0)