1package cors 2 3import "strings" 4 5const toLower = 'a' - 'A' 6 7type converter func(string) string 8 9type wildcard struct { 10 prefix string 11 suffix string 12} 13 14func (w wildcard) match(s string) bool { 15 return len(s) >= len(w.prefix+w.suffix) && strings.HasPrefix(s, w.prefix) && strings.HasSuffix(s, w.suffix) 16} 17 18// convert converts a list of string using the passed converter function 19func convert(s []string, c converter) []string { 20 out := []string{} 21 for _, i := range s { 22 out = append(out, c(i)) 23 } 24 return out 25} 26 27// parseHeaderList tokenize + normalize a string containing a list of headers 28func parseHeaderList(headerList string) []string { 29 l := len(headerList) 30 h := make([]byte, 0, l) 31 upper := true 32 // Estimate the number headers in order to allocate the right splice size 33 t := 0 34 for i := 0; i < l; i++ { 35 if headerList[i] == ',' { 36 t++ 37 } 38 } 39 headers := make([]string, 0, t) 40 for i := 0; i < l; i++ { 41 b := headerList[i] 42 if b >= 'a' && b <= 'z' { 43 if upper { 44 h = append(h, b-toLower) 45 } else { 46 h = append(h, b) 47 } 48 } else if b >= 'A' && b <= 'Z' { 49 if !upper { 50 h = append(h, b+toLower) 51 } else { 52 h = append(h, b) 53 } 54 } else if b == '-' || (b >= '0' && b <= '9') { 55 h = append(h, b) 56 } 57 58 if b == ' ' || b == ',' || i == l-1 { 59 if len(h) > 0 { 60 // Flush the found header 61 headers = append(headers, string(h)) 62 h = h[:0] 63 upper = true 64 } 65 } else { 66 upper = b == '-' 67 } 68 } 69 return headers 70} 71