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 //! `ConnDetail` trait and `HttpStream` implementation. 15 16 use std::pin::Pin; 17 use std::task::{Context, Poll}; 18 19 #[cfg(feature = "http3")] 20 use crate::async_impl::quic::QuicConn; 21 use crate::runtime::{AsyncRead, AsyncWrite, ReadBuf}; 22 use crate::util::{ConnData, ConnInfo}; 23 24 /// A connection wrapper containing io and io information. 25 pub struct HttpStream<T> { 26 conn_data: ConnData, 27 stream: T, 28 #[cfg(feature = "http3")] 29 quic_conn: Option<QuicConn>, 30 } 31 32 impl<T> AsyncRead for HttpStream<T> 33 where 34 T: AsyncRead + AsyncWrite + Unpin, 35 { 36 // poll_read separately. poll_read( mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>, ) -> Poll<std::io::Result<()>>37 fn poll_read( 38 mut self: Pin<&mut Self>, 39 cx: &mut Context<'_>, 40 buf: &mut ReadBuf<'_>, 41 ) -> Poll<std::io::Result<()>> { 42 Pin::new(&mut self.stream).poll_read(cx, buf) 43 } 44 } 45 46 impl<T> AsyncWrite for HttpStream<T> 47 where 48 T: AsyncRead + AsyncWrite + Unpin, 49 { 50 // poll_write separately. poll_write( mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll<std::io::Result<usize>>51 fn poll_write( 52 mut self: Pin<&mut Self>, 53 cx: &mut Context<'_>, 54 buf: &[u8], 55 ) -> Poll<std::io::Result<usize>> { 56 Pin::new(&mut self.stream).poll_write(cx, buf) 57 } 58 poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>>59 fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> { 60 Pin::new(&mut self.stream).poll_flush(cx) 61 } 62 poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>>63 fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> { 64 Pin::new(&mut self.stream).poll_shutdown(cx) 65 } 66 } 67 68 impl<T> ConnInfo for HttpStream<T> { is_proxy(&self) -> bool69 fn is_proxy(&self) -> bool { 70 self.conn_data.is_proxy() 71 } 72 conn_data(&self) -> ConnData73 fn conn_data(&self) -> ConnData { 74 self.conn_data.clone() 75 } 76 77 #[cfg(feature = "http3")] quic_conn(&mut self) -> Option<QuicConn>78 fn quic_conn(&mut self) -> Option<QuicConn> { 79 self.quic_conn.take() 80 } 81 } 82 83 impl<T> HttpStream<T> { 84 /// HttpStream constructor. new(io: T, conn_data: ConnData) -> HttpStream<T>85 pub(crate) fn new(io: T, conn_data: ConnData) -> HttpStream<T> { 86 HttpStream { 87 conn_data, 88 stream: io, 89 #[cfg(feature = "http3")] 90 quic_conn: None, 91 } 92 } 93 94 #[cfg(feature = "http3")] set_quic_conn(&mut self, conn: QuicConn)95 pub(crate) fn set_quic_conn(&mut self, conn: QuicConn) { 96 self.quic_conn = Some(conn); 97 } 98 99 #[cfg(feature = "http3")] set_conn_data(&mut self, conn_data: ConnData)100 pub(crate) fn set_conn_data(&mut self, conn_data: ConnData) { 101 self.conn_data = conn_data 102 } 103 } 104