/// <summary> /// Unlocks the URI passed if the token matches a lock token in use. /// </summary> /// <param name="path">URI to resource</param> /// <param name="locktoken">Token used to lock.</param> /// <param name="owner">Owner.</param> /// <returns></returns> public static int UnLock(Uri path, string locktoken, string owner) { CleanLocks(path); if (string.IsNullOrEmpty(locktoken)) { WebDavServer.Log.Debug("Unlock failed, No Token!."); return((int)HttpStatusCode.BadRequest); } lock (ObjectLocks) { if (!ObjectLocks.ContainsKey(path)) { WebDavServer.Log.Debug("Unlock failed, Lock does not exist!."); return((int)HttpStatusCode.Conflict); } WebDaveStoreItemLockInstance ilock = ObjectLocks[path].FirstOrDefault(d => d.Token == locktoken && d.Owner == owner); if (ilock == null) { return((int)HttpStatusCode.Conflict); } //Remove the lock ObjectLocks[path].Remove(ilock); //if there are no locks left of the uri then remove it from ObjectLocks. if (ObjectLocks[path].Count == 0) { ObjectLocks.Remove(path); } WebDavServer.Log.Debug("Unlock successful."); return((int)HttpStatusCode.NoContent); } }
/// <summary> /// This function will refresh an existing lock. /// </summary> /// <param name="path">Target URI to the file or folder </param> /// <param name="locktoken">The token issued when the lock was established</param> /// <param name="requestedlocktimeout">The requested timeout</param> /// <param name="requestDocument">Output parameter, returns the Request document that was used when the lock was established.</param> /// <returns></returns> public static int RefreshLock(Uri path, string locktoken, ref string requestedlocktimeout, out XmlDocument requestDocument) { CleanLocks(path); //Refreshing an existing lock //If a lock doesn't exist then lets just reply with a Precondition Failed. //412 (Precondition Failed), with 'lock-token-matches-request-uri' precondition code - The LOCK request was //made with an If header, indicating that the client wishes to refresh the given lock. However, the Request-URI //did not fall within the scope of the lock identified by the token. The lock may have a scope that does not //include the Request-URI, or the lock could have disappeared, or the token may be invalid. requestDocument = null; if (!ObjectLocks.ContainsKey(path)) { return(412); } string tmptoken = locktoken; lock (ObjectLocks) { WebDaveStoreItemLockInstance ilock = ObjectLocks[path].FirstOrDefault(d => (d.Token == tmptoken)); if (ilock == null) { WebDavServer.Log.Debug("Lock Refresh Failed , Lock does not exist."); return(412); } WebDavServer.Log.Debug("Lock Refresh Successful."); ilock.RefreshLock(ref requestedlocktimeout); requestDocument = ilock.RequestDocument; return((int)HttpStatusCode.OK); } }
/// <summary> /// Unlocks the URI passed if the token matches a lock token in use. /// </summary> /// <param name="path">URI to resource</param> /// <param name="locktoken">Token used to lock.</param> /// <param name="owner">Owner.</param> /// <returns></returns> public static int UnLock(Uri path, string locktoken, string owner) { LoadAndCleanLocks(path); if (string.IsNullOrEmpty(locktoken)) { WebDavServer.Log.Debug("Unlock failed for {0}, No Token!.", path); return((int)HttpStatusCode.BadRequest); } lock (ObjectLocks) { if (!ObjectLocks.ContainsKey(path)) { WebDavServer.Log.Debug("Unlock failed, Lock does not exist for {0}!.", path); return((int)HttpStatusCode.Conflict); } WebDaveStoreItemLockInstance ilock = ObjectLocks[path].FirstOrDefault(d => d.Token == locktoken && d.Owner == owner); if (ilock == null) { if (WebDavServer.Log.IsEnabled(Serilog.Events.LogEventLevel.Debug)) { StringBuilder sb = new StringBuilder(); foreach (var uriLock in ObjectLocks[path]) { sb.AppendFormat("token:{0} owner:{1} type: {2}\n", uriLock.Token, uriLock.Owner, uriLock.LockType); } WebDavServer.Log.Debug("Unlock failed: {0} is locked but not whith the same token. All tokens:{1}", path, sb.ToString()); } return((int)HttpStatusCode.Conflict); } //Remove the lock ObjectLocks[path].Remove(ilock); WebDavServer.Log.Debug("Unlock successful {0} token {1} owner {2}", path, locktoken, owner); LockPersister.Persist(path, ObjectLocks[path]); return((int)HttpStatusCode.NoContent); } }