Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow building an HTTP/1 connection with prebuffered bytes #3705

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/proto/h1/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,14 @@ where
B: Buf,
T: Http1Transaction,
{
#[cfg(any(all(feature = "client", feature = "http1"), test))]
pub(crate) fn new(io: I) -> Conn<I, B, T> {
Self::new_buffered(Bytes::new(), io)
}

pub(crate) fn new_buffered(buffered: Bytes, io: I) -> Conn<I, B, T> {
Conn {
io: Buffered::new(io),
io: Buffered::new_buffered(buffered, io),
state: State {
allow_half_close: false,
cached_headers: None,
Expand Down
9 changes: 8 additions & 1 deletion src/proto/h1/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ where
T: Read + Write + Unpin,
B: Buf,
{
#[cfg(test)]
pub(crate) fn new(io: T) -> Buffered<T, B> {
Self::new_buffered(Bytes::new(), io)
}

pub(crate) fn new_buffered(buffered: Bytes, io: T) -> Buffered<T, B> {
let strategy = if io.is_write_vectored() {
WriteStrategy::Queue
} else {
Expand All @@ -66,7 +71,9 @@ where
flush_pipeline: false,
io,
read_blocked: false,
read_buf: BytesMut::with_capacity(0),
// FIXME: This should use `From<Bytes>` which has not been released yet.
// https://github.com/tokio-rs/bytes/commit/fa1daac3ae1dcb07dffe3a41a041dffd6edf177b
read_buf: BytesMut::from(&*buffered),
read_buf_strategy: ReadStrategy::default(),
write_buf,
}
Expand Down
22 changes: 21 additions & 1 deletion src/server/conn/http1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,27 @@ impl Builder {
<S::ResBody as Body>::Error: Into<Box<dyn StdError + Send + Sync>>,
I: Read + Write + Unpin,
{
let mut conn = proto::Conn::new(io);
self.serve_buffered_connection(Bytes::new(), io, service)
}

/// Bind a connection together with a [`Service`](crate::service::Service)
/// with a prebuffered chunk of bytes.
///
/// See [`Self::serve_connection`] for more info.
pub fn serve_buffered_connection<I, S>(
&self,
buffered: Bytes,
io: I,
service: S,
) -> Connection<I, S>
where
S: HttpService<IncomingBody>,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
S::ResBody: 'static,
<S::ResBody as Body>::Error: Into<Box<dyn StdError + Send + Sync>>,
I: Read + Write + Unpin,
{
let mut conn = proto::Conn::new_buffered(buffered, io);
conn.set_timer(self.timer.clone());
if !self.h1_keep_alive {
conn.disable_keep_alive();
Expand Down
Loading