parseLinkHeader is responsible for parsing Link header and returning list of found resources. Accepted formats are: Link: <resource>; as=script Link: <resource>; as=script,<resource>; as=style Link: <resource>;<resource2> where <resource> begins with a forward slash (/).
(header string)
| 35 | // |
| 36 | // where <resource> begins with a forward slash (/). |
| 37 | func parseLinkHeader(header string) []linkResource { |
| 38 | resources := []linkResource{} |
| 39 | |
| 40 | if header == "" { |
| 41 | return resources |
| 42 | } |
| 43 | |
| 44 | for link := range strings.SplitSeq(header, comma) { |
| 45 | l := linkResource{params: make(map[string]string)} |
| 46 | |
| 47 | li, ri := strings.Index(link, "<"), strings.Index(link, ">") |
| 48 | if li == -1 || ri == -1 { |
| 49 | continue |
| 50 | } |
| 51 | |
| 52 | l.uri = strings.TrimSpace(link[li+1 : ri]) |
| 53 | |
| 54 | for param := range strings.SplitSeq(strings.TrimSpace(link[ri+1:]), semicolon) { |
| 55 | before, after, isCut := strings.Cut(strings.TrimSpace(param), equal) |
| 56 | key := strings.TrimSpace(before) |
| 57 | if key == "" { |
| 58 | continue |
| 59 | } |
| 60 | if isCut { |
| 61 | l.params[key] = strings.TrimSpace(after) |
| 62 | } else { |
| 63 | l.params[key] = key |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | resources = append(resources, l) |
| 68 | } |
| 69 | |
| 70 | return resources |
| 71 | } |
| 72 | |
| 73 | const ( |
| 74 | comma = "," |
no outgoing calls