forked from srfrog/go-relax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linking.go
60 lines (55 loc) · 1.82 KB
/
linking.go
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
// Copyright 2014 Codehack.com All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package relax
import (
"fmt"
"reflect"
"strings"
)
// Link represents a hypertext relation link. It implements HTTP web links
// between resources that are not format specific. For details see also,
// Web Linking: :https://tools.ietf.org/html/rfc5988
// URI Template: http://tools.ietf.org/html/rfc6570
type Link struct {
URI string `json:"href"`
Rel string `json:"rel"`
Anchor string `json:"anchor,omitempty"`
Rev string `json:"rev,omitempty"`
HrefLang string `json:"hreflang,omitempty"`
Media string `json:"media,omitempty"`
Title string `json:"title,omitempty"`
Type string `json:"type,omitempty"`
}
// String returns a string representation of a Link object. Suitable for use
// in Link: headers.
func (l *Link) String() string {
link := fmt.Sprintf(`<%s>`, l.URI)
e := reflect.ValueOf(l).Elem()
for i := 1; i < e.NumField(); i++ {
n, v := e.Type().Field(i).Name, e.Field(i).String()
if v == "" {
if n != "Rel" {
continue
}
v = "alternate"
}
link += fmt.Sprintf(`; %s=%q`, strings.ToLower(n), v)
}
return link
}
// LinkHeader returns a complete Link: header value that can be plugged
// into http.Header().Add(). Use this when you don't need a Link object
// for your relation, just a header.
// uri is the URI of target.
// param is one or more name=value pairs for link values. if nil, will default
// to rel="alternate" (as per RFC 4287).
// Returns two strings: "Link","Link header spec"
func LinkHeader(uri string, param ...string) (string, string) {
value := []string{fmt.Sprintf(`<%s>`, uri)}
if param == nil {
param = []string{`rel="alternate"`}
}
value = append(value, param...)
return "Link", strings.Join(value, "; ")
}