1 // Copyright 2021 Ian Jackson and contributors to Hippotat
2 // SPDX-License-Identifier: GPL-3.0-or-later
3 // There is NO WARRANTY.
7 // xxx are we using this at all ?
8 #[derive(Default,Clone)]
9 pub struct PacketQueue<D> {
14 impl<D> PacketQueue<D> where D: AsRef<[u8]> {
15 pub fn push_back(&mut self, data: D) {
16 self.content += data.as_ref().len();
17 self.queue.push_back(data);
20 pub fn pop_front(&mut self) -> Option<D> {
21 let data = self.queue.pop_front()?;
22 self.content -= data.as_ref().len();
26 pub fn is_empty(&self) -> bool { self.queue.is_empty() }
27 pub fn peek_front(&self) -> Option<&D> { self.queue.front() }
30 #[derive(Default,Clone)]
31 pub struct QueueBuf<E> {
33 eaten1: usize, // 0 <= eaten1 < queue.front()...len()
37 #[derive(Default,Debug,Clone)]
38 pub struct FrameQueueBuf {
39 queue: QueueBuf<Cervine<'static, Box<[u8]>, [u8]>>,
42 impl<E> Debug for QueueBuf<E> where E: AsRef<[u8]> {
44 fn fmt(&self, f: &mut fmt::Formatter) {
45 write!(f, "Queue{{content={},eaten1={},queue=[",
46 self.content, self.eaten1)?;
47 for q in &self.queue { write!(f, "{},", q.as_ref().len())?; }
52 impl<E> QueueBuf<E> where E: AsRef<[u8]> {
53 pub fn push<B: Into<E>>(&mut self, b: B) {
56 pub fn push_(&mut self, b: E) {
57 let l = b.as_ref().len();
58 self.queue.push_back(b);
61 pub fn is_empty(&self) -> bool { self.content == 0 }
62 pub fn len(&self) -> usize { self.content }
66 pub fn push<B: Into<Box<[u8]>>>(&mut self, b: B) {
69 pub fn push_(&mut self, b: Box<[u8]>) {
70 self.queue.push_(Cervine::Owned(b));
71 self.queue.push_(Cervine::Borrowed(&SLIP_END_SLICE));
73 pub fn is_empty(&self) -> bool { self.queue.is_empty() }
74 pub fn len(&self) -> usize { self.queue.len() }
77 impl<E> Extend<E> for FrameQueueBuf where E: Into<Box<[u8]>> {
78 fn extend<I>(&mut self, it: I)
79 where I: IntoIterator<Item=E>
81 for b in it { self.push(b) }
85 impl<E> hyper::body::Buf for QueueBuf<E> where E: AsRef<[u8]> {
86 fn remaining(&self) -> usize { self.content }
87 fn chunk(&self) -> &[u8] {
88 let front = if let Some(f) = self.queue.front() { f } else { return &[] };
89 &front.as_ref()[ self.eaten1.. ]
91 fn advance(&mut self, cnt: usize) {
95 if self.eaten1 == 0 { break }
96 let front = self.queue.front().unwrap();
97 if self.eaten1 < front.as_ref().len() { break; }
98 self.eaten1 -= front.as_ref().len();
99 self.queue.pop_front().unwrap();
104 impl hyper::body::Buf for FrameQueueBuf {
105 fn remaining(&self) -> usize { self.queue.remaining() }
106 fn chunk(&self) -> &[u8] { self.queue.chunk() }
107 fn advance(&mut self, cnt: usize) { self.queue.advance(cnt) }
111 pub struct BufBody<B:Buf> {
115 impl<B:Buf> BufBody<B> {
116 pub fn new(body: B) -> Self { Self { body: Some(body ) } }
118 impl BufBody<FrameQueueBuf> {
119 pub fn display<S:Display>(s: S) -> Self {
120 let s = s.to_string().into_bytes();
121 let mut buf: FrameQueueBuf = default();
127 impl<B:Buf> HttpBody for BufBody<B> {
130 fn poll_data(self: Pin<&mut Self>, _: &mut std::task::Context<'_>)
131 -> Poll<Option<Result<B, Void>>> {
132 Poll::Ready(Ok(self.project().body.take()).transpose())
134 fn poll_trailers(self: Pin<&mut Self>, _: &mut std::task::Context<'_>)
135 -> Poll<Result<Option<hyper::HeaderMap<hyper::header::HeaderValue>>, Void>> {
136 Poll::Ready(Ok(None))