httpcan 0.5.4

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
426
427
428
429
430
//! HTTPCan - HTTP Request & Response Service Library
//!
//! This library provides a programmatic way to start an HTTPCan server,
//! which is compatible with httpbin.org and provides various HTTP testing endpoints.

use actix_cors::Cors;
use actix_files as fs;
use actix_web::{web, App, HttpServer};
use std::path::PathBuf;

pub mod config;
pub mod handlers;
pub mod middleware;

pub use config::AppConfig;
use handlers::*;
use middleware::RequestLogger;

/// Configuration for the HTTPCan server
#[derive(Debug, Clone)]
pub struct ServerConfig {
    /// Port to bind the server to
    pub port: u16,
    /// Host address to bind to (default: "0.0.0.0")
    pub host: String,
    /// Whether to add current server to OpenAPI specification
    pub add_current_server: bool,
    /// Headers to exclude from responses
    pub exclude_headers: Vec<String>,
    /// Custom static files directory
    pub static_dir: Option<PathBuf>,
}

impl Default for ServerConfig {
    fn default() -> Self {
        Self {
            port: 8080,
            host: "0.0.0.0".to_string(),
            add_current_server: true,
            exclude_headers: Vec::new(),
            static_dir: None,
        }
    }
}

impl ServerConfig {
    /// Create a new server configuration with default values
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the port for the server
    pub fn port(mut self, port: u16) -> Self {
        self.port = port;
        self
    }

    /// Set the host address for the server
    pub fn host<S: Into<String>>(mut self, host: S) -> Self {
        self.host = host.into();
        self
    }

    /// Enable or disable adding current server to OpenAPI specification
    pub fn add_current_server(mut self, add: bool) -> Self {
        self.add_current_server = add;
        self
    }

    /// Set headers to exclude from responses
    pub fn exclude_headers(mut self, headers: Vec<String>) -> Self {
        self.exclude_headers = headers;
        self
    }

    /// Add a header to exclude from responses
    pub fn exclude_header<S: Into<String>>(mut self, header: S) -> Self {
        self.exclude_headers.push(header.into());
        self
    }

    /// Set custom static files directory
    pub fn static_dir<P: Into<PathBuf>>(mut self, dir: P) -> Self {
        self.static_dir = Some(dir.into());
        self
    }
}

/// HTTPCan server builder and runner
pub struct HttpCanServer {
    config: ServerConfig,
}

impl HttpCanServer {
    /// Create a new HTTPCan server with default configuration
    pub fn new() -> Self {
        Self {
            config: ServerConfig::default(),
        }
    }

    /// Create a new HTTPCan server with custom configuration
    pub fn with_config(config: ServerConfig) -> Self {
        Self { config }
    }

    /// Set the port for the server
    pub fn port(mut self, port: u16) -> Self {
        self.config.port = port;
        self
    }

    /// Set the host address for the server
    pub fn host<S: Into<String>>(mut self, host: S) -> Self {
        self.config.host = host.into();
        self
    }

    /// Enable or disable adding current server to OpenAPI specification
    pub fn add_current_server(mut self, add: bool) -> Self {
        self.config.add_current_server = add;
        self
    }

    /// Set headers to exclude from responses
    pub fn exclude_headers(mut self, headers: Vec<String>) -> Self {
        self.config.exclude_headers = headers;
        self
    }

    /// Add a header to exclude from responses
    pub fn exclude_header<S: Into<String>>(mut self, header: S) -> Self {
        self.config.exclude_headers.push(header.into());
        self
    }

    /// Set custom static files directory
    pub fn static_dir<P: Into<PathBuf>>(mut self, dir: P) -> Self {
        self.config.static_dir = Some(dir.into());
        self
    }

    /// Start the HTTPCan server
    pub async fn run(self) -> std::io::Result<()> {
        let bind_address = format!("{}:{}", self.config.host, self.config.port);

        println!("Starting HTTPCan server on http://{}", bind_address);
        if self.config.add_current_server {
            println!("OpenAPI will include current server in servers list");
        } else {
            println!("OpenAPI will use static servers list only");
        }

        let config = self.config.clone();

        HttpServer::new(move || create_app(config.clone()))
            .client_request_timeout(std::time::Duration::from_secs(120))
            .client_disconnect_timeout(std::time::Duration::from_secs(10))
            .bind(&bind_address)?
            .run()
            .await
    }

    /// Get the server configuration
    pub fn config(&self) -> &ServerConfig {
        &self.config
    }
}

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

/// Create an Actix Web application with all HTTPCan routes
fn create_app(
    server_config: ServerConfig,
) -> App<
    impl actix_web::dev::ServiceFactory<
        actix_web::dev::ServiceRequest,
        Config = (),
        Response = actix_web::dev::ServiceResponse<
            actix_web::body::EitherBody<actix_web::body::BoxBody>,
        >,
        Error = actix_web::Error,
        InitError = (),
    >,
