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 //! Http network interceptor. 15 16 use ylong_http::response::Response as HttpResp; 17 18 use crate::async_impl::{HttpBody, Request, Response}; 19 use crate::{ConnDetail, HttpClientError}; 20 21 pub(crate) type Interceptors = dyn Interceptor + Sync + Send + 'static; 22 23 /// Transport layer protocol type. 24 #[derive(Clone)] 25 pub enum ConnProtocol { 26 /// Tcp protocol. 27 Tcp, 28 /// Udp Protocol. 29 Udp, 30 /// Quic Protocol 31 Quic, 32 } 33 34 /// Network interceptor. 35 /// 36 /// Provides intercepting behavior at various stages of http message passing. 37 pub trait Interceptor { 38 /// Intercepts the created transport layer protocol. 39 // TODO add cache and response interceptor. 40 // Is it necessary to add a response interceptor? 41 // Does the input and output interceptor need to be added to http2 or http3 42 // encoded packets? intercept_connection(&self, _info: ConnDetail) -> Result<(), HttpClientError>43 fn intercept_connection(&self, _info: ConnDetail) -> Result<(), HttpClientError> { 44 Ok(()) 45 } 46 47 /// Intercepts the input of transport layer io. intercept_input(&self, _bytes: &[u8]) -> Result<(), HttpClientError>48 fn intercept_input(&self, _bytes: &[u8]) -> Result<(), HttpClientError> { 49 Ok(()) 50 } 51 52 /// Intercepts the output of transport layer io. intercept_output(&self, _bytes: &[u8]) -> Result<(), HttpClientError>53 fn intercept_output(&self, _bytes: &[u8]) -> Result<(), HttpClientError> { 54 Ok(()) 55 } 56 57 /// Intercepts the Request that is eventually transmitted to the peer end. intercept_request(&self, _request: &Request) -> Result<(), HttpClientError>58 fn intercept_request(&self, _request: &Request) -> Result<(), HttpClientError> { 59 Ok(()) 60 } 61 62 /// Intercepts the response that is eventually returned. intercept_response(&self, _response: &Response) -> Result<(), HttpClientError>63 fn intercept_response(&self, _response: &Response) -> Result<(), HttpClientError> { 64 Ok(()) 65 } 66 67 /// Intercepts the error cause of the retry. intercept_retry(&self, _error: &HttpClientError) -> Result<(), HttpClientError>68 fn intercept_retry(&self, _error: &HttpClientError) -> Result<(), HttpClientError> { 69 Ok(()) 70 } 71 72 /// Intercepts the redirect request. intercept_redirect_request(&self, _request: &Request) -> Result<(), HttpClientError>73 fn intercept_redirect_request(&self, _request: &Request) -> Result<(), HttpClientError> { 74 Ok(()) 75 } 76 77 /// Intercepts the response returned by the redirect intercept_redirect_response( &self, _response: &HttpResp<HttpBody>, ) -> Result<(), HttpClientError>78 fn intercept_redirect_response( 79 &self, 80 _response: &HttpResp<HttpBody>, 81 ) -> Result<(), HttpClientError> { 82 Ok(()) 83 } 84 } 85 86 /// The default Interceptor does not do any intercepting. 87 pub(crate) struct IdleInterceptor; 88 89 impl Interceptor for IdleInterceptor {} 90