From 439e9546e71d1b95b13b1ab1d7f2647bc79a0361 Mon Sep 17 00:00:00 2001 From: Hubert Date: Tue, 13 Jul 2021 14:24:38 +0200 Subject: [PATCH] save --- src/main.rs | 25 ++++++++++++++----------- src/{tostream.rs => reader.rs} | 6 +++--- src/writer.rs | 8 ++++++++ 3 files changed, 25 insertions(+), 14 deletions(-) rename src/{tostream.rs => reader.rs} (80%) create mode 100644 src/writer.rs diff --git a/src/main.rs b/src/main.rs index eb3bccc..7560eae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ mod git; mod ite; -mod tostream; +mod reader; +mod writer; use actix_files::Files; use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder, Error, HttpRequest, HttpMessage}; @@ -19,8 +20,8 @@ use crate::ite::SuperIterator; use std::ops::Add; use std::path::{PathBuf, Path}; use serde::Deserialize; -use tokio::process::{Command, Child}; -use tokio::io::{AsyncWriteExt, AsyncBufReadExt, AsyncReadExt}; +use tokio::process::{Command, Child, ChildStdout}; +use tokio::io::{AsyncWriteExt, AsyncBufReadExt, AsyncReadExt, BufReader}; use std::process::Stdio; use actix_web::http::{header, StatusCode}; use std::io; @@ -30,6 +31,7 @@ use futures::{Stream, StreamExt, TryStreamExt, future, stream, TryFutureExt, pin use actix_web::web::{Buf, Bytes}; use actix_web::http::header::IntoHeaderValue; use actix_web::client::PayloadError; +use crate::reader::ToStream; #[derive(Template)] #[template(path = "hello.html")] @@ -183,7 +185,7 @@ async fn git_proto(payload : web::Payload, web::Path((owner, reponame)): web::Pa let mut rdr = tokio::io::BufReader::new(p.stdout.take().unwrap()); let mut headers = HashMap::new(); - while true { + loop { let mut line = String::new(); let len = rdr.read_line(&mut line).await?; if line == "" || line == "\r" { @@ -223,14 +225,15 @@ async fn git_proto(payload : web::Payload, web::Path((owner, reponame)): web::Pa println!("Write body..."); - let unfold = stream::try_unfold(rdr, move |mut rdr| { - let mut buff: [u8; 1024] = [0; 1024]; - let read = rdr.read(&mut buff[..]); - let result = read.map_ok(|l| Some((Bytes::copy_from_slice(&buff[0..l]), rdr))); - return result; - }); + // let unfold = stream::try_unfold(rdr, move |mut rdr| { + // let mut buff: [u8; 1024] = [0; 1024]; + // let read = rdr.read(&mut buff[..]); + // let result = read.map_ok(|l| Some((Bytes::copy_from_slice(&buff[0..l]), rdr))); + // return result; + // }); // pin_mut!(unfold); - let response = builder.streaming(unfold); + // let response = builder.streaming(unfold); + let response = builder.streaming(ToStream(rdr)); return Ok(response); } diff --git a/src/tostream.rs b/src/reader.rs similarity index 80% rename from src/tostream.rs rename to src/reader.rs index 34b0830..7ed4990 100644 --- a/src/tostream.rs +++ b/src/reader.rs @@ -1,10 +1,10 @@ -use futures::{Stream, AsyncRead}; +// use futures::{Stream, AsyncRead, TryStream}; use std::task::{Context, Poll}; use std::pin::Pin; use actix_web::web::Bytes; use std::io::Error; -struct ToStream(T); +pub struct ToStream(pub T); impl ToStream { fn get_field(self: Pin<&mut Self>) -> Pin<&mut T> { @@ -14,7 +14,7 @@ impl ToStream { impl Unpin for ToStream{} -impl Stream for ToStream{ +impl futures::Stream for ToStream{ type Item = Result; fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { diff --git a/src/writer.rs b/src/writer.rs new file mode 100644 index 0000000..9dfea19 --- /dev/null +++ b/src/writer.rs @@ -0,0 +1,8 @@ +use actix_web::web::Bytes; + +pub struct Writer{ + pub t : T, + pub bytes : Bytes, +} + +impl \ No newline at end of file