salvo-rs / salvo

A powerful web framework built with a simplified design.
https://salvo.rs
Apache License 2.0
3.42k stars 207 forks source link

http 2 protocol #181

Closed sysmat closed 1 year ago

sysmat commented 2 years ago

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.

chrislearn commented 2 years ago

Did you tested it in your local browser?

sysmat commented 2 years ago

No with k6 for load, stress, ... tests, https://k6.io/docs/using-k6/protocols/http-2/

sysmat commented 2 years ago

salvo_http1_chrome

sysmat commented 2 years ago
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))
}
chrislearn commented 2 years ago

You need to use RustlsListener or other Tls Listener, You are using scheme HTTP, this is not supports Http2.

sysmat commented 2 years ago
chrislearn commented 2 years ago

You need to enable it in Cargo.toml.

salvo = {features=["rustls"]}
sysmat commented 2 years ago
sysmat commented 2 years ago

@chrislearn thx again

albertoortiz commented 1 year ago

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.

sysmat commented 1 year ago
chrislearn commented 1 year ago

rustls is not moved to another crate. https://github.com/salvo-rs/salvo/blob/010fa46335e24995df1297e63904f04de5e8cee3/crates/salvo/Cargo.toml#LL28C14-L28C14

sysmat commented 1 year ago

what then to another module?

sysmat commented 1 year ago
sysmat commented 1 year ago
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; 
chrislearn commented 1 year ago

salvo::conn::rustls

You can found it if you search it on docs.rs.

sysmat commented 1 year ago

listener -> conn

sysmat commented 1 year ago

salvo::conn::rustls results to

No results :( Try on DuckDuckGo?

Or try looking in one of these:

sysmat commented 1 year ago

ERROR salvo_core::server: http serve connection failed error=Custom { kind: Other, error: "http2 error: connection error detected: unspecific protocol error detected" }

sysmat commented 1 year ago

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
sysmat commented 1 year ago

Docs are very poor, what is inner?

chrislearn commented 1 year ago

Please check latest version in github main branch, I checked it and it works.

sysmat commented 1 year ago
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" }
sysmat commented 1 year ago

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

sysmat commented 1 year ago

2023-05-03T06:22:09.670380Z ERROR salvo_core::server: http serve connection failed error=Custom { kind: Other, error: "received fatal alert: UnknownCA" }

chrislearn commented 1 year ago

Please check it use git main branch, I think I fixed it.