forked from brianolson/go-openid
/
yadis.go
181 lines (159 loc) · 4.26 KB
/
yadis.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2010 Florian Duraffourg. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package openid
import (
"os"
"http"
"url"
//"xml"
//"fmt"
"io"
"io/ioutil"
"bytes"
"log"
"regexp"
"strings"
)
func Yadis(ID string) (io.Reader, os.Error) {
return YadisVerbose(ID, nil)
}
func YadisVerbose(ID string, verbose *log.Logger) (io.Reader, os.Error) {
r, err := YadisRequest(ID, "GET")
if err != nil || r == nil {
return nil, err
}
var contentType = r.Header.Get("Content-Type")
// If it is an XRDS document, return the Reader
if strings.HasPrefix(contentType, "application/xrds+xml") {
if verbose != nil {
verbose.Printf("got xrds from \"%s\"", ID)
}
return r.Body, nil
}
// If it is an HTML doc search for meta tags
if bytes.Equal([]byte(contentType), []byte("text/html")) {
url_, err := searchHTMLMetaXRDS(r.Body)
if err != nil {
return nil, err
}
if verbose != nil {
verbose.Printf("fetching xrds found in html \"%s\"", url_)
}
return Yadis(url_)
}
// If the response contain an X-XRDS-Location header
var xrds_location = r.Header.Get("X-Xrds-Location")
if len(xrds_location) > 0 {
if verbose != nil {
verbose.Printf("fetching xrds found in http header \"%s\"", xrds_location)
}
return Yadis(xrds_location)
}
if verbose != nil {
verbose.Printf("Yadis fails out, nothing found. status=%#v", r.StatusCode)
}
// If nothing is found try to parse it as a XRDS doc
return nil, nil
}
func YadisRequest(url_ string, method string) (resp *http.Response, err os.Error) {
resp = nil
var request = new(http.Request)
var client = new(http.Client)
var Header = make(http.Header)
request.Method = method
request.RawURL = url_
request.URL, err = url.Parse(url_)
if err != nil {
return
}
// Common parameters
request.Proto = "HTTP/1.0"
request.ProtoMajor = 1
request.ProtoMinor = 0
request.ContentLength = 0
request.Close = true
Header.Add("Accept", "application/xrds+xml")
request.Header = Header
// Follow a maximum of 5 redirections
for i := 0; i < 5; i++ {
response, err := client.Do(request)
if err != nil {
return nil, err
}
if response.StatusCode == 301 || response.StatusCode == 302 || response.StatusCode == 303 || response.StatusCode == 307 {
location := response.Header.Get("Location")
request.RawURL = location
request.URL, err = url.Parse(location)
if err != nil {
return
}
} else {
return response, nil
}
}
return nil, os.NewError("Too many redirections")
}
// this is a ridiculous way to make a case insensitive pattern.
var metaRE *regexp.Regexp
var xrdsRE *regexp.Regexp
func init() {
metaRE = regexp.MustCompile("<[ \t]*[mM][eE][tT][aA][^>]*[hH][tT][tT][pP]-[eE][qQ][uU][iI][vV]=[\"'][xX]-[xX][rR][dD][sS]-[lL][oO][cC][aA][tT][iI][oO][nN][\"'][^>]*>")
xrdsRE = regexp.MustCompile("[cC][oO][nN][tT][eE][nN][tT]=[\"']([^\"]+)[\"']")
//xrdsRE = regexp.MustCompile("content=[\"']([^\"']+)[\"']")
}
func searchHTMLMetaXRDS(r io.Reader) (string, os.Error) {
data, err := ioutil.ReadAll(r)
if err != nil {
return "", err
}
part := metaRE.Find(data)
if part == nil {
return "", os.NewError("No -meta- match")
}
content := xrdsRE.FindSubmatch(part)
if content == nil {
return "", os.NewError("No content in meta tag: " + string(part))
}
return string(content[1]), nil
}
/*
func searchHTMLMetaXRDS_OLD(r io.Reader) (string, os.Error) {
parser := xml.NewParser(r)
var token xml.Token
var err os.Error
for {
token, err = parser.Token()
if token == nil || err != nil {
if err == os.EOF {
break
}
return "", err
}
switch token.(type) {
case xml.StartElement:
if token.(xml.StartElement).Name.Local == "meta" {
// Found a meta token. Verify that it is a X-XRDS-Location and return the content
var content string
var contentE bool
var httpEquivOK bool
contentE = false
httpEquivOK = false
for _, v := range token.(xml.StartElement).Attr {
if v.Name.Local == "http-equiv" && v.Value == "X-XRDS-Location" {
httpEquivOK = true
}
if v.Name.Local == "content" {
content = v.Value
contentE = true
}
}
if contentE && httpEquivOK {
return fmt.Sprint(content), nil
}
}
}
}
return "", os.NewError("Value not found")
}
*/