mirror of
https://github.com/actix/actix-web.git
synced 2024-11-26 11:31:09 +00:00
Fix server websockets big payloads support
This commit is contained in:
parent
35ee5d36d8
commit
6cd40df387
8 changed files with 178 additions and 154 deletions
|
@ -12,9 +12,6 @@ matrix:
|
|||
- rust: stable
|
||||
- rust: beta
|
||||
- rust: nightly
|
||||
allow_failures:
|
||||
- rust: nightly
|
||||
- rust: beta
|
||||
|
||||
#rust:
|
||||
# - 1.21.0
|
||||
|
|
|
@ -8,6 +8,8 @@
|
|||
|
||||
* Allow to set client websocket handshake timeout
|
||||
|
||||
* Fix server websockets big payloads support
|
||||
|
||||
|
||||
## 0.4.9 (2018-03-16)
|
||||
|
||||
|
|
|
@ -145,9 +145,7 @@ impl HttpResponseParser {
|
|||
// convert headers
|
||||
let mut hdrs = HeaderMap::new();
|
||||
for header in headers[..headers_len].iter() {
|
||||
let n_start = header.name.as_ptr() as usize - bytes_ptr;
|
||||
let n_end = n_start + header.name.len();
|
||||
if let Ok(name) = HeaderName::try_from(slice.slice(n_start, n_end)) {
|
||||
if let Ok(name) = HeaderName::try_from(header.name) {
|
||||
let v_start = header.value.as_ptr() as usize - bytes_ptr;
|
||||
let v_end = v_start + header.value.len();
|
||||
let value = unsafe {
|
||||
|
|
|
@ -453,9 +453,10 @@ impl<S: 'static, H> ProcessResponse<S, H> {
|
|||
fn poll_io(mut self, io: &mut Writer, info: &mut PipelineInfo<S>)
|
||||
-> Result<PipelineState<S, H>, PipelineState<S, H>>
|
||||
{
|
||||
loop {
|
||||
if self.drain.is_none() && self.running != RunningState::Paused {
|
||||
// if task is paused, write buffer is probably full
|
||||
'outter: loop {
|
||||
'inner: loop {
|
||||
let result = match mem::replace(&mut self.iostate, IOState::Done) {
|
||||
IOState::Response => {
|
||||
let encoding = self.resp.content_encoding().unwrap_or(info.encoding);
|
||||
|
@ -485,11 +486,11 @@ impl<S: 'static, H> ProcessResponse<S, H> {
|
|||
match self.resp.replace_body(Body::Empty) {
|
||||
Body::Streaming(stream) => {
|
||||
self.iostate = IOState::Payload(stream);
|
||||
continue
|
||||
continue 'inner
|
||||
},
|
||||
Body::Actor(ctx) => {
|
||||
self.iostate = IOState::Actor(ctx);
|
||||
continue
|
||||
continue 'inner
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
|
@ -545,7 +546,7 @@ impl<S: 'static, H> ProcessResponse<S, H> {
|
|||
return Ok(
|
||||
FinishingMiddlewares::init(info, self.resp))
|
||||
}
|
||||
break 'outter
|
||||
break 'inner
|
||||
},
|
||||
Frame::Chunk(Some(chunk)) => {
|
||||
match io.write(chunk) {
|
||||
|
@ -563,7 +564,7 @@ impl<S: 'static, H> ProcessResponse<S, H> {
|
|||
self.iostate = IOState::Actor(ctx);
|
||||
if self.drain.is_some() {
|
||||
self.running.resume();
|
||||
break 'outter
|
||||
break 'inner
|
||||
}
|
||||
res.unwrap()
|
||||
},
|
||||
|
@ -606,15 +607,18 @@ impl<S: 'static, H> ProcessResponse<S, H> {
|
|||
let _ = tx.send(());
|
||||
}
|
||||
// restart io processing
|
||||
return self.poll_io(io, info);
|
||||
continue
|
||||
},
|
||||
Ok(Async::NotReady) => return Err(PipelineState::Response(self)),
|
||||
Ok(Async::NotReady) =>
|
||||
return Err(PipelineState::Response(self)),
|
||||
Err(err) => {
|
||||
info.error = Some(err.into());
|
||||
return Ok(FinishingMiddlewares::init(info, self.resp))
|
||||
}
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
// response is completed
|
||||
match self.iostate {
|
||||
|
|
|
@ -82,7 +82,9 @@ impl<T: AsyncWrite, H: 'static> H1Writer<T, H> {
|
|||
self.disconnected();
|
||||
return Err(io::Error::new(io::ErrorKind::WriteZero, ""))
|
||||
},
|
||||
Ok(n) => written += n,
|
||||
Ok(n) => {
|
||||
written += n;
|
||||
},
|
||||
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
|
||||
return Ok(written)
|
||||
}
|
||||
|
@ -229,7 +231,7 @@ impl<T: AsyncWrite, H: 'static> Writer for H1Writer<T, H> {
|
|||
if self.buffer.is_empty() {
|
||||
let pl: &[u8] = payload.as_ref();
|
||||
let n = self.write_data(pl)?;
|
||||
if pl.len() < n {
|
||||
if n < pl.len() {
|
||||
self.buffer.extend_from_slice(&pl[n..]);
|
||||
return Ok(WriterState::Done);
|
||||
}
|
||||
|
|
|
@ -454,13 +454,13 @@ impl Stream for ClientReader {
|
|||
// read
|
||||
match Frame::parse(&mut inner.rx, false, max_size) {
|
||||
Ok(Async::Ready(Some(frame))) => {
|
||||
let (finished, opcode, payload) = frame.unpack();
|
||||
let (_finished, opcode, payload) = frame.unpack();
|
||||
|
||||
match opcode {
|
||||
// continuation is not supported
|
||||
OpCode::Continue => {
|
||||
inner.closed = true;
|
||||
return Err(ProtocolError::NoContinuation)
|
||||
Err(ProtocolError::NoContinuation)
|
||||
},
|
||||
OpCode::Bad => {
|
||||
inner.closed = true;
|
||||
|
|
|
@ -329,7 +329,8 @@ impl<S> Stream for WsStream<S> where S: Stream<Item=Bytes, Error=PayloadError> {
|
|||
match String::from_utf8(tmp) {
|
||||
Ok(s) =>
|
||||
Ok(Async::Ready(Some(Message::Text(s)))),
|
||||
Err(_) => {
|
||||
Err(e) => {
|
||||
println!("ENC: {:?}", e);
|
||||
self.closed = true;
|
||||
Err(ProtocolError::BadEncoding)
|
||||
}
|
||||
|
|
|
@ -93,7 +93,8 @@ fn test_large_bin() {
|
|||
}
|
||||
|
||||
struct Ws2 {
|
||||
count: usize
|
||||
count: usize,
|
||||
bin: bool,
|
||||
}
|
||||
|
||||
impl Actor for Ws2 {
|
||||
|
@ -106,10 +107,14 @@ impl Actor for Ws2 {
|
|||
|
||||
impl Ws2 {
|
||||
fn send(&mut self, ctx: &mut ws::WebsocketContext<Self>) {
|
||||
if self.bin {
|
||||
ctx.binary(Vec::from("0".repeat(65_536)));
|
||||
} else {
|
||||
ctx.text("0".repeat(65_536));
|
||||
}
|
||||
ctx.drain().and_then(|_, act, ctx| {
|
||||
act.count += 1;
|
||||
if act.count != 100 {
|
||||
if act.count != 10_000 {
|
||||
act.send(ctx);
|
||||
}
|
||||
actix::fut::ok(())
|
||||
|
@ -135,10 +140,25 @@ fn test_server_send_text() {
|
|||
let data = Some(ws::Message::Text("0".repeat(65_536)));
|
||||
|
||||
let mut srv = test::TestServer::new(
|
||||
|app| app.handler(|req| ws::start(req, Ws2{count:0})));
|
||||
|app| app.handler(|req| ws::start(req, Ws2{count:0, bin: false})));
|
||||
let (mut reader, _writer) = srv.ws().unwrap();
|
||||
|
||||
for _ in 0..100 {
|
||||
for _ in 0..10_000 {
|
||||
let (item, r) = srv.execute(reader.into_future()).unwrap();
|
||||
reader = r;
|
||||
assert_eq!(item, data);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_server_send_bin() {
|
||||
let data = Some(ws::Message::Binary(Binary::from("0".repeat(65_536))));
|
||||
|
||||
let mut srv = test::TestServer::new(
|
||||
|app| app.handler(|req| ws::start(req, Ws2{count:0, bin: true})));
|
||||
let (mut reader, _writer) = srv.ws().unwrap();
|
||||
|
||||
for _ in 0..10_000 {
|
||||
let (item, r) = srv.execute(reader.into_future()).unwrap();
|
||||
reader = r;
|
||||
assert_eq!(item, data);
|
||||
|
|
Loading…
Reference in a new issue