Skip to main content

tea_coding_tools/web_fetch/
provider.rs

1use std::fmt;
2use std::sync::Arc;
3
4use tea_control::CancellationScope;
5use tea_provider_http::ProviderHttpConfig;
6
7use super::cache::{FetchCacheKey, FetchCachePolicyKey};
8use super::http::{FETCH_ACCEPT_ENCODING_VALUE, FETCH_ACCEPT_VALUE};
9use super::{
10    FetchBodyDecoder, FetchCacheScope, FetchFuture, FetchHttpConfig, FetchHttpResponse,
11    FetchHttpTransport, FetchProvider, FetchProviderError, FetchProviderErrorCode, FetchRequest,
12    FetchResult, FetchResultCache, SystemFetchDnsResolver,
13};
14
15/// Version mixed into cache keys whenever fetch security semantics change.
16pub const FETCH_SECURITY_POLICY_VERSION: u32 = 1;
17
18/// Production client fetch backend composed from transport, decoding, and cache layers.
19#[derive(Clone)]
20pub struct HttpFetchProvider {
21    transport: FetchHttpTransport,
22    decoder: FetchBodyDecoder,
23    scope: FetchCacheScope,
24    cache: Arc<FetchResultCache>,
25}
26
27impl HttpFetchProvider {
28    /// Creates a provider from explicit independently testable layers.
29    #[must_use]
30    pub const fn new(
31        transport: FetchHttpTransport,
32        decoder: FetchBodyDecoder,
33        scope: FetchCacheScope,
34        cache: Arc<FetchResultCache>,
35    ) -> Self {
36        Self {
37            transport,
38            decoder,
39            scope,
40            cache,
41        }
42    }
43
44    /// Creates the production HTTPS/public-network provider.
45    #[must_use]
46    pub fn production(
47        scope: FetchCacheScope,
48        cache: Arc<FetchResultCache>,
49        http: &ProviderHttpConfig,
50    ) -> Self {
51        let transport = FetchHttpTransport::new(
52            FetchHttpConfig::production(),
53            Arc::new(SystemFetchDnsResolver),
54            http.clone(),
55        );
56        Self::new(transport, FetchBodyDecoder::default(), scope, cache)
57    }
58
59    async fn execute(
60        &self,
61        request: FetchRequest,
62        cancellation: CancellationScope,
63    ) -> Result<FetchResult, FetchProviderError> {
64        if cancellation.is_cancelled() {
65            return Err(FetchProviderError::cancelled());
66        }
67        let policy = self.transport.url_policy();
68        let url = policy
69            .validate(request.url())
70            .map_err(|_| FetchProviderError::new(FetchProviderErrorCode::InvalidRequest))?;
71        let key = FetchCacheKey::new(
72            self.scope.clone(),
73            url.as_str().to_owned(),
74            request.max_chars(),
75            FetchCachePolicyKey::new(
76                FETCH_SECURITY_POLICY_VERSION,
77                self.transport.config(),
78                self.decoder.limits(),
79                FETCH_ACCEPT_VALUE,
80                FETCH_ACCEPT_ENCODING_VALUE,
81            ),
82        );
83        if let Some(result) = self.cache.get(&key) {
84            return if cancellation.is_cancelled() {
85                Err(FetchProviderError::cancelled())
86            } else {
87                Ok(result)
88            };
89        }
90
91        let response = self
92            .transport
93            .get(url.as_str(), cancellation.child())
94            .await?;
95        if cancellation.is_cancelled() {
96            return Err(FetchProviderError::cancelled());
97        }
98        if !(200..=299).contains(&response.status()) {
99            return Err(status_error(response.status()));
100        }
101        let result = self.normalize_response(&response, request.max_chars())?;
102        if cancellation.is_cancelled() {
103            return Err(FetchProviderError::cancelled());
104        }
105        self.cache.insert(key, result.clone());
106        Ok(result)
107    }
108
109    fn normalize_response(
110        &self,
111        response: &FetchHttpResponse,
112        max_chars: usize,
113    ) -> Result<FetchResult, FetchProviderError> {
114        let decoded = self
115            .decoder
116            .decode(response.headers(), response.body(), max_chars)?;
117        let mut result = FetchResult::new_with_policy(
118            response.requested_url(),
119            response.final_url(),
120            decoded.mime_type(),
121            decoded.body(),
122            &self.transport.url_policy(),
123        )?;
124        if let Some(title) = decoded.title() {
125            result = result.with_title(title)?;
126        }
127        if let Some(truncation) = decoded.truncation() {
128            result = result.with_truncation(truncation);
129        }
130        result.with_redirects(response.redirects().to_vec())
131    }
132}
133
134impl FetchProvider for HttpFetchProvider {
135    fn fetch(&self, request: FetchRequest, cancellation: CancellationScope) -> FetchFuture<'_> {
136        Box::pin(self.execute(request, cancellation))
137    }
138}
139
140impl fmt::Debug for HttpFetchProvider {
141    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
142        formatter
143            .debug_struct("HttpFetchProvider")
144            .field("transport", &self.transport)
145            .field("decoder", &self.decoder)
146            .field("scope", &self.scope)
147            .field("cache", &self.cache)
148            .finish_non_exhaustive()
149    }
150}
151
152const fn status_error(status: u16) -> FetchProviderError {
153    let code = match status {
154        400..=499 => FetchProviderErrorCode::InvalidRequest,
155        500..=599 => FetchProviderErrorCode::Transport,
156        _ => FetchProviderErrorCode::MalformedResponse,
157    };
158    FetchProviderError::new(code)
159}