|
|
|
@ -93,17 +93,18 @@ impl fmt::Display for GenBodyError
|
|
|
|
|
|
|
|
|
|
async fn gen_body(chain: Arc<RwLock<Chain<String>>>, num: Option<usize>, mut output: mpsc::Sender<String>) -> Result<(), GenBodyError>
|
|
|
|
|
{
|
|
|
|
|
let chain = chain.read().await;
|
|
|
|
|
if !chain.is_empty() {
|
|
|
|
|
match num {
|
|
|
|
|
Some(num) if num < MAX_GEN_SIZE => {
|
|
|
|
|
for string in chain.str_iter_for(num) {
|
|
|
|
|
output.send(string).await.map_err(|e| GenBodyError(e.0))?;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
_ => output.send(chain.generate_str()).await.map_err(|e| GenBodyError(e.0))?,
|
|
|
|
|
let chain = chain.read().await;
|
|
|
|
|
if !chain.is_empty() {
|
|
|
|
|
match num {
|
|
|
|
|
Some(num) if num < MAX_GEN_SIZE => {
|
|
|
|
|
//This could DoS `full_body` and writes, potentially.
|
|
|
|
|
for string in chain.str_iter_for(num) {
|
|
|
|
|
output.send(string).await.map_err(|e| GenBodyError(e.0))?;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
_ => output.send(chain.generate_str()).await.map_err(|e| GenBodyError(e.0))?,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -149,7 +150,7 @@ async fn main() {
|
|
|
|
|
.and(warp::path::param().map(|opt: usize| Some(opt)).or(warp::any().map(|| Option::<usize>::None)).unify())
|
|
|
|
|
.and_then(|chain: Arc<RwLock<Chain<String>>>, host: IpAddr, num: Option<usize>| {
|
|
|
|
|
async move {
|
|
|
|
|
let (tx, rx) = mpsc::channel(16);
|
|
|
|
|
let (tx, rx) = mpsc::channel(MAX_GEN_SIZE);
|
|
|
|
|
tokio::spawn(gen_body(chain, num, tx));
|
|
|
|
|
Ok::<_, std::convert::Infallible>(Response::new(Body::wrap_stream(rx.map(move |x| {
|
|
|
|
|
info!("{} <- {:?}", host, x);
|
|
|
|
|