> {
    let static_path = server_config.static_dir.unwrap_or_else(get_static_path);
    let app_config = AppConfig {
        add_current_server: server_config.add_current_server,
        exclude_headers: server_config.exclude_headers,
    };

    let mut app = App::new()
        .app_data(web::Data::new(app_config))
        // Increase payload size limit for large static files
        .app_data(web::PayloadConfig::new(10 * 1024 * 1024)) // 10MB limit
        .wrap(
            Cors::default()
                .allowed_origin_fn(|_origin, _req_head| {
                    // Dynamically set Origin to fully mimic httpbin behavior
                    // httpbin: response.headers["Access-Control-Allow-Origin"] = request.headers.get("Origin", "*")
                    true // Allow all origins, actix-cors will automatically echo Origin header or set to "*"
                })
                .allowed_methods(vec!["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"])
                .allow_any_header()
                .supports_credentials() // Equivalent to Access-Control-Allow-Credentials: true
                .max_age(3600), // Equivalent to Access-Control-Max-Age: 3600
        )
        .wrap(RequestLogger)
        // Dynamic OpenAPI specification endpoint
        .route("/openapi.json", web::get().to(openapi_handler));

    // Only add static file services if the static directory exists
    if static_path.exists() {
        app = app.service(fs::Files::new("/static", &static_path).show_files_listing());
    }

    app = app
        // Echo endpoint - mirrors request body and headers
        .route("/echo", web::get().to(echo_handler_get))
        .route("/echo", web::post().to(echo_handler))
        .route("/echo", web::put().to(echo_handler))
        .route("/echo", web::patch().to(echo_handler))
        .route("/echo", web::delete().to(echo_handler))
        // HTTP Methods
        .route("/get", web::get().to(get_handler))
        .route("/post", web::post().to(post_handler))
        .route("/put", web::put().to(put_handler))
        .route("/patch", web::patch().to(patch_handler))
        .route("/delete", web::delete().to(delete_handler))
        // Anything endpoints - supporting multiple methods
        .route("/anything", web::get().to(anything_handler_get))
        .route("/anything", web::post().to(anything_handler))
        .route("/anything", web::put().to(anything_handler))
        .route("/anything", web::patch().to(anything_handler))
        .route("/anything", web::delete().to(anything_handler))
        .route("/anything", web::trace().to(anything_handler_get))
        // Support for any path after /anything (single or multi-segment)
        .route(
            "/anything/{path:.*}",
            web::get().to(anything_with_param_handler_get),
        )
        .route(
            "/anything/{path:.*}",
            web::post().to(anything_with_param_handler),
        )
        .route(
            "/anything/{path:.*}",
            web::put().to(anything_with_param_handler),
        )
        .route(
            "/anything/{path:.*}",
            web::patch().to(anything_with_param_handler),
        )
        .route(
            "/anything/{path:.*}",
            web::delete().to(anything_with_param_handler),
        )
        .route(
            "/anything/{path:.*}",
            web::trace().to(anything_with_param_handler_get),
        )
        // Auth endpoints
        .route(
            "/basic-auth/{user}/{passwd}",
            web::get().to(basic_auth_handler),
        )
        .route(
            "/basic-auth/{user}",
            web::get().to(basic_auth_user_only_handler),
        )
        .route(
            "/hidden-basic-auth/{user}/{passwd}",
            web::get().to(hidden_basic_auth_handler),
        )
        .route(
            "/hidden-basic-auth/{user}",
            web::get().to(hidden_basic_auth_user_only_handler),
        )
        .route("/bearer", web::get().to(bearer_auth_handler))
        .route("/jwt-bearer", web::get().to(jwt_bearer_handler))
        // Digest auth endpoints - support both GET and POST for auth-int with body
        .route(
            "/digest-auth/{qop}/{user}/{passwd}",
            web::get().to(digest_auth_handler),
        )
        .route(
            "/digest-auth/{qop}/{user}/{passwd}",
            web::post().to(digest_auth_handler),
        )
        .route(
            "/digest-auth/{qop}/{user}/{passwd}/{algorithm}",
            web::get().to(digest_auth_with_algorithm_handler),
        )
        .route(
            "/digest-auth/{qop}/{user}/{passwd}/{algorithm}",
            web::post().to(digest_auth_with_algorithm_handler),
        )
        .route(
            "/digest-auth/{qop}/{user}/{passwd}/{algorithm}/{stale_after}",
            web::get().to(digest_auth_full_handler),
        )
        .route(
            "/digest-auth/{qop}/{user}/{passwd}/{algorithm}/{stale_after}",
            web::post().to(digest_auth_full_handler),
        )
        // Response formats
        .route("/json", web::get().to(json_handler))
        .route("/xml", web::get().to(xml_handler))
        .route("/html", web::get().to(html_handler))
        .route("/robots.txt", web::get().to(robots_txt_handler))
        .route("/deny", web::get().to(deny_handler))
        .route("/encoding/utf8", web::get().to(utf8_handler))
        .route("/gzip", web::get().to(gzip_handler))
        .route("/deflate", web::get().to(deflate_handler))
        .route("/brotli", web::get().to(brotli_handler))
        // Dynamic data
        .route("/uuid", web::get().to(uuid_handler))
        .route("/base64/{value}", web::get().to(base64_handler))
        .route("/bytes/{n}", web::get().to(bytes_handler))
        .route("/stream-bytes/{n}", web::get().to(stream_bytes_handler))
        .route("/stream/{n}", web::get().to(stream_handler))
        .route("/range/{numbytes}", web::get().to(range_handler))
        .route("/links/{n}/{offset}", web::get().to(links_handler))
        .route("/links/{n}", web::get().to(links_redirect_handler))
        .route("/drip", web::get().to(drip_handler))
        // Delay endpoint - supporting multiple methods
        .route("/delay/{delay}", web::get().to(delay_handler_get))
        .route("/delay/{delay}", web::post().to(delay_handler))
        .route("/delay/{delay}", web::put().to(delay_handler))
        .route("/delay/{delay}", web::patch().to(delay_handler))
        .route("/delay/{delay}", web::delete().to(delay_handler))
        .route("/delay/{delay}", web::trace().to(delay_handler_get))
        // Status codes - supporting multiple methods
        .route("/status/{codes}", web::get().to(status_handler_get))
        .route("/status/{codes}", web::post().to(status_handler))
        .route("/status/{codes}", web::put().to(status_handler))
        .route("/status/{codes}", web::patch().to(status_handler))
        .route("/status/{codes}", web::delete().to(status_handler))
        .route("/status/{codes}", web::trace().to(status_handler_get))
        .route(
            "/status/{codes}",
            web::method(actix_web::http::Method::OPTIONS).to(status_options_handler),
        )
        // Redirects
        .route("/redirect/{n}", web::get().to(redirect_handler))
        .route(
            "/relative-redirect/{n}",
            web::get().to(relative_redirect_handler),
        )
        .route(
            "/absolute-redirect/{n}",
            web::get().to(absolute_redirect_handler),
        )
        .route("/redirect-to", web::get().to(redirect_to_handler_get))
        .route("/redirect-to", web::post().to(redirect_to_handler))
        .route("/redirect-to", web::put().to(redirect_to_handler))
        .route("/redirect-to", web::patch().to(redirect_to_handler))
        .route("/redirect-to", web::delete().to(redirect_to_handler))
        .route("/redirect-to", web::trace().to(redirect_to_handler_get))
        // Request inspection
        .route("/headers", web::get().to(headers_handler))
        .route("/ip", web::get().to(ip_handler))
        .route("/user-agent", web::get().to(user_agent_handler))
        // Response inspection
        .route("/cache", web::get().to(cache_handler))
        .route("/cache/{value}", web::get().to(cache_control_handler))
        .route("/etag/{etag}", web::get().to(etag_handler))
        .route(
            "/response-headers",
            web::get().to(response_headers_get_handler),
        )
        .route(
            "/response-headers",
            web::post().to(response_headers_post_handler),
        )
        // Cookies
        .route("/cookies", web::get().to(cookies_handler))
        .route("/cookies/set", web::get().to(cookies_set_handler))
        .route(
            "/cookies/set/{name}/{value}",
            web::get().to(cookies_set_named_handler),
        )
        .route("/cookies/delete", web::get().to(cookies_delete_handler))
        // Images
        .route("/image", web::get().to(image_handler))
        .route("/image/png", web::get().to(image_png_handler))
        .route("/image/jpeg", web::get().to(image_jpeg_handler))
        .route("/image/webp", web::get().to(image_webp_handler))
        .route("/image/svg", web::get().to(image_svg_handler))
        // Server-Sent Events (SSE)
        .route("/sse", web::get().to(sse_handler))
        .route("/sse/{count}", web::get().to(sse_path_handler))
        .route(
            "/sse/{count}/{delay}",
            web::get().to(sse_path_with_delay_handler),
        )
        // NDJSON streaming endpoints
        .route("/ndjson", web::get().to(ndjson_handler))
        .route("/ndjson/{count}", web::get().to(ndjson_path_handler))
        .route(
            "/ndjson/{count}/{delay}",
            web::get().to(ndjson_path_with_delay_handler),
        )
        // Root endpoint - returns HTML or API info based on Accept header
        .route("/", web::get().to(root_handler));

    // Only add root static file service if the static directory exists
    // This is added after all routes to serve as fallback for static resources
    // Use prefer_utf8 and configure for large files
    if static_path.exists() {
        app = app.service(
            fs::Files::new("/", &static_path)
                .index_file("index.html")
                .prefer_utf8(true)
                .use_last_modified(true)
                .use_etag(true),
        );
    }

    app
}

/// Get the default static files path
fn get_static_path() -> PathBuf {
    handlers::utils::get_static_path()
}