actix-web 4.14.1

Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
#[cfg(feature = "openssl")]
extern crate tls_openssl as openssl;

use std::{
    convert::Infallible,
    io,
    sync::{
        atomic::{AtomicBool, Ordering},
        mpsc, Arc,
    },
    thread,
    time::Duration,
};

use actix_web::{
    rt::{
        net::TcpStream,
        time::{sleep, timeout},
    },
    web, App, HttpRequest, HttpResponse, HttpServer,
};
use bytes::Bytes;
use futures_util::stream;
use tokio::{
    io::{AsyncReadExt as _, AsyncWriteExt as _},
    sync::{oneshot, Notify},
};

// Read the response head while retaining direct ownership of the connection.
async fn read_http1_response_head(stream: &mut TcpStream) -> io::Result<Vec<u8>> {
    let mut response = Vec::new();

    loop {
        response.push(stream.read_u8().await?);

        if response.ends_with(b"\r\n\r\n") {
            return Ok(response);
        }
    }
}

#[actix_rt::test]
async fn test_start() {
    let addr = actix_test::unused_addr();
    let (tx, rx) = mpsc::channel();

    thread::spawn(move || {
        actix_rt::System::new()
            .block_on(async {
                let srv = HttpServer::new(|| {
                    App::new().service(
                        web::resource("/")
                            .route(web::to(|| async { HttpResponse::Ok().body("test") })),
                    )
                })
                .workers(1)
                .backlog(1)
                .max_connections(10)
                .max_connection_rate(10)
                .keep_alive(Duration::from_secs(10))
                .client_request_timeout(Duration::from_secs(5))
                .client_disconnect_timeout(Duration::ZERO)
                .server_hostname("localhost")
                .system_exit()
                .disable_signals()
                .bind(format!("{}", addr))
                .unwrap()
                .run();

                tx.send(srv.handle()).unwrap();

                srv.await
            })
            .unwrap();
    });

    let srv = rx.recv().unwrap();

    let client = awc::Client::builder()
        .connector(awc::Connector::new().timeout(Duration::from_millis(100)))
        .finish();

    let host = format!("http://{}", addr);
    let response = client.get(host.clone()).send().await.unwrap();
    assert!(response.status().is_success());

    // Attempt to start a second server using the same address.
    let result = HttpServer::new(|| {
        App::new().service(
            web::resource("/").route(web::to(|| async { HttpResponse::Ok().body("test") })),
        )
    })
    .workers(1)
    .backlog(1)
    .max_connections(10)
    .max_connection_rate(10)
    .keep_alive(Duration::from_secs(10))
    .client_request_timeout(Duration::from_secs(5))
    .client_disconnect_timeout(Duration::ZERO)
    .server_hostname("localhost")
    .system_exit()
    .disable_signals()
    .bind(format!("{}", addr));

    // This should fail: the address is in use.
    assert!(result.is_err());

    srv.stop(false).await;
}

#[actix_rt::test]
async fn test_app_data_dropped_after_graceful_shutdown_with_slow_request() {
    struct State {
        _data: Arc<String>,
    }

    async fn echo(_body: web::Json<String>) -> HttpResponse {
        HttpResponse::Ok().finish()
    }

    let (weak_data, app_data) = {
        let data = Arc::new("data".to_owned());
        (Arc::downgrade(&data), web::Data::new(State { _data: data }))
    };

    let server = HttpServer::new(move || {
        App::new()
            .app_data(app_data.clone())
            .service(web::resource("/echo").route(web::post().to(echo)))
    })
    .workers(1)
    .shutdown_timeout(1)
    .bind(("127.0.0.1", 0))
    .unwrap();

    let addr = server.addrs()[0];
    let server = server.run();
    let server_handle = server.handle();

    let send_request = async move {
        sleep(Duration::from_millis(100)).await;

        let slow_body = stream::unfold(0, |idx| async move {
            if idx < 8 {
                sleep(Duration::from_millis(200)).await;
                Some((Ok::<_, Infallible>(Bytes::from_static(b" ")), idx + 1))
            } else {
                None
            }
        });

        let client = awc::Client::default();
        let _ = client
            .post(format!("http://{addr}/echo"))
            .insert_header(("content-type", "application/json"))
            .send_stream(slow_body)
            .await;
    };

    let graceful_stop = async move {
        sleep(Duration::from_millis(300)).await;
        server_handle.stop(true).await;
    };

    let (server_res, (), ()) = tokio::join!(server, send_request, graceful_stop);
    server_res.unwrap();

    for _ in 0..20 {
        sleep(Duration::from_millis(100)).await;

        if weak_data.upgrade().is_none() {
            return;
        }
    }

    panic!("app data still referenced after graceful shutdown");
}

