Commit 80ca2afd authored by Robert Griesemer's avatar Robert Griesemer

issue http redirect when needed

R=rsc
DELTA=31  (6 added, 8 deleted, 17 changed)
OCL=30046
CL=30051
parent 63c2d521
...@@ -437,28 +437,30 @@ func isPackageFile(dirname, filename, pakname string) bool { ...@@ -437,28 +437,30 @@ func isPackageFile(dirname, filename, pakname string) bool {
} }
// Returns the package denoted by path and the list of // Returns the canonical URL path, the package denoted by path, and
// sub-directories in the corresponding package directory. // the list of sub-directories in the corresponding package directory.
// If there is no such package, the first result is nil. If // If there is no such package, the package descriptor pd is nil.
// there are no sub-directories, that list is nil. // If there are no sub-directories, the dirs list is nil.
func findPackage(path string) (*pakDesc, dirList) { func findPackage(path string) (canonical string, pd *pakDesc, dirs dirList) {
canonical = pathutil.Clean(Pkg + path) + "/";
// get directory contents, if possible // get directory contents, if possible
importpath := pathutil.Clean(path); // no trailing '/' importpath := pathutil.Clean(path); // no trailing '/'
dirname := pathutil.Join(*pkgroot, importpath); dirname := pathutil.Join(*pkgroot, importpath);
if !isDir(dirname) { if !isDir(dirname) {
return nil, nil; return;
} }
fd, err1 := os.Open(dirname, os.O_RDONLY, 0); fd, err1 := os.Open(dirname, os.O_RDONLY, 0);
if err1 != nil { if err1 != nil {
log.Stderrf("open %s: %v", dirname, err1); log.Stderrf("open %s: %v", dirname, err1);
return nil, nil; return;
} }
list, err2 := fd.Readdir(-1); list, err2 := fd.Readdir(-1);
if err2 != nil { if err2 != nil {
log.Stderrf("readdir %s: %v", dirname, err2); log.Stderrf("readdir %s: %v", dirname, err2);
return nil, nil; return;
} }
// the package name is is the directory name within its parent // the package name is is the directory name within its parent
...@@ -501,10 +503,10 @@ func findPackage(path string) (*pakDesc, dirList) { ...@@ -501,10 +503,10 @@ func findPackage(path string) (*pakDesc, dirList) {
// if there are no package files, then there is no package // if there are no package files, then there is no package
if len(filenames) == 0 { if len(filenames) == 0 {
return nil, subdirs; return canonical, nil, subdirs;
} }
return &pakDesc{dirname, pakname, importpath, filenames}, subdirs; return canonical, &pakDesc{dirname, pakname, importpath, filenames}, subdirs;
} }
...@@ -541,20 +543,12 @@ type PageInfo struct { ...@@ -541,20 +543,12 @@ type PageInfo struct {
func servePkg(c *http.Conn, r *http.Request) { func servePkg(c *http.Conn, r *http.Request) {
path := r.Url.Path; path := r.Url.Path;
path = path[len(Pkg) : len(path)]; path = path[len(Pkg) : len(path)];
desc, dirs := findPackage(path); canonical, desc, dirs := findPackage(path);
if path == "" { if r.Url.Path != canonical {
path = "."; // don't display an empty path http.Redirect(c, canonical, http.StatusMovedPermanently);
}
// TODO Decide what canonical URL is (w/ or w/o trailing slash)
// and make sure it's the one used to get to the page.
/*
if r.Url.Path != Pkg + info.Path {
http.Redirect(c, info.Path, http.StatusMovedPermanently);
return; return;
} }
*/
pdoc, errors := desc.Doc(); pdoc, errors := desc.Doc();
if errors != nil { if errors != nil {
...@@ -576,6 +570,10 @@ func servePkg(c *http.Conn, r *http.Request) { ...@@ -576,6 +570,10 @@ func servePkg(c *http.Conn, r *http.Request) {
if err != nil { if err != nil {
log.Stderrf("packageHtml.Execute: %s", err); log.Stderrf("packageHtml.Execute: %s", err);
} }
if path == "" {
path = "."; // don't display an empty path
}
servePage(c, path + " - Go package documentation", buf.Data()); servePage(c, path + " - Go package documentation", buf.Data());
} }
...@@ -716,7 +714,7 @@ func main() { ...@@ -716,7 +714,7 @@ func main() {
parseerrorText = parseerrorHtml; parseerrorText = parseerrorHtml;
} }
desc, dirs := findPackage(flag.Arg(0)); _, desc, dirs := findPackage(flag.Arg(0));
pdoc, errors := desc.Doc(); pdoc, errors := desc.Doc();
if errors != nil { if errors != nil {
err := parseerrorText.Execute(errors, os.Stderr); err := parseerrorText.Execute(errors, os.Stderr);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment