httpcan 0.6.1

A simple HTTP Request & Response Service, with httpbin compatibility
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
use super::*;
use actix_multipart::Multipart;
use actix_web::web::BytesMut;
use futures_util::{StreamExt, TryStreamExt};
use serde_json::Value;
use std::collections::HashMap;
use url::form_urlencoded;

// Helper function to create case-insensitive parameter map
fn to_case_insensitive_map(
    params: &web::Query<HashMap<String, String>>,
) -> HashMap<String, String> {
    let mut case_insensitive = HashMap::new();
    for (key, value) in params.iter() {
        case_insensitive.insert(key.to_lowercase(), value.clone());
    }
    case_insensitive
}

#[derive(Deserialize)]
pub struct RedirectQuery {
    absolute: Option<String>,
}

// Helper function to parse query string into HashMap
fn parse_query_params(query_string: &str) -> HashMap<String, String> {
    let mut params = HashMap::new();

    if query_string.is_empty() {
        return params;
    }

    for pair in query_string.split('&') {
        if let Some((key, value)) = pair.split_once('=') {
            // URL decode the key and value
            if let (Ok(decoded_key), Ok(decoded_value)) =
                (urlencoding::decode(key), urlencoding::decode(value))
            {
                params.insert(decoded_key.to_string(), decoded_value.to_string());
            }
        } else if !pair.is_empty() {
            // Handle keys without values
            if let Ok(decoded_key) = urlencoding::decode(pair) {
                params.insert(decoded_key.to_string(), String::new());
            }
        }
    }

    params
}

// Helper function to extract parameters from request body based on content type
async fn extract_body_params(
    req: &HttpRequest,
    mut payload: web::Payload,
) -> Result<HashMap<String, String>> {
    let content_type = req
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("");

    let mut params = HashMap::new();

    if content_type
        .to_lowercase()
        .starts_with("application/x-www-form-urlencoded")
    {
        // Parse URL-encoded form data
        let mut body = BytesMut::new();
        while let Some(chunk) = payload.next().await {
            let chunk = chunk?;
            body.extend_from_slice(&chunk);
        }

        let body_string = String::from_utf8_lossy(&body);
        for (key, value) in form_urlencoded::parse(body_string.as_bytes()) {
            params.insert(key.to_string(), value.to_string());
        }
    } else if content_type
        .to_lowercase()
        .starts_with("multipart/form-data")
    {
        // Parse multipart form data
        let multipart = Multipart::new(req.headers(), payload);
        params = extract_multipart_params(multipart).await?;
    } else if content_type.to_lowercase().starts_with("application/json") {
        // Parse JSON data
        let mut body = BytesMut::new();
        while let Some(chunk) = payload.next().await {
            let chunk = chunk?;
            body.extend_from_slice(&chunk);
        }

        let body_string = String::from_utf8_lossy(&body);
        if let Ok(Value::Object(obj)) = serde_json::from_str::<Value>(&body_string) {
            for (key, value) in obj {
                if let Value::String(string_value) = value {
                    params.insert(key, string_value);
                } else {
                    // Convert non-string JSON values to strings
                    params.insert(key, value.to_string().trim_matches('"').to_string());
                }
            }
        }
    }

    Ok(params)
}

// Helper function to extract parameters from multipart form data
async fn extract_multipart_params(mut multipart: Multipart) -> Result<HashMap<String, String>> {
    let mut params = HashMap::new();

    while let Some(mut field) = multipart.try_next().await? {
        let content_disposition = field.content_disposition();
        let field_name = content_disposition
            .and_then(|cd| cd.get_name())
            .map(|s| s.to_string());

        if let Some(name) = field_name {
            let mut data = Vec::new();

            // Read field data
            while let Some(chunk) = field.try_next().await? {
                data.extend_from_slice(&chunk);
            }

            // Only handle text fields for redirect parameters
            if let Ok(value) = String::from_utf8(data) {
                params.insert(name, value);
            }
        }
    }

    Ok(params)
}

// Helper function to convert HashMap to case-insensitive HashMap
fn to_case_insensitive_hashmap(params: &HashMap<String, String>) -> HashMap<String, String> {
    let mut case_insensitive = HashMap::new();
    for (key, value) in params.iter() {
        case_insensitive.insert(key.to_lowercase(), value.clone());
    }
    case_insensitive
}

