summaryrefslogtreecommitdiff
path: root/src/core/http.rs
blob: 4857f051ce7aaf5c05f633fbb69aded2d9179543 (plain)
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
use reqwest::{Client, Response, StatusCode};
use std::collections::HashMap;
use std::time::Duration;
use thiserror::Error;
use tracing::debug;
use url::Url;

use super::circuit::CircuitBreaker;

#[derive(Error, Debug)]
pub enum HttpError {
    #[error("Request failed: {0}")]
    RequestFailed(#[from] reqwest::Error),
    #[error("Circuit breaker open for host: {0}")]
    CircuitBreakerOpen(String),
    #[error("Too many redirects")]
    TooManyRedirects,
    #[error("Invalid URL: {0}")]
    InvalidUrl(String),
    #[error("HTTP error {status}: {message}")]
    HttpStatus { status: StatusCode, message: String },
    #[error("Network operation disabled in airgap mode")]
    AirgapMode,
}

pub type HttpResult<T> = Result<T, HttpError>;

#[derive(Debug, Clone)]
pub struct HttpClient {
    client: Client,
    circuit_breakers: HashMap<String, CircuitBreaker>,
    max_redirects: usize,
}

impl HttpClient {
    pub fn new() -> Self {
        let client = Client::builder()
            .timeout(Duration::from_secs(30))
            .connect_timeout(Duration::from_secs(10))
            .redirect(reqwest::redirect::Policy::none()) // Handle redirects manually
            .user_agent("spandx-rs/0.1.0")
            .build()
            .expect("Failed to create HTTP client");

        Self {
            client,
            circuit_breakers: HashMap::new(),
            max_redirects: 3,
        }
    }

    pub async fn get(&mut self, url: &str) -> HttpResult<Response> {
        if crate::is_airgap_mode() {
            return Err(HttpError::AirgapMode);
        }

        let parsed_url = Url::parse(url)
            .map_err(|_| HttpError::InvalidUrl(url.to_string()))?;

        let host = parsed_url.host_str()
            .ok_or_else(|| HttpError::InvalidUrl("No host in URL".to_string()))?
            .to_string();

        // Check circuit breaker state first
        let is_open = self.circuit_breakers
            .get(&host)
            .map(|cb| cb.is_open())
            .unwrap_or(false);

        if is_open {
            return Err(HttpError::CircuitBreakerOpen(host));
        }

        // Make the request
        let result = self.make_request(url, 0).await;

        // Update circuit breaker based on result
        let circuit_breaker = self.circuit_breakers
            .entry(host)
            .or_insert_with(CircuitBreaker::new);

        match &result {
            Ok(_) => circuit_breaker.record_success(),
            Err(_) => circuit_breaker.record_failure(),
        }

        result
    }

    async fn make_request(&self, url: &str, redirect_count: usize) -> HttpResult<Response> {
        if redirect_count > self.max_redirects {
            return Err(HttpError::TooManyRedirects);
        }

        debug!("Making HTTP GET request to: {}", url);
        
        let response = self.client
            .get(url)
            .send()
            .await?;

        let status = response.status();
        
        if status.is_redirection() {
            if let Some(location) = response.headers().get("location") {
                let location_str = location.to_str()
                    .map_err(|_| HttpError::InvalidUrl("Invalid redirect location".to_string()))?;
                
                // Handle relative URLs
                let redirect_url = if location_str.starts_with("http") {
                    location_str.to_string()
                } else {
                    let base = Url::parse(url)
                        .map_err(|_| HttpError::InvalidUrl(url.to_string()))?;
                    base.join(location_str)
                        .map_err(|_| HttpError::InvalidUrl("Invalid redirect URL".to_string()))?
                        .to_string()
                };

                debug!("Following redirect to: {}", redirect_url);
                return Box::pin(self.make_request(&redirect_url, redirect_count + 1)).await;
            }
        }

        if !status.is_success() {
            let error_text = response.text().await.unwrap_or_default();
            return Err(HttpError::HttpStatus {
                status,
                message: error_text,
            });
        }

        Ok(response)
    }

    pub async fn get_json<T>(&mut self, url: &str) -> HttpResult<T>
    where
        T: serde::de::DeserializeOwned,
    {
        let response = self.get(url).await?;
        let json = response.json::<T>().await?;
        Ok(json)
    }

    pub async fn get_text(&mut self, url: &str) -> HttpResult<String> {
        let response = self.get(url).await?;
        let text = response.text().await?;
        Ok(text)
    }

    pub fn reset_circuit_breaker(&mut self, host: &str) {
        if let Some(cb) = self.circuit_breakers.get_mut(host) {
            cb.reset();
        }
    }

    pub fn get_circuit_breaker_status(&self, host: &str) -> Option<bool> {
        self.circuit_breakers.get(host).map(|cb| cb.is_open())
    }
}

impl Default for HttpClient {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    #[tokio::test]
    async fn test_http_client_get() {
        let mock_server = MockServer::start().await;

        Mock::given(method("GET"))
            .and(path("/test"))
            .respond_with(ResponseTemplate::new(200).set_body_string("Hello, World!"))
            .mount(&mock_server)
            .await;

        let mut client = HttpClient::new();
        let url = format!("{}/test", mock_server.uri());
        
        let response = client.get(&url).await.unwrap();
        assert_eq!(response.status(), StatusCode::OK);
        
        let text = response.text().await.unwrap();
        assert_eq!(text, "Hello, World!");
    }

    #[tokio::test]
    async fn test_http_client_json() {
        let mock_server = MockServer::start().await;

        Mock::given(method("GET"))
            .and(path("/json"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_json(serde_json::json!({"message": "Hello, JSON!"}))
            )
            .mount(&mock_server)
            .await;

        let mut client = HttpClient::new();
        let url = format!("{}/json", mock_server.uri());
        
        let json: serde_json::Value = client.get_json(&url).await.unwrap();
        assert_eq!(json["message"], "Hello, JSON!");
    }

    #[tokio::test]
    async fn test_http_client_redirect() {
        let mock_server = MockServer::start().await;

        Mock::given(method("GET"))
            .and(path("/redirect"))
            .respond_with(
                ResponseTemplate::new(302)
                    .insert_header("location", format!("{}/final", mock_server.uri()).as_str())
            )
            .mount(&mock_server)
            .await;

        Mock::given(method("GET"))
            .and(path("/final"))
            .respond_with(ResponseTemplate::new(200).set_body_string("Final destination"))
            .mount(&mock_server)
            .await;

        let mut client = HttpClient::new();
        let url = format!("{}/redirect", mock_server.uri());
        
        let response = client.get(&url).await.unwrap();
        let text = response.text().await.unwrap();
        assert_eq!(text, "Final destination");
    }

    #[tokio::test]
    async fn test_airgap_mode() {
        crate::set_airgap_mode(true);
        
        let mut client = HttpClient::new();
        let result = client.get("https://example.com").await;
        
        assert!(matches!(result, Err(HttpError::AirgapMode)));
        
        // Reset for other tests
        crate::set_airgap_mode(false);
    }
}