#[actix_rt::test]
async fn graceful_shutdown_closes_idle_http1_connection_after_in_flight_request() {
    let request_started = Arc::new(Notify::new());
    let finish_request = Arc::new(Notify::new());
    let queued_request_started = Arc::new(AtomicBool::new(false));

    let server_request_started = Arc::clone(&request_started);
    let server_finish_request = Arc::clone(&finish_request);
    let server_queued_request_started = Arc::clone(&queued_request_started);

    let server = HttpServer::new(move || {
        let request_started = Arc::clone(&server_request_started);
        let finish_request = Arc::clone(&server_finish_request);
        let queued_request_started = Arc::clone(&server_queued_request_started);

        App::new()
            .route(
                "/idle",
                web::get().to(|| async { HttpResponse::Ok().finish() }),
            )
            .route(
                "/in-flight",
                web::get().to(move || {
                    let request_started = Arc::clone(&request_started);
                    let finish_request = Arc::clone(&finish_request);

                    async move {
                        request_started.notify_one();
                        finish_request.notified().await;
                        HttpResponse::Ok().finish()
                    }
                }),
            )
            .route(
                "/queued",
                web::get().to(move || {
                    queued_request_started.store(true, Ordering::SeqCst);
                    async { HttpResponse::Ok().finish() }
                }),
            )
    })
    .workers(1)
    // Keep these timeouts above the test assertions so they cannot cause EOF.
    .keep_alive(Duration::from_secs(30))
    .shutdown_timeout(5)
    .disable_signals()
    .bind(("127.0.0.1", 0))
    .unwrap();

    let addr = server.addrs()[0];
    let server = server.run();
    let server_handle = server.handle();
    let server_task = actix_web::rt::spawn(server);

    // Complete one request, leaving this connection idle and eligible for HTTP/1 keep-alive.
    let mut idle_connection = TcpStream::connect(addr).await.unwrap();
    idle_connection
        .write_all(b"GET /idle HTTP/1.1\r\nhost: localhost\r\n\r\n")
        .await
        .unwrap();
    let response = read_http1_response_head(&mut idle_connection)
        .await
        .unwrap();
    assert!(response.starts_with(b"HTTP/1.1 200 OK\r\n"));

    // Establish keep-alive on a second connection before giving it active and queued work.
    let mut in_flight_connection = TcpStream::connect(addr).await.unwrap();
    in_flight_connection
        .write_all(b"GET /idle HTTP/1.1\r\nhost: localhost\r\n\r\n")
        .await
        .unwrap();
    let response = read_http1_response_head(&mut in_flight_connection)
        .await
        .unwrap();
    assert!(response.starts_with(b"HTTP/1.1 200 OK\r\n"));

    // Pipeline one request that blocks in its handler and one request that must remain queued.
    in_flight_connection
        .write_all(
            b"GET /in-flight HTTP/1.1\r\nhost: localhost\r\n\r\n\
              GET /queued HTTP/1.1\r\nhost: localhost\r\n\r\n",
        )
        .await
        .unwrap();

    // Do not start shutdown until the first pipelined request is in flight.
    request_started.notified().await;

    let shutdown_task = actix_web::rt::spawn(async move {
        server_handle.stop(true).await;
    });

    // The idle connection must close while the active request remains blocked.
    let mut buf = [0];
    let idle_connection_closed =
        timeout(Duration::from_millis(500), idle_connection.read(&mut buf)).await;

    // Allow the current request, but not its queued successor, to finish during shutdown.
    finish_request.notify_one();

    let response = timeout(
        Duration::from_millis(500),
        read_http1_response_head(&mut in_flight_connection),
    )
    .await
    .expect("in-flight request did not finish during graceful shutdown")
    .unwrap();
    assert!(response.starts_with(b"HTTP/1.1 200 OK\r\n"));

    // The active connection must close instead of returning to keep-alive after its response.
    let in_flight_connection_closed = timeout(
        Duration::from_millis(500),
        in_flight_connection.read(&mut buf),
    )
    .await;

    // Do not leave the server waiting on client sockets when an EOF assertion fails.
    drop((idle_connection, in_flight_connection));

    timeout(Duration::from_secs(2), shutdown_task)
        .await
        .expect("server did not stop after its connections closed")
        .unwrap();
    server_task.await.unwrap().unwrap();

    assert!(
        matches!(idle_connection_closed, Ok(Ok(0))),
        "idle keep-alive connection did not close immediately"
    );
    assert!(
        matches!(in_flight_connection_closed, Ok(Ok(0))),
        "keep-alive connection did not close after its in-flight request"
    );
    assert!(
        !queued_request_started.load(Ordering::SeqCst),
        "queued request started during graceful shutdown"
    );
}

