|
1 |
| -use bytes::Buf; |
2 |
| -use h2::SendStream; |
| 1 | +use bytes::{Buf, Bytes}; |
| 2 | +use h2::{RecvStream, SendStream}; |
3 | 3 | use http::header::{
|
4 | 4 | HeaderName, CONNECTION, PROXY_AUTHENTICATE, PROXY_AUTHORIZATION, TE, TRAILER,
|
5 | 5 | TRANSFER_ENCODING, UPGRADE,
|
6 | 6 | };
|
7 | 7 | use http::HeaderMap;
|
8 | 8 | use pin_project::pin_project;
|
| 9 | +use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; |
9 | 10 | use std::error::Error as StdError;
|
10 |
| -use std::io::IoSlice; |
| 11 | +use std::io::{self, Cursor, IoSlice}; |
| 12 | +use std::task::Context; |
11 | 13 |
|
12 | 14 | use crate::body::{DecodedLength, HttpBody};
|
13 | 15 | use crate::common::{task, Future, Pin, Poll};
|
14 | 16 | use crate::headers::content_length_parse_all;
|
| 17 | +use crate::proto::h2::ping::Recorder; |
15 | 18 |
|
16 | 19 | pub(crate) mod ping;
|
17 | 20 |
|
@@ -84,12 +87,7 @@ fn strip_connection_headers(headers: &mut HeaderMap, is_request: bool) {
|
84 | 87 | }
|
85 | 88 |
|
86 | 89 | fn decode_content_length(headers: &HeaderMap) -> DecodedLength {
|
87 |
| - if let Some(len) = content_length_parse_all(headers) { |
88 |
| - // If the length is u64::MAX, oh well, just reported chunked. |
89 |
| - DecodedLength::checked_new(len).unwrap_or_else(|_| DecodedLength::CHUNKED) |
90 |
| - } else { |
91 |
| - DecodedLength::CHUNKED |
92 |
| - } |
| 90 | + content_length_parse_all(headers).into() |
93 | 91 | }
|
94 | 92 |
|
95 | 93 | // body adapters used by both Client and Server
|
@@ -172,7 +170,7 @@ where
|
172 | 170 | is_eos,
|
173 | 171 | );
|
174 | 172 |
|
175 |
| - let buf = SendBuf(Some(chunk)); |
| 173 | + let buf = SendBuf::Buf(chunk); |
176 | 174 | me.body_tx
|
177 | 175 | .send_data(buf, is_eos)
|
178 | 176 | .map_err(crate::Error::new_body_write)?;
|
@@ -243,32 +241,149 @@ impl<B: Buf> SendStreamExt for SendStream<SendBuf<B>> {
|
243 | 241 |
|
244 | 242 | fn send_eos_frame(&mut self) -> crate::Result<()> {
|
245 | 243 | trace!("send body eos");
|
246 |
| - self.send_data(SendBuf(None), true) |
| 244 | + self.send_data(SendBuf::None, true) |
247 | 245 | .map_err(crate::Error::new_body_write)
|
248 | 246 | }
|
249 | 247 | }
|
250 | 248 |
|
251 |
| -struct SendBuf<B>(Option<B>); |
| 249 | +enum SendBuf<B> { |
| 250 | + Buf(B), |
| 251 | + Cursor(Cursor<Box<[u8]>>), |
| 252 | + None, |
| 253 | +} |
252 | 254 |
|
253 | 255 | impl<B: Buf> Buf for SendBuf<B> {
|
254 | 256 | #[inline]
|
255 | 257 | fn remaining(&self) -> usize {
|
256 |
| - self.0.as_ref().map(|b| b.remaining()).unwrap_or(0) |
| 258 | + match *self { |
| 259 | + Self::Buf(ref b) => b.remaining(), |
| 260 | + Self::Cursor(ref c) => c.remaining(), |
| 261 | + Self::None => 0, |
| 262 | + } |
257 | 263 | }
|
258 | 264 |
|
259 | 265 | #[inline]
|
260 | 266 | fn chunk(&self) -> &[u8] {
|
261 |
| - self.0.as_ref().map(|b| b.chunk()).unwrap_or(&[]) |
| 267 | + match *self { |
| 268 | + Self::Buf(ref b) => b.chunk(), |
| 269 | + Self::Cursor(ref c) => c.chunk(), |
| 270 | + Self::None => &[], |
| 271 | + } |
262 | 272 | }
|
263 | 273 |
|
264 | 274 | #[inline]
|
265 | 275 | fn advance(&mut self, cnt: usize) {
|
266 |
| - if let Some(b) = self.0.as_mut() { |
267 |
| - b.advance(cnt) |
| 276 | + match *self { |
| 277 | + Self::Buf(ref mut b) => b.advance(cnt), |
| 278 | + Self::Cursor(ref mut c) => c.advance(cnt), |
| 279 | + Self::None => {}, |
268 | 280 | }
|
269 | 281 | }
|
270 | 282 |
|
271 | 283 | fn chunks_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize {
|
272 |
| - self.0.as_ref().map(|b| b.chunks_vectored(dst)).unwrap_or(0) |
| 284 | + match *self { |
| 285 | + Self::Buf(ref b) => b.chunks_vectored(dst), |
| 286 | + Self::Cursor(ref c) => c.chunks_vectored(dst), |
| 287 | + Self::None => 0, |
| 288 | + } |
| 289 | + } |
| 290 | +} |
| 291 | + |
| 292 | +struct H2Upgraded<B> |
| 293 | +where |
| 294 | + B: Buf, |
| 295 | +{ |
| 296 | + ping: Recorder, |
| 297 | + send_stream: SendStream<SendBuf<B>>, |
| 298 | + recv_stream: RecvStream, |
| 299 | + buf: Bytes, |
| 300 | +} |
| 301 | + |
| 302 | +impl<B> AsyncRead for H2Upgraded<B> |
| 303 | +where |
| 304 | + B: Buf, |
| 305 | +{ |
| 306 | + fn poll_read( |
| 307 | + mut self: Pin<&mut Self>, |
| 308 | + cx: &mut Context<'_>, |
| 309 | + read_buf: &mut ReadBuf<'_>, |
| 310 | + ) -> Poll<Result<(), io::Error>> { |
| 311 | + if self.buf.is_empty() { |
| 312 | + self.buf = match ready!(self.recv_stream.poll_data(cx)) { |
| 313 | + None => return Poll::Ready(Ok(())), |
| 314 | + Some(Ok(buf)) => { |
| 315 | + self.ping.record_data(buf.len()); |
| 316 | + buf |
| 317 | + } |
| 318 | + Some(Err(e)) => { |
| 319 | + return Poll::Ready(Err(h2_to_io_error(e))); |
| 320 | + } |
| 321 | + }; |
| 322 | + } |
| 323 | + let cnt = std::cmp::min(self.buf.len(), read_buf.remaining()); |
| 324 | + read_buf.put_slice(&self.buf[..cnt]); |
| 325 | + self.buf.advance(cnt); |
| 326 | + let _ = self.recv_stream.flow_control().release_capacity(cnt); |
| 327 | + Poll::Ready(Ok(())) |
| 328 | + } |
| 329 | +} |
| 330 | + |
| 331 | +impl<B> AsyncWrite for H2Upgraded<B> |
| 332 | +where |
| 333 | + B: Buf, |
| 334 | +{ |
| 335 | + fn poll_write( |
| 336 | + mut self: Pin<&mut Self>, |
| 337 | + cx: &mut Context<'_>, |
| 338 | + buf: &[u8], |
| 339 | + ) -> Poll<Result<usize, io::Error>> { |
| 340 | + if buf.is_empty() { |
| 341 | + return Poll::Ready(Ok(0)); |
| 342 | + } |
| 343 | + // FIXME(nox): PipeToSendStream does some weird stuff, first reserving |
| 344 | + // one byte and then polling reset if the capacity is 0, should we do |
| 345 | + // that here too? Should we poll reset somewhere? |
| 346 | + self.send_stream.reserve_capacity(buf.len()); |
| 347 | + Poll::Ready(match ready!(self.send_stream.poll_capacity(cx)) { |
| 348 | + None => Ok(0), |
| 349 | + Some(Ok(cnt)) => self.write(&buf[..cnt], false).map(|()| cnt), |
| 350 | + Some(Err(e)) => { |
| 351 | + // FIXME(nox): Should all H2 errors be returned as is with a |
| 352 | + // ErrorKind::Other, or should some be special-cased, say for |
| 353 | + // example, CANCEL? |
| 354 | + Err(h2_to_io_error(e)) |
| 355 | + }, |
| 356 | + }) |
| 357 | + } |
| 358 | + |
| 359 | + fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> { |
| 360 | + Poll::Ready(Ok(())) |
| 361 | + } |
| 362 | + |
| 363 | + fn poll_shutdown( |
| 364 | + mut self: Pin<&mut Self>, |
| 365 | + _cx: &mut Context<'_>, |
| 366 | + ) -> Poll<Result<(), io::Error>> { |
| 367 | + Poll::Ready(self.write(&[], true)) |
| 368 | + } |
| 369 | +} |
| 370 | + |
| 371 | +impl<B> H2Upgraded<B> |
| 372 | +where |
| 373 | + B: Buf, |
| 374 | +{ |
| 375 | + fn write(&mut self, buf: &[u8], end_of_stream: bool) -> Result<(), io::Error> { |
| 376 | + let send_buf = SendBuf::Cursor(Cursor::new(buf.into())); |
| 377 | + self.send_stream |
| 378 | + .send_data(send_buf, end_of_stream) |
| 379 | + .map_err(h2_to_io_error) |
| 380 | + } |
| 381 | +} |
| 382 | + |
| 383 | +fn h2_to_io_error(e: h2::Error) -> io::Error { |
| 384 | + if e.is_io() { |
| 385 | + e.into_io().unwrap() |
| 386 | + } else { |
| 387 | + io::Error::new(io::ErrorKind::Other, e) |
273 | 388 | }
|
274 | 389 | }
|
0 commit comments