git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/httpx/middlewarex/http3_alt_svc.go (about)

     1  package middlewarex
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"git.sr.ht/~pingoo/stdx/httpx"
     8  )
     9  
    10  func Http3AltSvc(port *string) func(next http.Handler) http.Handler {
    11  	portStr := "443"
    12  	if port != nil {
    13  		portStr = *port
    14  	}
    15  	headerValue := fmt.Sprintf(`h3=":%s"; ma=86400, h3-29=":%s"; ma=86400`, portStr, portStr)
    16  
    17  	return func(next http.Handler) http.Handler {
    18  		fn := func(w http.ResponseWriter, r *http.Request) {
    19  			w.Header().Set(httpx.HeaderAltSvc, headerValue)
    20  			next.ServeHTTP(w, r)
    21  		}
    22  		return http.HandlerFunc(fn)
    23  	}
    24  }