chiark / gitweb /
own http body type
[hippotat.git] / src / queue.rs
1 // Copyright 2021 Ian Jackson and contributors to Hippotat
2 // SPDX-License-Identifier: GPL-3.0-or-later
3 // There is NO WARRANTY.
4
5 use crate::prelude::*;
6
7 // xxx are we using this at all ?
8 #[derive(Default,Clone)]
9 pub struct PacketQueue<D> {
10   queue: VecDeque<D>,
11   content: usize,
12 }
13
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);
18   }
19
20   pub fn pop_front(&mut self) -> Option<D> {
21     let data = self.queue.pop_front()?;
22     self.content -= data.as_ref().len();
23     Some(data)
24   }
25
26   pub fn is_empty(&self) -> bool { self.queue.is_empty() }
27   pub fn peek_front(&self) -> Option<&D> { self.queue.front() }
28 }
29
30 #[derive(Default,Clone)]
31 pub struct QueueBuf<E> {
32   content: usize,
33   eaten1: usize, // 0 <= eaten1 < queue.front()...len()
34   queue: VecDeque<E>,
35 }
36
37 #[derive(Default,Debug,Clone)]
38 pub struct FrameQueueBuf {
39   queue: QueueBuf<Cervine<'static, Box<[u8]>, [u8]>>,
40 }
41
42 impl<E> Debug for QueueBuf<E> where E: AsRef<[u8]> {
43   #[throws(fmt::Error)]
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())?; }
48     write!(f, "]}}")?;
49   }
50 }
51
52 impl<E> QueueBuf<E> where E: AsRef<[u8]> {
53   pub fn push<B: Into<E>>(&mut self, b: B) {
54     self.push_(b.into());
55   }
56   pub fn push_(&mut self, b: E) {
57     let l = b.as_ref().len();
58     self.queue.push_back(b);
59     self.content += l;
60   }
61   pub fn is_empty(&self) -> bool { self.content == 0 }
62   pub fn len(&self) -> usize { self.content }
63 }
64
65 impl FrameQueueBuf {
66   pub fn push<B: Into<Box<[u8]>>>(&mut self, b: B) {
67     self.push_(b.into());
68   }
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));
72   }
73   pub fn is_empty(&self) -> bool { self.queue.is_empty() }
74   pub fn len(&self) -> usize { self.queue.len() }
75 }
76
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>
80   {
81     for b in it { self.push(b) }
82   }
83 }
84
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.. ]
90   }
91   fn advance(&mut self, cnt: usize) {
92     self.content -= cnt;
93     self.eaten1 += cnt;
94     loop {
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();
100     }
101   }
102 }
103
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) }
108 }
109
110 pin_project!{
111   pub struct BufBody<B:Buf> {
112     body: Option<B>,
113   }
114 }
115 impl<B:Buf> BufBody<B> {
116   pub fn new(body: B) -> Self { Self { body: Some(body ) } }
117 }
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();
122     buf.push(s);
123     Self::new(buf)
124   }
125 }
126
127 impl<B:Buf> HttpBody for BufBody<B> {
128   type Error = Void;
129   type Data = 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())
133   }
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))
137   }
138 }