]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: Fix go get when the fetched XML uses ASCII encoding
authorAlberto GarcĂ­a Hierro <alberto@garciahierro.com>
Fri, 2 Aug 2013 21:15:33 +0000 (14:15 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Fri, 2 Aug 2013 21:15:33 +0000 (14:15 -0700)
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

src/cmd/go/discovery.go
src/cmd/go/vcs.go

index 0478340505bc3973ea85f9ce2ed833d5f8470efe..75228b52a5f3f154a6ba2cc40faa8b4fd3c20ea6 100644 (file)
@@ -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 <head> section or the beginning of the <body>.
-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
                }
index 59bc2adbe7ecea67232f0467905225747fa53ada..d857c1446277ecebc687060812ec932fef69be4d 100644 (file)
@@ -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)
                }