Example #1
0
// getPropValue gets a property for a given file, potentially generating
// synthetic properties that are expected. It will always return a value
// with the correct name, but potentially lack a value if not present.
func (s *WebDAV) getPropValue(pn string, f File) (x.Any, bool) {
	a := x.NewAny(pn)
	switch pn {
	case "DAV::resourcetype":
		if f.IsDirectory() {
			a.Inner = "<collection xmlns=\"DAV:\"/>"
		}
		return a, true
	case "DAV::supportedlock":
		a.Inner = `
<D:lockentry xmlns:D="DAV::">
<D:lockscope><D:exclusive/></D:lockscope>
<D:locktype><D:write/></D:locktype>
</D:lockentry>`
		return a, true
	case "DAV::lockdiscovery":
		l := s.lm.getLockForPath(f.GetPath())
		if l != nil {
			a.Inner = l.toXML()
		}
		return a, true
	case "DAV::displayname":
		a.Value = path.Base(f.GetPath())
		return a, true
	}

	if fileStatProps[pn] {
		v, err := getFileStatProp(pn, f)
		if err != nil {
			return a, false
		}
		a.Value = v
		return a, true
	}
	v, ok := f.GetProp(pn)
	a.Value = v
	return a, ok
}
Example #2
0
// http://www.webdav.org/specs/rfc4918.html#METHOD_LOCK
func (s *WebDAV) doLock(ctx context, w http.ResponseWriter, r *http.Request) {
	req, err := x.ParseLock(r.Body)
	if err != nil {
		s.errorHeader(ctx, w, ErrorBadLock.WithCause(err))
		return
	}
	log.Printf("REQ %+v", req)

	// We don't let you lock on anything without a parent.
	_, err = ctx.p.Parent().Lookup()
	if err != nil {
		s.errorHeader(ctx, w, ErrorMissingParent)
		return
	}

	var l *lock
	if req.Refresh {
		if ctx.cond == nil {
			s.errorHeader(ctx, w, ErrorBadLock)
			return
		}
		tok, ok := ctx.cond.GetSingleState()
		if !ok {
			s.errorHeader(ctx, w, ErrorBadLock)
			return
		}
		l, err = s.lm.refreshLock(tok, ctx.p, ctx.timeout)
	} else {
		l, err = s.lm.createLock(req.Owner, ctx.p, ctx.depth, ctx.timeout)
	}
	if err != nil {
		s.errorHeader(ctx, w, err)
		return
	}

	if !req.Refresh {
		w.Header().Set("Lock-Token", "<"+l.token+">")
	}

	// Now that we have a successful lock, create the resource
	// if it didn't exist already.
	_, err = ctx.p.Lookup()
	if err != nil {
		_, fh, err := ctx.p.Create()
		if err != nil {
			// Unlock, as we're failing.
			s.lm.unlock(l.token)
			s.errorHeader(ctx, w, err)
			return
		}
		fh.Close()
		w.WriteHeader(http.StatusCreated)
	} else {
		w.WriteHeader(http.StatusOK)
	}

	log.Println(l)

	a := x.NewAny("DAV::lockdiscovery")
	a.Inner = l.toXML()
	x.SendProp(a, w)
}