webpage_quality_analyzer 1.0.2

High-performance webpage quality analyzer with 115 comprehensive metrics - Rust library with WASM, C++, and Python bindings
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! Web fetching module for URL-based analysis
//!
//! This module provides HTTP client functionality to fetch web pages
//! for quality analysis, supporting various configuration options including
//! caching, retry logic, and connection pooling.

use crate::models::models::{AnalyzeError, Result};

#[cfg(feature = "async")]
use once_cell::sync::Lazy;
#[cfg(feature = "async")]
use reqwest::Client;
#[cfg(feature = "async")]
use std::collections::HashMap;
#[cfg(feature = "async")]
use std::sync::{Arc, Mutex};
#[cfg(feature = "async")]
use std::time::Duration;
#[cfg(feature = "async")]
use tokio::time::sleep;

/// Shared HTTP client instance for better resource management
#[cfg(feature = "async")]
static HTTP_CLIENT: Lazy<Client> = Lazy::new(|| {
    Client::builder()
        .timeout(Duration::from_secs(30)) // Default timeout
        .pool_max_idle_per_host(10) // Connection pooling
        .pool_idle_timeout(Duration::from_secs(90))
        .build()
        .unwrap_or_else(|_| {
            // Fallback to default client if builder fails
            Client::new()
        })
});

/// Cache entry for fetched content
#[cfg(feature = "async")]
#[derive(Debug, Clone)]
struct CacheEntry {
    content: String,
    headers: reqwest::header::HeaderMap,
    size: usize,
    timestamp: std::time::Instant,
}

/// Shared cache for HTTP responses
#[cfg(feature = "async")]
static RESPONSE_CACHE: Lazy<Arc<Mutex<HashMap<String, CacheEntry>>>> =
    Lazy::new(|| Arc::new(Mutex::new(HashMap::new())));

/// Retry configuration for failed requests
#[derive(Debug, Clone, Copy)]
pub struct RetryConfig {
    /// Maximum number of retry attempts
    pub max_retries: u32,
    /// Initial delay between retries (exponential backoff)
    pub initial_delay_ms: u64,
    /// Maximum delay between retries
    pub max_delay_ms: u64,
}

impl Default for RetryConfig {
    fn default() -> Self {
        Self {
            max_retries: 3,
            initial_delay_ms: 100,
            max_delay_ms: 5000,
        }
    }
}

/// Configuration options for web fetching
#[derive(Debug, Clone)]
pub struct FetchOptions {
    /// Timeout in seconds for HTTP requests
    #[cfg(feature = "async")]
    pub timeout_seconds: u64,
    /// User agent string to use for requests
    #[cfg(feature = "async")]
    pub user_agent: String,
    /// Whether to follow HTTP redirects
    #[cfg(feature = "async")]
    pub follow_redirects: bool,
    /// Maximum number of redirects to follow
    #[cfg(feature = "async")]
    pub max_redirects: usize,
    /// Future feature: JavaScript rendering support
    #[cfg(feature = "async")]
    pub javascript_rendering: bool,
    /// Cache TTL in seconds (0 = no caching)
    #[cfg(feature = "async")]
    pub cache_ttl_seconds: u64,
    /// Retry configuration
    #[cfg(feature = "async")]
    pub retry_config: RetryConfig,
    /// Placeholder for non-async builds
    #[cfg(not(feature = "async"))]
    _phantom: (),
}

impl Default for FetchOptions {
    fn default() -> Self {
        Self {
            #[cfg(feature = "async")]
            timeout_seconds: 10,
            #[cfg(feature = "async")]
            user_agent: "Mozilla/5.0 (compatible; PageQualityAnalyzer/1.0; +https://github.com/NotGyashu/webpage-quality-analyser)".to_string(),
            #[cfg(feature = "async")]
            follow_redirects: true,
            #[cfg(feature = "async")]
            max_redirects: 5,
            #[cfg(feature = "async")]
            javascript_rendering: false,
            #[cfg(feature = "async")]
            cache_ttl_seconds: 300, // 5 minutes default cache
            #[cfg(feature = "async")]
            retry_config: RetryConfig::default(),
            #[cfg(not(feature = "async"))]
            _phantom: (),
        }
    }
}

