Before this commit, When reading long paste, It would read until read
bytes > 0. This means, It'll only read 1 packet, Generate URL, return it
and exit

Now it has been updated to keep reading bytes until there is nothing for
500ms
This commit is contained in:
Ishan Jain 2023-02-11 22:50:34 +05:30
parent b4119095d1
commit f424f47ce7
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
1 changed files with 31 additions and 11 deletions

View File

@ -26,6 +26,7 @@ mod schema;
use crate::routes::{config, display_paste, index, insert_paste, metrics, raw_paste};
use chrono::{Duration, Utc};
use diesel::prelude::*;
use models::paste;
use rocket::fairing::AdHoc;
use rocket::http::Header;
use rocket::shield::{Policy, Referrer, Shield};
@ -160,27 +161,46 @@ async fn save_paste(
let mut paste = vec![];
let mut buffer = [0; BUFFER_SIZE];
loop {
let read = socket.read(&mut buffer).await?;
if read < BUFFER_SIZE {
paste.extend(&buffer[..read]);
break;
}
'outer: loop {
let five_hundred_millis_timeout = tokio::time::Duration::from_millis(500);
paste.extend(buffer);
buffer.fill(0);
let response =
tokio::time::timeout(five_hundred_millis_timeout, socket.read(&mut buffer)).await;
match response {
Ok(Ok(read)) => {
if read == 0 {
break 'outer;
}
paste.extend(buffer);
buffer.fill(0);
}
Ok(Err(e)) => {
eprintln!("error in reading from socket: {e}");
break;
}
Err(_) => break,
}
}
use models::paste;
while let Some(&c) = paste.last() {
if c == 0x00 {
paste.pop();
} else {
break;
}
}
let string = String::from_utf8_lossy(&paste);
let v = paste::insert(
db,
// Terminal paste should stay for 3 days
Some(Utc::now() + Duration::days(90)),
&String::from_utf8_lossy(&paste),
&string,
)
.unwrap();
let output = format!("https://{}/{}\n", host, v);
let output = format!("https://{}/raw/{}\n", host, v);
let output: Vec<u8> = output.into_bytes();
socket.write(&output).await?;