#[actix_rt::test]
async fn shutdown_signal_closes_idle_http1_connection() {
    // Control the exact point at which the builder-configured shutdown signal resolves.
    let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();

    let server = HttpServer::new(|| {
        App::new().route("/", web::get().to(|| async { HttpResponse::Ok().finish() }))
    })
    .workers(1)
    .keep_alive(Duration::from_secs(30))
    .shutdown_timeout(5)
    .shutdown_signal(async move {
        let _ = shutdown_rx.await;
    })
    .bind(("127.0.0.1", 0))
    .unwrap();

    let addr = server.addrs()[0];
    let server_task = actix_web::rt::spawn(server.run());

    // Complete a request and leave the connection idle in HTTP/1 keep-alive.
    let mut connection = TcpStream::connect(addr).await.unwrap();
    connection
        .write_all(b"GET / HTTP/1.1\r\nhost: localhost\r\n\r\n")
        .await
        .unwrap();
    let response = read_http1_response_head(&mut connection).await.unwrap();
    assert!(response.starts_with(b"HTTP/1.1 200 OK\r\n"));

    // Resolve HttpServer::shutdown_signal and expect it to reach the HTTP/1 dispatcher.
    shutdown_tx.send(()).unwrap();

    let mut buf = [0];
    let connection_closed = timeout(Duration::from_millis(500), connection.read(&mut buf)).await;

    timeout(Duration::from_secs(2), server_task)
        .await
        .expect("server did not stop after the shutdown signal")
        .unwrap()
        .unwrap();

    assert!(
        matches!(connection_closed, Ok(Ok(0))),
        "idle keep-alive connection did not close after the shutdown signal"
    );
}

#[cfg(feature = "openssl")]
fn ssl_acceptor() -> openssl::ssl::SslAcceptorBuilder {
    use openssl::{
        pkey::PKey,
        ssl::{SslAcceptor, SslMethod},
        x509::X509,
    };

    let rcgen::CertifiedKey { cert, signing_key } =
        rcgen::generate_simple_self_signed(["localhost".to_owned()]).unwrap();
    let cert_file = cert.pem();
    let key_file = signing_key.serialize_pem();

    let cert = X509::from_pem(cert_file.as_bytes()).unwrap();
    let key = PKey::private_key_from_pem(key_file.as_bytes()).unwrap();

    let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
    builder.set_certificate(&cert).unwrap();
    builder.set_private_key(&key).unwrap();

    builder
}

