2024-01-16 16:17:16 +01:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2024-01-16 20:04:36 +01:00
|
|
|
var (
|
|
|
|
CdnMethods = []string{http.MethodGet, http.MethodHead, http.MethodOptions}
|
|
|
|
)
|
|
|
|
|
2024-01-16 16:17:16 +01:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2024-01-16 20:04:36 +01:00
|
|
|
// AbortCdnRequest checks if the request should not be served through a CDN.
|
|
|
|
func AbortCdnRequest(req *http.Request) bool {
|
2024-01-16 16:17:16 +01:00
|
|
|
if !IsCdn(req) {
|
|
|
|
return false
|
2024-01-16 20:04:36 +01:00
|
|
|
} else if req.Header.Get(XAuthToken) != "" {
|
|
|
|
return true
|
|
|
|
} else if req.URL.Path == "/" {
|
2024-01-16 16:17:16 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-01-16 20:04:36 +01:00
|
|
|
return list.Excludes(CdnMethods, req.Method)
|
2024-01-16 16:17:16 +01:00
|
|
|
}
|