Skip to content
Browse files

add redir following to verify (but maybe we shouldn't) note fetching …

…static test data for yadis. tweak yadis tests. add YadisVerbose mode with logging
  • Loading branch information...
1 parent 9262c4e commit 824fab5f731ac83876944b0171cc48d4a76cf7b0 @brianolson brianolson committed Sep 11, 2011
Showing with 44 additions and 9 deletions.
  1. +13 −0 verify.go
  2. +21 −2 yadis.go
  3. +10 −7 yadis_test.go
View
13 verify.go
@@ -85,6 +85,17 @@ func VerifyValues(values url.Values) (grant bool, identifier string, err os.Erro
return false, "", err
}
+ redirLimit := 3
+ for (redirLimit > 0) && (response.StatusCode == 301 || response.StatusCode == 302 || response.StatusCode == 303 || response.StatusCode == 307) {
+ location := response.Header.Get("Location")
+ response, err = client.Get(location)
+ redirLimit -= 1
+ if err != nil {
+ log.Printf("Failed following redir \"%s\"\n", location)
+ return false, "", err
+ }
+ }
+
// Parse the response
// Convert the reader
// We limit the size of the response to 1024 bytes but it should be large enough for most cases
@@ -117,6 +128,7 @@ func VerifyValues(values url.Values) (grant bool, identifier string, err os.Erro
}
// Transform an url string into a map of parameters/value
+/*
func url2map(url_ string) (map[string]string, os.Error) {
pmap := make(map[string]string)
var start, end, eq, length int
@@ -156,3 +168,4 @@ func url2map(url_ string) (map[string]string, os.Error) {
}
return pmap, nil
}
+*/
View
23 yadis.go
@@ -8,16 +8,21 @@ import (
"os"
"http"
"url"
- "xml"
- "fmt"
+ //"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
@@ -27,6 +32,9 @@ func Yadis(ID string) (io.Reader, os.Error) {
// 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
}
@@ -36,15 +44,24 @@ func Yadis(ID string) (io.Reader, os.Error) {
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
}
@@ -121,6 +138,7 @@ func searchHTMLMetaXRDS(r io.Reader) (string, os.Error) {
return string(content[1]), nil
}
+/*
func searchHTMLMetaXRDS_OLD(r io.Reader) (string, os.Error) {
parser := xml.NewParser(r)
var token xml.Token
@@ -160,3 +178,4 @@ func searchHTMLMetaXRDS_OLD(r io.Reader) (string, os.Error) {
}
return "", os.NewError("Value not found")
}
+*/
View
17 yadis_test.go
@@ -3,18 +3,20 @@
// license that can be found in the LICENSE file.
// Getting test data:
-// curl -o test_data/py_id.html --dump-header test_data/py_id.http 'http://localhost:8000/id/bob'
-// curl -o test_data/google_yadis.html --dump-header test_data/google_yadis.http 'https://www.google.com/accounts/o8/id'
-// curl -o test_data/orange_yadis.html --dump-header test_data/orange_yadis.http "http://orange.fr/"
-// curl -o test_data/yahoo_yadis.html --dump-header test_data/yahoo_yadis.http "http://yahoo.com/"
+// curl -o test_data/py_id.html --dump-header test_data/py_id.http --header "Accept: application/xrds+xml" 'http://localhost:8000/id/bob'
+// curl -o test_data/google_yadis.html --dump-header test_data/google_yadis.http --header "Accept: application/xrds+xml" 'https://www.google.com/accounts/o8/id'
+// curl -o test_data/orange_yadis.html --dump-header test_data/orange_yadis.http --header "Accept: application/xrds+xml" "http://www.orange.fr/"
+// curl -o test_data/yahoo_yadis.html --dump-header test_data/yahoo_yadis.http --header "Accept: application/xrds+xml" "http://www.yahoo.com/"
// TODO: facebook? livejournal?
package openid
import (
- "testing"
"bytes"
+ "log"
+ "os"
+ "testing"
)
// searchHTMLMetaXRDS Test
@@ -52,14 +54,15 @@ type YadisTest struct {
var YadisTests = []YadisTest{
YadisTest{"https://www.google.com/accounts/o8/id"},
YadisTest{"http://orange.fr/"},
- YadisTest{"http://yahoo.com/"},
+ YadisTest{"http://www.yahoo.com/"},
}
// Test whether the Yadis function returns no errors and a non nil reader
// Doesn't test the content received
func TestYadis(t *testing.T) {
+ logger := log.New(os.Stdout, "TestYadis ", log.Ltime|log.Lmicroseconds|log.Lshortfile)
for _, yt := range YadisTests {
- var reader, err = Yadis(yt.url)
+ var reader, err = YadisVerbose(yt.url, logger)
if err != nil {
t.Errorf("Yadis(%s) returned a error: %s", yt.url, err.String())
continue

0 comments on commit 824fab5

Please sign in to comment.
Something went wrong with that request. Please try again.