#[actix_rt::test]
#[cfg(feature = "openssl")]
async fn test_start_ssl() {
    use actix_web::HttpRequest;
    use openssl::ssl::{SslConnector, SslMethod, SslVerifyMode};

    let addr = actix_test::unused_addr();
    let (tx, rx) = mpsc::channel();

    thread::spawn(move || {
        actix_rt::System::new()
            .block_on(async {
                let builder = ssl_acceptor();

                let srv = HttpServer::new(|| {
                    App::new().service(web::resource("/").route(web::to(|req: HttpRequest| {
                        assert!(req.app_config().secure());
                        async { HttpResponse::Ok().body("test") }
                    })))
                })
                .workers(1)
                .shutdown_timeout(1)
                .system_exit()
                .disable_signals()
                .bind_openssl(format!("{}", addr), builder)
                .unwrap();

                let srv = srv.run();
                tx.send(srv.handle()).unwrap();

                srv.await
            })
            .unwrap()
    });
    let srv = rx.recv().unwrap();

    let mut builder = SslConnector::builder(SslMethod::tls()).unwrap();
    builder.set_verify(SslVerifyMode::NONE);
    let _ = builder
        .set_alpn_protos(b"\x02h2\x08http/1.1")
        .map_err(|e| log::error!("Can not set alpn protocol: {:?}", e));

    let client = awc::Client::builder()
        .connector(
            awc::Connector::new()
                .openssl(builder.build())
                .timeout(Duration::from_millis(100)),
        )
        .finish();

    let host = format!("https://{}", addr);
    let response = client.get(host.clone()).send().await.unwrap();
    assert!(response.status().is_success());

    srv.stop(false).await;
}

async fn assert_tcp_nodelay_config(nodelay: bool) {
    let addr = actix_test::unused_addr();
    let (tx, rx) = mpsc::channel();

    thread::spawn(move || {
        actix_rt::System::new()
            .block_on(async move {
                let srv = HttpServer::new(move || {
                    let expected = nodelay;

                    App::new().service(web::resource("/").route(web::to(
                        move |req: HttpRequest| {
                            let expected = expected;

                            async move {
                                let actual = req.conn_data::<bool>().copied().unwrap_or(!expected);
                                if actual == expected {
                                    HttpResponse::Ok().finish()
                                } else {
                                    HttpResponse::InternalServerError().finish()
                                }
                            }
                        },
                    )))
                })
                .workers(1)
                .tcp_nodelay(nodelay)
                .on_connect(move |io, ext| {
                    if let Some(io) = io.downcast_ref::<actix_web::rt::net::TcpStream>() {
                        ext.insert(io.nodelay().unwrap());
                    }
                })
                .bind(format!("{}", addr))
                .unwrap()
                .run();

                tx.send(srv.handle()).unwrap();
                srv.await
            })
            .unwrap()
    });

    let srv = rx.recv().unwrap();

    let client = awc::Client::builder()
        .connector(awc::Connector::new().timeout(Duration::from_millis(100)))
        .finish();

    let response = client.get(format!("http://{}", addr)).send().await.unwrap();
    assert!(response.status().is_success());

    srv.stop(false).await;
}

#[actix_rt::test]
async fn test_tcp_nodelay_enabled() {
    assert_tcp_nodelay_config(true).await;
}

#[actix_rt::test]
async fn test_tcp_nodelay_disabled() {
    assert_tcp_nodelay_config(false).await;
}

#[actix_rt::test]
#[cfg(windows)]
async fn test_dual_stack_ipv6_on_windows() {
    let (tx, rx) = mpsc::channel();

    thread::spawn(move || {
        actix_rt::System::new()
            .block_on(async {
                let srv = HttpServer::new(|| {
                    App::new().service(
                        web::resource("/")
                            .route(web::to(|| async { HttpResponse::Ok().body("test") })),
                    )
                })
                .workers(1)
                .disable_signals()
                .bind("[::]:0")
                .unwrap();

                let port = srv.addrs()[0].port();
                let srv = srv.run();

                tx.send((srv.handle(), port)).unwrap();
                srv.await
            })
            .unwrap();
    });

    let (srv, port) = rx.recv().unwrap();

    let client = awc::Client::builder()
        .connector(awc::Connector::new().timeout(Duration::from_secs(1)))
        .finish();

    let response = client
        .get(format!("http://127.0.0.1:{port}"))
        .send()
        .await
        .unwrap();

    assert!(response.status().is_success());

    srv.stop(false).await;
}