/// Check if the request appears to be from a browser (HTML-capable client)
fn is_browser_request(req: &HttpRequest) -> bool {
    req.headers()
        .get("accept")
        .and_then(|h| h.to_str().ok())
        .map(|accept| accept.to_lowercase().contains("text/html"))
        .unwrap_or(false)
}

/// Check if a URL uses a safe scheme for clickable links (http/https only)
fn is_safe_url_scheme(url: &str) -> bool {
    let lower = url.to_lowercase();
    lower.starts_with("http://") || lower.starts_with("https://")
}

/// HTML-escape a string for safe display in HTML content
fn html_escape(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
        .replace('\'', "&#x27;")
}

/// Build an HTML interstitial page for external redirect-to requests.
///
/// Instead of returning a 302 that silently redirects browser users to an
/// arbitrary URL, return a 200 HTML page that shows the destination and
/// requires a click. Programmatic clients (curl, etc.) still get the 302.
fn build_redirect_interstitial(url: &str) -> String {
    let escaped_url = html_escape(url);
    let link_html = if is_safe_url_scheme(url) {
        format!(
            r#"<a href="{}" rel="noopener noreferrer" class="btn-primary">Continue to external site</a>"#,
            escaped_url
        )
    } else {
        r#"<span class="btn-disabled">Link disabled: unsupported URL scheme</span>"#.to_string()
    };

    let template = r#"<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>External Link - HTTPCan</title>
<style>
body { font-family: system-ui, -apple-system, sans-serif; max-width: 640px; margin: 80px auto; padding: 0 20px; color: #1a1a1a; }
h1 { font-size: 1.5em; }
.warning { color: #c41e3a; font-weight: 600; }
.url { word-break: break-all; background: #f5f5f5; padding: 12px 16px; border-radius: 8px; font-family: monospace; font-size: 0.9em; margin: 16px 0; }
.actions { margin-top: 24px; }
.actions a, .actions span { display: inline-block; padding: 10px 24px; border-radius: 8px; margin-right: 12px; font-weight: 500; text-decoration: none; }
.btn-primary { background: #c41e3a; color: #fff; }
.btn-secondary { background: #e8e8e8; color: #333; }
.btn-disabled { color: #999; }
</style>
</head>
<body>
<h1>External Link</h1>
<p class="warning">&#9888; You are about to leave HTTPCan and visit an external website.</p>
<p>Destination URL:</p>
<div class="url">__URL__</div>
<p>HTTPCan does not control the content of external sites and cannot guarantee their safety. Proceed only if you trust this destination.</p>
<div class="actions">
__LINK__
<a href="/" class="btn-secondary">Return to HTTPCan</a>
</div>
</body>
</html>"#;

    template
        .replace("__URL__", &escaped_url)
        .replace("__LINK__", &link_html)
}

pub async fn redirect_handler(
    req: HttpRequest,
    path: web::Path<usize>,
    query: web::Query<RedirectQuery>,
) -> Result<HttpResponse> {
    let n = path.into_inner();

    if n == 0 {
        return Err(actix_web::error::ErrorBadRequest(
            "n must be greater than 0",
        ));
    }

    let absolute = query
        .absolute
        .as_ref()
        .map(|s| s.to_lowercase() == "true")
        .unwrap_or(false);

    if n == 1 {
        // Final redirect to /get
        if absolute {
            let host = req.connection_info().host().to_string();
            let scheme = if req.connection_info().scheme() == "https" {
                "https"
            } else {
                "http"
            };
            Ok(HttpResponse::Found()
                .append_header(("Location", format!("{}://{}/get", scheme, host)))
                .body(""))
        } else {
            Ok(HttpResponse::Found()
                .append_header(("Location", "/get"))
                .body(""))
        }
    } else {
        // Redirect to the next step
        if absolute {
            let host = req.connection_info().host().to_string();
            let scheme = if req.connection_info().scheme() == "https" {
                "https"
            } else {
                "http"
            };
            Ok(HttpResponse::Found()
                .append_header((
                    "Location",
                    format!("{}://{}/absolute-redirect/{}", scheme, host, n - 1),
                ))
                .body(""))
        } else {
            Ok(HttpResponse::Found()
                .append_header(("Location", format!("/relative-redirect/{}", n - 1)))
                .body(""))
        }
    }
}

pub async fn relative_redirect_handler(
    _req: HttpRequest,
    path: web::Path<usize>,
) -> Result<HttpResponse> {
    let n = path.into_inner();

    if n == 0 {
        return Err(actix_web::error::ErrorBadRequest(
            "n must be greater than 0",
        ));
    }

    if n == 1 {
        // Final redirect to /get
        Ok(HttpResponse::Found()
            .append_header(("Location", "/get"))
            .body(""))
    } else {
        // Relative redirect to the next step
        Ok(HttpResponse::Found()
            .append_header(("Location", format!("/relative-redirect/{}", n - 1)))
            .body(""))
    }
}

pub async fn absolute_redirect_handler(
    req: HttpRequest,
    path: web::Path<usize>,
) -> Result<HttpResponse> {
    let n = path.into_inner();

    if n == 0 {
        return Err(actix_web::error::ErrorBadRequest(
            "n must be greater than 0",
        ));
    }

    let host = req.connection_info().host().to_string();
    let scheme = if req.connection_info().scheme() == "https" {
        "https"
    } else {
        "http"
    };

    if n == 1 {
        // Final redirect to /get
        Ok(HttpResponse::Found()
            .append_header(("Location", format!("{}://{}/get", scheme, host)))
            .body(""))
    } else {
        // Absolute redirect to the next step
        Ok(HttpResponse::Found()
            .append_header((
                "Location",
                format!("{}://{}/absolute-redirect/{}", scheme, host, n - 1),
            ))
            .body(""))
    }
}

pub async fn redirect_to_handler_get(
    req: HttpRequest,
    query: web::Query<HashMap<String, String>>,
) -> Result<HttpResponse> {
    let params = to_case_insensitive_map(&query);

    let url = params
        .get("url")
        .ok_or_else(|| actix_web::error::ErrorBadRequest("Missing required parameter: url"))?;

    // Browser requests get an interstitial page instead of an automatic redirect.
    // This prevents open-redirect abuse where phishers use /redirect-to?url=<malicious>
    // to redirect victims from the trusted httpcan.org domain.
    // Programmatic clients (curl, etc.) still receive the 302 for API compatibility.
    if is_browser_request(&req) {
        return Ok(HttpResponse::Ok()
            .content_type("text/html; charset=utf-8")
            .append_header(("X-Robots-Tag", "noindex"))
            .body(build_redirect_interstitial(url)));
    }

    let mut status_code = 302u16;
    if let Some(status_str) = params.get("status_code") {
        if let Ok(code) = status_str.parse::<u16>() {
            if (300..400).contains(&code) {
                status_code = code;
            }
        }
    }
    let status = StatusCode::from_u16(status_code).unwrap_or(StatusCode::FOUND);

    // Match httpbin's exact UTF-8 encoding behavior
    let location_bytes = url.as_bytes();

    Ok(HttpResponse::build(status)
        .append_header(("Location", location_bytes))
        .body(""))
}

pub async fn redirect_to_handler(req: HttpRequest, payload: web::Payload) -> Result<HttpResponse> {
    // Extract parameters from query string first (lower priority)
    let query_params = parse_query_params(req.query_string());

    // Extract parameters from request body based on content type
    let body_params = extract_body_params(&req, payload).await?;

    // Merge parameters with body taking priority over query
    let mut all_params = query_params;
    all_params.extend(body_params);

    // Convert to case-insensitive map
    let params = to_case_insensitive_hashmap(&all_params);

    let url = params
        .get("url")
        .ok_or_else(|| actix_web::error::ErrorBadRequest("Missing required parameter: url"))?;

    // Browser requests get an interstitial page instead of an automatic redirect.
    // See redirect_to_handler_get for rationale.
    if is_browser_request(&req) {
        return Ok(HttpResponse::Ok()
            .content_type("text/html; charset=utf-8")
            .append_header(("X-Robots-Tag", "noindex"))
            .body(build_redirect_interstitial(url)));
    }

    let mut status_code = 302u16;
    if let Some(status_str) = params.get("status_code") {
        if let Ok(code) = status_str.parse::<u16>() {
            if (300..400).contains(&code) {
                status_code = code;
            }
        }
    }
    let status = StatusCode::from_u16(status_code).unwrap_or(StatusCode::FOUND);

    // Match httpbin's exact UTF-8 encoding behavior
    let location_bytes = url.as_bytes();

    Ok(HttpResponse::build(status)
        .append_header(("Location", location_bytes))
        .body(""))
}