--- /dev/null
+<!--
+ Copyright 2009 The Go Authors. All rights reserved.
+ Use of this source code is governed by a BSD-style
+ license that can be found in the LICENSE file.
+-->
+
+<p>
+<table class="layout">
+<tr>
+ <th align="left">File</th>
+ <th width="100" align="right">Size</th>
+</tr>
+<tr>
+ <td><a href=".." class="noline">..</a></td>
+</tr>
+{.repeated section @}
+<tr>
+ <td align="left"><a href="{Name|html}" class="noline">{Name|html}</a></td>
+ <td align="right">{Size|html}</td>
+</tr>
+{.end}
+</table>
+</p>
var (
dirsHtml,
godocHtml,
+ listingHtml,
packageHtml,
packageText,
parseerrorHtml,
// 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");
}
+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) {
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);
}
}
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;