Fixed regression in get_binary

This commit is contained in:
int 80h
2021-12-05 15:00:33 -05:00
parent f332604088
commit 2de9594c0c
2 changed files with 15 additions and 8 deletions

View File

@@ -1,6 +1,5 @@
use std::fs; use tokio::fs::{self, File};
use std::fs::File; use tokio::io::{self, BufReader, AsyncWrite, AsyncBufReadExt};
use std::io::{self, BufRead, BufReader};
use std::os::unix::fs::PermissionsExt; use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use url::Url; use url::Url;
@@ -35,13 +34,14 @@ fn get_mime(path: &Path) -> String {
} }
async fn get_binary(mut con: conn::Connection, path: PathBuf, meta: String) -> io::Result<()> { async fn get_binary(mut con: conn::Connection, path: PathBuf, meta: String) -> io::Result<()> {
let fd = File::open(path)?; let fd = File::open(path).await?;
let mut reader = BufReader::with_capacity(1024 * 1024, fd); let mut reader = BufReader::with_capacity(1024 * 1024, fd);
con.send_status(Status::Success, Some(&meta)).await?; con.send_raw(format!("{} {}\r\n", Status::Success as u8, &meta).as_bytes()).await?;
loop { loop {
let len = { let len = {
let buf = reader.fill_buf()?; let buf = reader.fill_buf().await?;
con.send_raw(buf).await?; con.send_raw(buf).await?;
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
buf.len() buf.len()
}; };
if len == 0 { if len == 0 {
@@ -49,6 +49,11 @@ async fn get_binary(mut con: conn::Connection, path: PathBuf, meta: String) -> i
} }
reader.consume(len); reader.consume(len);
} }
futures_util::future::poll_fn(|ctx| {
std::pin::Pin::new(&mut con.stream).poll_shutdown(ctx)
})
.await?;
Ok(()) Ok(())
} }
@@ -62,8 +67,9 @@ async fn get_content(path: PathBuf, u: &url::Url) -> Result<String> {
let mut files: Vec<String> = Vec::new(); let mut files: Vec<String> = Vec::new();
// needs work // needs work
for file in (fs::read_dir(&path)?).flatten() { let mut dir = fs::read_dir(&path).await?;
let m = file.metadata()?; while let Some(file) = dir.next_entry().await? {
let m = file.metadata().await?;
let perm = m.permissions(); let perm = m.permissions();
if perm.mode() & 0o0444 != 0o0444 { if perm.mode() & 0o0444 != 0o0444 {
continue; continue;

View File

@@ -47,6 +47,7 @@ impl Connection {
Ok(()) Ok(())
} }
// when using send_raw you must manually shutdown the connection
pub async fn send_raw(&mut self, body: &[u8]) -> Result<(), io::Error> { pub async fn send_raw(&mut self, body: &[u8]) -> Result<(), io::Error> {
self.stream.write_all(body).await?; self.stream.write_all(body).await?;
self.stream.flush().await?; self.stream.flush().await?;