util.go 494 B

123456789101112131415161718192021222324
  1. package entry
  2. import (
  3. "regexp"
  4. "strconv"
  5. )
  6. var maxAgeExp = regexp.MustCompile(`maxage=(\d+)`)
  7. // ParseMaxAge parses the max age from the receiver parameter, "cache-control" header
  8. // returns seconds as int64
  9. // if header not found or parse failed then it returns -1
  10. func ParseMaxAge(header string) int64 {
  11. if header == "" {
  12. return -1
  13. }
  14. m := maxAgeExp.FindStringSubmatch(header)
  15. if len(m) == 2 {
  16. if v, err := strconv.Atoi(m[1]); err == nil {
  17. return int64(v)
  18. }
  19. }
  20. return -1
  21. }