Closed sysmat closed 1 year ago
Did you tested it in your local browser?
No with k6 for load, stress, ... tests, https://k6.io/docs/using-k6/protocols/http-2/
let listener = TcpListener::bind("127.0.0.1:7878");
Server::new(listener).serve(route()).await;
fn route() -> Router {
let auth_handler = BasicAuth::new(Validator {
db: "mana".to_string(),
});
Router::with_path("api")
.get(ping)
.push(Router::with_path("user").hoop(auth_handler).hoop(Logger).get(get_user)
.push(Router::with_path("<id>").get(get_user_by_id))
)
}
#[async_trait]
impl BasicAuthValidator for Validator {
async fn validate(&self, username: &str, password: &str) -> bool {
true
}
}
#[handler]
pub async fn get_user_by_id(req: &mut Request, res: &mut Response) {
let id = req.param::<i32>("id").unwrap();
let u = user {
id: 1,
username: "sabin"
}
res.render(Json(u))
}
You need to use RustlsListener or other Tls Listener, You are using scheme HTTP, this is not supports Http2.
You need to enable it in Cargo.toml.
salvo = {features=["rustls"]}
@chrislearn thx again
Hey, there is a way to add a listener to enable http2 without the TLS layer ? I don't need the security part for my micro-services.
rustls is not moved to another crate. https://github.com/salvo-rs/salvo/blob/010fa46335e24995df1297e63904f04de5e8cee3/crates/salvo/Cargo.toml#LL28C14-L28C14
what then to another module?
use salvo::listener::rustls::{Keycert, RustlsConfig};
there was before
salvo = {version = "0.37", features = ["basic-auth", "test", "logging", "rustls"]}
use salvo::conn::rustls::{Keycert, RustlsConfig};
let config = RustlsConfig::new(
Keycert::new()
.with_cert(include_bytes!("../certs/cert.pem").as_ref())
.with_key(include_bytes!("../certs/key.pem").as_ref()),
);
let acceptor = TcpListener::new("127.0.0.1:7878")
.rustls(config)
.bind()
.await;
Server::new(acceptor).serve(route()).await;
output
INFO salvo_core::server: listening HTTP/2.0 on https://127.0.0.1:7878
INFO rust_salvo: Listening on http://127.0.0.1:7878
in postman Parse Error: The server returned a malformed response
in curl: Received HTTP/0.9 when not allowed
this very confusing
salvo::conn::rustls
You can found it if you search it on docs.rs.
listener -> conn
ERROR salvo_core::server: http serve connection failed error=Custom { kind: Other, error: "http2 error: connection error detected: unspecific protocol error detected" }
2023-05-02T15:09:18.155154Z ERROR salvo_core::server: http serve connection failed error=Custom { kind: Other, error: "received corrupt message" }
postman: Error: socket hang up
curl: (52) Empty reply from server
Docs are very poor, what is inner?
Please check latest version in github main branch, I checked it and it works.
2023-05-03T06:14:22.034998Z INFO salvo_core::server: listening HTTP/2.0 on https://127.0.0.1:5800
2023-05-03T06:14:22.036707Z INFO salvo_core::conn::rustls::listener: tls config loaded.
2023-05-03T06:14:39.007584Z ERROR salvo_core::server: http serve connection failed error=Custom { kind: Other, error: "http2 error: connection error detected: unspecific protocol error detected" }
2023-05-03T06:16:02.261637Z ERROR salvo_core::server: http serve connection failed error=Custom { kind: Other, error: "received corrupt message" }
I have also REST API in many languages(java, kotlin, deno, js, ...) and all works fine with curl, postman, k6, chrome and http 2 protocol
2023-05-03T06:22:09.670380Z ERROR salvo_core::server: http serve connection failed error=Custom { kind: Other, error: "received fatal alert: UnknownCA" }
Please check it use git main branch, I think I fixed it.
Is your feature request related to a problem? Please describe. salvo support HTTP 2 version?
Describe the solution you'd like
Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.
Additional context Add any other context or screenshots about the feature request here.