2017-11-04 17:07:44 +01:00
|
|
|
use std::{io, cmp};
|
2017-11-09 01:44:23 +01:00
|
|
|
use bytes::Bytes;
|
2017-11-04 17:07:44 +01:00
|
|
|
use futures::{Async, Poll};
|
|
|
|
use http2::{Reason, SendStream};
|
|
|
|
use http2::server::Respond;
|
|
|
|
use http::{Version, HttpTryFrom, Response};
|
2017-11-09 01:44:23 +01:00
|
|
|
use http::header::{HeaderValue, CONNECTION, CONTENT_TYPE, TRANSFER_ENCODING, DATE};
|
2017-11-04 17:07:44 +01:00
|
|
|
|
|
|
|
use date;
|
|
|
|
use body::Body;
|
2017-11-09 01:44:23 +01:00
|
|
|
use encoding::PayloadEncoder;
|
2017-11-04 17:07:44 +01:00
|
|
|
use httprequest::HttpRequest;
|
|
|
|
use httpresponse::HttpResponse;
|
|
|
|
use h1writer::{Writer, WriterState};
|
|
|
|
|
|
|
|
const CHUNK_SIZE: usize = 16_384;
|
|
|
|
const MAX_WRITE_BUFFER_SIZE: usize = 65_536; // max buffer size 64k
|
|
|
|
|
|
|
|
|
|
|
|
pub(crate) struct H2Writer {
|
|
|
|
respond: Respond<Bytes>,
|
|
|
|
stream: Option<SendStream<Bytes>>,
|
|
|
|
started: bool,
|
2017-11-09 01:44:23 +01:00
|
|
|
encoder: PayloadEncoder,
|
2017-11-04 17:07:44 +01:00
|
|
|
disconnected: bool,
|
|
|
|
eof: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl H2Writer {
|
|
|
|
|
|
|
|
pub fn new(respond: Respond<Bytes>) -> H2Writer {
|
|
|
|
H2Writer {
|
|
|
|
respond: respond,
|
|
|
|
stream: None,
|
|
|
|
started: false,
|
2017-11-09 01:44:23 +01:00
|
|
|
encoder: PayloadEncoder::default(),
|
2017-11-04 17:07:44 +01:00
|
|
|
disconnected: false,
|
|
|
|
eof: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reset(&mut self, reason: Reason) {
|
|
|
|
if let Some(mut stream) = self.stream.take() {
|
|
|
|
stream.send_reset(reason)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_to_stream(&mut self) -> Result<WriterState, io::Error> {
|
|
|
|
if !self.started {
|
|
|
|
return Ok(WriterState::Done)
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ref mut stream) = self.stream {
|
2017-11-09 01:44:23 +01:00
|
|
|
let buffer = self.encoder.get_mut();
|
|
|
|
|
|
|
|
if buffer.is_empty() {
|
2017-11-04 17:07:44 +01:00
|
|
|
if self.eof {
|
|
|
|
let _ = stream.send_data(Bytes::new(), true);
|
|
|
|
}
|
|
|
|
return Ok(WriterState::Done)
|
|
|
|
}
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match stream.poll_capacity() {
|
|
|
|
Ok(Async::NotReady) => {
|
2017-11-09 01:44:23 +01:00
|
|
|
if buffer.len() > MAX_WRITE_BUFFER_SIZE {
|
2017-11-04 17:07:44 +01:00
|
|
|
return Ok(WriterState::Pause)
|
|
|
|
} else {
|
|
|
|
return Ok(WriterState::Done)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(Async::Ready(None)) => {
|
|
|
|
return Ok(WriterState::Done)
|
|
|
|
}
|
|
|
|
Ok(Async::Ready(Some(cap))) => {
|
2017-11-09 01:44:23 +01:00
|
|
|
let len = buffer.len();
|
|
|
|
let bytes = buffer.split_to(cmp::min(cap, len));
|
|
|
|
let eof = buffer.is_empty() && self.eof;
|
2017-11-04 17:07:44 +01:00
|
|
|
|
2017-11-04 20:33:14 +01:00
|
|
|
if let Err(err) = stream.send_data(bytes.freeze(), eof) {
|
|
|
|
return Err(io::Error::new(io::ErrorKind::Other, err))
|
2017-11-09 01:44:23 +01:00
|
|
|
} else if !buffer.is_empty() {
|
|
|
|
let cap = cmp::min(buffer.len(), CHUNK_SIZE);
|
2017-11-04 20:33:14 +01:00
|
|
|
stream.reserve_capacity(cap);
|
2017-11-04 17:07:44 +01:00
|
|
|
} else {
|
2017-11-04 20:33:14 +01:00
|
|
|
return Ok(WriterState::Done)
|
2017-11-04 17:07:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
return Err(io::Error::new(io::ErrorKind::Other, ""))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-04 20:33:14 +01:00
|
|
|
Ok(WriterState::Done)
|
2017-11-04 17:07:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Writer for H2Writer {
|
|
|
|
|
2017-11-09 01:44:23 +01:00
|
|
|
fn start(&mut self, req: &mut HttpRequest, msg: &mut HttpResponse)
|
2017-11-04 17:07:44 +01:00
|
|
|
-> Result<WriterState, io::Error>
|
|
|
|
{
|
|
|
|
trace!("Prepare message status={:?}", msg);
|
|
|
|
|
|
|
|
// prepare response
|
|
|
|
self.started = true;
|
2017-11-09 01:44:23 +01:00
|
|
|
self.encoder = PayloadEncoder::new(req, msg);
|
|
|
|
self.eof = if let Body::Empty = *msg.body() { true } else { false };
|
2017-11-04 17:07:44 +01:00
|
|
|
|
|
|
|
// http2 specific
|
|
|
|
msg.headers.remove(CONNECTION);
|
|
|
|
msg.headers.remove(TRANSFER_ENCODING);
|
|
|
|
|
|
|
|
// 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) {
|
2017-11-09 01:44:23 +01:00
|
|
|
let mut bytes = [0u8; 29];
|
|
|
|
date::extend(&mut bytes[..]);
|
|
|
|
msg.headers.insert(DATE, HeaderValue::try_from(&bytes[..]).unwrap());
|
2017-11-04 17:07:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// default content-type
|
|
|
|
if !msg.headers.contains_key(CONTENT_TYPE) {
|
|
|
|
msg.headers.insert(
|
|
|
|
CONTENT_TYPE, HeaderValue::from_static("application/octet-stream"));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut resp = Response::new(());
|
|
|
|
*resp.status_mut() = msg.status;
|
|
|
|
*resp.version_mut() = Version::HTTP_2;
|
|
|
|
for (key, value) in msg.headers().iter() {
|
|
|
|
resp.headers_mut().insert(key, value.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.respond.send_response(resp, self.eof) {
|
2017-11-09 01:44:23 +01:00
|
|
|
Ok(stream) =>
|
|
|
|
self.stream = Some(stream),
|
|
|
|
Err(_) =>
|
|
|
|
return Err(io::Error::new(io::ErrorKind::Other, "err")),
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.body().is_binary() {
|
|
|
|
if let Body::Binary(bytes) = msg.replace_body(Body::Empty) {
|
|
|
|
self.eof = true;
|
|
|
|
self.encoder.write(bytes.as_ref())?;
|
|
|
|
if let Some(ref mut stream) = self.stream {
|
|
|
|
stream.reserve_capacity(cmp::min(self.encoder.len(), CHUNK_SIZE));
|
|
|
|
}
|
|
|
|
return Ok(WriterState::Done)
|
2017-11-04 17:07: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-04 17:07:44 +01:00
|
|
|
} else {
|
|
|
|
// might be response for EXCEPT
|
2017-11-09 01:44:23 +01:00
|
|
|
self.encoder.get_mut().extend_from_slice(payload)
|
2017-11-04 17:07: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-04 17:07:44 +01:00
|
|
|
} else {
|
2017-11-04 20:33:14 +01:00
|
|
|
Ok(WriterState::Done)
|
2017-11-04 17:07:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_eof(&mut self) -> Result<WriterState, io::Error> {
|
2017-11-09 01:44:23 +01:00
|
|
|
self.encoder.write_eof()?;
|
|
|
|
|
2017-11-04 17:07:44 +01:00
|
|
|
self.eof = true;
|
2017-11-09 01:44:23 +01:00
|
|
|
if !self.encoder.is_eof() {
|
2017-11-04 17:07:44 +01:00
|
|
|
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-04 17:07:44 +01:00
|
|
|
} else {
|
2017-11-04 20:33:14 +01:00
|
|
|
Ok(WriterState::Done)
|
2017-11-04 17:07: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|