Skip to content
This repository has been archived by the owner on Feb 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #53 from JosVerburg/GetActiveLockFromWebDavRespons…
Browse files Browse the repository at this point in the history
…eMessage

Add GetActiveLockFromWebDavResponseMessage to the WebDavHelper
  • Loading branch information
DecaTec authored Aug 16, 2017
2 parents a79981e + b1fc229 commit 75d20bd
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions DecaTec.WebDav/Tools/WebDavHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,39 @@ namespace DecaTec.WebDav.Tools
/// </summary>
public static class WebDavHelper
{
/// <summary>
/// Gets the <see cref="ActiveLock"/> from a <see cref="WebDavResponseMessage"/>.
/// </summary>
/// <param name="responseMessage">The <see cref="WebDavResponseMessage"/> whose <see cref="ActiveLock"/> should be retrieved.</param>
/// <returns>The <see cref="ActiveLock"/> of the <see cref="WebDavResponseMessage"/> or null if the <see cref="WebDavResponseMessage"/> does not contain a lock token.</returns>
/// <exception cref="ArgumentNullException">Thrown if the <paramref name="responseMessage"/> is null.</exception>
public static ActiveLock GetActiveLockFromWebDavResponseMessage(WebDavResponseMessage responseMessage)
{
if (responseMessage == null)
throw new ArgumentNullException(nameof(responseMessage));

var prop = WebDavResponseContentParser.ParsePropResponseContentAsync(responseMessage.Content).Result;
var activeLock = prop.LockDiscovery?.ActiveLock.FirstOrDefault();

if (activeLock == null)
return null;

// If lock token was not be found in the response content, it should be submitted by response header.
if (activeLock.LockToken == null)
// Try to get lock token from response header.
if (responseMessage.Headers.TryGetValues(WebDavRequestHeader.LockToken, out IEnumerable<string> lockTokenHeaderValues))
{
// We assume only one Lock-Token header is sent, based on the spec: https://tools.ietf.org/html/rfc4918#section-9.10.1
var lockTokenHeaderValue = lockTokenHeaderValues.FirstOrDefault();

// Make sure the lockTokenHeaderValue is valid according to spec (https://tools.ietf.org/html/rfc4918#section-10.5).
if (lockTokenHeaderValue != null && CodedUrl.TryParse(lockTokenHeaderValue, out var _))
activeLock.LockToken = new WebDavLockToken { Href = lockTokenHeaderValue };
}

return activeLock;
}

/// <summary>
/// Gets a UTF-8 encoded string by serializing the object specified.
/// </summary>
Expand Down

0 comments on commit 75d20bd

Please sign in to comment.