mirror of
https://github.com/actix/actix-extras.git
synced 2024-12-01 02:44:37 +01:00
use more binary
This commit is contained in:
parent
33dbe15760
commit
7060f298b4
18
src/body.rs
18
src/body.rs
@ -1,4 +1,4 @@
|
|||||||
use std::fmt;
|
use std::{fmt, mem};
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
@ -122,6 +122,22 @@ impl Binary {
|
|||||||
pub fn from_slice(s: &[u8]) -> Binary {
|
pub fn from_slice(s: &[u8]) -> Binary {
|
||||||
Binary::Bytes(Bytes::from(s))
|
Binary::Bytes(Bytes::from(s))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Convert Binary to a Bytes instance
|
||||||
|
pub fn take(&mut self) -> Bytes {
|
||||||
|
mem::replace(self, Binary::Slice(b"")).into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Clone for Binary {
|
||||||
|
fn clone(&self) -> Binary {
|
||||||
|
match *self {
|
||||||
|
Binary::Bytes(ref bytes) => Binary::Bytes(bytes.clone()),
|
||||||
|
Binary::Slice(slice) => Binary::Bytes(Bytes::from(slice)),
|
||||||
|
Binary::SharedString(ref s) => Binary::Bytes(Bytes::from(s.as_str())),
|
||||||
|
Binary::ArcSharedString(ref s) => Binary::Bytes(Bytes::from(s.as_str())),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Into<Bytes> for Binary {
|
impl Into<Bytes> for Binary {
|
||||||
|
@ -396,7 +396,7 @@ impl PayloadEncoder {
|
|||||||
ContentEncoding::Auto => unreachable!()
|
ContentEncoding::Auto => unreachable!()
|
||||||
};
|
};
|
||||||
// TODO return error!
|
// TODO return error!
|
||||||
let _ = enc.write(bytes.as_ref());
|
let _ = enc.write(bytes.clone());
|
||||||
let _ = enc.write_eof();
|
let _ = enc.write_eof();
|
||||||
|
|
||||||
*bytes = Binary::from(tmp.get_mut().take());
|
*bytes = Binary::from(tmp.get_mut().take());
|
||||||
@ -520,7 +520,7 @@ impl PayloadEncoder {
|
|||||||
|
|
||||||
#[cfg_attr(feature = "cargo-clippy", allow(inline_always))]
|
#[cfg_attr(feature = "cargo-clippy", allow(inline_always))]
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn write(&mut self, payload: &[u8]) -> Result<(), io::Error> {
|
pub fn write(&mut self, payload: Binary) -> Result<(), io::Error> {
|
||||||
self.0.write(payload)
|
self.0.write(payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -629,10 +629,10 @@ impl ContentEncoder {
|
|||||||
|
|
||||||
#[cfg_attr(feature = "cargo-clippy", allow(inline_always))]
|
#[cfg_attr(feature = "cargo-clippy", allow(inline_always))]
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn write(&mut self, data: &[u8]) -> Result<(), io::Error> {
|
pub fn write(&mut self, data: Binary) -> Result<(), io::Error> {
|
||||||
match *self {
|
match *self {
|
||||||
ContentEncoder::Br(ref mut encoder) => {
|
ContentEncoder::Br(ref mut encoder) => {
|
||||||
match encoder.write(data) {
|
match encoder.write(data.as_ref()) {
|
||||||
Ok(_) =>
|
Ok(_) =>
|
||||||
encoder.flush(),
|
encoder.flush(),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
@ -642,7 +642,7 @@ impl ContentEncoder {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
ContentEncoder::Gzip(ref mut encoder) => {
|
ContentEncoder::Gzip(ref mut encoder) => {
|
||||||
match encoder.write(data) {
|
match encoder.write(data.as_ref()) {
|
||||||
Ok(_) =>
|
Ok(_) =>
|
||||||
encoder.flush(),
|
encoder.flush(),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
@ -652,7 +652,7 @@ impl ContentEncoder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ContentEncoder::Deflate(ref mut encoder) => {
|
ContentEncoder::Deflate(ref mut encoder) => {
|
||||||
match encoder.write(data) {
|
match encoder.write(data.as_ref()) {
|
||||||
Ok(_) =>
|
Ok(_) =>
|
||||||
encoder.flush(),
|
encoder.flush(),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
@ -727,10 +727,10 @@ impl TransferEncoding {
|
|||||||
|
|
||||||
/// Encode message. Return `EOF` state of encoder
|
/// Encode message. Return `EOF` state of encoder
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn encode(&mut self, msg: &[u8]) -> io::Result<bool> {
|
pub fn encode(&mut self, msg: Binary) -> io::Result<bool> {
|
||||||
match self.kind {
|
match self.kind {
|
||||||
TransferEncodingKind::Eof => {
|
TransferEncodingKind::Eof => {
|
||||||
self.buffer.get_mut().extend_from_slice(msg);
|
self.buffer.get_mut().extend_from_slice(msg.as_ref());
|
||||||
Ok(msg.is_empty())
|
Ok(msg.is_empty())
|
||||||
},
|
},
|
||||||
TransferEncodingKind::Chunked(ref mut eof) => {
|
TransferEncodingKind::Chunked(ref mut eof) => {
|
||||||
@ -744,7 +744,7 @@ impl TransferEncoding {
|
|||||||
} else {
|
} else {
|
||||||
write!(self.buffer.get_mut(), "{:X}\r\n", msg.len())
|
write!(self.buffer.get_mut(), "{:X}\r\n", msg.len())
|
||||||
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
|
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
|
||||||
self.buffer.get_mut().extend_from_slice(msg);
|
self.buffer.get_mut().extend_from_slice(msg.as_ref());
|
||||||
self.buffer.get_mut().extend_from_slice(b"\r\n");
|
self.buffer.get_mut().extend_from_slice(b"\r\n");
|
||||||
}
|
}
|
||||||
Ok(*eof)
|
Ok(*eof)
|
||||||
@ -754,7 +754,7 @@ impl TransferEncoding {
|
|||||||
return Ok(*remaining == 0)
|
return Ok(*remaining == 0)
|
||||||
}
|
}
|
||||||
let max = cmp::min(*remaining, msg.len() as u64);
|
let max = cmp::min(*remaining, msg.len() as u64);
|
||||||
self.buffer.get_mut().extend_from_slice(msg[..max as usize].as_ref());
|
self.buffer.get_mut().extend_from_slice(msg.as_ref()[..max as usize].as_ref());
|
||||||
|
|
||||||
*remaining -= max as u64;
|
*remaining -= max as u64;
|
||||||
Ok(*remaining == 0)
|
Ok(*remaining == 0)
|
||||||
@ -781,7 +781,7 @@ impl io::Write for TransferEncoding {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||||
self.encode(buf)?;
|
self.encode(Binary::from_slice(buf))?;
|
||||||
Ok(buf.len())
|
Ok(buf.len())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,7 +174,7 @@ impl<T: AsyncWrite> Writer for H1Writer<T> {
|
|||||||
|
|
||||||
if let Body::Binary(bytes) = body {
|
if let Body::Binary(bytes) = body {
|
||||||
self.written = bytes.len() as u64;
|
self.written = bytes.len() as u64;
|
||||||
self.encoder.write(bytes.as_ref())?;
|
self.encoder.write(bytes)?;
|
||||||
} else {
|
} else {
|
||||||
msg.replace_body(body);
|
msg.replace_body(body);
|
||||||
}
|
}
|
||||||
@ -186,7 +186,7 @@ impl<T: AsyncWrite> Writer for H1Writer<T> {
|
|||||||
if !self.flags.contains(Flags::DISCONNECTED) {
|
if !self.flags.contains(Flags::DISCONNECTED) {
|
||||||
if self.flags.contains(Flags::STARTED) {
|
if self.flags.contains(Flags::STARTED) {
|
||||||
// TODO: add warning, write after EOF
|
// TODO: add warning, write after EOF
|
||||||
self.encoder.write(payload.as_ref())?;
|
self.encoder.write(payload)?;
|
||||||
return Ok(WriterState::Done)
|
return Ok(WriterState::Done)
|
||||||
} else {
|
} else {
|
||||||
// might be response to EXCEPT
|
// might be response to EXCEPT
|
||||||
|
@ -168,7 +168,7 @@ impl Writer for H2Writer {
|
|||||||
if let Body::Binary(bytes) = body {
|
if let Body::Binary(bytes) = body {
|
||||||
self.flags.insert(Flags::EOF);
|
self.flags.insert(Flags::EOF);
|
||||||
self.written = bytes.len() as u64;
|
self.written = bytes.len() as u64;
|
||||||
self.encoder.write(bytes.as_ref())?;
|
self.encoder.write(bytes)?;
|
||||||
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));
|
||||||
}
|
}
|
||||||
@ -185,7 +185,7 @@ impl Writer for H2Writer {
|
|||||||
if !self.flags.contains(Flags::DISCONNECTED) {
|
if !self.flags.contains(Flags::DISCONNECTED) {
|
||||||
if self.flags.contains(Flags::STARTED) {
|
if self.flags.contains(Flags::STARTED) {
|
||||||
// TODO: add warning, write after EOF
|
// TODO: add warning, write after EOF
|
||||||
self.encoder.write(payload.as_ref())?;
|
self.encoder.write(payload)?;
|
||||||
} else {
|
} else {
|
||||||
// might be response for EXCEPT
|
// might be response for EXCEPT
|
||||||
self.encoder.get_mut().extend_from_slice(payload.as_ref())
|
self.encoder.get_mut().extend_from_slice(payload.as_ref())
|
||||||
|
Loading…
Reference in New Issue
Block a user