Merge pull request #191 from matrix-org/daniel/fixspeculator

302 to spec/head/index.html rather than serving it on /spec/head

Otherwise relative links are broken
pull/977/head
Daniel Wagner-Hall 9 years ago
commit 611e2be229

@ -193,15 +193,31 @@ func (s *server) getSHAOf(ref string) (string, error) {
return strings.TrimSpace(b.String()), nil return strings.TrimSpace(b.String()), nil
} }
func extractPath(path, base string) string { // extractPath extracts the file path within the gen directory which should be served for the request.
// Assume exactly one flat directory // Returns one of (file to serve, path to redirect to).
max := strings.Count(base, "/") + 2 // path is the actual path being requested, e.g. "/spec/head/client_server.html".
// base is the base path of the handler, including a trailing slash, before the PR number, e.g. "/spec/".
func extractPath(path, base string) (string, string) {
// Assumes exactly one flat directory
// Count slashes in /spec/head/client_server.html
// base is /spec/
// +1 for the PR number - /spec/head
// +1 for the path-part after the slash after the PR number
max := strings.Count(base, "/") + 1
parts := strings.SplitN(path, "/", max) parts := strings.SplitN(path, "/", max)
if len(parts) < max || parts[max-1] == "" { if len(parts) < max {
return "index.html" // Path is base/pr - redirect to base/pr/index.html
return "", path + "/index.html"
} }
return parts[max-1] if parts[max-1] == "" {
// Path is base/pr/ - serve index.html
return "index.html", ""
}
// Path is base/pr/file.html - serve file
return parts[max-1], ""
} }
func (s *server) serveSpec(w http.ResponseWriter, req *http.Request) { func (s *server) serveSpec(w http.ResponseWriter, req *http.Request) {
@ -283,7 +299,11 @@ func (s *server) serveSpec(w http.ResponseWriter, req *http.Request) {
cache.Add(sha, pathToContent) cache.Add(sha, pathToContent)
} }
requestedPath := extractPath(req.URL.Path, "/spec/pr") requestedPath, redirect := extractPath(req.URL.Path, "/spec/")
if redirect != "" {
s.redirectTo(w, req, redirect)
return
}
if b, ok := pathToContent[requestedPath]; ok { if b, ok := pathToContent[requestedPath]; ok {
w.Write(b) w.Write(b)
return return
@ -299,6 +319,12 @@ func (s *server) serveSpec(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("Not found")) w.Write([]byte("Not found"))
} }
func (s *server) redirectTo(w http.ResponseWriter, req *http.Request, path string) {
req.URL.Path = path
w.Header().Set("Location", req.URL.String())
w.WriteHeader(302)
}
// lookupHeadSHA looks up what origin/master's HEAD SHA is. // lookupHeadSHA looks up what origin/master's HEAD SHA is.
// It attempts to `git fetch` before doing so. // It attempts to `git fetch` before doing so.
// If this fails, it may still return a stale sha, but will also return an error. // If this fails, it may still return a stale sha, but will also return an error.
@ -397,7 +423,11 @@ func (s *server) serveHTMLDiff(w http.ResponseWriter, req *http.Request) {
return return
} }
requestedPath := extractPath(req.URL.Path, "/diff/spec/pr") requestedPath, redirect := extractPath(req.URL.Path, "/diff/spec/")
if redirect != "" {
s.redirectTo(w, req, redirect)
return
}
cmd := exec.Command(htmlDiffer, path.Join(base, "scripts", "gen", requestedPath), path.Join(head, "scripts", "gen", requestedPath)) cmd := exec.Command(htmlDiffer, path.Join(base, "scripts", "gen", requestedPath), path.Join(head, "scripts", "gen", requestedPath))
var b bytes.Buffer var b bytes.Buffer
cmd.Stdout = &b cmd.Stdout = &b

Loading…
Cancel
Save