mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2024-12-02 18:45:29 +00:00
b9966487f6
close #100 close #109 close #113 close #28 close #63 Reviewed-on: https://codeberg.org/Codeberg/pages-server/pulls/106
28 lines
845 B
Go
28 lines
845 B
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"codeberg.org/codeberg/pages/server/cache"
|
|
"codeberg.org/codeberg/pages/server/context"
|
|
"codeberg.org/codeberg/pages/server/utils"
|
|
)
|
|
|
|
func SetupHTTPACMEChallengeServer(challengeCache cache.SetGetKey) http.HandlerFunc {
|
|
challengePath := "/.well-known/acme-challenge/"
|
|
|
|
return func(w http.ResponseWriter, req *http.Request) {
|
|
ctx := context.New(w, req)
|
|
if strings.HasPrefix(ctx.Path(), challengePath) {
|
|
challenge, ok := challengeCache.Get(utils.TrimHostPort(ctx.Host()) + "/" + string(strings.TrimPrefix(ctx.Path(), challengePath)))
|
|
if !ok || challenge == nil {
|
|
ctx.String("no challenge for this token", http.StatusNotFound)
|
|
}
|
|
ctx.String(challenge.(string))
|
|
} else {
|
|
ctx.Redirect("https://"+string(ctx.Host())+string(ctx.Path()), http.StatusMovedPermanently)
|
|
}
|
|
}
|
|
}
|