/// Web fetcher for retrieving HTML content from URLs
#[derive(Debug)]
pub struct WebFetcher {
    #[cfg(feature = "async")]
    options: FetchOptions,
}

impl WebFetcher {
    /// Create a new web fetcher with the given options
    pub fn new(options: FetchOptions) -> Result<Self> {
        #[cfg(feature = "async")]
        {
            Ok(Self { options })
        }
        #[cfg(not(feature = "async"))]
        {
            Err(crate::models::models::AnalyzeError::InternalError(
                "Async functionality not available. Enable the 'async' feature to use URL fetching.".to_string()
            ))
        }
    }

    /// Get a configured request builder using the shared client
    #[cfg(feature = "async")]
    fn request_builder(&self, url: &str) -> reqwest::RequestBuilder {
        HTTP_CLIENT
            .get(url)
            .timeout(Duration::from_secs(self.options.timeout_seconds))
            .header("User-Agent", &self.options.user_agent)
    }

    /// Fetch HTML content from a URL
    #[cfg(feature = "async")]
    pub async fn fetch_html(&self, url: &str) -> Result<String> {
        let response = self
            .request_builder(url)
            .send()
            .await
            .map_err(|e| AnalyzeError::InternalError(e.to_string()))?;

        if !response.status().is_success() {
            return Err(AnalyzeError::InternalError(format!(
                "HTTP {}: Failed to fetch {}",
                response.status(),
                url
            )));
        }

        let html = response
            .text()
            .await
            .map_err(|e| AnalyzeError::InternalError(e.to_string()))?;

        Ok(html)
    }

    /// Fetch HTML content and HTTP headers from a URL
    #[cfg(feature = "async")]
    pub async fn fetch_with_headers(
        &self,
        url: &str,
    ) -> Result<(String, reqwest::header::HeaderMap)> {
        let response = self
            .request_builder(url)
            .send()
            .await
            .map_err(|e| AnalyzeError::InternalError(e.to_string()))?;

        if !response.status().is_success() {
            return Err(AnalyzeError::InternalError(format!(
                "HTTP {}: Failed to fetch {}",
                response.status(),
                url
            )));
        }

        let headers = response.headers().clone();
        let html = response
            .text()
            .await
            .map_err(|e| AnalyzeError::InternalError(e.to_string()))?;

        Ok((html, headers))
    }

    /// Fetch HTML content, headers, and response size from a URL
    #[cfg(feature = "async")]
    pub async fn fetch_with_metrics(
        &self,
        url: &str,
    ) -> Result<(String, reqwest::header::HeaderMap, usize)> {
        let response = self
            .request_builder(url)
            .send()
            .await
            .map_err(|e| AnalyzeError::InternalError(e.to_string()))?;

        if !response.status().is_success() {
            return Err(AnalyzeError::InternalError(format!(
                "HTTP {}: Failed to fetch {}",
                response.status(),
                url
            )));
        }

        let headers = response.headers().clone();
        let bytes = response
            .bytes()
            .await
            .map_err(|e| AnalyzeError::InternalError(e.to_string()))?;

        let response_size = bytes.len();
        let html = String::from_utf8(bytes.to_vec())
            .map_err(|e| AnalyzeError::InternalError(format!("Invalid UTF-8: {}", e)))?;

        Ok((html, headers, response_size))
    }

    /// Get the fetch options used by this fetcher
    #[cfg(feature = "async")]
    pub fn options(&self) -> &FetchOptions {
        &self.options
    }

    /// Get cached response if available and not expired
    #[cfg(feature = "async")]
    fn get_cached(&self, url: &str) -> Option<CacheEntry> {
        if let Ok(cache) = RESPONSE_CACHE.lock() {
            if let Some(entry) = cache.get(url) {
                let age = entry.timestamp.elapsed().as_secs();
                if age < self.options.cache_ttl_seconds {
                    return Some(entry.clone());
                }
            }
        }
        None
    }

