2017-11-09 01:44:23 +01:00
|
|
|
use std::io;
|
2017-11-03 19:29:44 +01:00
|
|
|
use std::fmt::Write;
|
|
|
|
use futures::{Async, Poll};
|
|
|
|
use tokio_io::AsyncWrite;
|
|
|
|
use http::{Version, StatusCode};
|
2017-11-09 01:44:23 +01:00
|
|
|
use http::header::{HeaderValue, CONNECTION, CONTENT_TYPE, DATE};
|
2017-11-03 19:29:44 +01:00
|
|
|
|
|
|
|
use date;
|
|
|
|
use body::Body;
|
2017-11-09 01:44:23 +01:00
|
|
|
use encoding::PayloadEncoder;
|
2017-11-03 19:29:44 +01:00
|
|
|
use httprequest::HttpRequest;
|
|
|
|
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-11-03 19:29:44 +01:00
|
|
|
pub(crate) enum WriterState {
|
|
|
|
Done,
|
|
|
|
Pause,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send stream
|
|
|
|
pub(crate) trait Writer {
|
|
|
|
fn start(&mut self, req: &mut HttpRequest, resp: &mut HttpResponse)
|
|
|
|
-> Result<WriterState, io::Error>;
|
|
|
|
|
|
|
|
fn write(&mut self, payload: &[u8]) -> Result<WriterState, io::Error>;
|
|
|
|
|
|
|
|
fn write_eof(&mut self) -> Result<WriterState, io::Error>;
|
|
|
|
|
|
|
|
fn poll_complete(&mut self) -> Poll<(), io::Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub(crate) struct H1Writer<T: AsyncWrite> {
|
|
|
|
stream: Option<T>,
|
|
|
|
started: bool,
|
2017-11-09 01:44:23 +01:00
|
|
|
encoder: PayloadEncoder,
|
2017-11-03 19:29:44 +01:00
|
|
|
upgrade: bool,
|
|
|
|
keepalive: bool,
|
|
|
|
disconnected: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: AsyncWrite> H1Writer<T> {
|
|
|
|
|
|
|
|
pub fn new(stream: T) -> H1Writer<T> {
|
|
|
|
H1Writer {
|
|
|
|
stream: Some(stream),
|
|
|
|
started: false,
|
2017-11-09 01:44:23 +01:00
|
|
|
encoder: PayloadEncoder::default(),
|
2017-11-03 19:29:44 +01:00
|
|
|
upgrade: false,
|
|
|
|
keepalive: false,
|
|
|
|
disconnected: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_mut(&mut self) -> &mut T {
|
|
|
|
self.stream.as_mut().unwrap()
|
|
|
|
}
|
|
|
|
|
2017-11-04 20:33:14 +01:00
|
|
|
pub fn unwrap(&mut self) -> T {
|
2017-11-03 19:29:44 +01:00
|
|
|
self.stream.take().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
self.keepalive && !self.upgrade
|
|
|
|
}
|
|
|
|
|
|
|
|
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-11-03 19:29:44 +01:00
|
|
|
if let Some(ref mut stream) = self.stream {
|
2017-11-09 01:44:23 +01:00
|
|
|
while !buffer.is_empty() {
|
|
|
|
match stream.write(buffer.as_ref()) {
|
2017-11-03 19:29:44 +01:00
|
|
|
Ok(n) => {
|
2017-11-09 01:44:23 +01:00
|
|
|
buffer.split_to(n);
|
2017-11-03 19:29:44 +01:00
|
|
|
},
|
|
|
|
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
|
2017-11-09 01:44:23 +01:00
|
|
|
if buffer.len() > MAX_WRITE_BUFFER_SIZE {
|
2017-11-03 19:29:44 +01:00
|
|
|
return Ok(WriterState::Pause)
|
|
|
|
} else {
|
|
|
|
return Ok(WriterState::Done)
|
|
|
|
}
|
|
|
|
}
|
2017-11-04 20:33:14 +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> {
|
|
|
|
|
|
|
|
fn start(&mut self, req: &mut HttpRequest, msg: &mut HttpResponse)
|
|
|
|
-> Result<WriterState, io::Error>
|
|
|
|
{
|
|
|
|
trace!("Prepare message status={:?}", msg.status);
|
|
|
|
|
|
|
|
// prepare task
|
|
|
|
self.started = true;
|
2017-11-09 01:44:23 +01:00
|
|
|
self.encoder = PayloadEncoder::new(req, msg);
|
2017-11-03 19:29:44 +01:00
|
|
|
self.keepalive = msg.keep_alive().unwrap_or_else(|| req.keep_alive());
|
|
|
|
|
|
|
|
// Connection upgrade
|
2017-11-09 01:44:23 +01:00
|
|
|
let version = msg.version().unwrap_or_else(|| req.version());
|
2017-11-03 19:29:44 +01:00
|
|
|
if msg.upgrade() {
|
|
|
|
msg.headers.insert(CONNECTION, HeaderValue::from_static("upgrade"));
|
|
|
|
}
|
|
|
|
// keep-alive
|
|
|
|
else if self.keepalive {
|
|
|
|
if version < Version::HTTP_11 {
|
|
|
|
msg.headers.insert(CONNECTION, HeaderValue::from_static("keep-alive"));
|
|
|
|
}
|
|
|
|
} else if version >= Version::HTTP_11 {
|
|
|
|
msg.headers.insert(CONNECTION, HeaderValue::from_static("close"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// render message
|
2017-11-09 01:44:23 +01:00
|
|
|
{
|
|
|
|
let buffer = self.encoder.get_mut();
|
|
|
|
if let Body::Binary(ref bytes) = *msg.body() {
|
|
|
|
buffer.reserve(100 + msg.headers.len() * AVERAGE_HEADER_SIZE + bytes.len());
|
|
|
|
} else {
|
|
|
|
buffer.reserve(100 + msg.headers.len() * AVERAGE_HEADER_SIZE);
|
|
|
|
}
|
2017-11-03 19:29:44 +01:00
|
|
|
|
2017-11-09 01:44:23 +01:00
|
|
|
if version == Version::HTTP_11 && msg.status == StatusCode::OK {
|
|
|
|
buffer.extend(b"HTTP/1.1 200 OK\r\n");
|
|
|
|
} else {
|
|
|
|
let _ = write!(buffer, "{:?} {}\r\n", version, msg.status);
|
|
|
|
}
|
|
|
|
for (key, value) in &msg.headers {
|
|
|
|
let t: &[u8] = key.as_ref();
|
|
|
|
buffer.extend(t);
|
|
|
|
buffer.extend(b": ");
|
|
|
|
buffer.extend(value.as_ref());
|
|
|
|
buffer.extend(b"\r\n");
|
|
|
|
}
|
2017-11-03 19:29:44 +01:00
|
|
|
|
2017-11-09 01:44:23 +01:00
|
|
|
// using http::h1::date is quite a lot faster than generating
|
|
|
|
// a unique Date header each time like req/s goes up about 10%
|
|
|
|
if !msg.headers.contains_key(DATE) {
|
|
|
|
buffer.reserve(date::DATE_VALUE_LENGTH + 8);
|
|
|
|
buffer.extend(b"Date: ");
|
|
|
|
let mut bytes = [0u8; 29];
|
|
|
|
date::extend(&mut bytes[..]);
|
|
|
|
buffer.extend(&bytes);
|
|
|
|
buffer.extend(b"\r\n");
|
|
|
|
}
|
2017-11-03 19:29:44 +01:00
|
|
|
|
2017-11-09 01:44:23 +01:00
|
|
|
// default content-type
|
|
|
|
if !msg.headers.contains_key(CONTENT_TYPE) {
|
|
|
|
buffer.extend(b"ContentType: application/octet-stream\r\n".as_ref());
|
|
|
|
}
|
2017-11-03 19:29:44 +01:00
|
|
|
|
2017-11-09 01:44:23 +01:00
|
|
|
// msg eof
|
|
|
|
buffer.extend(b"\r\n");
|
2017-11-03 19:29:44 +01:00
|
|
|
}
|
|
|
|
|
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> {
|
|
|
|
if !self.disconnected {
|
|
|
|
if self.started {
|
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll_complete(&mut self) -> Poll<(), io::Error> {
|
|
|
|
match self.write_to_stream() {
|
|
|
|
Ok(WriterState::Done) => Ok(Async::Ready(())),
|
|
|
|
Ok(WriterState::Pause) => Ok(Async::NotReady),
|
|
|
|
Err(err) => Err(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|