This commit is contained in:
Hubert 2021-07-13 07:05:28 +02:00
parent b22b5f81c7
commit 5464c78e37
2 changed files with 19 additions and 64 deletions

View File

@ -1,5 +1,6 @@
mod git; mod git;
mod ite; mod ite;
mod tostream;
use actix_files::Files; use actix_files::Files;
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder, Error, HttpRequest, HttpMessage}; use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder, Error, HttpRequest, HttpMessage};
@ -179,12 +180,6 @@ async fn git_proto(payload : web::Payload, web::Path((owner, reponame)): web::Pa
// future::ready(Ok(())) // future::ready(Ok(()))
}).await; }).await;
println!("input sent"); println!("input sent");
//io::copy(&mut req.take_payload(), &mut p.stdin.take().unwrap())?;
// Parse the headers coming out, and the pass through the rest of the
// process back down the stack.
//
// Note that we have to be careful to not drop the process which will wait
// for the process to exit (and we haven't read stdout)
let mut rdr = tokio::io::BufReader::new(p.stdout.take().unwrap()); let mut rdr = tokio::io::BufReader::new(p.stdout.take().unwrap());
let mut headers = HashMap::new(); let mut headers = HashMap::new();
@ -204,22 +199,6 @@ async fn git_proto(payload : web::Payload, web::Path((owner, reponame)): web::Pa
.or_insert_with(Vec::new) .or_insert_with(Vec::new)
.push(value.to_string()); .push(value.to_string());
} }
// for line in rdr.by_ref().lines() {
// let line = line?;
// if line == "" || line == "\r" {
// break;
// }
//
// let mut parts = line.splitn(2, ':');
// let key = parts.next().unwrap();
// let value = parts.next().unwrap();
// let value = &value[1..];
// headers
// .entry(key.to_string())
// .or_insert_with(Vec::new)
// .push(value.to_string());
// }
println!("headers : {:?}", headers); println!("headers : {:?}", headers);
let status_code : u16 = { let status_code : u16 = {
@ -243,56 +222,16 @@ async fn git_proto(payload : web::Payload, web::Path((owner, reponame)): web::Pa
} }
println!("Write body..."); println!("Write body...");
//let mut body = Vec::new();
//rdr.bytes()
// let stream = stream::try_unfold(0, |state| async move { let unfold = stream::try_unfold(rdr, move |mut rdr| {
//
// if state <= 2 {
// let next_state = state + 1;
// let yielded = state * 2;
// Ok(Some((yielded, next_state)))
// } else {
// Ok(None)
// }
// });
let unfold = stream::try_unfold(0, |_useless| {
let mut buff: [u8; 1024] = [0; 1024]; let mut buff: [u8; 1024] = [0; 1024];
let read = rdr.read(&mut buff[..]); let read = rdr.read(&mut buff[..]);
let result = read.map_ok(|l| Some((Bytes::copy_from_slice(&buff[0..l]), 0))); let result = read.map_ok(|l| Some((Bytes::copy_from_slice(&buff[0..l]), rdr)));
return result; return result;
// match result {
// Ok(l) => {Ok(Some((Bytes::copy_from_slice(&buff[0..l]), 0)))}
// Err(e) => {Err(e)}
// }
// result.map(|l| {
// print!("{}", String::from_utf8_lossy(&buff[0..l]));
// Some((Bytes::copy_from_slice(&buff[0..l]), 0))
// });
}); });
// pin_mut!(unfold); // pin_mut!(unfold);
let response = builder.streaming(unfold); let response = builder.streaming(unfold);
// let response = builder.streaming(stream::repeat_with(move || {
// let mut buff : [u8; 1024] = [0; 1024];
// match rdr.read(&mut buff[..]) {
// Ok(l) => {
// print!("{}", String::from_utf8_lossy(&buff[0..l]));
// Ok(Bytes::copy_from_slice(&buff[0..l])) }
// Err(e) => {Err(e)}
// }
// })
// .take_while(|bytes| {
// match bytes {
// Ok(bytes) => {future::ready(bytes.len() != 0)}
// Err(_) => {future::ready(false)}
// }
// })
// );
return Ok(response); return Ok(response);
} }
fn header(req: &HttpRequest, name: header::HeaderName) -> &str { fn header(req: &HttpRequest, name: header::HeaderName) -> &str {

16
src/tostream.rs Normal file
View File

@ -0,0 +1,16 @@
use futures::{Stream, AsyncRead};
use std::task::{Context, Poll};
use std::pin::Pin;
use actix_web::web::Bytes;
use std::io::Error;
struct ToStream<T>(T);
impl <T : AsyncRead> Stream for ToStream<T>{
type Item = Result<Bytes, Error>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let mut buff: [u8; 1024] = [0; 1024];
self.0.poll_read(cx, &mut buff[..]).map_ok(|l| {Bytes::copy_from_slice(&buff[0..l])}) // il manque Option
}
}