lh

package module
v0.0.0-...-9b6849e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 8, 2017 License: MIT Imports: 2 Imported by: 1

README

linkheader

Golang tool to parse and set Link headers

Build Status Coverage Status GoReport GoDoc

Install

go get -u github.com/deiu/linkparser

Usage

The ParseHeader() function returns a map where link values are keyed based on rel values.

// the header value usually comes from an http.Request object, using
// req.Header.Get("Link")
header := `<http://example.org>; rel="foo" title="hi", <http://test.com>; rel="bar"`

links = ParseHeader(header)

println(links["foo"]["href"]) // -> http://example.org
println(links["foo"]["title"]) // -> hi
println(links["bar"]["href"]) // -> http://test.com

Golang's normal behavior when setting Link headers is a bit weird. For instance, doing something like request.Header.Add("Link", headerValue) several times with different values seems to append those values to the Link header slice. However, when reading back from that header with request.Header.Get("Link"), it only returns the first element of the slice. This behavior forced me to write my own function AddLink() to set multiple values to a Link header.

// the header value usually comes from an http.Request object, using
// req.Header.Get("Link")
oldHeader := req.Header.Get("Link")
println(oldHeader) // -> ""

// we need to build our list of parameters as per https://tools.ietf.org/html/rfc2068#section-19.6.2.4
params := map[string]string{"rel": "foo", "title": "bar"}
newHeader := AddLink(oldHeader, "http://example.org", params)
req.Header.Set("Link", newHeader)

oldHeader = req.Header.Get("Link")
println(oldHeader) // -> <http://example.org>; rel="foo"; title="bar"

params = map[string]string{"rel": "baz"}
newHeader = AddLink(oldHeader, "http://test.com", params)
req.Header.Set("Link", newHeader)

println(req.Header.Get("Link"))
// -> <http://example.org>; rel="foo"; title="bar", <http://test.com>; rel="baz"

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddLink(oldHeader, link string, params map[string]string) string

AddLink returns a Link header with multiple values by adding new links to an existing Link header (which can also be empty). It only supports two parameters, the <link> and the rel="" value.

func ParseHeader

func ParseHeader(header string) map[string]map[string]string

ParseHeader takes a the value of a Link header (e.g. usually using req.Header.Get("Link")) and returns a map where link values are keyed based on `rel` values.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL