aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/tomnomnom/linkheader/main.go
blob: 6b81321b84567f03ef2b16af7fe3ef4e6d8a58bc (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Package linkheader provides functions for parsing HTTP Link headers
package linkheader

import (
	"fmt"
	"strings"
)

// A Link is a single URL and related parameters
type Link struct {
	URL    string
	Rel    string
	Params map[string]string
}

// HasParam returns if a Link has a particular parameter or not
func (l Link) HasParam(key string) bool {
	for p := range l.Params {
		if p == key {
			return true
		}
	}
	return false
}

// Param returns the value of a parameter if it exists
func (l Link) Param(key string) string {
	for k, v := range l.Params {
		if key == k {
			return v
		}
	}
	return ""
}

// String returns the string representation of a link
func (l Link) String() string {

	p := make([]string, 0, len(l.Params))
	for k, v := range l.Params {
		p = append(p, fmt.Sprintf("%s=\"%s\"", k, v))
	}
	if l.Rel != "" {
		p = append(p, fmt.Sprintf("%s=\"%s\"", "rel", l.Rel))
	}
	return fmt.Sprintf("<%s>; %s", l.URL, strings.Join(p, "; "))
}

// Links is a slice of Link structs
type Links []Link

// FilterByRel filters a group of Links by the provided Rel attribute
func (l Links) FilterByRel(r string) Links {
	links := make(Links, 0)
	for _, link := range l {
		if link.Rel == r {
			links = append(links, link)
		}
	}
	return links
}

// String returns the string representation of multiple Links
// for use in HTTP responses etc
func (l Links) String() string {
	if l == nil {
		return fmt.Sprint(nil)
	}

	var strs []string
	for _, link := range l {
		strs = append(strs, link.String())
	}
	return strings.Join(strs, ", ")
}

// Parse parses a raw Link header in the form:
//   <url>; rel="foo", <url>; rel="bar"; wat="dis"
// returning a slice of Link structs
func Parse(raw string) Links {
	var links Links

	// One chunk: <url>; rel="foo"
	for _, chunk := range strings.Split(raw, ",") {

		link := Link{URL: "", Rel: "", Params: make(map[string]string)}

		// Figure out what each piece of the chunk is
		for _, piece := range strings.Split(chunk, ";") {

			piece = strings.Trim(piece, " ")
			if piece == "" {
				continue
			}

			// URL
			if piece[0] == '<' && piece[len(piece)-1] == '>' {
				link.URL = strings.Trim(piece, "<>")
				continue
			}

			// Params
			key, val := parseParam(piece)
			if key == "" {
				continue
			}

			// Special case for rel
			if strings.ToLower(key) == "rel" {
				link.Rel = val
			} else {
				link.Params[key] = val
			}
		}

		if link.URL != "" {
			links = append(links, link)
		}
	}

	return links
}

// ParseMultiple is like Parse, but accepts a slice of headers
// rather than just one header string
func ParseMultiple(headers []string) Links {
	links := make(Links, 0)
	for _, header := range headers {
		links = append(links, Parse(header)...)
	}
	return links
}

// parseParam takes a raw param in the form key="val" and
// returns the key and value as seperate strings
func parseParam(raw string) (key, val string) {

	parts := strings.SplitN(raw, "=", 2)
	if len(parts) == 1 {
		return parts[0], ""
	}
	if len(parts) != 2 {
		return "", ""
	}

	key = parts[0]
	val = strings.Trim(parts[1], "\"")

	return key, val

}