2017-11-09 01:44:23 +01:00
|
|
|
use std::io;
|
2017-11-03 19:29:44 +01:00
|
|
|
use futures::{Async, Poll};
|
|
|
|
use tokio_io::AsyncWrite;
|
2017-12-13 20:10:03 +01:00
|
|
|
use http::Version;
|
2017-12-14 20:20:45 +01:00
|
|
|
use http::header::{HeaderValue, CONNECTION, DATE};
|
2017-11-03 19:29:44 +01:00
|
|
|
|
2017-12-14 07:54:52 +01:00
|
|
|
use helpers;
|
2017-11-03 19:29:44 +01:00
|
|
|
use body::Body;
|
2017-12-15 04:34:31 +01:00
|
|
|
use helpers::SharedBytes;
|
2017-11-09 01:44:23 +01:00
|
|
|
use encoding::PayloadEncoder;
|
2017-12-09 13:33:40 +01:00
|
|
|
use httprequest::HttpMessage;
|
2017-11-03 19:29:44 +01:00
|
|
|
use httpresponse::HttpResponse;
|
|
|
|
|
|
|
|
const AVERAGE_HEADER_SIZE: usize = 30; // totally scientific
|
|
|
|
const MAX_WRITE_BUFFER_SIZE: usize = 65_536; // max buffer size 64k
|
|
|
|
|
|
|
|
|
2017-11-04 17:07:44 +01:00
|
|
|
#[derive(Debug)]
|
2017-12-09 13:33:40 +01:00
|
|
|
pub enum WriterState {
|
2017-11-03 19:29:44 +01:00
|
|
|
Done,
|
|
|
|
Pause,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send stream
|
2017-12-09 13:33:40 +01:00
|
|
|
pub trait Writer {
|
2017-11-10 21:29:54 +01:00
|
|
|
fn written(&self) -> u64;
|
|
|
|
|
2017-12-09 13:33:40 +01:00
|
|
|
fn start(&mut self, req: &mut HttpMessage, resp: &mut HttpResponse)
|
2017-11-03 19:29:44 +01:00
|
|
|
-> Result<WriterState, io::Error>;
|
|
|
|
|
|
|
|
fn write(&mut self, payload: &[u8]) -> Result<WriterState, io::Error>;
|
|
|
|
|
|
|
|
fn write_eof(&mut self) -> Result<WriterState, io::Error>;
|
|
|
|
|
2017-12-15 04:34:31 +01:00
|
|
|
fn poll_completed(&mut self) -> Poll<(), io::Error>;
|
2017-11-03 19:29:44 +01:00
|
|
|
}
|
|
|
|
|
2017-12-08 06:52:46 +01:00
|
|
|
bitflags! {
|
|
|
|
struct Flags: u8 {
|
|
|
|
const STARTED = 0b0000_0001;
|
|
|
|
const UPGRADE = 0b0000_0010;
|
|
|
|
const KEEPALIVE = 0b0000_0100;
|
|
|
|
const DISCONNECTED = 0b0000_1000;
|
|
|
|
}
|
|
|
|
}
|
2017-11-03 19:29:44 +01:00
|
|
|
|
|
|
|
pub(crate) struct H1Writer<T: AsyncWrite> {
|
2017-12-08 06:52:46 +01:00
|
|
|
flags: Flags,
|
2017-12-14 01:44:35 +01:00
|
|
|
stream: T,
|
2017-11-09 01:44:23 +01:00
|
|
|
encoder: PayloadEncoder,
|
2017-11-10 21:29:54 +01:00
|
|
|
written: u64,
|
2017-12-08 06:52:46 +01:00
|
|
|
headers_size: u32,
|
2017-12-15 04:34:31 +01:00
|
|
|
buffer: SharedBytes,
|
2017-11-03 19:29:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: AsyncWrite> H1Writer<T> {
|
|
|
|
|
2017-12-15 04:34:31 +01:00
|
|
|
pub fn new(stream: T, buf: SharedBytes) -> H1Writer<T> {
|
2017-11-03 19:29:44 +01:00
|
|
|
H1Writer {
|
2017-12-08 06:52:46 +01:00
|
|
|
flags: Flags::empty(),
|
2017-12-14 01:44:35 +01:00
|
|
|
stream: stream,
|
2017-12-15 04:34:31 +01:00
|
|
|
encoder: PayloadEncoder::empty(buf.clone()),
|
2017-11-10 21:29:54 +01:00
|
|
|
written: 0,
|
|
|
|
headers_size: 0,
|
2017-12-15 04:34:31 +01:00
|
|
|
buffer: buf,
|
2017-11-03 19:29:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_mut(&mut self) -> &mut T {
|
2017-12-14 01:44:35 +01:00
|
|
|
&mut self.stream
|
2017-11-03 19:29:44 +01:00
|
|
|
}
|
|
|
|
|
2017-12-14 01:44:35 +01:00
|
|
|
pub fn reset(&mut self) {
|
|
|
|
self.written = 0;
|
|
|
|
self.flags = Flags::empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn into_inner(self) -> T {
|
|
|
|
self.stream
|
2017-11-03 19:29:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn disconnected(&mut self) {
|
2017-11-09 01:44:23 +01:00
|
|
|
self.encoder.get_mut().take();
|
2017-11-03 19:29:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn keepalive(&self) -> bool {
|
2017-12-08 06:52:46 +01:00
|
|
|
self.flags.contains(Flags::KEEPALIVE) && !self.flags.contains(Flags::UPGRADE)
|
2017-11-03 19:29:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn write_to_stream(&mut self) -> Result<WriterState, io::Error> {
|
2017-11-09 01:44:23 +01:00
|
|
|
let buffer = self.encoder.get_mut();
|
|
|
|
|
2017-12-14 01:44:35 +01:00
|
|
|
while !buffer.is_empty() {
|
|
|
|
match self.stream.write(buffer.as_ref()) {
|
|
|
|
Ok(n) => {
|
|
|
|
buffer.split_to(n);
|
|
|
|
self.written += n as u64;
|
|
|
|
},
|
|
|
|
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
|
|
|
|
if buffer.len() > MAX_WRITE_BUFFER_SIZE {
|
|
|
|
return Ok(WriterState::Pause)
|
|
|
|
} else {
|
|
|
|
return Ok(WriterState::Done)
|
2017-11-03 19:29:44 +01:00
|
|
|
}
|
|
|
|
}
|
2017-12-14 01:44:35 +01:00
|
|
|
Err(err) => return Err(err),
|
2017-11-03 19:29:44 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-04 20:33:14 +01:00
|
|
|
Ok(WriterState::Done)
|
2017-11-03 19:29:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: AsyncWrite> Writer for H1Writer<T> {
|
|
|
|
|
2017-12-08 06:52:46 +01:00
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(cast_lossless))]
|
2017-11-10 21:29:54 +01:00
|
|
|
fn written(&self) -> u64 {
|
2017-12-08 06:52:46 +01:00
|
|
|
if self.written > self.headers_size as u64 {
|
|
|
|
self.written - self.headers_size as u64
|
2017-11-10 21:29:54 +01:00
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-09 13:33:40 +01:00
|
|
|
fn start(&mut self, req: &mut HttpMessage, msg: &mut HttpResponse)
|
2017-11-03 19:29:44 +01:00
|
|
|
-> Result<WriterState, io::Error>
|
|
|
|
{
|
2017-12-08 02:38:18 +01:00
|
|
|
trace!("Prepare response with status: {:?}", msg.status());
|
2017-11-03 19:29:44 +01:00
|
|
|
|
|
|
|
// prepare task
|
2017-12-08 06:52:46 +01:00
|
|
|
self.flags.insert(Flags::STARTED);
|
2017-12-15 04:34:31 +01:00
|
|
|
self.encoder = PayloadEncoder::new(self.buffer.clone(), req, msg);
|
2017-12-08 06:52:46 +01:00
|
|
|
if msg.keep_alive().unwrap_or_else(|| req.keep_alive()) {
|
|
|
|
self.flags.insert(Flags::KEEPALIVE);
|
|
|
|
}
|
2017-11-03 19:29:44 +01:00
|
|
|
|
|
|
|
// Connection upgrade
|
2017-12-09 13:33:40 +01:00
|
|
|
let version = msg.version().unwrap_or_else(|| req.version);
|
2017-11-03 19:29:44 +01:00
|
|
|
if msg.upgrade() {
|
2017-12-08 02:38:18 +01:00
|
|
|
msg.headers_mut().insert(CONNECTION, HeaderValue::from_static("upgrade"));
|
2017-11-03 19:29:44 +01:00
|
|
|
}
|
|
|
|
// keep-alive
|
2017-12-08 06:52:46 +01:00
|
|
|
else if self.flags.contains(Flags::KEEPALIVE) {
|
2017-11-03 19:29:44 +01:00
|
|
|
if version < Version::HTTP_11 {
|
2017-12-08 02:38:18 +01:00
|
|
|
msg.headers_mut().insert(CONNECTION, HeaderValue::from_static("keep-alive"));
|
2017-11-03 19:29:44 +01:00
|
|
|
}
|
|
|
|
} else if version >= Version::HTTP_11 {
|
2017-12-08 02:38:18 +01:00
|
|
|
msg.headers_mut().insert(CONNECTION, HeaderValue::from_static("close"));
|
2017-11-03 19:29:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// render message
|
2017-11-09 01:44:23 +01:00
|
|
|
{
|
2017-12-14 01:44:35 +01:00
|
|
|
let mut buffer = self.encoder.get_mut();
|
2017-11-09 01:44:23 +01:00
|
|
|
if let Body::Binary(ref bytes) = *msg.body() {
|
2017-12-15 04:34:31 +01:00
|
|
|
buffer.reserve(256 + msg.headers().len() * AVERAGE_HEADER_SIZE + bytes.len());
|
2017-11-09 01:44:23 +01:00
|
|
|
} else {
|
2017-12-15 04:34:31 +01:00
|
|
|
buffer.reserve(256 + msg.headers().len() * AVERAGE_HEADER_SIZE);
|
2017-11-09 01:44:23 +01:00
|
|
|
}
|
2017-11-03 19:29:44 +01:00
|
|
|
|
2017-12-13 20:10:03 +01:00
|
|
|
match version {
|
2017-12-14 01:44:35 +01:00
|
|
|
Version::HTTP_11 => buffer.extend_from_slice(b"HTTP/1.1 "),
|
|
|
|
Version::HTTP_2 => buffer.extend_from_slice(b"HTTP/2.0 "),
|
|
|
|
Version::HTTP_10 => buffer.extend_from_slice(b"HTTP/1.0 "),
|
|
|
|
Version::HTTP_09 => buffer.extend_from_slice(b"HTTP/0.9 "),
|
2017-11-09 01:44:23 +01:00
|
|
|
}
|
2017-12-14 07:54:52 +01:00
|
|
|
helpers::convert_u16(msg.status().as_u16(), &mut buffer);
|
2017-12-14 01:44:35 +01:00
|
|
|
buffer.extend_from_slice(b" ");
|
|
|
|
buffer.extend_from_slice(msg.reason().as_bytes());
|
|
|
|
buffer.extend_from_slice(b"\r\n");
|
2017-12-13 20:10:03 +01:00
|
|
|
|
2017-12-08 02:38:18 +01:00
|
|
|
for (key, value) in msg.headers() {
|
2017-11-09 01:44:23 +01:00
|
|
|
let t: &[u8] = key.as_ref();
|
2017-12-14 01:44:35 +01:00
|
|
|
buffer.extend_from_slice(t);
|
|
|
|
buffer.extend_from_slice(b": ");
|
|
|
|
buffer.extend_from_slice(value.as_ref());
|
|
|
|
buffer.extend_from_slice(b"\r\n");
|
2017-11-09 01:44:23 +01:00
|
|
|
}
|
2017-11-03 19:29:44 +01:00
|
|
|
|
2017-12-14 07:54:52 +01:00
|
|
|
// using helpers::date is quite a lot faster
|
2017-12-08 02:38:18 +01:00
|
|
|
if !msg.headers().contains_key(DATE) {
|
2017-12-14 07:54:52 +01:00
|
|
|
buffer.reserve(helpers::DATE_VALUE_LENGTH + 8);
|
2017-12-14 01:44:35 +01:00
|
|
|
buffer.extend_from_slice(b"Date: ");
|
2017-12-14 07:54:52 +01:00
|
|
|
helpers::date(&mut buffer);
|
2017-12-14 01:44:35 +01:00
|
|
|
buffer.extend_from_slice(b"\r\n");
|
2017-11-09 01:44:23 +01:00
|
|
|
}
|
2017-11-03 19:29:44 +01:00
|
|
|
|
2017-11-09 01:44:23 +01:00
|
|
|
// msg eof
|
2017-12-14 01:44:35 +01:00
|
|
|
buffer.extend_from_slice(b"\r\n");
|
2017-12-08 06:52:46 +01:00
|
|
|
self.headers_size = buffer.len() as u32;
|
2017-11-03 19:29:44 +01:00
|
|
|
}
|
|
|
|
|
2017-11-09 04:31:25 +01:00
|
|
|
trace!("Response: {:?}", msg);
|
|
|
|
|
2017-11-09 01:44:23 +01:00
|
|
|
if msg.body().is_binary() {
|
|
|
|
let body = msg.replace_body(Body::Empty);
|
|
|
|
if let Body::Binary(bytes) = body {
|
|
|
|
self.encoder.write(bytes.as_ref())?;
|
|
|
|
return Ok(WriterState::Done)
|
|
|
|
}
|
|
|
|
}
|
2017-11-03 19:29:44 +01:00
|
|
|
Ok(WriterState::Done)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write(&mut self, payload: &[u8]) -> Result<WriterState, io::Error> {
|
2017-12-08 06:52:46 +01:00
|
|
|
if !self.flags.contains(Flags::DISCONNECTED) {
|
|
|
|
if self.flags.contains(Flags::STARTED) {
|
2017-11-03 19:29:44 +01:00
|
|
|
// TODO: add warning, write after EOF
|
2017-11-09 01:44:23 +01:00
|
|
|
self.encoder.write(payload)?;
|
2017-11-03 19:29:44 +01:00
|
|
|
} else {
|
2017-11-09 01:44:23 +01:00
|
|
|
// might be response to EXCEPT
|
|
|
|
self.encoder.get_mut().extend_from_slice(payload)
|
2017-11-03 19:29:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-09 01:44:23 +01:00
|
|
|
if self.encoder.len() > MAX_WRITE_BUFFER_SIZE {
|
2017-11-04 20:33:14 +01:00
|
|
|
Ok(WriterState::Pause)
|
2017-11-03 19:29:44 +01:00
|
|
|
} else {
|
2017-11-04 20:33:14 +01:00
|
|
|
Ok(WriterState::Done)
|
2017-11-03 19:29:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_eof(&mut self) -> Result<WriterState, io::Error> {
|
2017-11-09 01:44:23 +01:00
|
|
|
self.encoder.write_eof()?;
|
|
|
|
|
|
|
|
if !self.encoder.is_eof() {
|
2017-11-03 19:29:44 +01:00
|
|
|
//debug!("last payload item, but it is not EOF ");
|
|
|
|
Err(io::Error::new(io::ErrorKind::Other,
|
|
|
|
"Last payload item, but eof is not reached"))
|
2017-11-09 01:44:23 +01:00
|
|
|
} else if self.encoder.len() > MAX_WRITE_BUFFER_SIZE {
|
2017-11-04 20:33:14 +01:00
|
|
|
Ok(WriterState::Pause)
|
2017-11-03 19:29:44 +01:00
|
|
|
} else {
|
2017-11-04 20:33:14 +01:00
|
|
|
Ok(WriterState::Done)
|
2017-11-03 19:29:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-15 04:34:31 +01:00
|
|
|
fn poll_completed(&mut self) -> Poll<(), io::Error> {
|
2017-11-03 19:29:44 +01:00
|
|
|
match self.write_to_stream() {
|
|
|
|
Ok(WriterState::Done) => Ok(Async::Ready(())),
|
|
|
|
Ok(WriterState::Pause) => Ok(Async::NotReady),
|
|
|
|
Err(err) => Err(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|