diff --git a/go/vcs/discovery.go b/go/vcs/discovery.go index 7d614598..d5c3fc6b 100644 --- a/go/vcs/discovery.go +++ b/go/vcs/discovery.go @@ -6,17 +6,35 @@ package vcs import ( "encoding/xml" + "fmt" "io" "strings" ) +// charsetReader returns a reader for the given charset. Currently +// it only supports UTF-8 and ASCII. Otherwise, it returns a meaningful +// error which is printed by go get, so the user can find why the package +// wasn't downloaded if the encoding is not supported. Note that, in +// order to reduce potential errors, ASCII is treated as UTF-8 (i.e. characters +// greater than 0x7f are not rejected). +func charsetReader(charset string, input io.Reader) (io.Reader, error) { + switch strings.ToLower(charset) { + case "ascii": + return input, nil + default: + return nil, fmt.Errorf("can't decode XML document using charset %q", charset) + } +} + // parseMetaGoImports returns meta imports from the HTML in r. // Parsing ends at the end of the section or the beginning of the . -func parseMetaGoImports(r io.Reader) (imports []metaImport) { +func parseMetaGoImports(r io.Reader) (imports []metaImport, err error) { d := xml.NewDecoder(r) + d.CharsetReader = charsetReader d.Strict = false + var t xml.Token for { - t, err := d.Token() + t, err = d.Token() if err != nil { return } diff --git a/go/vcs/vcs.go b/go/vcs/vcs.go index e5aae8f8..586e1b8a 100644 --- a/go/vcs/vcs.go +++ b/go/vcs/vcs.go @@ -492,7 +492,11 @@ func RepoRootForImportDynamic(importPath string, verbose bool) (*RepoRoot, error return nil, fmt.Errorf("http/https fetch: %v", err) } defer body.Close() - metaImport, err := matchGoImport(parseMetaGoImports(body), importPath) + imports, err := parseMetaGoImports(body) + if err != nil { + return nil, fmt.Errorf("parsing %s: %v", importPath, err) + } + metaImport, err := matchGoImport(imports, importPath) if err != nil { if err != errNoMatch { return nil, fmt.Errorf("parse %s: %v", urlStr, err) @@ -517,7 +521,10 @@ func RepoRootForImportDynamic(importPath string, verbose bool) (*RepoRoot, error if err != nil { return nil, fmt.Errorf("fetch %s: %v", urlStr, err) } - imports := parseMetaGoImports(body) + imports, err := parseMetaGoImports(body) + if err != nil { + return nil, fmt.Errorf("parsing %s: %v", importPath, err) + } if len(imports) == 0 { return nil, fmt.Errorf("fetch %s: no go-import meta tag", urlStr) }