Debian Codegraph @

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

raw
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 vcs
6
7 import (
8 "encoding/xml"
9 "fmt"
10 "io"
11 "strings"
12 )
13
14 // charsetReader returns a reader that converts from the given charset to UTF-8.
15 // Currently 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 "utf-8", "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, mod ModuleMode) ([]metaImport, error) {
32 d := xml.NewDecoder(r)
33 d.CharsetReader = charsetReader
34 d.Strict = false
35 var imports []metaImport
36 for {
37 t, err := d.RawToken()
38 if err != nil {
39 if err != io.EOF && len(imports) == 0 {
40 return nil, err
41 }
42 break
43 }
44 if e, ok := t.(xml.StartElement); ok && strings.EqualFold(e.Name.Local, "body") {
45 break
46 }
47 if e, ok := t.(xml.EndElement); ok && strings.EqualFold(e.Name.Local, "head") {
48 break
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 || len(f) == 4 {
58 mi := metaImport{
59 Prefix: f[0],
60 VCS: f[1],
61 RepoRoot: f[2],
62 }
63 // An optional subdirectory may be provided.
64 if len(f) == 4 {
65 mi.SubDir = f[3]
66 }
67 imports = append(imports, mi)
68 }
69 }
70
71 // Extract mod entries if we are paying attention to them.
72 var list []metaImport
73 var have map[string]bool
74 if mod == PreferMod {
75 have = make(map[string]bool)
76 for _, m := range imports {
77 if m.VCS == "mod" {
78 have[m.Prefix] = true
79 list = append(list, m)
80 }
81 }
82 }
83
84 // Append non-mod entries, ignoring those superseded by a mod entry.
85 for _, m := range imports {
86 if m.VCS != "mod" && !have[m.Prefix] {
87 list = append(list, m)
88 }
89 }
90 return list, nil
91 }
92
93 // attrValue returns the attribute value for the case-insensitive key
94 // `name`, or the empty string if nothing is found.
95 func attrValue(attrs []xml.Attr, name string) string {
96 for _, a := range attrs {
97 if strings.EqualFold(a.Name.Local, name) {
98 return a.Value
99 }
100 }
101 return ""
102 }