-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathslug.go
More file actions
35 lines (32 loc) · 703 Bytes
/
slug.go
File metadata and controls
35 lines (32 loc) · 703 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package onionize
import (
"crypto/subtle"
"fmt"
"net/http"
"strings"
)
func checkSlug(req *http.Request, slug string) error {
if slug == "" {
return nil
}
shost := strings.Split(req.Host, ".")
if len(shost) < 3 {
return fmt.Errorf("hostname is too short")
}
if 1 != subtle.ConstantTimeCompare([]byte(slug), []byte(shost[len(shost)-3])) {
return fmt.Errorf("wrong slug")
}
return nil
}
func subdomainSluggedHandler(h http.Handler, slug string) http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
err := checkSlug(req, slug)
if err != nil {
http.NotFound(w, req)
return
}
h.ServeHTTP(w, req)
})
return mux
}