mirror of https://github.com/golang/go.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
This commit is contained in:
parent
869774cbf9
commit
bbf1430021
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue