Experiment with using a Stream for a Response Body

This commit is contained in:
Tangent 128 2017-09-21 00:59:11 -04:00
parent ef45728779
commit db1731f776

View file

@ -3,6 +3,7 @@ extern crate hyper;
extern crate lab_ebml; extern crate lab_ebml;
use futures::future::FutureResult; use futures::future::FutureResult;
use futures::stream::{iter, Stream};
use hyper::{Get, StatusCode}; use hyper::{Get, StatusCode};
use hyper::server::{Http, Request, Response, Service}; use hyper::server::{Http, Request, Response, Service};
use std::env::args; use std::env::args;
@ -12,16 +13,20 @@ use std::net::ToSocketAddrs;
struct WebmServer; struct WebmServer;
type BodyStream = Box<Stream<Item = &'static str, Error = hyper::Error>>;
impl Service for WebmServer { impl Service for WebmServer {
type Request = Request; type Request = Request;
type Response = Response; type Response = Response<BodyStream>;
type Error = hyper::Error; type Error = hyper::Error;
type Future = FutureResult<Response, hyper::Error>; type Future = FutureResult<Self::Response, hyper::Error>;
fn call(&self, req: Request) -> Self::Future { fn call(&self, req: Request) -> Self::Future {
let response = match (req.method(), req.path()) { let response = match (req.method(), req.path()) {
(&Get, "/loop") => { (&Get, "/loop") => {
let pieces = vec!["<", "Insert WebM stream here.", ">"];
let stream: BodyStream = iter(pieces.into_iter().map(|x| Ok(x))).boxed();
Response::new() Response::new()
.with_body("<Insert WebM stream here>") .with_body(stream)
}, },
_ => { _ => {
Response::new() Response::new()