summaryrefslogtreecommitdiff
path: root/src/cmd/godoc/godoc.go
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2009-11-07 21:12:46 -0800
committerRobert Griesemer <gri@golang.org>2009-11-07 21:12:46 -0800
commitcf9b832fd1fb8b7a16a3a93c30ec075448959845 (patch)
treeb04841924cf8847a3cbe1e5eeb3cd4d0b9b930a9 /src/cmd/godoc/godoc.go
parent5f7e4a0eb973f35c08e363768fbce93aebcbb174 (diff)
downloadgolang-cf9b832fd1fb8b7a16a3a93c30ec075448959845.tar.gz
nice directory listings
R=rsc http://go/go-review/1026020
Diffstat (limited to 'src/cmd/godoc/godoc.go')
-rw-r--r--src/cmd/godoc/godoc.go55
1 files changed, 46 insertions, 9 deletions
diff --git a/src/cmd/godoc/godoc.go b/src/cmd/godoc/godoc.go
index c1dd4ead8..56fc5d5a1 100644
--- a/src/cmd/godoc/godoc.go
+++ b/src/cmd/godoc/godoc.go
@@ -554,6 +554,7 @@ func readTemplate(name string) *template.Template {
var (
dirsHtml,
godocHtml,
+ listingHtml,
packageHtml,
packageText,
parseerrorHtml,
@@ -566,6 +567,7 @@ func readTemplates() {
// so that main has chdir'ed to goroot.
dirsHtml = readTemplate("dirs.html");
godocHtml = readTemplate("godoc.html");
+ listingHtml = readTemplate("listing.html");
packageHtml = readTemplate("package.html");
packageText = readTemplate("package.txt");
parseerrorHtml = readTemplate("parseerror.html");
@@ -673,6 +675,36 @@ func serveGoSource(c *http.Conn, filename string, styler printer.Styler) {
}
+func redirect(c *http.Conn, r *http.Request) (redirected bool) {
+ if canonical := pathutil.Clean(r.Url.Path) + "/"; r.Url.Path != canonical {
+ http.Redirect(c, canonical, http.StatusMovedPermanently);
+ redirected = true;
+ }
+ return;
+}
+
+
+func serveDirectory(c *http.Conn, r *http.Request) {
+ if redirect(c, r) {
+ return;
+ }
+
+ path := pathutil.Join(".", r.Url.Path);
+ list, err := io.ReadDir(path);
+ if err != nil {
+ http.NotFound(c, r);
+ return;
+ }
+
+ var buf bytes.Buffer;
+ if err := listingHtml.Execute(list, &buf); err != nil {
+ log.Stderrf("listingHtml.Execute: %s", err);
+ }
+
+ servePage(c, "Directory " + path, "", buf.Bytes());
+}
+
+
var fileServer = http.FileServer(".", "")
func serveFile(c *http.Conn, r *http.Request) {
@@ -694,9 +726,17 @@ func serveFile(c *http.Conn, r *http.Request) {
serveGoSource(c, path, &Styler{highlight: r.FormValue("h")});
default:
- // TODO:
- // - need to decide what to serve and what not to serve
- // - don't want to download files, want to see them
+ dir, err := os.Lstat(pathutil.Join(".", path));
+ if err != nil {
+ http.NotFound(c, r);
+ return;
+ }
+
+ if dir != nil && dir.IsDirectory() {
+ serveDirectory(c, r);
+ return;
+ }
+
fileServer.ServeHTTP(c, r);
}
}
@@ -783,15 +823,12 @@ func (h *httpHandler) getPageInfo(path string) PageInfo {
func (h *httpHandler) ServeHTTP(c *http.Conn, r *http.Request) {
- path := r.Url.Path;
- path = path[len(h.pattern):len(path)];
-
- // canonicalize URL path and redirect if necessary
- if canonical := pathutil.Clean(h.pattern + path) + "/"; r.Url.Path != canonical {
- http.Redirect(c, canonical, http.StatusMovedPermanently);
+ if redirect(c, r) {
return;
}
+ path := r.Url.Path;
+ path = path[len(h.pattern):len(path)];
info := h.getPageInfo(path);
var buf bytes.Buffer;