From 0350ecef651518957a4e00b69eec72e689578424 Mon Sep 17 00:00:00 2001 From: joe Date: Thu, 22 May 2014 19:54:45 -0400 Subject: [PATCH] more granular locking --- server.go | 43 +++++++++++++++++++++++++++++++------------ triple_test.go | 4 ++-- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/server.go b/server.go index 895ceae..6f9f95a 100644 --- a/server.go +++ b/server.go @@ -169,8 +169,12 @@ func NewServer(root string, vhosts bool) (s *Server) { return } -func (s *Server) GraphPath(g AnyGraph) (path string) { - lst := strings.SplitN(g.URI(), "://", 2) +func (s *Server) reqPath(r *httpRequest) (path string) { + return s.uriPath(r.BaseURI()) +} + +func (s *Server) uriPath(uri string) (path string) { + lst := strings.SplitN(uri, "://", 2) if s.vhosts { paths := strings.SplitN(lst[1], "/", 2) host, _, _ := net.SplitHostPort(paths[0]) @@ -219,11 +223,7 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) { func (h *Server) handle(w http.ResponseWriter, req *httpRequest) (r *response) { r = new(response) - var ( - err error - - data, path string - ) + var err error user := req.Auth() w.Header().Set("User", user) @@ -251,11 +251,6 @@ func (h *Server) handle(w http.ResponseWriter, req *httpRequest) (r *response) { w.Header().Set("Access-Control-Max-Age", "60") w.Header().Set("MS-Author-Via", "DAV, SPARQL") - g := NewGraph(req.BaseURI()) - path = h.GraphPath(g) - unlock := lock(path) - defer unlock() - // TODO: WAC origin := "" origins := req.Header["Origin"] // all CORS requests @@ -293,6 +288,8 @@ func (h *Server) handle(w http.ResponseWriter, req *httpRequest) (r *response) { return r.respond(200) case "GET", "HEAD": + path := h.reqPath(req) + var ( magicType string maybeRDF bool @@ -315,6 +312,10 @@ func (h *Server) handle(w http.ResponseWriter, req *httpRequest) (r *response) { return r.respond(403) } + unlock := lock(path) + defer unlock() + g := NewGraph(req.BaseURI()) + if glob { matches, err := filepath.Glob(globPath) if err == nil { @@ -514,6 +515,7 @@ func (h *Server) handle(w http.ResponseWriter, req *httpRequest) (r *response) { return r.respond(status) } + data := "" if Streaming { errCh := make(chan error, 8) go func() { @@ -548,8 +550,13 @@ func (h *Server) handle(w http.ResponseWriter, req *httpRequest) (r *response) { } else if len(data) > 0 { fmt.Fprint(w, data) } + return case "PATCH", "POST", "PUT": + path := h.reqPath(req) + unlock := lock(path) + defer unlock() + // check append first if !acl.AllowAppend(req.BaseURI()) && !acl.AllowWrite(req.BaseURI()) { return r.respond(403) @@ -563,6 +570,8 @@ func (h *Server) handle(w http.ResponseWriter, req *httpRequest) (r *response) { return r.respond(412, "Precondition Failed") } + g := NewGraph(req.BaseURI()) + // LDP gotLDP := false if req.Method == "POST" && len(req.Header.Get("Link")) > 0 { @@ -743,6 +752,10 @@ func (h *Server) handle(w http.ResponseWriter, req *httpRequest) (r *response) { } case "DELETE": + path := h.reqPath(req) + unlock := lock(path) + defer unlock() + if !acl.AllowWrite(req.BaseURI()) { return r.respond(403) } @@ -761,7 +774,13 @@ func (h *Server) handle(w http.ResponseWriter, req *httpRequest) (r *response) { return r.respond(409) } } + return + case "MKCOL": + path := h.reqPath(req) + unlock := lock(path) + defer unlock() + if !acl.AllowWrite(req.BaseURI()) { return r.respond(403) } diff --git a/triple_test.go b/triple_test.go index d3b62d1..e570509 100644 --- a/triple_test.go +++ b/triple_test.go @@ -1,11 +1,11 @@ package gold import ( - "testing" "github.com/stretchr/testify/assert" + "testing" ) func TestTripleEquals(t *testing.T) { one := NewTriple(NewResource("a"), NewResource("b"), NewResource("c")) assert.True(t, one.Equal(NewTriple(NewResource("a"), NewResource("b"), NewResource("c")))) -} \ No newline at end of file +}