github.com/corona10/go@v0.0.0-20180224231303-7a218942be57/src/cmd/go/internal/get/discovery.go (about) 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. 4 5 package get 6 7 import ( 8 "encoding/xml" 9 "fmt" 10 "io" 11 "strings" 12 ) 13 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) { 22 case "ascii": 23 return input, nil 24 default: 25 return nil, fmt.Errorf("can't decode XML document using charset %q", charset) 26 } 27 } 28 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>. 31 func parseMetaGoImports(r io.Reader) (imports []metaImport, err error) { 32 d := xml.NewDecoder(r) 33 d.CharsetReader = charsetReader 34 d.Strict = false 35 var t xml.Token 36 for { 37 t, err = d.RawToken() 38 if err != nil { 39 if err == io.EOF || len(imports) > 0 { 40 err = nil 41 } 42 return 43 } 44 if e, ok := t.(xml.StartElement); ok && strings.EqualFold(e.Name.Local, "body") { 45 return 46 } 47 if e, ok := t.(xml.EndElement); ok && strings.EqualFold(e.Name.Local, "head") { 48 return 49 } 50 e, ok := t.(xml.StartElement) 51 if !ok || !strings.EqualFold(e.Name.Local, "meta") { 52 continue 53 } 54 if attrValue(e.Attr, "name") != "go-import" { 55 continue 56 } 57 if f := strings.Fields(attrValue(e.Attr, "content")); len(f) == 3 { 58 imports = append(imports, metaImport{ 59 Prefix: f[0], 60 VCS: f[1], 61 RepoRoot: f[2], 62 }) 63 } 64 } 65 } 66 67 // attrValue returns the attribute value for the case-insensitive key 68 // `name', or the empty string if nothing is found. 69 func attrValue(attrs []xml.Attr, name string) string { 70 for _, a := range attrs { 71 if strings.EqualFold(a.Name.Local, name) { 72 return a.Value 73 } 74 } 75 return "" 76 }