MCPcopy
hub / github.com/willnorris/imageproxy / hostMatches

Function hostMatches

imageproxy.go:424–445  ·  view source on GitHub ↗

hostMatches returns whether the host in u matches one of hosts.

(hosts []string, u *url.URL)

Source from the content-addressed store, hash-verified

422
423// hostMatches returns whether the host in u matches one of hosts.
424func hostMatches(hosts []string, u *url.URL) bool {
425 for _, host := range hosts {
426 if u.Hostname() == host {
427 return true
428 }
429 if strings.HasPrefix(host, "*.") && strings.HasSuffix(u.Hostname(), host[2:]) {
430 return true
431 }
432 // Checks whether the host in u is an IP
433 if ip := net.ParseIP(u.Hostname()); ip != nil {
434 // Checks whether our current host is a CIDR
435 if _, ipnet, err := net.ParseCIDR(host); err == nil {
436 // Checks if our host contains the IP in u
437 if ipnet.Contains(ip) {
438 return true
439 }
440 }
441 }
442 }
443
444 return false
445}
446
447// returns whether the referrer from the request is in the host list.
448func referrerMatches(hosts []string, r *http.Request) bool {

Callers 4

TestHostMatchesFunction · 0.85
serveImageMethod · 0.85
allowedMethod · 0.85
referrerMatchesFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestHostMatchesFunction · 0.68