From: Alberto GarcĂ­a Hierro Date: Fri, 2 Aug 2013 21:15:33 +0000 (-0700) Subject: cmd/go: Fix go get when the fetched XML uses ASCII encoding X-Git-Tag: go1.2rc2~812 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=bbf143002188a7af7d60e28da472f06c6d99aa03;p=gostls13.git cmd/go: Fix go get when the fetched XML uses ASCII encoding Also, add a meaningful error message when an encoding which can't be parsed is found. Fixes #5801. R=golang-dev, bradfitz, rsc CC=golang-dev https://golang.org/cl/12343043 --- diff --git a/src/cmd/go/discovery.go b/src/cmd/go/discovery.go index 0478340505..75228b52a5 100644 --- a/src/cmd/go/discovery.go +++ b/src/cmd/go/discovery.go @@ -13,17 +13,35 @@ package main 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/src/cmd/go/vcs.go b/src/cmd/go/vcs.go index 59bc2adbe7..d857c14462 100644 --- a/src/cmd/go/vcs.go +++ b/src/cmd/go/vcs.go @@ -442,7 +442,11 @@ func repoRootForImportDynamic(importPath string) (*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) @@ -467,7 +471,10 @@ func repoRootForImportDynamic(importPath string) (*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) }