Add http response in tokio/hyper serve_connection

Hi.

I try to create a small fcgi health checker based on that example async-fcgi/examples/webserver.rs at master · User65k/async-fcgi · GitHub

Now would I like to add a "500 internal server error" in error case, in this code part.

As I'm quite new to rust have I try to use the hyper feature for response building.

My code.

                tokio::task::spawn(async move {
                    if let Err(err) = auto::Builder::new(TokioExecutor::new()).serve_connection(io, make_service).await
                    {
                        println!("Error serving connection: {:?}", err);
                        let response = Response::builder()
                           .status(http::StatusCode::INTERNAL_SERVER_ERROR)
                           .header("Location", "https://www.rust-lang.org/install.html")
                           .body(())
                           .unwrap();
                        Ok::<Response<()>, err>(response);
                    }

When I now run this code tells me the rust compiler this.

cargo run --example webserver --features="con_pool"
warning: the type `[httparse::Header<'_>; 100]` does not permit being left uninitialized
--> src/httpparse.rs:22:69
|
22 |     let mut headers: [httparse::Header<'_>; MAX_HEADERS] = unsafe { mem::uninitialized() };
|                                                                     ^^^^^^^^^^^^^^^^^^^^
|                                                                     |
|                                                                     this code causes undefined behavior when executed
|                                                                     help: use `MaybeUninit<T>` instead, and only call `assume_init` after initialization is done
|
= note: references must be non-null
= note: `#[warn(invalid_value)]` on by default

warning: `async-fcgi` (lib) generated 1 warning
Compiling async-fcgi v0.5.0 (/datadisk/git-repos/async-fcgi)
error[E0435]: attempt to use a non-constant value in a constant
--> examples/webserver.rs:99:44
|
91 |                     if let Err(err) = auto::Builder::new(TokioExecutor::new()).serve_connection(io, make_service).await
|                        ----------- help: consider using `const` instead of `let`: `const err`
...
99 |                         Ok::<Response<()>, err>(response);
|                                            ^^^ non-constant value

For more information about this error, try `rustc --explain E0435`.
error: could not compile `async-fcgi` (example "webserver") due to 1 previous error

My question is how can I send the Response to the client?

My Rust version is this

rustup --version
rustup 1.27.0 (bbb9276d2 2024-03-08)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.77.1 (7cf61ebde 2024-03-27)`

Thanks for any help.

The src/httpparse.rs file seems to be based on a five-year old version of hyper doing something that is now considered incorrect. That's why you get that warning. Newer versions of hyper do it correctly.

1 Like

Thank you. I will try to use tokio-fastcgi/examples/simple.rs at master · FlashSystems/tokio-fastcgi · GitHub instead.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.