Skip to content

Commit 1f0ade2

Browse files
committed
Import ww/goautoneg to internal/ (Fix #17)
1 parent 369ec04 commit 1f0ade2

File tree

4 files changed

+263
-1
lines changed

4 files changed

+263
-1
lines changed

expfmt/encode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import (
1818
"io"
1919
"net/http"
2020

21-
"bitbucket.org/ww/goautoneg"
2221
"github.com/golang/protobuf/proto"
2322
"github.com/matttproud/golang_protobuf_extensions/pbutil"
23+
"github.com/prometheus/common/internal/bitbucket.org/ww/goautoneg"
2424

2525
dto "github.com/prometheus/client_model/go"
2626
)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
PACKAGE
2+
3+
package goautoneg
4+
import "bitbucket.org/ww/goautoneg"
5+
6+
HTTP Content-Type Autonegotiation.
7+
8+
The functions in this package implement the behaviour specified in
9+
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
10+
11+
Copyright (c) 2011, Open Knowledge Foundation Ltd.
12+
All rights reserved.
13+
14+
Redistribution and use in source and binary forms, with or without
15+
modification, are permitted provided that the following conditions are
16+
met:
17+
18+
Redistributions of source code must retain the above copyright
19+
notice, this list of conditions and the following disclaimer.
20+
21+
Redistributions in binary form must reproduce the above copyright
22+
notice, this list of conditions and the following disclaimer in
23+
the documentation and/or other materials provided with the
24+
distribution.
25+
26+
Neither the name of the Open Knowledge Foundation Ltd. nor the
27+
names of its contributors may be used to endorse or promote
28+
products derived from this software without specific prior written
29+
permission.
30+
31+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35+
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42+
43+
44+
FUNCTIONS
45+
46+
func Negotiate(header string, alternatives []string) (content_type string)
47+
Negotiate the most appropriate content_type given the accept header
48+
and a list of alternatives.
49+
50+
func ParseAccept(header string) (accept []Accept)
51+
Parse an Accept Header string returning a sorted list
52+
of clauses
53+
54+
55+
TYPES
56+
57+
type Accept struct {
58+
Type, SubType string
59+
Q float32
60+
Params map[string]string
61+
}
62+
Structure to represent a clause in an HTTP Accept Header
63+
64+
65+
SUBDIRECTORIES
66+
67+
.hg
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
HTTP Content-Type Autonegotiation.
3+
4+
The functions in this package implement the behaviour specified in
5+
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
6+
7+
Copyright (c) 2011, Open Knowledge Foundation Ltd.
8+
All rights reserved.
9+
10+
Redistribution and use in source and binary forms, with or without
11+
modification, are permitted provided that the following conditions are
12+
met:
13+
14+
Redistributions of source code must retain the above copyright
15+
notice, this list of conditions and the following disclaimer.
16+
17+
Redistributions in binary form must reproduce the above copyright
18+
notice, this list of conditions and the following disclaimer in
19+
the documentation and/or other materials provided with the
20+
distribution.
21+
22+
Neither the name of the Open Knowledge Foundation Ltd. nor the
23+
names of its contributors may be used to endorse or promote
24+
products derived from this software without specific prior written
25+
permission.
26+
27+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31+
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38+
39+
40+
*/
41+
package goautoneg
42+
43+
import (
44+
"sort"
45+
"strconv"
46+
"strings"
47+
)
48+
49+
// Structure to represent a clause in an HTTP Accept Header
50+
type Accept struct {
51+
Type, SubType string
52+
Q float64
53+
Params map[string]string
54+
}
55+
56+
// For internal use, so that we can use the sort interface
57+
type accept_slice []Accept
58+
59+
func (accept accept_slice) Len() int {
60+
slice := []Accept(accept)
61+
return len(slice)
62+
}
63+
64+
func (accept accept_slice) Less(i, j int) bool {
65+
slice := []Accept(accept)
66+
ai, aj := slice[i], slice[j]
67+
if ai.Q > aj.Q {
68+
return true
69+
}
70+
if ai.Type != "*" && aj.Type == "*" {
71+
return true
72+
}
73+
if ai.SubType != "*" && aj.SubType == "*" {
74+
return true
75+
}
76+
return false
77+
}
78+
79+
func (accept accept_slice) Swap(i, j int) {
80+
slice := []Accept(accept)
81+
slice[i], slice[j] = slice[j], slice[i]
82+
}
83+
84+
// Parse an Accept Header string returning a sorted list
85+
// of clauses
86+
func ParseAccept(header string) (accept []Accept) {
87+
parts := strings.Split(header, ",")
88+
accept = make([]Accept, 0, len(parts))
89+
for _, part := range parts {
90+
part := strings.Trim(part, " ")
91+
92+
a := Accept{}
93+
a.Params = make(map[string]string)
94+
a.Q = 1.0
95+
96+
mrp := strings.Split(part, ";")
97+
98+
media_range := mrp[0]
99+
sp := strings.Split(media_range, "/")
100+
a.Type = strings.Trim(sp[0], " ")
101+
102+
switch {
103+
case len(sp) == 1 && a.Type == "*":
104+
a.SubType = "*"
105+
case len(sp) == 2:
106+
a.SubType = strings.Trim(sp[1], " ")
107+
default:
108+
continue
109+
}
110+
111+
if len(mrp) == 1 {
112+
accept = append(accept, a)
113+
continue
114+
}
115+
116+
for _, param := range mrp[1:] {
117+
sp := strings.SplitN(param, "=", 2)
118+
if len(sp) != 2 {
119+
continue
120+
}
121+
token := strings.Trim(sp[0], " ")
122+
if token == "q" {
123+
a.Q, _ = strconv.ParseFloat(sp[1], 32)
124+
} else {
125+
a.Params[token] = strings.Trim(sp[1], " ")
126+
}
127+
}
128+
129+
accept = append(accept, a)
130+
}
131+
132+
slice := accept_slice(accept)
133+
sort.Sort(slice)
134+
135+
return
136+
}
137+
138+
// Negotiate the most appropriate content_type given the accept header
139+
// and a list of alternatives.
140+
func Negotiate(header string, alternatives []string) (content_type string) {
141+
asp := make([][]string, 0, len(alternatives))
142+
for _, ctype := range alternatives {
143+
asp = append(asp, strings.SplitN(ctype, "/", 2))
144+
}
145+
for _, clause := range ParseAccept(header) {
146+
for i, ctsp := range asp {
147+
if clause.Type == ctsp[0] && clause.SubType == ctsp[1] {
148+
content_type = alternatives[i]
149+
return
150+
}
151+
if clause.Type == ctsp[0] && clause.SubType == "*" {
152+
content_type = alternatives[i]
153+
return
154+
}
155+
if clause.Type == "*" && clause.SubType == "*" {
156+
content_type = alternatives[i]
157+
return
158+
}
159+
}
160+
}
161+
return
162+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package goautoneg
2+
3+
import (
4+
"testing"
5+
)
6+
7+
var chrome = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
8+
9+
func TestParseAccept(t *testing.T) {
10+
alternatives := []string{"text/html", "image/png"}
11+
content_type := Negotiate(chrome, alternatives)
12+
if content_type != "image/png" {
13+
t.Errorf("got %s expected image/png", content_type)
14+
}
15+
16+
alternatives = []string{"text/html", "text/plain", "text/n3"}
17+
content_type = Negotiate(chrome, alternatives)
18+
if content_type != "text/html" {
19+
t.Errorf("got %s expected text/html", content_type)
20+
}
21+
22+
alternatives = []string{"text/n3", "text/plain"}
23+
content_type = Negotiate(chrome, alternatives)
24+
if content_type != "text/plain" {
25+
t.Errorf("got %s expected text/plain", content_type)
26+
}
27+
28+
alternatives = []string{"text/n3", "application/rdf+xml"}
29+
content_type = Negotiate(chrome, alternatives)
30+
if content_type != "text/n3" {
31+
t.Errorf("got %s expected text/n3", content_type)
32+
}
33+
}

0 commit comments

Comments
 (0)