Features detects client features from HTTP headers
(header http.Header)
| 42 | |
| 43 | // Features detects client features from HTTP headers |
| 44 | func (d *Detector) Features(header http.Header) Features { |
| 45 | var f Features |
| 46 | |
| 47 | headerAccept := header.Get("Accept") |
| 48 | |
| 49 | if (d.config.AutoWebp || d.config.EnforceWebp) && strings.Contains(headerAccept, "image/webp") { |
| 50 | f.PreferWebP = true |
| 51 | f.EnforceWebP = d.config.EnforceWebp |
| 52 | } |
| 53 | |
| 54 | if (d.config.AutoAvif || d.config.EnforceAvif) && strings.Contains(headerAccept, "image/avif") { |
| 55 | f.PreferAvif = true |
| 56 | f.EnforceAvif = d.config.EnforceAvif |
| 57 | } |
| 58 | |
| 59 | if (d.config.AutoJxl || d.config.EnforceJxl) && strings.Contains(headerAccept, "image/jxl") { |
| 60 | f.PreferJxl = true |
| 61 | f.EnforceJxl = d.config.EnforceJxl |
| 62 | } |
| 63 | |
| 64 | if !d.config.EnableClientHints { |
| 65 | return f |
| 66 | } |
| 67 | for _, key := range []string{httpheaders.SecChDpr, httpheaders.Dpr} { |
| 68 | val := header.Get(key) |
| 69 | if len(val) == 0 { |
| 70 | continue |
| 71 | } |
| 72 | |
| 73 | if d, err := strconv.ParseFloat(val, 64); err == nil && (d > 0 && d <= maxClientHintDPR) { |
| 74 | f.ClientHintsDPR = d |
| 75 | break |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | for _, key := range []string{httpheaders.SecChWidth, httpheaders.Width} { |
| 80 | val := header.Get(key) |
| 81 | if len(val) == 0 { |
| 82 | continue |
| 83 | } |
| 84 | |
| 85 | if w, err := strconv.Atoi(val); err == nil && w > 0 { |
| 86 | f.ClientHintsWidth = w |
| 87 | break |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return f |
| 92 | } |
| 93 | |
| 94 | // SetVary sets the Vary header value based on enabled features |
| 95 | func (d *Detector) SetVary(header http.Header) { |