1 // Copyright (c) 2023 Huawei Device Co., Ltd. 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 //! defines `BodyDataRef`. 15 16 use std::pin::Pin; 17 use std::task::{Context, Poll}; 18 19 use crate::runtime::{AsyncRead, ReadBuf}; 20 use crate::util::request::RequestArc; 21 22 pub(crate) struct BodyDataRef { 23 body: Option<RequestArc>, 24 } 25 26 impl BodyDataRef { 27 pub(crate) fn new(request: RequestArc) -> Self { 28 Self { 29 body: Some(request), 30 } 31 } 32 33 pub(crate) fn clear(&mut self) { 34 self.body = None; 35 } 36 37 pub(crate) fn poll_read( 38 &mut self, 39 cx: &mut Context<'_>, 40 buf: &mut [u8], 41 ) -> Poll<Option<usize>> { 42 let request = if let Some(ref mut request) = self.body { 43 request 44 } else { 45 return Poll::Ready(Some(0)); 46 }; 47 let data = request.ref_mut().body_mut(); 48 let mut read_buf = ReadBuf::new(buf); 49 let data = Pin::new(data); 50 match data.poll_read(cx, &mut read_buf) { 51 Poll::Ready(Err(_)) => Poll::Ready(None), 52 Poll::Ready(Ok(_)) => Poll::Ready(Some(read_buf.filled().len())), 53 Poll::Pending => Poll::Pending, 54 } 55 } 56 } 57