1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 17:52:56 +01:00

fix http/2 support

This commit is contained in:
Nikolay Kim 2017-12-31 13:22:11 -08:00
parent 73e2773a10
commit 967d3244d7
5 changed files with 43 additions and 37 deletions

View File

@ -8,7 +8,8 @@ use std::io::Read;
use actix::*;
use actix_web::*;
#[cfg(target_os = "linux")] use actix::actors::signal::{ProcessSignals, Subscribe};
#[cfg(target_os = "linux")]
use actix::actors::signal::{ProcessSignals, Subscribe};
/// somple handle
fn index(req: HttpRequest) -> Result<HttpResponse> {
@ -47,7 +48,9 @@ fn main() {
.bind("127.0.0.1:8443").unwrap()
.start_ssl(&pkcs12).unwrap();
if cfg!(target_os = "linux") { // Subscribe to unix signals
// Subscribe to unix signals
#[cfg(target_os = "linux")]
{
let signals = Arbiter::system_registry().get::<ProcessSignals>();
signals.send(Subscribe(addr.subscriber()));
}

View File

@ -192,7 +192,6 @@ impl<T: AsyncWrite> Writer for H1Writer<T> {
if let Body::Binary(bytes) = body {
self.encoder.write(bytes.as_ref())?;
return Ok(WriterState::Done)
} else {
msg.replace_body(body);
}

View File

@ -92,7 +92,7 @@ impl H2Writer {
let cap = cmp::min(buffer.len(), CHUNK_SIZE);
stream.reserve_capacity(cap);
} else {
return Ok(WriterState::Done)
return Ok(WriterState::Pause)
}
}
Err(_) => {
@ -130,9 +130,23 @@ impl Writer for H2Writer {
// using helpers::date is quite a lot faster
if !msg.headers().contains_key(DATE) {
let mut bytes = BytesMut::with_capacity(29);
helpers::date(&mut bytes);
msg.headers_mut().insert(
DATE, HeaderValue::try_from(bytes.freeze()).unwrap());
helpers::date_value(&mut bytes);
msg.headers_mut().insert(DATE, HeaderValue::try_from(bytes.freeze()).unwrap());
}
let body = msg.replace_body(Body::Empty);
match body {
Body::Binary(ref bytes) => {
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());
}
Body::Empty => {
msg.headers_mut().insert(CONTENT_LENGTH, HeaderValue::from_static("0"));
},
_ => (),
}
let mut resp = Response::new(());
@ -142,19 +156,6 @@ impl Writer for H2Writer {
resp.headers_mut().insert(key, value.clone());
}
match *msg.body() {
Body::Binary(ref bytes) => {
let mut val = BytesMut::new();
helpers::convert_usize(bytes.len(), &mut val);
resp.headers_mut().insert(
CONTENT_LENGTH, HeaderValue::try_from(val.freeze()).unwrap());
}
Body::Empty => {
resp.headers_mut().insert(CONTENT_LENGTH, HeaderValue::from_static("0"));
},
_ => (),
}
match self.respond.send_response(resp, self.flags.contains(Flags::EOF)) {
Ok(stream) =>
self.stream = Some(stream),
@ -162,20 +163,19 @@ impl Writer for H2Writer {
return Err(io::Error::new(io::ErrorKind::Other, "err")),
}
// trace!("Response: {:?}", msg);
trace!("Response: {:?}", msg);
if msg.body().is_binary() {
if let Body::Binary(bytes) = msg.replace_body(Body::Empty) {
self.flags.insert(Flags::EOF);
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)
if let Body::Binary(bytes) = body {
self.flags.insert(Flags::EOF);
self.encoder.write(bytes.as_ref())?;
if let Some(ref mut stream) = self.stream {
stream.reserve_capacity(cmp::min(self.encoder.len(), CHUNK_SIZE));
}
Ok(WriterState::Pause)
} else {
msg.replace_body(body);
Ok(WriterState::Done)
}
Ok(WriterState::Done)
}
fn write(&mut self, payload: &[u8]) -> Result<WriterState, io::Error> {

View File

@ -11,9 +11,9 @@ use http::Version;
use httprequest::HttpMessage;
// "Sun, 06 Nov 1994 08:49:37 GMT".len()
pub const DATE_VALUE_LENGTH: usize = 29;
pub(crate) const DATE_VALUE_LENGTH: usize = 29;
pub fn date(dst: &mut BytesMut) {
pub(crate) fn date(dst: &mut BytesMut) {
CACHED.with(|cache| {
let mut buf: [u8; 39] = unsafe { mem::uninitialized() };
buf[..6].copy_from_slice(b"date: ");
@ -23,7 +23,13 @@ pub fn date(dst: &mut BytesMut) {
})
}
pub fn update_date() {
pub(crate) fn date_value(dst: &mut BytesMut) {
CACHED.with(|cache| {
dst.extend_from_slice(cache.borrow().buffer());
})
}
pub(crate) fn update_date() {
CACHED.with(|cache| {
cache.borrow_mut().update();
});

View File

@ -576,12 +576,10 @@ impl<S, H> ProcessResponse<S, H> {
{
if self.drain.is_none() && self.running != RunningState::Paused {
// if task is paused, write buffer is probably full
loop {
let result = match mem::replace(&mut self.iostate, IOState::Done) {
IOState::Response => {
let result = match io.start(info.req_mut().get_inner(),
&mut self.resp) {
let result = match io.start(info.req_mut().get_inner(), &mut self.resp) {
Ok(res) => res,
Err(err) => {
info.error = Some(err.into());