File: link.go

package info (click to toggle)
golang-github-peterhellberg-link 1.0.0-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 80 kB
  • sloc: makefile: 2
file content (111 lines) | stat: -rw-r--r-- 2,350 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package link

import (
	"net/http"
	"regexp"
	"strings"
)

var (
	commaRegexp      = regexp.MustCompile(`,\s{0,}`)
	valueCommaRegexp = regexp.MustCompile(`([^"]),`)
	equalRegexp      = regexp.MustCompile(` *= *`)
	keyRegexp        = regexp.MustCompile(`[a-z*]+`)
	linkRegexp       = regexp.MustCompile(`\A<(.+)>;(.+)\z`)
	semiRegexp       = regexp.MustCompile(`; +`)
	valRegexp        = regexp.MustCompile(`"+([^"]+)"+`)
)

// Group returned by Parse, contains multiple links indexed by "rel"
type Group map[string]*Link

// Link contains a Link item with URI, Rel, and other non-URI components in Extra.
type Link struct {
	URI   string
	Rel   string
	Extra map[string]string
}

// String returns the URI
func (l *Link) String() string {
	return l.URI
}

// ParseRequest parses the provided *http.Request into a Group
func ParseRequest(req *http.Request) Group {
	if req == nil {
		return nil
	}

	return ParseHeader(req.Header)
}

// ParseResponse parses the provided *http.Response into a Group
func ParseResponse(resp *http.Response) Group {
	if resp == nil {
		return nil
	}

	return ParseHeader(resp.Header)
}

// ParseHeader retrieves the Link header from the provided http.Header and parses it into a Group
func ParseHeader(h http.Header) Group {
	if headers, found := h["Link"]; found {
		return Parse(strings.Join(headers, ", "))
	}

	return nil
}

// Parse parses the provided string into a Group
func Parse(s string) Group {
	if s == "" {
		return nil
	}

	s = valueCommaRegexp.ReplaceAllString(s, "$1")

	group := Group{}

	for _, l := range commaRegexp.Split(s, -1) {
		linkMatches := linkRegexp.FindAllStringSubmatch(l, -1)

		if len(linkMatches) == 0 {
			return nil
		}

		pieces := linkMatches[0]

		link := &Link{URI: pieces[1], Extra: map[string]string{}}

		for _, extra := range semiRegexp.Split(pieces[2], -1) {
			vals := equalRegexp.Split(extra, -1)

			key := keyRegexp.FindString(vals[0])
			val := valRegexp.FindStringSubmatch(vals[1])[1]

			if key == "rel" {
				vals := strings.Split(val, " ")
				rels := []string{vals[0]}

				if len(vals) > 1 {
					for _, v := range vals[1:] {
						if !strings.HasPrefix(v, "http") {
							rels = append(rels, v)
						}
					}
				}

				rel := strings.Join(rels, " ")

				link.Rel = rel
				group[rel] = link
			} else {
				link.Extra[key] = val
			}
		}
	}

	return group
}