Update http body

This commit is contained in:
Lucio Franco
2019-08-18 01:06:27 -04:00
parent 2ebad2d778
commit b2a9ab97d7
17 changed files with 146 additions and 369 deletions
+14 -4
View File
@@ -3,8 +3,8 @@
use http::Request;
use std::task::{Context, Poll};
use tokio::net::TcpStream;
use tokio_buf::BufStream;
use tower_h2::Connection;
use std::pin::Pin;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -30,11 +30,14 @@ impl From<Vec<u8>> for Body {
}
}
impl BufStream for Body {
type Item = std::io::Cursor<Vec<u8>>;
impl http_body::Body for Body {
type Data = std::io::Cursor<Vec<u8>>;
type Error = std::io::Error;
fn poll_buf(&mut self, _cx: &mut Context<'_>) -> Poll<Option<Result<Self::Item, Self::Error>>> {
fn poll_data(
mut self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Option<Result<Self::Data, Self::Error>>> {
if self.0.is_empty() {
return None.into();
}
@@ -46,4 +49,11 @@ impl BufStream for Body {
Some(Ok(buf)).into()
}
fn poll_trailers(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Result<Option<http::HeaderMap>, Self::Error>> {
Ok(None).into()
}
}
+14 -4
View File
@@ -4,7 +4,7 @@ use futures_util::future;
use http::{Request, Response};
use std::task::{Context, Poll};
use tokio::net::TcpListener;
use tokio_buf::BufStream;
use std::pin::Pin;
use tower_h2::{RecvBody, Server};
use tower_service::Service;
@@ -84,11 +84,14 @@ impl From<Vec<u8>> for Body {
}
}
impl BufStream for Body {
type Item = std::io::Cursor<Vec<u8>>;
impl http_body::Body for Body {
type Data = std::io::Cursor<Vec<u8>>;
type Error = std::io::Error;
fn poll_buf(&mut self, _cx: &mut Context<'_>) -> Poll<Option<Result<Self::Item, Self::Error>>> {
fn poll_data(
mut self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Option<Result<Self::Data, Self::Error>>> {
if self.0.is_empty() {
return None.into();
}
@@ -100,4 +103,11 @@ impl BufStream for Body {
Some(Ok(buf)).into()
}
fn poll_trailers(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Result<Option<http::HeaderMap>, Self::Error>> {
Ok(None).into()
}
}