/
cdn.go
48 lines (39 loc) Β· 987 Bytes
/
cdn.go
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
36
37
38
39
40
41
42
43
44
45
46
47
48
package header
import (
"net/http"
"github.com/photoprism/photoprism/pkg/list"
)
// Content Delivery Network (CDN) headers.
const (
CdnHost = "Cdn-Host"
CdnMobileDevice = "Cdn-Mobiledevice"
CdnServerZone = "Cdn-Serverzone"
CdnServerID = "Cdn-Serverid"
CdnConnectionID = "Cdn-Connectionid"
)
var (
CdnMethods = []string{http.MethodGet, http.MethodHead, http.MethodOptions}
)
// IsCdn checks whether the request seems to come from a CDN.
func IsCdn(req *http.Request) bool {
if req == nil {
return false
} else if req.Header == nil || req.URL == nil {
return false
}
if req.Header.Get(CdnHost) != "" {
return true
}
return false
}
// AbortCdnRequest checks if the request should not be served through a CDN.
func AbortCdnRequest(req *http.Request) bool {
if !IsCdn(req) {
return false
} else if req.Header.Get(XAuthToken) != "" {
return true
} else if req.URL.Path == "/" {
return true
}
return list.Excludes(CdnMethods, req.Method)
}