Skip to content

Commit

Permalink
wip: update to hyper 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzdong committed Nov 21, 2023
1 parent fd33701 commit e641abb
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 176 deletions.
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ tls = [ "tokio-rustls" ]
[dependencies]
async-trait = "0.1"
bytes = "1.0"
cookie = "0.17"
cookie = "0.18"
futures = "0.3"
futures-util = "0.3"
headers = "0.3"
hyper = { version="0.14", features=["server", "http1", "http2", "stream"] }
http-body-util = "0.1"
hyper = { version="1.0", features=["server", "http1", "http2"] }
hyper-util = { version = "0.1", features=["tokio", "server-auto"] }
lazy_static = "1.4"
mime = "0.3"
mime_guess = "2.0"
Expand Down
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ pub enum Error {
FormDecodeError(#[from] serde_urlencoded::de::Error),
#[error("serde_urlencoded decode error")]
FormEncodeError(#[from] serde_urlencoded::ser::Error),
#[error("infallible")]
Infallible(#[from] std::convert::Infallible),
#[error("mime error")]
Mime(#[from] mime::FromStrError),
#[error("lieweb error")]
Message(String),
#[error("invalid request header {name:?}")]
Expand Down
31 changes: 15 additions & 16 deletions src/extracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use std::{
ops::{Deref, DerefMut},
};

use bytes::{Buf, Bytes, BytesMut};
use headers::HeaderMapExt;
use hyper::{body::HttpBody, Body, StatusCode};
use bytes::Bytes;
use http_body_util::BodyExt;
use hyper::StatusCode;
use mime::Mime;
use serde::de::DeserializeOwned;

Expand Down Expand Up @@ -365,7 +365,7 @@ impl FromRequest for BytesBody {
}

#[crate::async_trait]
impl FromRequest for Body {
impl FromRequest for hyper::body::Incoming {
type Rejection = BodyBeenTaken;

async fn from_request(req: &mut RequestParts) -> Result<Self, Self::Rejection> {
Expand All @@ -383,27 +383,26 @@ impl FromRequest for Body {

fn get_content_type(req: &mut RequestParts) -> mime::Mime {
req.headers()
.typed_get::<headers::ContentType>()
.map(Into::into)
.get(hyper::header::CONTENT_TYPE)
.and_then(|v| {
String::from_utf8_lossy(v.as_bytes())
.parse::<mime::Mime>()
.ok()
})
.unwrap_or(mime::APPLICATION_OCTET_STREAM)
}

async fn read_body(req: &mut RequestParts) -> Result<Bytes, ReadBodyRejection> {
let mut body = req
let body = req
.body_mut()
.take()
.ok_or(ReadBodyRejection::BodyBeenTaken(BodyBeenTaken))?;

let mut bufs = BytesMut::new();
let body = BodyExt::collect(body)
.await
.map_err(ReadBodyRejection::ReadFailed)?;

while let Some(buf) = body.data().await {
let buf = buf.map_err(ReadBodyRejection::ReadFailed)?;
if buf.has_remaining() {
bufs.extend(buf);
}
}

Ok(bufs.freeze())
Ok(body.to_bytes())
}

mod params_de {
Expand Down
49 changes: 22 additions & 27 deletions src/request.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::net::SocketAddr;

use bytes::{Buf, Bytes, BytesMut};
use headers::{Header, HeaderMapExt, HeaderName, HeaderValue};
use hyper::body::HttpBody;
use bytes::Bytes;
use cookie::Cookie;
use http_body_util::BodyExt;
use hyper::http;
use hyper::http::{HeaderName, HeaderValue};
use pathrouter::Params;
use serde::de::DeserializeOwned;

pub type Request = hyper::Request<hyper::Body>;
pub type Request = hyper::Request<hyper::body::Incoming>;

use crate::error::{invalid_header, invalid_param, missing_cookie, missing_header, missing_param};
use crate::response::IntoResponse;
Expand All @@ -20,7 +21,7 @@ pub trait FromRequest: Sized {
async fn from_request(req: &mut RequestParts) -> Result<Self, Self::Rejection>;
}

pub type RequestParts = hyper::Request<Option<hyper::Body>>;
pub type RequestParts = hyper::Request<Option<hyper::body::Incoming>>;

#[crate::async_trait]
pub trait LieRequest {
Expand All @@ -34,7 +35,7 @@ pub trait LieRequest {
fn get_header<K>(&self, header: K) -> Result<&HeaderValue, Error>
where
HeaderName: From<K>;
fn get_typed_header<T: Header + Send + 'static>(&self) -> Result<T, Error>;
// fn get_typed_header<T: Header + Send + 'static>(&self) -> Result<T, Error>;

async fn read_body(&mut self) -> Result<Bytes, Error>;
async fn read_form<T: DeserializeOwned>(&mut self) -> Result<T, Error>;
Expand Down Expand Up @@ -85,32 +86,26 @@ impl LieRequest for Request {
Ok(value)
}

fn get_typed_header<T: Header + Send + 'static>(&self) -> Result<T, Error> {
self.headers()
.typed_get::<T>()
.ok_or_else(|| invalid_header(T::name().as_str()))
}
// fn get_typed_header<T: Header + Send + 'static>(&self) -> Result<T, Error> {
// self.headers()
// .typed_get::<T>()
// .ok_or_else(|| invalid_header(T::name().as_str()))
// }

fn get_cookie(&self, name: &str) -> Result<String, Error> {
let cookie: headers::Cookie = self.get_typed_header()?;

cookie
.get(name)
.ok_or_else(|| missing_cookie(name))
.map(|s| s.to_string())
let cookie = self.get_header(hyper::header::COOKIE)?;
let cookie = String::from_utf8_lossy(cookie.as_bytes());

Cookie::split_parse(cookie)
.flatten()
.find(|item| item.name() == name)
.map(|item| item.to_string())
.ok_or(missing_cookie(name))
}

async fn read_body(&mut self) -> Result<Bytes, Error> {
let mut bufs = BytesMut::new();

while let Some(buf) = self.body_mut().data().await {
let buf = buf?;
if buf.has_remaining() {
bufs.extend(buf);
}
}

Ok(bufs.freeze())
let body = BodyExt::collect(self.body_mut()).await?;
Ok(body.to_bytes())
}

async fn read_form<T: DeserializeOwned>(&mut self) -> Result<T, Error> {
Expand Down
Loading

0 comments on commit e641abb

Please sign in to comment.