feat: enable H2C for the HTTP server (#8861)

This PR adds HTTP/2 Cleartext (H2C) support for the HTTP server, this allows for reverse proxies to use HTTP/2 instead.

## Test
1. Start Forgejo.
2. Run `curl --http2-prior-knowledge http://localhost:3000`.
3. Observe it doesn't return a error.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8861
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: sim <git@sgougeon.fr>
Co-committed-by: sim <git@sgougeon.fr>
This commit is contained in:
sim 2025-08-16 21:00:20 +02:00 committed by Gusted
commit b9bd821fb2

View file

@ -16,6 +16,11 @@ func newHTTPServer(network, address, name string, handler http.Handler) (*Server
Handler: handler,
BaseContext: func(net.Listener) context.Context { return GetManager().HammerContext() },
}
// Enable H2C for HTTP server
httpServer.Protocols = new(http.Protocols)
httpServer.Protocols.SetHTTP1(true)
httpServer.Protocols.SetHTTP2(true)
httpServer.Protocols.SetUnencryptedHTTP2(true)
server.OnShutdown = func() {
httpServer.SetKeepAlivesEnabled(false)
}