MCPcopy
hub / github.com/caddyserver/caddy / parseLinkHeader

Function parseLinkHeader

modules/caddyhttp/push/link.go:37–71  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

35//
36// where <resource> begins with a forward slash (/).
37func 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
73const (
74 comma = ","

Callers 2

servePreloadLinksMethod · 0.85
TestParseLinkHeaderFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestParseLinkHeaderFunction · 0.68