From 483db7028cb5e23d38318366446c55af7a6367a4 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 20 May 2018 20:37:19 -0700 Subject: [PATCH] expose low level data --- src/server/h1writer.rs | 20 +++++++++++++++----- src/server/h2writer.rs | 10 ++++++++++ src/server/helpers.rs | 2 +- src/server/mod.rs | 20 +++++++++++++++++--- 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/server/h1writer.rs b/src/server/h1writer.rs index 5bb23dd98..6f10fc71a 100644 --- a/src/server/h1writer.rs +++ b/src/server/h1writer.rs @@ -1,6 +1,6 @@ #![cfg_attr(feature = "cargo-clippy", allow(redundant_field_names))] -use bytes::BufMut; +use bytes::{BytesMut, BufMut}; use futures::{Async, Poll}; use std::io; use std::rc::Rc; @@ -45,7 +45,7 @@ impl H1Writer { stream: T, buf: SharedBytes, settings: Rc>, ) -> H1Writer { H1Writer { - flags: Flags::empty(), + flags: Flags::KEEPALIVE, encoder: ContentEncoder::empty(buf.clone()), written: 0, headers_size: 0, @@ -62,7 +62,7 @@ impl H1Writer { pub fn reset(&mut self) { self.written = 0; - self.flags = Flags::empty(); + self.flags = Flags::KEEPALIVE; } pub fn disconnected(&mut self) { @@ -100,6 +100,16 @@ impl Writer for H1Writer { self.written } + #[inline] + fn set_date(&self, dst: &mut BytesMut) { + self.settings.set_date(dst) + } + + #[inline] + fn buffer(&self) -> &mut BytesMut { + self.buffer.get_mut() + } + fn start( &mut self, req: &mut HttpInnerMessage, msg: &mut HttpResponse, encoding: ContentEncoding, @@ -108,9 +118,9 @@ impl Writer for H1Writer { self.encoder = ContentEncoder::for_server(self.buffer.clone(), req, msg, encoding); if msg.keep_alive().unwrap_or_else(|| req.keep_alive()) { - self.flags.insert(Flags::STARTED | Flags::KEEPALIVE); + self.flags = Flags::STARTED | Flags::KEEPALIVE; } else { - self.flags.insert(Flags::STARTED); + self.flags = Flags::STARTED; } // Connection upgrade diff --git a/src/server/h2writer.rs b/src/server/h2writer.rs index a20d77593..5fc13154a 100644 --- a/src/server/h2writer.rs +++ b/src/server/h2writer.rs @@ -71,6 +71,16 @@ impl Writer for H2Writer { self.written } + #[inline] + fn set_date(&self, dst: &mut BytesMut) { + self.settings.set_date(dst) + } + + #[inline] + fn buffer(&self) -> &mut BytesMut { + self.buffer.get_mut() + } + fn start( &mut self, req: &mut HttpInnerMessage, msg: &mut HttpResponse, encoding: ContentEncoding, diff --git a/src/server/helpers.rs b/src/server/helpers.rs index 7f2f47346..f447a6cab 100644 --- a/src/server/helpers.rs +++ b/src/server/helpers.rs @@ -143,7 +143,7 @@ pub(crate) fn write_status_line(version: Version, mut n: u16, bytes: &mut BytesM } /// NOTE: bytes object has to contain enough space -pub(crate) fn write_content_length(mut n: usize, bytes: &mut BytesMut) { +pub fn write_content_length(mut n: usize, bytes: &mut BytesMut) { if n < 10 { let mut buf: [u8; 21] = [ b'\r', b'\n', b'c', b'o', b'n', b't', b'e', b'n', b't', b'-', b'l', b'e', diff --git a/src/server/mod.rs b/src/server/mod.rs index 36d80e2de..51ec32160 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -3,7 +3,8 @@ use std::net::Shutdown; use std::{io, time}; use actix; -use futures::Poll; +use bytes::BytesMut; +use futures::{Async, Poll}; use tokio_core::net::TcpStream; use tokio_io::{AsyncRead, AsyncWrite}; @@ -24,6 +25,9 @@ mod worker; pub use self::settings::ServerSettings; pub use self::srv::HttpServer; +#[doc(hidden)] +pub use self::helpers::write_content_length; + use body::Binary; use error::Error; use header::ContentEncoding; @@ -132,13 +136,15 @@ impl HttpHandler for Box { #[doc(hidden)] pub trait HttpHandlerTask { /// Poll task, this method is used before or after *io* object is available - fn poll(&mut self) -> Poll<(), Error>; + fn poll(&mut self) -> Poll<(), Error>{ + Ok(Async::Ready(())) + } /// Poll task when *io* object is available fn poll_io(&mut self, io: &mut Writer) -> Poll; /// Connection is disconnected - fn disconnected(&mut self); + fn disconnected(&mut self) {} } /// Conversion helper trait @@ -168,8 +174,16 @@ pub enum WriterState { #[doc(hidden)] /// Stream writer pub trait Writer { + /// number of bytes written to the stream fn written(&self) -> u64; + #[doc(hidden)] + fn set_date(&self, st: &mut BytesMut); + + #[doc(hidden)] + #[cfg_attr(feature = "cargo-clippy", allow(mut_from_ref))] + fn buffer(&self) -> &mut BytesMut; + fn start( &mut self, req: &mut HttpInnerMessage, resp: &mut HttpResponse, encoding: ContentEncoding,