Skip to content

Commit

Permalink
Fix the actix example while the actix_web remains on the old version
Browse files Browse the repository at this point in the history
of "http" crate actix/actix-web#3384
  • Loading branch information
quasiyoke committed Jun 15, 2024
1 parent c56671d commit 499eaff
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions src/actix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//! }
//! ```
//!
use std::convert::TryInto;
use std::io;

use std::pin::Pin;
Expand Down Expand Up @@ -45,11 +46,11 @@ impl FromRequest for DavRequest {

fn from_request(req: &HttpRequest, payload: &mut dev::Payload) -> Self::Future {
let mut builder = http::Request::builder()
.method(req.method().to_owned())
.uri(req.uri().to_owned())
.version(req.version().to_owned());
.method(req.method().as_ref())
.uri(req.uri().to_string())
.version(from_actix_http_version(req.version()));
for (name, value) in req.headers().iter() {
builder = builder.header(name, value);
builder = builder.header(name.as_str(), value.as_ref());
}
let path = req.match_info().unprocessed();
let tail = req.match_info().unprocessed();
Expand Down Expand Up @@ -82,13 +83,13 @@ impl http_body::Body for DavBody {
type Data = Bytes;
type Error = io::Error;

fn poll_data(
fn poll_frame(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Self::Data, Self::Error>>> {
) -> Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
let this = self.project();
match this.body.poll_next(cx) {
Poll::Ready(Some(Ok(data))) => Poll::Ready(Some(Ok(data))),
Poll::Ready(Some(Ok(data))) => Poll::Ready(Some(Ok(http_body::Frame::data(data)))),
Poll::Ready(Some(Err(err))) => Poll::Ready(Some(Err(match err {
PayloadError::Incomplete(Some(err)) => err,
PayloadError::Incomplete(None) => io::ErrorKind::BrokenPipe.into(),
Expand All @@ -99,13 +100,6 @@ impl http_body::Body for DavBody {
Poll::Pending => Poll::Pending,
}
}

fn poll_trailers(
self: Pin<&mut Self>,
_cx: &mut Context,
) -> Poll<Result<Option<http::HeaderMap>, Self::Error>> {
Poll::Ready(Ok(None))
}
}

/// `http::Response` compatibility.
Expand All @@ -126,9 +120,9 @@ impl actix_web::Responder for DavResponse {
use crate::body::{Body, BodyType};

let (parts, body) = self.0.into_parts();
let mut builder = HttpResponse::build(parts.status);
let mut builder = HttpResponse::build(parts.status.as_u16().try_into().unwrap());
for (name, value) in parts.headers.into_iter() {
builder.append_header((name.unwrap(), value));
builder.append_header((name.unwrap().as_str(), value.as_ref()));
}
// I noticed that actix-web returns an empty chunked body
// (\r\n0\r\n\r\n) and _no_ Transfer-Encoding header on
Expand All @@ -144,3 +138,16 @@ impl actix_web::Responder for DavResponse {
resp
}
}

/// Converts HTTP version from `actix_web` version of `http` crate while `actix_web` remains on old version.
/// https://github.com/actix/actix-web/issues/3384
fn from_actix_http_version(v: actix_web::http::Version) -> http::Version {
match v {
actix_web::http::Version::HTTP_3 => http::Version::HTTP_3,
actix_web::http::Version::HTTP_2 => http::Version::HTTP_2,
actix_web::http::Version::HTTP_11 => http::Version::HTTP_11,
actix_web::http::Version::HTTP_10 => http::Version::HTTP_10,
actix_web::http::Version::HTTP_09 => http::Version::HTTP_09,
v => unreachable!("unexpected HTTP version {:?}", v),
}
}

0 comments on commit 499eaff

Please sign in to comment.