1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use std::fmt::{self, Display, Formatter};
use std::mem;
use std::pin::Pin;
use std::task::{Context, Poll};
use bytes::{Buf, Bytes};
use futures_util::ready;
use futures_util::stream::{Fuse, IntoStream, Stream, StreamExt, TryStream, TryStreamExt};
use http_body::Body;
use pin_project_lite::pin_project;
use crate::error::Error;
macro_rules! str_enum {
(
$(#[$attr:meta])*
pub enum $E:ident {
$(
$(#[$v_attr:meta])*
$V:ident = $by:expr
),*$(,)?
}
) => {
$(#[$attr])*
pub enum $E {
$(
$(#[$v_attr])*
$V,
)*
}
impl std::convert::AsRef<str> for $E {
fn as_ref(&self) -> &str {
match *self {
$($E::$V => $by,)*
}
}
}
}
}
pin_project! {
pub struct Lines<S> {
#[pin]
stream: Fuse<IntoStream<S>>,
buf: Bytes,
}
}
pin_project! {
pub struct HttpBodyAsStream<B> {
#[pin]
pub inner: B,
}
}
impl<S: TryStream> Lines<S> {
pub fn new(stream: S) -> Self {
Lines {
stream: stream.into_stream().fuse(),
buf: Bytes::new(),
}
}
}
impl<S: TryStream<Ok = Bytes, Error = Error<E>>, E> Stream for Lines<S> {
type Item = Result<Bytes, Error<E>>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let mut this = self.project();
if let Some(line) = remove_first_line(&mut this.buf) {
return Poll::Ready(Some(Ok(line)));
}
loop {
let mut chunk = loop {
if let Some(c) = ready!(this.stream.as_mut().poll_next(cx)?) {
if !c.is_empty() {
break c;
}
} else if this.buf.is_empty() {
return Poll::Ready(None);
} else {
let ret = mem::take(this.buf);
return Poll::Ready(Some(Ok(ret)));
}
};
if chunk[0] == b'\n' && this.buf.last() == Some(&b'\r') {
this.buf.truncate(this.buf.len() - 1);
chunk.advance(1);
return Poll::Ready(Some(Ok(mem::replace(this.buf, chunk))));
} else if let Some(line) = remove_first_line(&mut chunk) {
let ret = if this.buf.is_empty() {
line
} else {
let mut ret = Vec::with_capacity(this.buf.len() + line.len());
ret.extend_from_slice(&this.buf);
ret.extend_from_slice(&line);
ret.into()
};
*this.buf = chunk;
return Poll::Ready(Some(Ok(ret)));
} else {
*this.buf = if this.buf.is_empty() {
chunk
} else {
let mut buf = Vec::with_capacity(this.buf.len() + chunk.len());
buf.extend_from_slice(&this.buf);
buf.extend_from_slice(&chunk);
buf.into()
}
}
}
}
}
impl<B: Body> HttpBodyAsStream<B> {
pub fn new(inner: B) -> Self {
HttpBodyAsStream { inner }
}
}
impl<B: Body> Stream for HttpBodyAsStream<B> {
type Item = Result<Bytes, Error<B::Error>>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.project().inner.poll_data(cx).map(|opt| {
opt.map(|result| result.map(|mut buf| buf.to_bytes()).map_err(Error::Service))
})
}
}
pub fn fmt_join<T: Display>(t: &[T], sep: &str, f: &mut Formatter<'_>) -> fmt::Result {
let mut iter = t.iter();
if let Some(t) = iter.next() {
Display::fmt(t, f)?;
for t in iter {
write!(f, "{}{}", sep, t)?;
}
}
Ok(())
}
fn remove_first_line(buf: &mut Bytes) -> Option<Bytes> {
if buf.len() < 2 {
return None;
}
if let Some(i) = memchr::memchr(b'\n', &buf[1..]) {
if buf[i] == b'\r' {
let mut line = buf.split_to(i + 2);
line.truncate(i);
return Some(line);
}
}
None
}
#[cfg(test)]
mod test {
use super::*;
use bytes::Bytes;
use futures::executor::block_on_stream;
use futures_util::stream;
#[test]
fn lines() {
let body = [
"abc\r\n",
"d\r\nefg\r\n",
"hi",
"jk",
"",
"\r\n",
"\r\n",
"lmn\r\nop",
"q\rrs\r",
"\n\n\rtuv\r\r\n",
"wxyz\n",
];
let concat = body.concat();
let expected = concat.split("\r\n");
let lines = Lines::new(stream::iter(&body).map(|&c| Ok(Bytes::from_static(c.as_bytes()))));
let lines = block_on_stream(lines)
.map(|s: Result<_, Error>| String::from_utf8(s.unwrap().to_vec()).unwrap());
assert_eq!(lines.collect::<Vec<_>>(), expected.collect::<Vec<_>>());
}
}