Skip to content

Commit

Permalink
Merge branch 'release/v0.1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
rousan committed Apr 6, 2020
2 parents f7f1233 + fbb4811 commit 4ca8d42
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
name = "stream-body"
version = "0.1.0"
version = "0.1.1"
description = "An HttpBody implementation with efficient streaming support for the Rust HTTP library hyper"
homepage = "https://github.com/rousan/stream-body"
repository = "https://github.com/rousan/stream-body"
keywords = ["rust", "hyper", "asynchronous", "stream", "writer", "channel"]
keywords = ["rust", "hyper", "asynchronous", "stream", "writer"]
categories = ["asynchronous"]
authors = ["Rousan Ali <[email protected]>"]
readme = "README.md"
Expand Down
File renamed without changes.
29 changes: 29 additions & 0 deletions examples/from-reader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request, Response, Server};
use std::{convert::Infallible, net::SocketAddr};
use stream_body::StreamBody;
use tokio::fs::File;

async fn handle(_: Request<Body>) -> Result<Response<StreamBody>, Infallible> {
let f = File::open("large-file.pdf").await.unwrap();
let file_size = f.metadata().await.unwrap().len();

Ok(Response::builder()
.header("Content-Type", "application/pdf")
.header("Content-Length", file_size.to_string())
.body(StreamBody::from_reader(f))
.unwrap())
}

#[tokio::main]
async fn main() {
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));

let make_svc = make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(handle)) });

let server = Server::bind(&addr).serve(make_svc);

if let Err(e) = server.await {
eprintln!("server error: {}", e);
}
}
19 changes: 19 additions & 0 deletions src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use http::{HeaderMap, HeaderValue};
use http_body::{Body, SizeHint};
use pin_project_lite::pin_project;
use std::borrow::Cow;
use std::marker::Unpin;
use std::mem::MaybeUninit;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -94,6 +95,24 @@ impl StreamBody {

(w, body)
}

/// A helper method to convert an [AsyncRead](https://docs.rs/tokio/0.2.16/tokio/io/trait.AsyncRead.html) to a `StreamBody`. If there is any error
/// thrown during the reading/writing, it will be logged via [log::error!](https://docs.rs/log/0.4.10/log/macro.error.html).
pub fn from_reader<R: AsyncRead + Unpin + Send + 'static>(mut r: R) -> StreamBody {
let (mut w, body) = StreamBody::channel();

tokio::spawn(async move {
if let Err(err) = io::copy(&mut r, &mut w).await {
log::error!(
"{}: StreamBody: Something went wrong while piping the provided reader to the body: {}",
env!("CARGO_PKG_NAME"),
err
)
}
});

body
}
}

impl Body for StreamBody {
Expand Down

0 comments on commit 4ca8d42

Please sign in to comment.