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::*;
use actix_web::*; 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 /// somple handle
fn index(req: HttpRequest) -> Result<HttpResponse> { fn index(req: HttpRequest) -> Result<HttpResponse> {
@ -47,7 +48,9 @@ fn main() {
.bind("127.0.0.1:8443").unwrap() .bind("127.0.0.1:8443").unwrap()
.start_ssl(&pkcs12).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>(); let signals = Arbiter::system_registry().get::<ProcessSignals>();
signals.send(Subscribe(addr.subscriber())); signals.send(Subscribe(addr.subscriber()));
} }

View File

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

View File

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

View File

@ -11,9 +11,9 @@ use http::Version;
use httprequest::HttpMessage; use httprequest::HttpMessage;
// "Sun, 06 Nov 1994 08:49:37 GMT".len() // "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| { CACHED.with(|cache| {
let mut buf: [u8; 39] = unsafe { mem::uninitialized() }; let mut buf: [u8; 39] = unsafe { mem::uninitialized() };
buf[..6].copy_from_slice(b"date: "); 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| { CACHED.with(|cache| {
cache.borrow_mut().update(); 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 self.drain.is_none() && self.running != RunningState::Paused {
// if task is paused, write buffer is probably full // if task is paused, write buffer is probably full
loop { loop {
let result = match mem::replace(&mut self.iostate, IOState::Done) { let result = match mem::replace(&mut self.iostate, IOState::Done) {
IOState::Response => { IOState::Response => {
let result = match io.start(info.req_mut().get_inner(), let result = match io.start(info.req_mut().get_inner(), &mut self.resp) {
&mut self.resp) {
Ok(res) => res, Ok(res) => res,
Err(err) => { Err(err) => {
info.error = Some(err.into()); info.error = Some(err.into());