Merge pull request #2442 from aarroyoc/fix-http-at-end-of-stream

Fix at_end_of_stream/1 for http read stream
This commit is contained in:
Mark Thom
2024-07-07 16:02:51 -06:00
committed by GitHub
3 changed files with 17 additions and 15 deletions

View File

@@ -1,4 +1,4 @@
use std::io::BufRead; use bytes::{Bytes, buf::Reader};
use std::sync::{Arc, Condvar, Mutex}; use std::sync::{Arc, Condvar, Mutex};
use warp::http; use warp::http;
@@ -19,5 +19,5 @@ pub struct HttpRequestData {
pub headers: http::HeaderMap, pub headers: http::HeaderMap,
pub path: String, pub path: String,
pub query: String, pub query: String,
pub body: Box<dyn BufRead + Send>, pub body: Reader<Bytes>,
} }

View File

@@ -12,6 +12,7 @@ use crate::machine::machine_indices::*;
use crate::machine::machine_state::*; use crate::machine::machine_state::*;
use crate::types::*; use crate::types::*;
use bytes::Buf;
pub use scryer_modular_bitfield::prelude::*; pub use scryer_modular_bitfield::prelude::*;
use std::cmp::Ordering; use std::cmp::Ordering;
@@ -22,7 +23,7 @@ use std::fs::{File, OpenOptions};
use std::hash::Hash; use std::hash::Hash;
use std::io; use std::io;
#[cfg(feature = "http")] #[cfg(feature = "http")]
use std::io::BufRead; use bytes::{buf::Reader as BufReader, Bytes};
use std::io::{Cursor, ErrorKind, Read, Seek, SeekFrom, Write}; use std::io::{Cursor, ErrorKind, Read, Seek, SeekFrom, Write};
use std::net::{Shutdown, TcpStream}; use std::net::{Shutdown, TcpStream};
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
@@ -274,7 +275,7 @@ impl Write for NamedTlsStream {
#[cfg(feature = "http")] #[cfg(feature = "http")]
pub struct HttpReadStream { pub struct HttpReadStream {
url: Atom, url: Atom,
body_reader: Box<dyn BufRead>, body_reader: BufReader<Bytes>,
} }
#[cfg(feature = "http")] #[cfg(feature = "http")]
@@ -1115,6 +1116,13 @@ impl Stream {
} }
} }
} }
Stream::HttpRead(stream_layout) => {
if stream_layout.stream.get_ref().body_reader.get_ref().has_remaining() {
AtEndOfStream::Not
} else {
AtEndOfStream::Past
}
}
_ => AtEndOfStream::Not, _ => AtEndOfStream::Not,
} }
} }
@@ -1203,7 +1211,7 @@ impl Stream {
#[inline] #[inline]
pub(crate) fn from_http_stream( pub(crate) fn from_http_stream(
url: Atom, url: Atom,
http_stream: Box<dyn BufRead>, http_stream: BufReader<Bytes>,
arena: &mut Arena, arena: &mut Arena,
) -> Self { ) -> Self {
Stream::HttpRead(arena_alloc!( Stream::HttpRead(arena_alloc!(

View File

@@ -50,8 +50,6 @@ use std::env;
use std::ffi::CString; use std::ffi::CString;
use std::fs; use std::fs;
use std::hash::{BuildHasher, BuildHasherDefault}; use std::hash::{BuildHasher, BuildHasherDefault};
#[cfg(feature = "http")]
use std::io::BufRead;
use std::io::{ErrorKind, Read, Write}; use std::io::{ErrorKind, Read, Write};
use std::iter::{once, FromIterator}; use std::iter::{once, FromIterator};
use std::mem; use std::mem;
@@ -4352,7 +4350,7 @@ impl Machine {
let mut stream = Stream::from_http_stream( let mut stream = Stream::from_http_stream(
AtomTable::build_with(&self.machine_st.atom_tbl, &address_string), AtomTable::build_with(&self.machine_st.atom_tbl, &address_string),
Box::new(reader), reader,
&mut self.machine_st.arena, &mut self.machine_st.arena,
); );
*stream.options_mut() = StreamOptions::default(); *stream.options_mut() = StreamOptions::default();
@@ -4447,11 +4445,7 @@ impl Machine {
let runtime = tokio::runtime::Handle::current(); let runtime = tokio::runtime::Handle::current();
let _guard = runtime.enter(); let _guard = runtime.enter();
fn get_reader(body: impl Buf + Send + 'static) -> Box<dyn BufRead + Send> { let serve = warp::body::bytes()
Box::new(body.reader())
}
let serve = warp::body::aggregate()
.and(warp::header::optional::<u64>( .and(warp::header::optional::<u64>(
warp::http::header::CONTENT_LENGTH.as_str(), warp::http::header::CONTENT_LENGTH.as_str(),
)) ))
@@ -4462,7 +4456,7 @@ impl Machine {
future::ready(Ok::<(String,), warp::Rejection>(("".to_string(),))) future::ready(Ok::<(String,), warp::Rejection>(("".to_string(),)))
})) }))
.map( .map(
move |body, move |body: bytes::Bytes,
content_length, content_length,
method, method,
headers: warp::http::HeaderMap, headers: warp::http::HeaderMap,
@@ -4482,7 +4476,7 @@ impl Machine {
headers, headers,
path: path.as_str().to_string(), path: path.as_str().to_string(),
query, query,
body: get_reader(body), body: body.reader(),
}; };
let response = let response =
Arc::new((Mutex::new(false), Mutex::new(None), Condvar::new())); Arc::new((Mutex::new(false), Mutex::new(None), Condvar::new()));