Only return the meta for requested package

the go tool currenty has a pathology where it mistakingly claims it
can't clone a repo because it checks prefix by bytes, not by splitting
the path on slash.

Once https://github.com/golang/go/issues/15947 comes out (go1.8) then go
tools will be able to handle being provided with valid but overlapping
packages.

For now though we'll just return meta for the requested package.

Change-Id: Ie5026e7d5c1377ff7d2c2140b21f9b745af69764
This commit is contained in:
Stephen McQuay 2016-06-03 16:44:50 -07:00
parent 5458745af9
commit 295a7c3840
No known key found for this signature in database
GPG Key ID: 1ABF428F71BAFC3D
3 changed files with 75 additions and 4 deletions

View File

@ -508,3 +508,57 @@ func TestDelete(t *testing.T) {
}
}
}
func TestSingleGet(t *testing.T) {
db, done := testDB(t)
if db == nil {
t.Fatalf("could not create temp db")
}
defer done()
sm := http.NewServeMux()
NewServer(sm, db, "", window)
ts := httptest.NewServer(sm)
tok, err := db.addUser("sm@example.org")
if err != nil {
t.Fatalf("failure to add user: %v", err)
}
ns := "foo"
if err := db.NSForToken(ns, tok); err != nil {
t.Fatalf("could not initialize namespace %q for user %q: %v", ns, tok, err)
}
p := Package{
Vcs: "git",
Repo: "https://example.org/foo",
Path: fmt.Sprintf("%s/foo/bar", strings.TrimPrefix(ts.URL, "http://")),
Ns: ns,
}
if err := db.AddPackage(p); err != nil {
t.Fatalf("couldn't add package %v: %v", p, err)
}
{
// expected failure
resp, err := http.Get(ts.URL + "/bleh/blah?go-get=1")
if err != nil {
t.Fatalf("problem getting route: %v", err)
}
if got, want := resp.StatusCode, http.StatusNotFound; got != want {
t.Fatalf("should have failed to GET unknown route; got %s, want %s", http.StatusText(got), http.StatusText(want))
}
}
{
url := ts.URL + "/foo/bar?go-get=1"
resp, err := http.Get(url)
if err != nil {
t.Fatalf("problem getting route: %v", err)
}
if got, want := resp.StatusCode, http.StatusOK; got != want {
t.Fatalf("should have failed to GET unknown route; got %s, want %s", http.StatusText(got), http.StatusText(want))
}
}
}

7
db.go
View File

@ -1,6 +1,7 @@
package vain
import (
"database/sql"
"fmt"
"log"
"net/http"
@ -100,6 +101,12 @@ func (db *DB) PackageExists(path string) bool {
func (db *DB) Package(path string) (Package, error) {
r := Package{}
err := db.conn.Get(&r, "SELECT * FROM packages WHERE path = ?", path)
if err == sql.ErrNoRows {
return r, verrors.HTTP{
Message: fmt.Sprintf("couldn't find package %q", path),
Code: http.StatusNotFound,
}
}
return r, err
}

View File

@ -54,11 +54,20 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
http.Redirect(w, req, prefix["static"], http.StatusTemporaryRedirect)
return
}
if req.URL.Path == "/" {
fmt.Fprintf(w, "<!DOCTYPE html>\n<html><head>\n")
for _, p := range s.db.Pkgs() {
fmt.Fprintf(w, "%s\n", p)
}
fmt.Fprintf(w, "</head>\n<body><p>go tool metadata in head</p></body>\n</html>\n")
} else {
p, err := s.db.Package(req.Host + req.URL.Path)
if err := verrors.ToHTTP(err); err != nil {
http.Error(w, err.Message, err.Code)
return
}
fmt.Fprintf(w, "<!DOCTYPE html>\n<html><head>\n%s\n</head>\n<body><p>go tool metadata in head</p></body>\n</html>\n", p)
}
return
}
@ -207,6 +216,7 @@ func (s *Server) forgot(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-type", "application/json")
json.NewEncoder(w).Encode(resp)
}
func (s *Server) pkgs(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-type", "application/json")
json.NewEncoder(w).Encode(s.db.Pkgs())