Skip to content

Commit

Permalink
more granular locking
Browse files Browse the repository at this point in the history
  • Loading branch information
presbrey committed May 22, 2014
1 parent 27009b7 commit 0350ece
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
43 changes: 31 additions & 12 deletions server.go
Expand Up @@ -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])
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions 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"))))
}
}

0 comments on commit 0350ece

Please sign in to comment.