1// Copyright 2009 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// godoc: Go Documentation Server
6
7// Web server tree:
8//
9//	http://godoc/		redirect to /pkg/
10//	http://godoc/src/	serve files from $GOROOT/src; .go gets pretty-printed
11//	http://godoc/cmd/	serve documentation about commands
12//	http://godoc/pkg/	serve documentation about packages
13//				(idea is if you say import "compress/zlib", you go to
14//				http://godoc/pkg/compress/zlib)
15//
16
17package main
18
19import (
20	"archive/zip"
21	"bytes"
22	"context"
23	"encoding/json"
24	_ "expvar" // to serve /debug/vars
25	"flag"
26	"fmt"
27	"go/build"
28	exec "golang.org/x/sys/execabs"
29	"io"
30	"log"
31	"net/http"
32	_ "net/http/pprof" // to serve /debug/pprof/*
33	"net/url"
34	"os"
35	"path"
36	"path/filepath"
37	"regexp"
38	"runtime"
39	"strings"
40
41	"golang.org/x/tools/godoc"
42	"golang.org/x/tools/godoc/analysis"
43	"golang.org/x/tools/godoc/static"
44	"golang.org/x/tools/godoc/vfs"
45	"golang.org/x/tools/godoc/vfs/gatefs"
46	"golang.org/x/tools/godoc/vfs/mapfs"
47	"golang.org/x/tools/godoc/vfs/zipfs"
48	"golang.org/x/tools/internal/gocommand"
49	"golang.org/x/xerrors"
50)
51
52const defaultAddr = "localhost:6060" // default webserver address
53
54var (
55	// file system to serve
56	// (with e.g.: zip -r go.zip $GOROOT -i \*.go -i \*.html -i \*.css -i \*.js -i \*.txt -i \*.c -i \*.h -i \*.s -i \*.png -i \*.jpg -i \*.sh -i favicon.ico)
57	zipfile = flag.String("zip", "", "zip file providing the file system to serve; disabled if empty")
58
59	// file-based index
60	writeIndex = flag.Bool("write_index", false, "write index to a file; the file name must be specified with -index_files")
61
62	analysisFlag = flag.String("analysis", "", `comma-separated list of analyses to perform when in GOPATH mode (supported: type, pointer). See https://golang.org/lib/godoc/analysis/help.html`)
63
64	// network
65	httpAddr = flag.String("http", defaultAddr, "HTTP service address")
66
67	// layout control
68	urlFlag = flag.String("url", "", "print HTML for named URL")
69
70	verbose = flag.Bool("v", false, "verbose mode")
71
72	// file system roots
73	// TODO(gri) consider the invariant that goroot always end in '/'
74	goroot = flag.String("goroot", findGOROOT(), "Go root directory")
75
76	// layout control
77	showTimestamps = flag.Bool("timestamps", false, "show timestamps with directory listings")
78	templateDir    = flag.String("templates", "", "load templates/JS/CSS from disk in this directory")
79	showPlayground = flag.Bool("play", false, "enable playground")
80	declLinks      = flag.Bool("links", true, "link identifiers to their declarations")
81
82	// search index
83	indexEnabled  = flag.Bool("index", false, "enable search index")
84	indexFiles    = flag.String("index_files", "", "glob pattern specifying index files; if not empty, the index is read from these files in sorted order")
85	indexInterval = flag.Duration("index_interval", 0, "interval of indexing; 0 for default (5m), negative to only index once at startup")
86	maxResults    = flag.Int("maxresults", 10000, "maximum number of full text search results shown")
87	indexThrottle = flag.Float64("index_throttle", 0.75, "index throttle value; 0.0 = no time allocated, 1.0 = full throttle")
88
89	// source code notes
90	notesRx = flag.String("notes", "BUG", "regular expression matching note markers to show")
91)
92
93// An httpResponseRecorder is an http.ResponseWriter
94type httpResponseRecorder struct {
95	body   *bytes.Buffer
96	header http.Header
97	code   int
98}
99
100func (w *httpResponseRecorder) Header() http.Header         { return w.header }
101func (w *httpResponseRecorder) Write(b []byte) (int, error) { return w.body.Write(b) }
102func (w *httpResponseRecorder) WriteHeader(code int)        { w.code = code }
103
104func usage() {
105	fmt.Fprintf(os.Stderr, "usage: godoc -http="+defaultAddr+"\n")
106	flag.PrintDefaults()
107	os.Exit(2)
108}
109
110func loggingHandler(h http.Handler) http.Handler {
111	return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
112		log.Printf("%s\t%s", req.RemoteAddr, req.URL)
113		h.ServeHTTP(w, req)
114	})
115}
116
117func handleURLFlag() {
118	// Try up to 10 fetches, following redirects.
119	urlstr := *urlFlag
120	for i := 0; i < 10; i++ {
121		// Prepare request.
122		u, err := url.Parse(urlstr)
123		if err != nil {
124			log.Fatal(err)
125		}
126		req := &http.Request{
127			URL: u,
128		}
129
130		// Invoke default HTTP handler to serve request
131		// to our buffering httpWriter.
132		w := &httpResponseRecorder{code: 200, header: make(http.Header), body: new(bytes.Buffer)}
133		http.DefaultServeMux.ServeHTTP(w, req)
134
135		// Return data, error, or follow redirect.
136		switch w.code {
137		case 200: // ok
138			os.Stdout.Write(w.body.Bytes())
139			return
140		case 301, 302, 303, 307: // redirect
141			redirect := w.header.Get("Location")
142			if redirect == "" {
143				log.Fatalf("HTTP %d without Location header", w.code)
144			}
145			urlstr = redirect
146		default:
147			log.Fatalf("HTTP error %d", w.code)
148		}
149	}
150	log.Fatalf("too many redirects")
151}
152
153func initCorpus(corpus *godoc.Corpus) {
154	err := corpus.Init()
155	if err != nil {
156		log.Fatal(err)
157	}
158}
159
160func main() {
161	flag.Usage = usage
162	flag.Parse()
163
164	// Check usage.
165	if flag.NArg() > 0 {
166		fmt.Fprintln(os.Stderr, `Unexpected arguments. Use "go doc" for command-line help output instead. For example, "go doc fmt.Printf".`)
167		usage()
168	}
169	if *httpAddr == "" && *urlFlag == "" && !*writeIndex {
170		fmt.Fprintln(os.Stderr, "At least one of -http, -url, or -write_index must be set to a non-zero value.")
171		usage()
172	}
173
174	// Set the resolved goroot.
175	vfs.GOROOT = *goroot
176
177	fsGate := make(chan bool, 20)
178
179	// Determine file system to use.
180	if *zipfile == "" {
181		// use file system of underlying OS
182		rootfs := gatefs.New(vfs.OS(*goroot), fsGate)
183		fs.Bind("/", rootfs, "/", vfs.BindReplace)
184	} else {
185		// use file system specified via .zip file (path separator must be '/')
186		rc, err := zip.OpenReader(*zipfile)
187		if err != nil {
188			log.Fatalf("%s: %s\n", *zipfile, err)
189		}
190		defer rc.Close() // be nice (e.g., -writeIndex mode)
191		fs.Bind("/", zipfs.New(rc, *zipfile), *goroot, vfs.BindReplace)
192	}
193	if *templateDir != "" {
194		fs.Bind("/lib/godoc", vfs.OS(*templateDir), "/", vfs.BindBefore)
195		fs.Bind("/favicon.ico", vfs.OS(*templateDir), "/favicon.ico", vfs.BindReplace)
196	} else {
197		fs.Bind("/lib/godoc", mapfs.New(static.Files), "/", vfs.BindReplace)
198		fs.Bind("/favicon.ico", mapfs.New(static.Files), "/favicon.ico", vfs.BindReplace)
199	}
200
201	// Get the GOMOD value, use it to determine if godoc is being invoked in module mode.
202	goModFile, err := goMod()
203	if err != nil {
204		fmt.Fprintf(os.Stderr, "failed to determine go env GOMOD value: %v", err)
205		goModFile = "" // Fall back to GOPATH mode.
206	}
207
208	if goModFile != "" {
209		fmt.Printf("using module mode; GOMOD=%s\n", goModFile)
210
211		if *analysisFlag != "" {
212			fmt.Fprintln(os.Stderr, "The -analysis flag is supported only in GOPATH mode at this time.")
213			fmt.Fprintln(os.Stderr, "See https://golang.org/issue/34473.")
214			usage()
215		}
216
217		// Detect whether to use vendor mode or not.
218		mainMod, vendorEnabled, err := gocommand.VendorEnabled(context.Background(), gocommand.Invocation{}, &gocommand.Runner{})
219		if err != nil {
220			fmt.Fprintf(os.Stderr, "failed to determine if vendoring is enabled: %v", err)
221			os.Exit(1)
222		}
223		if vendorEnabled {
224			// Bind the root directory of the main module.
225			fs.Bind(path.Join("/src", mainMod.Path), gatefs.New(vfs.OS(mainMod.Dir), fsGate), "/", vfs.BindAfter)
226
227			// Bind the vendor directory.
228			//
229			// Note that in module mode, vendor directories in locations
230			// other than the main module's root directory are ignored.
231			// See https://golang.org/ref/mod#vendoring.
232			vendorDir := filepath.Join(mainMod.Dir, "vendor")
233			fs.Bind("/src", gatefs.New(vfs.OS(vendorDir), fsGate), "/", vfs.BindAfter)
234
235		} else {
236			// Try to download dependencies that are not in the module cache in order to
237			// to show their documentation.
238			// This may fail if module downloading is disallowed (GOPROXY=off) or due to
239			// limited connectivity, in which case we print errors to stderr and show
240			// documentation only for packages that are available.
241			fillModuleCache(os.Stderr, goModFile)
242
243			// Determine modules in the build list.
244			mods, err := buildList(goModFile)
245			if err != nil {
246				fmt.Fprintf(os.Stderr, "failed to determine the build list of the main module: %v", err)
247				os.Exit(1)
248			}
249
250			// Bind module trees into Go root.
251			for _, m := range mods {
252				if m.Dir == "" {
253					// Module is not available in the module cache, skip it.
254					continue
255				}
256				dst := path.Join("/src", m.Path)
257				fs.Bind(dst, gatefs.New(vfs.OS(m.Dir), fsGate), "/", vfs.BindAfter)
258			}
259		}
260	} else {
261		fmt.Println("using GOPATH mode")
262
263		// Bind $GOPATH trees into Go root.
264		for _, p := range filepath.SplitList(build.Default.GOPATH) {
265			fs.Bind("/src", gatefs.New(vfs.OS(p), fsGate), "/src", vfs.BindAfter)
266		}
267	}
268
269	var typeAnalysis, pointerAnalysis bool
270	if *analysisFlag != "" {
271		for _, a := range strings.Split(*analysisFlag, ",") {
272			switch a {
273			case "type":
274				typeAnalysis = true
275			case "pointer":
276				pointerAnalysis = true
277			default:
278				log.Fatalf("unknown analysis: %s", a)
279			}
280		}
281	}
282
283	var corpus *godoc.Corpus
284	if goModFile != "" {
285		corpus = godoc.NewCorpus(moduleFS{fs})
286	} else {
287		corpus = godoc.NewCorpus(fs)
288	}
289	corpus.Verbose = *verbose
290	corpus.MaxResults = *maxResults
291	corpus.IndexEnabled = *indexEnabled
292	if *maxResults == 0 {
293		corpus.IndexFullText = false
294	}
295	corpus.IndexFiles = *indexFiles
296	corpus.IndexDirectory = func(dir string) bool {
297		return dir != "/pkg" && !strings.HasPrefix(dir, "/pkg/")
298	}
299	corpus.IndexThrottle = *indexThrottle
300	corpus.IndexInterval = *indexInterval
301	if *writeIndex || *urlFlag != "" {
302		corpus.IndexThrottle = 1.0
303		corpus.IndexEnabled = true
304		initCorpus(corpus)
305	} else {
306		go initCorpus(corpus)
307	}
308
309	// Initialize the version info before readTemplates, which saves
310	// the map value in a method value.
311	corpus.InitVersionInfo()
312
313	pres = godoc.NewPresentation(corpus)
314	pres.ShowTimestamps = *showTimestamps
315	pres.ShowPlayground = *showPlayground
316	pres.DeclLinks = *declLinks
317	if *notesRx != "" {
318		pres.NotesRx = regexp.MustCompile(*notesRx)
319	}
320
321	readTemplates(pres)
322	registerHandlers(pres)
323
324	if *writeIndex {
325		// Write search index and exit.
326		if *indexFiles == "" {
327			log.Fatal("no index file specified")
328		}
329
330		log.Println("initialize file systems")
331		*verbose = true // want to see what happens
332
333		corpus.UpdateIndex()
334
335		log.Println("writing index file", *indexFiles)
336		f, err := os.Create(*indexFiles)
337		if err != nil {
338			log.Fatal(err)
339		}
340		index, _ := corpus.CurrentIndex()
341		_, err = index.WriteTo(f)
342		if err != nil {
343			log.Fatal(err)
344		}
345
346		log.Println("done")
347		return
348	}
349
350	// Print content that would be served at the URL *urlFlag.
351	if *urlFlag != "" {
352		handleURLFlag()
353		return
354	}
355
356	var handler http.Handler = http.DefaultServeMux
357	if *verbose {
358		log.Printf("Go Documentation Server")
359		log.Printf("version = %s", runtime.Version())
360		log.Printf("address = %s", *httpAddr)
361		log.Printf("goroot = %s", *goroot)
362		switch {
363		case !*indexEnabled:
364			log.Print("search index disabled")
365		case *maxResults > 0:
366			log.Printf("full text index enabled (maxresults = %d)", *maxResults)
367		default:
368			log.Print("identifier search index enabled")
369		}
370		fs.Fprint(os.Stderr)
371		handler = loggingHandler(handler)
372	}
373
374	// Initialize search index.
375	if *indexEnabled {
376		go corpus.RunIndexer()
377	}
378
379	// Start type/pointer analysis.
380	if typeAnalysis || pointerAnalysis {
381		go analysis.Run(pointerAnalysis, &corpus.Analysis)
382	}
383
384	// Start http server.
385	if *verbose {
386		log.Println("starting HTTP server")
387	}
388	if err := http.ListenAndServe(*httpAddr, handler); err != nil {
389		log.Fatalf("ListenAndServe %s: %v", *httpAddr, err)
390	}
391}
392
393// goMod returns the go env GOMOD value in the current directory
394// by invoking the go command.
395//
396// GOMOD is documented at https://golang.org/cmd/go/#hdr-Environment_variables:
397//
398// 	The absolute path to the go.mod of the main module,
399// 	or the empty string if not using modules.
400//
401func goMod() (string, error) {
402	out, err := exec.Command("go", "env", "-json", "GOMOD").Output()
403	if ee := (*exec.ExitError)(nil); xerrors.As(err, &ee) {
404		return "", fmt.Errorf("go command exited unsuccessfully: %v\n%s", ee.ProcessState.String(), ee.Stderr)
405	} else if err != nil {
406		return "", err
407	}
408	var env struct {
409		GoMod string
410	}
411	err = json.Unmarshal(out, &env)
412	if err != nil {
413		return "", err
414	}
415	return env.GoMod, nil
416}
417
418// fillModuleCache does a best-effort attempt to fill the module cache
419// with all dependencies of the main module in the current directory
420// by invoking the go command. Module download logs are streamed to w.
421// If there are any problems encountered, they are also written to w.
422// It should only be used in module mode, when vendor mode isn't on.
423//
424// See https://golang.org/cmd/go/#hdr-Download_modules_to_local_cache.
425func fillModuleCache(w io.Writer, goMod string) {
426	if goMod == os.DevNull {
427		// No module requirements, nothing to do.
428		return
429	}
430
431	cmd := exec.Command("go", "mod", "download", "-json")
432	var out bytes.Buffer
433	cmd.Stdout = &out
434	cmd.Stderr = w
435	err := cmd.Run()
436	if ee := (*exec.ExitError)(nil); xerrors.As(err, &ee) && ee.ExitCode() == 1 {
437		// Exit code 1 from this command means there were some
438		// non-empty Error values in the output. Print them to w.
439		fmt.Fprintf(w, "documentation for some packages is not shown:\n")
440		for dec := json.NewDecoder(&out); ; {
441			var m struct {
442				Path    string // Module path.
443				Version string // Module version.
444				Error   string // Error loading module.
445			}
446			err := dec.Decode(&m)
447			if err == io.EOF {
448				break
449			} else if err != nil {
450				fmt.Fprintf(w, "error decoding JSON object from go mod download -json: %v\n", err)
451				continue
452			}
453			if m.Error == "" {
454				continue
455			}
456			fmt.Fprintf(w, "\tmodule %s@%s is not in the module cache and there was a problem downloading it: %s\n", m.Path, m.Version, m.Error)
457		}
458	} else if err != nil {
459		fmt.Fprintf(w, "there was a problem filling module cache: %v\n", err)
460	}
461}
462
463type mod struct {
464	Path string // Module path.
465	Dir  string // Directory holding files for this module, if any.
466}
467
468// buildList determines the build list in the current directory
469// by invoking the go command. It should only be used in module mode,
470// when vendor mode isn't on.
471//
472// See https://golang.org/cmd/go/#hdr-The_main_module_and_the_build_list.
473func buildList(goMod string) ([]mod, error) {
474	if goMod == os.DevNull {
475		// Empty build list.
476		return nil, nil
477	}
478
479	out, err := exec.Command("go", "list", "-m", "-json", "all").Output()
480	if ee := (*exec.ExitError)(nil); xerrors.As(err, &ee) {
481		return nil, fmt.Errorf("go command exited unsuccessfully: %v\n%s", ee.ProcessState.String(), ee.Stderr)
482	} else if err != nil {
483		return nil, err
484	}
485	var mods []mod
486	for dec := json.NewDecoder(bytes.NewReader(out)); ; {
487		var m mod
488		err := dec.Decode(&m)
489		if err == io.EOF {
490			break
491		} else if err != nil {
492			return nil, err
493		}
494		mods = append(mods, m)
495	}
496	return mods, nil
497}
498
499// moduleFS is a vfs.FileSystem wrapper used when godoc is running
500// in module mode. It's needed so that packages inside modules are
501// considered to be third party.
502//
503// It overrides the RootType method of the underlying filesystem
504// and implements it using a heuristic based on the import path.
505// If the first element of the import path does not contain a dot,
506// that package is considered to be inside GOROOT. If it contains
507// a dot, then that package is considered to be third party.
508//
509// TODO(dmitshur): The RootType abstraction works well when GOPATH
510// workspaces are bound at their roots, but scales poorly in the
511// general case. It should be replaced by a more direct solution
512// for determining whether a package is third party or not.
513//
514type moduleFS struct{ vfs.FileSystem }
515
516func (moduleFS) RootType(path string) vfs.RootType {
517	if !strings.HasPrefix(path, "/src/") {
518		return ""
519	}
520	domain := path[len("/src/"):]
521	if i := strings.Index(domain, "/"); i >= 0 {
522		domain = domain[:i]
523	}
524	if !strings.Contains(domain, ".") {
525		// No dot in the first element of import path
526		// suggests this is a package in GOROOT.
527		return vfs.RootTypeGoRoot
528	} else {
529		// A dot in the first element of import path
530		// suggests this is a third party package.
531		return vfs.RootTypeGoPath
532	}
533}
534func (fs moduleFS) String() string { return "module(" + fs.FileSystem.String() + ")" }
535