1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 // charsetReader returns a reader for the given charset. Currently
15 // it only supports UTF-8 and ASCII. Otherwise, it returns a meaningful
16 // error which is printed by go get, so the user can find why the package
17 // wasn't downloaded if the encoding is not supported. Note that, in
18 // order to reduce potential errors, ASCII is treated as UTF-8 (i.e. characters
19 // greater than 0x7f are not rejected).
20 func charsetReader(charset string, input io.Reader) (io.Reader, error) {
21 switch strings.ToLower(charset) {
25 return nil, fmt.Errorf("can't decode XML document using charset %q", charset)
29 // parseMetaGoImports returns meta imports from the HTML in r.
30 // Parsing ends at the end of the <head> section or the beginning of the <body>.
32 // This copy of cmd/go/internal/vcs.parseMetaGoImports always operates
33 // in IgnoreMod ModuleMode.
34 func parseMetaGoImports(r io.Reader) (imports []metaImport, err error) {
35 d := xml.NewDecoder(r)
36 d.CharsetReader = charsetReader
42 if err == io.EOF || len(imports) > 0 {
47 if e, ok := t.(xml.StartElement); ok && strings.EqualFold(e.Name.Local, "body") {
50 if e, ok := t.(xml.EndElement); ok && strings.EqualFold(e.Name.Local, "head") {
53 e, ok := t.(xml.StartElement)
54 if !ok || !strings.EqualFold(e.Name.Local, "meta") {
57 if attrValue(e.Attr, "name") != "go-import" {
60 if f := strings.Fields(attrValue(e.Attr, "content")); len(f) == 3 {
61 // Ignore VCS type "mod", which is applicable only in module mode.
65 imports = append(imports, metaImport{
74 // attrValue returns the attribute value for the case-insensitive key
75 // `name', or the empty string if nothing is found.
76 func attrValue(attrs []xml.Attr, name string) string {
77 for _, a := range attrs {
78 if strings.EqualFold(a.Name.Local, name) {