    /// Store response in cache
    #[cfg(feature = "async")]
    fn cache_response(
        &self,
        url: &str,
        content: String,
        headers: reqwest::header::HeaderMap,
        size: usize,
    ) {
        if self.options.cache_ttl_seconds == 0 {
            return;
        }

        if let Ok(mut cache) = RESPONSE_CACHE.lock() {
            cache.insert(
                url.to_string(),
                CacheEntry {
                    content,
                    headers,
                    size,
                    timestamp: std::time::Instant::now(),
                },
            );

            // Simple cache cleanup: remove entries older than TTL
            let ttl = Duration::from_secs(self.options.cache_ttl_seconds);
            cache.retain(|_, entry| entry.timestamp.elapsed() < ttl);
        }
    }

    /// Fetch with retry logic and exponential backoff
    #[cfg(feature = "async")]
    async fn fetch_with_retry(&self, url: &str) -> Result<reqwest::Response> {
        let mut last_error: Option<AnalyzeError> = None;
        let mut delay = self.options.retry_config.initial_delay_ms;

        for attempt in 0..=self.options.retry_config.max_retries {
            match self.request_builder(url).send().await {
                Ok(response) => {
                    if response.status().is_success() {
                        return Ok(response);
                    } else if response.status().is_server_error()
                        && attempt < self.options.retry_config.max_retries
                    {
                        // Retry on server errors (5xx)
                        last_error = Some(AnalyzeError::InternalError(format!(
                            "HTTP {}: Server error (attempt {})",
                            response.status(),
                            attempt + 1
                        )));
                    } else {
                        // Don't retry client errors (4xx)
                        return Err(AnalyzeError::InternalError(format!(
                            "HTTP {}: Failed to fetch {}",
                            response.status(),
                            url
                        )));
                    }
                }
                Err(e) => {
                    last_error = Some(AnalyzeError::InternalError(e.to_string()));
                    if attempt < self.options.retry_config.max_retries {
                        // Only sleep if we're going to retry
                        sleep(Duration::from_millis(delay)).await;
                        delay = (delay * 2).min(self.options.retry_config.max_delay_ms);
                    }
                }
            }
        }

        Err(last_error.unwrap_or_else(|| AnalyzeError::InternalError("Unknown error".to_string())))
    }

    /// Fetch HTML content with caching and retry logic
    #[cfg(feature = "async")]
    pub async fn fetch_html_cached(&self, url: &str) -> Result<String> {
        // Check cache first
        if let Some(cached) = self.get_cached(url) {
            return Ok(cached.content);
        }

        // Fetch with retry logic
        let response = self.fetch_with_retry(url).await?;
        let headers = response.headers().clone();
        let html = response
            .text()
            .await
            .map_err(|e| AnalyzeError::InternalError(e.to_string()))?;

        // Cache the response
        let size = html.len();
        self.cache_response(url, html.clone(), headers, size);

        Ok(html)
    }

    /// Fetch HTML content, headers, and metrics with caching and retry
    #[cfg(feature = "async")]
    pub async fn fetch_with_metrics_cached(
        &self,
        url: &str,
    ) -> Result<(String, reqwest::header::HeaderMap, usize)> {
        // Check cache first
        if let Some(cached) = self.get_cached(url) {
            return Ok((cached.content, cached.headers, cached.size));
        }

        // Fetch with retry logic
        let response = self.fetch_with_retry(url).await?;
        let headers = response.headers().clone();
        let bytes = response
            .bytes()
            .await
            .map_err(|e| AnalyzeError::InternalError(e.to_string()))?;

        let response_size = bytes.len();
        let html = String::from_utf8(bytes.to_vec())
            .map_err(|e| AnalyzeError::InternalError(format!("Invalid UTF-8: {}", e)))?;

        // Cache the response
        self.cache_response(url, html.clone(), headers.clone(), response_size);

        Ok((html, headers, response_size))
    }

    /// Clear the response cache
    #[cfg(feature = "async")]
    pub fn clear_cache() {
        if let Ok(mut cache) = RESPONSE_CACHE.lock() {
            cache.clear();
        }
    }

    /// Get cache statistics
    #[cfg(feature = "async")]
    pub fn cache_stats() -> (usize, usize) {
        if let Ok(cache) = RESPONSE_CACHE.lock() {
            let total_entries = cache.len();
            let total_size: usize = cache.values().map(|entry| entry.size).sum();
            (total_entries, total_size)
        } else {
            (0, 0)
        }
    }
}