Include linkheader.go directly

master
Peter Stuifzand 4 years ago
parent 045fbfead8
commit 1d529f57dd

@ -1,6 +1,7 @@
package indieauth
import (
"bytes"
"context"
"encoding/json"
"fmt"
@ -9,11 +10,8 @@ import (
"net"
"net/http"
"net/url"
"os"
"strings"
"linkheader"
"p83.nl/go/ekster/pkg/util"
"willnorris.com/go/microformats"
)
@ -47,10 +45,10 @@ func GetEndpoints(me *url.URL) (Endpoints, error) {
}
defer res.Body.Close()
var links linkheader.Links
var links Links
if headers, e := res.Header["Link"]; e {
links = linkheader.ParseMultiple(headers)
links = ParseMultiple(headers)
for _, link := range links {
if link.Rel == "authorization_endpoint" {
endpoints.AuthorizationEndpoint = link.URL
@ -214,12 +212,13 @@ func VerifyAuthCode(clientID, code, redirectURI, authEndpoint string) (bool, *Au
defer resp.Body.Close()
if resp.StatusCode == 200 {
input := io.TeeReader(resp.Body, os.Stderr)
var buf bytes.Buffer
input := io.TeeReader(resp.Body, &buf)
dec := json.NewDecoder(input)
var authResponse AuthResponse
err = dec.Decode(&authResponse)
if err != nil {
return false, nil, err
return false, nil, fmt.Errorf("error while decoding body=%q: %s", &buf, err)
}
return true, &authResponse, nil

@ -0,0 +1,8 @@
module p83.nl/go/indieauth
go 1.14
require (
p83.nl/go/ekster v0.0.0-20191119211024-4511657daa0b
willnorris.com/go/microformats v1.1.0
)

@ -0,0 +1,16 @@
github.com/PuerkitoBio/goquery v1.5.0/go.mod h1:qD2PgZ9lccMbQlc7eEOjaeRlFQON7xY8kdmcsrnKqMg=
github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa h1:F+8P+gmewFQYRk6JoLQLwjBCTu3mcIURZfNkVweuRKA=
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
p83.nl/go/ekster v0.0.0-20191119211024-4511657daa0b h1:MzIItcq83xZn1apSjCNovs55CZE/R/7GUnlOeIJ9HRI=
p83.nl/go/ekster v0.0.0-20191119211024-4511657daa0b/go.mod h1:NDCrcUlnixnkQ4u36jm3Hhvq0WcayEjg8SpAsFdQWLE=
willnorris.com/go/microformats v1.1.0 h1:a16gADl3aFxYVUQDxX8zS2AWAHKNnuaLlZFxyDzmSf8=
willnorris.com/go/microformats v1.1.0/go.mod h1:kvVnWrkkEscVAIITCEoiTX66Hcyg59C7q0E49mb9TJ0=

@ -1,5 +1,4 @@
// Package linkheader provides functions for parsing HTTP Link headers
package linkheader
package indieauth
import (
"fmt"

@ -1,2 +0,0 @@
cpu.out
linkheader.test

@ -1,6 +0,0 @@
language: go
go:
- 1.6
- 1.7
- tip

@ -1,10 +0,0 @@
# Contributing
* Raise an issue if appropriate
* Fork the repo
* Bootstrap the dev dependencies (run `./script/bootstrap`)
* Make your changes
* Use [gofmt](https://golang.org/cmd/gofmt/)
* Make sure the tests pass (run `./script/test`)
* Make sure the linters pass (run `./script/lint`)
* Issue a pull request

@ -1,21 +0,0 @@
MIT License
Copyright (c) 2016 Tom Hudson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

@ -1,35 +0,0 @@
# Golang Link Header Parser
Library for parsing HTTP Link headers. Requires Go 1.6 or higher.
Docs can be found on [the GoDoc page](https://godoc.org/github.com/tomnomnom/linkheader).
[![Build Status](https://travis-ci.org/tomnomnom/linkheader.svg)](https://travis-ci.org/tomnomnom/linkheader)
## Basic Example
```go
package main
import (
"fmt"
"github.com/tomnomnom/linkheader"
)
func main() {
header := "<https://api.github.com/user/58276/repos?page=2>; rel=\"next\"," +
"<https://api.github.com/user/58276/repos?page=2>; rel=\"last\""
links := linkheader.Parse(header)
for _, link := range links {
fmt.Printf("URL: %s; Rel: %s\n", link.URL, link.Rel)
}
}
// Output:
// URL: https://api.github.com/user/58276/repos?page=2; Rel: next
// URL: https://api.github.com/user/58276/repos?page=2; Rel: last
```

@ -1,76 +0,0 @@
package linkheader_test
import (
"fmt"
"github.com/tomnomnom/linkheader"
)
func ExampleParse() {
header := "<https://api.github.com/user/58276/repos?page=2>; rel=\"next\"," +
"<https://api.github.com/user/58276/repos?page=2>; rel=\"last\""
links := linkheader.Parse(header)
for _, link := range links {
fmt.Printf("URL: %s; Rel: %s\n", link.URL, link.Rel)
}
// Output:
// URL: https://api.github.com/user/58276/repos?page=2; Rel: next
// URL: https://api.github.com/user/58276/repos?page=2; Rel: last
}
func ExampleParseMultiple() {
headers := []string{
"<https://api.github.com/user/58276/repos?page=2>; rel=\"next\"",
"<https://api.github.com/user/58276/repos?page=2>; rel=\"last\"",
}
links := linkheader.ParseMultiple(headers)
for _, link := range links {
fmt.Printf("URL: %s; Rel: %s\n", link.URL, link.Rel)
}
// Output:
// URL: https://api.github.com/user/58276/repos?page=2; Rel: next
// URL: https://api.github.com/user/58276/repos?page=2; Rel: last
}
func ExampleLinks_FilterByRel() {
header := "<https://api.github.com/user/58276/repos?page=2>; rel=\"next\"," +
"<https://api.github.com/user/58276/repos?page=2>; rel=\"last\""
links := linkheader.Parse(header)
for _, link := range links.FilterByRel("last") {
fmt.Printf("URL: %s; Rel: %s\n", link.URL, link.Rel)
}
// Output:
// URL: https://api.github.com/user/58276/repos?page=2; Rel: last
}
func ExampleLink_String() {
link := linkheader.Link{
URL: "http://example.com/page/2",
Rel: "next",
}
fmt.Printf("Link: %s\n", link.String())
// Output:
// Link: <http://example.com/page/2>; rel="next"
}
func ExampleLinks_String() {
links := linkheader.Links{
{URL: "http://example.com/page/3", Rel: "next"},
{URL: "http://example.com/page/1", Rel: "last"},
}
fmt.Printf("Link: %s\n", links.String())
// Output:
// Link: <http://example.com/page/3>; rel="next", <http://example.com/page/1>; rel="last"
}

@ -1,173 +0,0 @@
package linkheader
import "testing"
func TestSimple(t *testing.T) {
// Test case stolen from https://github.com/thlorenz/parse-link-header :)
header := "<https://api.github.com/user/9287/repos?page=3&per_page=100>; rel=\"next\", " +
"<https://api.github.com/user/9287/repos?page=1&per_page=100>; rel=\"prev\"; pet=\"cat\", " +
"<https://api.github.com/user/9287/repos?page=5&per_page=100>; rel=\"last\""
links := Parse(header)
if len(links) != 3 {
t.Errorf("Should have been 3 links returned, got %d", len(links))
}
if links[0].URL != "https://api.github.com/user/9287/repos?page=3&per_page=100" {
t.Errorf("First link should have URL 'https://api.github.com/user/9287/repos?page=3&per_page=100'")
}
if links[0].Rel != "next" {
t.Errorf("First link should have rel=\"next\"")
}
if len(links[0].Params) != 0 {
t.Errorf("First link should have exactly 0 params, but has %d", len(links[0].Params))
}
if len(links[1].Params) != 1 {
t.Errorf("Second link should have exactly 1 params, but has %d", len(links[1].Params))
}
if links[1].Params["pet"] != "cat" {
t.Errorf("Second link's 'pet' param should be 'cat', but was %s", links[1].Params["pet"])
}
}
func TestEmpty(t *testing.T) {
links := Parse("")
if links != nil {
t.Errorf("Return value should be nil, but was %s", len(links))
}
}
// Although not often seen in the wild, the grammar in RFC 5988 suggests that it's
// valid for a link header to have nothing but a URL.
func TestNoRel(t *testing.T) {
links := Parse("<http://example.com>")
if len(links) != 1 {
t.Fatalf("Length of links should be 1, but was %d", len(links))
}
if links[0].URL != "http://example.com" {
t.Errorf("URL should be http://example.com, but was %s", links[0].URL)
}
}
func TestLinkMethods(t *testing.T) {
header := "<https://api.github.com/user/9287/repos?page=1&per_page=100>; rel=\"prev\"; pet=\"cat\""
links := Parse(header)
link := links[0]
if link.HasParam("foo") {
t.Errorf("Link should not have param 'foo'")
}
val := link.Param("pet")
if val != "cat" {
t.Errorf("Link should have param pet=\"cat\"")
}
val = link.Param("foo")
if val != "" {
t.Errorf("Link should not have value for param 'foo'")
}
}
func TestLinksMethods(t *testing.T) {
header := "<https://api.github.com/user/9287/repos?page=3&per_page=100>; rel=\"next\", " +
"<https://api.github.com/user/9287/repos?page=1&per_page=100>; rel=\"stylesheet\"; pet=\"cat\", " +
"<https://api.github.com/user/9287/repos?page=5&per_page=100>; rel=\"stylesheet\""
links := Parse(header)
filtered := links.FilterByRel("next")
if filtered[0].URL != "https://api.github.com/user/9287/repos?page=3&per_page=100" {
t.Errorf("URL did not match expected")
}
filtered = links.FilterByRel("stylesheet")
if len(filtered) != 2 {
t.Errorf("Filter for stylesheet should yield 2 results but got %d", len(filtered))
}
filtered = links.FilterByRel("notarel")
if len(filtered) != 0 {
t.Errorf("Filter by non-existant rel should yeild no results")
}
}
func TestParseMultiple(t *testing.T) {
headers := []string{
"<https://api.github.com/user/58276/repos?page=2>; rel=\"next\"",
"<https://api.github.com/user/58276/repos?page=2>; rel=\"last\"",
}
links := ParseMultiple(headers)
if len(links) != 2 {
t.Errorf("Should have returned 2 links")
}
}
func TestLinkToString(t *testing.T) {
l := Link{
URL: "http://example.com/page/2",
Rel: "next",
Params: map[string]string{
"foo": "bar",
},
}
have := l.String()
parsed := Parse(have)
if len(parsed) != 1 {
t.Errorf("Expected only 1 link")
}
if parsed[0].URL != l.URL {
t.Errorf("Re-parsed link header should have matching URL, but has `%s`", parsed[0].URL)
}
if parsed[0].Rel != l.Rel {
t.Errorf("Re-parsed link header should have matching rel, but has `%s`", parsed[0].Rel)
}
if parsed[0].Param("foo") != "bar" {
t.Errorf("Re-parsed link header should have foo=\"bar\" but doesn't")
}
}
func TestLinksToString(t *testing.T) {
ls := Links{
{URL: "http://example.com/page/3", Rel: "next"},
{URL: "http://example.com/page/1", Rel: "last"},
}
have := ls.String()
want := "<http://example.com/page/3>; rel=\"next\", <http://example.com/page/1>; rel=\"last\""
if have != want {
t.Errorf("Want `%s`, have `%s`", want, have)
}
}
func BenchmarkParse(b *testing.B) {
header := "<https://api.github.com/user/9287/repos?page=3&per_page=100>; rel=\"next\", " +
"<https://api.github.com/user/9287/repos?page=1&per_page=100>; rel=\"prev\"; pet=\"cat\", " +
"<https://api.github.com/user/9287/repos?page=5&per_page=100>; rel=\"last\""
for i := 0; i < b.N; i++ {
_ = Parse(header)
}
}

@ -1,6 +0,0 @@
#!/bin/sh
PROJDIR=$(cd `dirname $0`/.. && pwd)
echo "Installing gometalinter and linters..."
go get github.com/alecthomas/gometalinter
gometalinter --install

@ -1,6 +0,0 @@
#!/bin/sh
PROJDIR=$(cd `dirname $0`/.. && pwd)
cd ${PROJDIR}
go get
gometalinter

@ -1,7 +0,0 @@
#!/bin/sh
set -e
PROJDIR=$(cd `dirname $0`/.. && pwd)
cd ${PROJDIR}
go test -bench . -benchmem -cpuprofile cpu.out
go tool pprof linkheader.test cpu.out

@ -1,3 +0,0 @@
#!/bin/sh
PROJDIR=$(cd `dirname $0`/.. && pwd)
cd $PROJDIR && go test
Loading…
Cancel
Save