2018-02-26 23:33:56 +01:00
|
|
|
#![cfg_attr(feature = "cargo-clippy", allow(redundant_field_names))]
|
|
|
|
|
2017-12-14 01:44:35 +01:00
|
|
|
use bytes::{Bytes, BytesMut};
|
2017-11-04 17:07:44 +01:00
|
|
|
use futures::{Async, Poll};
|
2018-01-03 04:37:33 +01:00
|
|
|
use http2::server::SendResponse;
|
2018-04-14 01:02:01 +02:00
|
|
|
use http2::{Reason, SendStream};
|
2018-03-31 02:31:18 +02:00
|
|
|
use modhttp::Response;
|
2018-04-14 01:02:01 +02:00
|
|
|
use std::rc::Rc;
|
|
|
|
use std::{cmp, io};
|
2018-03-31 02:31:18 +02:00
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
use http::header::{HeaderValue, CONNECTION, CONTENT_LENGTH, DATE, TRANSFER_ENCODING};
|
|
|
|
use http::{HttpTryFrom, Version};
|
2017-11-04 17:07:44 +01:00
|
|
|
|
2018-06-24 04:54:01 +02:00
|
|
|
use super::encoding::{ContentEncoder, Output};
|
2018-04-14 01:02:01 +02:00
|
|
|
use super::helpers;
|
2018-03-18 19:05:44 +01:00
|
|
|
use super::settings::WorkerSettings;
|
2018-04-14 01:02:01 +02:00
|
|
|
use super::shared::SharedBytes;
|
2018-01-12 03:41:33 +01:00
|
|
|
use super::{Writer, WriterState, MAX_WRITE_BUFFER_SIZE};
|
2018-04-14 01:02:01 +02:00
|
|
|
use body::{Binary, Body};
|
|
|
|
use header::ContentEncoding;
|
|
|
|
use httprequest::HttpInnerMessage;
|
|
|
|
use httpresponse::HttpResponse;
|
2017-11-04 17:07:44 +01:00
|
|
|
|
|
|
|
const CHUNK_SIZE: usize = 16_384;
|
|
|
|
|
2017-12-08 06:52:46 +01:00
|
|
|
bitflags! {
|
|
|
|
struct Flags: u8 {
|
|
|
|
const STARTED = 0b0000_0001;
|
|
|
|
const DISCONNECTED = 0b0000_0010;
|
|
|
|
const EOF = 0b0000_0100;
|
2018-03-12 00:37:44 +01:00
|
|
|
const RESERVED = 0b0000_1000;
|
2017-12-08 06:52:46 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-04 17:07:44 +01:00
|
|
|
|
2018-03-18 19:05:44 +01:00
|
|
|
pub(crate) struct H2Writer<H: 'static> {
|
2018-01-03 04:37:33 +01:00
|
|
|
respond: SendResponse<Bytes>,
|
2017-11-04 17:07:44 +01:00
|
|
|
stream: Option<SendStream<Bytes>>,
|
2017-12-08 06:52:46 +01:00
|
|
|
flags: Flags,
|
2017-11-10 21:29:54 +01:00
|
|
|
written: u64,
|
2018-06-24 04:54:01 +02:00
|
|
|
buffer: Output,
|
2018-03-09 19:00:15 +01:00
|
|
|
buffer_capacity: usize,
|
2018-03-18 19:05:44 +01:00
|
|
|
settings: Rc<WorkerSettings<H>>,
|
2017-11-04 17:07:44 +01:00
|
|
|
}
|
|
|
|
|
2018-03-18 19:05:44 +01:00
|
|
|
impl<H: 'static> H2Writer<H> {
|
2018-04-14 01:02:01 +02:00
|
|
|
pub fn new(
|
2018-04-29 07:55:47 +02:00
|
|
|
respond: SendResponse<Bytes>, buf: SharedBytes, settings: Rc<WorkerSettings<H>>,
|
2018-04-14 01:02:01 +02:00
|
|
|
) -> H2Writer<H> {
|
2017-11-04 17:07:44 +01:00
|
|
|
H2Writer {
|
2018-02-26 23:33:56 +01:00
|
|
|
respond,
|
2018-03-18 19:05:44 +01:00
|
|
|
settings,
|
2017-11-04 17:07:44 +01:00
|
|
|
stream: None,
|
2017-12-08 06:52:46 +01:00
|
|
|
flags: Flags::empty(),
|
2017-11-10 21:29:54 +01:00
|
|
|
written: 0,
|
2018-06-24 04:54:01 +02:00
|
|
|
buffer: Output::Buffer(buf),
|
2018-03-09 19:00:15 +01:00
|
|
|
buffer_capacity: 0,
|
2017-11-04 17:07:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reset(&mut self, reason: Reason) {
|
|
|
|
if let Some(mut stream) = self.stream.take() {
|
|
|
|
stream.send_reset(reason)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 19:05:44 +01:00
|
|
|
impl<H: 'static> Writer for H2Writer<H> {
|
2017-11-10 21:29:54 +01:00
|
|
|
fn written(&self) -> u64 {
|
|
|
|
self.written
|
|
|
|
}
|
|
|
|
|
2018-05-21 05:37:19 +02:00
|
|
|
#[inline]
|
|
|
|
fn set_date(&self, dst: &mut BytesMut) {
|
2018-06-23 06:13:09 +02:00
|
|
|
self.settings.set_date(dst, true)
|
2018-05-21 05:37:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-06-19 03:44:01 +02:00
|
|
|
fn buffer(&mut self) -> &mut BytesMut {
|
2018-05-21 05:37:19 +02:00
|
|
|
self.buffer.get_mut()
|
|
|
|
}
|
|
|
|
|
2018-04-14 01:02:01 +02:00
|
|
|
fn start(
|
|
|
|
&mut self, req: &mut HttpInnerMessage, msg: &mut HttpResponse,
|
|
|
|
encoding: ContentEncoding,
|
|
|
|
) -> io::Result<WriterState> {
|
2017-11-04 17:07:44 +01:00
|
|
|
// prepare response
|
2017-12-08 06:52:46 +01:00
|
|
|
self.flags.insert(Flags::STARTED);
|
2018-06-24 04:54:01 +02:00
|
|
|
self.buffer = ContentEncoder::for_server(self.buffer.take(), req, msg, encoding);
|
2017-11-04 17:07:44 +01:00
|
|
|
|
|
|
|
// http2 specific
|
2017-12-08 02:38:18 +01:00
|
|
|
msg.headers_mut().remove(CONNECTION);
|
|
|
|
msg.headers_mut().remove(TRANSFER_ENCODING);
|
2017-11-04 17:07: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 01:44:35 +01:00
|
|
|
let mut bytes = BytesMut::with_capacity(29);
|
2018-06-23 06:13:09 +02:00
|
|
|
self.settings.set_date(&mut bytes, false);
|
2018-04-14 01:02:01 +02:00
|
|
|
msg.headers_mut()
|
|
|
|
.insert(DATE, HeaderValue::try_from(bytes.freeze()).unwrap());
|
2017-11-04 17:07:44 +01:00
|
|
|
}
|
|
|
|
|
2017-12-31 22:22:11 +01:00
|
|
|
let body = msg.replace_body(Body::Empty);
|
|
|
|
match body {
|
2017-12-16 16:29:15 +01:00
|
|
|
Body::Binary(ref bytes) => {
|
2018-06-12 23:47:45 +02:00
|
|
|
if bytes.is_empty() {
|
|
|
|
msg.headers_mut()
|
|
|
|
.insert(CONTENT_LENGTH, HeaderValue::from_static("0"));
|
|
|
|
self.flags.insert(Flags::EOF);
|
|
|
|
} else {
|
|
|
|
let mut val = BytesMut::new();
|
|
|
|
helpers::convert_usize(bytes.len(), &mut val);
|
|
|
|
let l = val.len();
|
|
|
|
msg.headers_mut().insert(
|
|
|
|
CONTENT_LENGTH,
|
|
|
|
HeaderValue::try_from(val.split_to(l - 2).freeze()).unwrap(),
|
|
|
|
);
|
|
|
|
}
|
2017-12-16 16:29:15 +01:00
|
|
|
}
|
|
|
|
Body::Empty => {
|
2018-06-12 23:47:45 +02:00
|
|
|
self.flags.insert(Flags::EOF);
|
2018-04-29 18:09:08 +02:00
|
|
|
msg.headers_mut()
|
|
|
|
.insert(CONTENT_LENGTH, HeaderValue::from_static("0"));
|
2018-04-14 01:02:01 +02:00
|
|
|
}
|
2017-12-16 16:29:15 +01:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
2017-12-31 22:22:11 +01:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2018-05-17 21:20:20 +02:00
|
|
|
match self
|
|
|
|
.respond
|
2018-04-29 18:09:08 +02:00
|
|
|
.send_response(resp, self.flags.contains(Flags::EOF))
|
|
|
|
{
|
2018-04-14 01:02:01 +02:00
|
|
|
Ok(stream) => self.stream = Some(stream),
|
|
|
|
Err(_) => return Err(io::Error::new(io::ErrorKind::Other, "err")),
|
2017-11-09 01:44:23 +01:00
|
|
|
}
|
|
|
|
|
2017-12-31 22:22:11 +01:00
|
|
|
trace!("Response: {:?}", msg);
|
2017-11-09 04:31:25 +01:00
|
|
|
|
2017-12-31 22:22:11 +01:00
|
|
|
if let Body::Binary(bytes) = body {
|
2018-06-12 23:47:45 +02:00
|
|
|
if bytes.is_empty() {
|
|
|
|
Ok(WriterState::Done)
|
|
|
|
} else {
|
|
|
|
self.flags.insert(Flags::EOF);
|
|
|
|
self.written = bytes.len() as u64;
|
2018-06-24 04:54:01 +02:00
|
|
|
self.buffer.write(bytes.as_ref())?;
|
2018-06-12 23:47:45 +02:00
|
|
|
if let Some(ref mut stream) = self.stream {
|
|
|
|
self.flags.insert(Flags::RESERVED);
|
|
|
|
stream.reserve_capacity(cmp::min(self.buffer.len(), CHUNK_SIZE));
|
|
|
|
}
|
|
|
|
Ok(WriterState::Pause)
|
2017-11-04 17:07:44 +01:00
|
|
|
}
|
2017-12-31 22:22:11 +01:00
|
|
|
} else {
|
|
|
|
msg.replace_body(body);
|
2018-03-09 19:00:15 +01:00
|
|
|
self.buffer_capacity = msg.write_buffer_capacity();
|
2017-12-31 22:22:11 +01:00
|
|
|
Ok(WriterState::Done)
|
2017-11-04 17:07:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-19 03:44:01 +02:00
|
|
|
fn write(&mut self, payload: &Binary) -> io::Result<WriterState> {
|
2018-01-02 23:53:51 +01:00
|
|
|
self.written = payload.len() as u64;
|
|
|
|
|
2017-12-08 06:52:46 +01:00
|
|
|
if !self.flags.contains(Flags::DISCONNECTED) {
|
|
|
|
if self.flags.contains(Flags::STARTED) {
|
2017-11-04 17:07:44 +01:00
|
|
|
// TODO: add warning, write after EOF
|
2018-06-24 04:54:01 +02:00
|
|
|
self.buffer.write(payload.as_ref())?;
|
2017-11-04 17:07:44 +01:00
|
|
|
} else {
|
|
|
|
// might be response for EXCEPT
|
2018-06-24 04:54:01 +02:00
|
|
|
error!("Not supported");
|
2017-11-04 17:07:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-12 00:37:44 +01:00
|
|
|
if self.buffer.len() > self.buffer_capacity {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-14 22:50:38 +01:00
|
|
|
fn write_eof(&mut self) -> io::Result<WriterState> {
|
2017-12-08 06:52:46 +01:00
|
|
|
self.flags.insert(Flags::EOF);
|
2018-06-24 04:54:01 +02:00
|
|
|
if !self.buffer.write_eof()? {
|
2018-04-14 01:02:01 +02:00
|
|
|
Err(io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
|
|
|
"Last payload item, but eof is not reached",
|
|
|
|
))
|
2018-01-15 02:00:28 +01:00
|
|
|
} else if self.buffer.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 03:21:34 +01:00
|
|
|
fn poll_completed(&mut self, _shutdown: bool) -> Poll<(), io::Error> {
|
2018-03-12 00:37:44 +01:00
|
|
|
if !self.flags.contains(Flags::STARTED) {
|
|
|
|
return Ok(Async::NotReady);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ref mut stream) = self.stream {
|
|
|
|
// reserve capacity
|
|
|
|
if !self.flags.contains(Flags::RESERVED) && !self.buffer.is_empty() {
|
|
|
|
self.flags.insert(Flags::RESERVED);
|
|
|
|
stream.reserve_capacity(cmp::min(self.buffer.len(), CHUNK_SIZE));
|
|
|
|
}
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match stream.poll_capacity() {
|
|
|
|
Ok(Async::NotReady) => return Ok(Async::NotReady),
|
|
|
|
Ok(Async::Ready(None)) => return Ok(Async::Ready(())),
|
|
|
|
Ok(Async::Ready(Some(cap))) => {
|
|
|
|
let len = self.buffer.len();
|
|
|
|
let bytes = self.buffer.split_to(cmp::min(cap, len));
|
2018-04-14 01:02:01 +02:00
|
|
|
let eof =
|
|
|
|
self.buffer.is_empty() && self.flags.contains(Flags::EOF);
|
2018-03-12 00:37:44 +01:00
|
|
|
self.written += bytes.len() as u64;
|
|
|
|
|
|
|
|
if let Err(e) = stream.send_data(bytes.freeze(), eof) {
|
2018-04-14 01:02:01 +02:00
|
|
|
return Err(io::Error::new(io::ErrorKind::Other, e));
|
2018-03-12 00:37:44 +01:00
|
|
|
} else if !self.buffer.is_empty() {
|
|
|
|
let cap = cmp::min(self.buffer.len(), CHUNK_SIZE);
|
|
|
|
stream.reserve_capacity(cap);
|
|
|
|
} else {
|
2018-03-12 17:04:54 +01:00
|
|
|
self.flags.remove(Flags::RESERVED);
|
2018-04-14 01:02:01 +02:00
|
|
|
return Ok(Async::NotReady);
|
2018-03-12 00:37:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e)),
|
|
|
|
}
|
|
|
|
}
|
2017-11-04 17:07:44 +01:00
|
|
|
}
|
2018-03-12 18:01:56 +01:00
|
|
|
Ok(Async::NotReady)
|
2017-11-04 17:07:44 +01:00
|
|
|
}
|
|
|
|
}
|