/// <summary> /// Gets a <see cref="LockToken"/> from a <see cref="WebDavResponseMessage"/>. /// </summary> /// <param name="responseMessage">The <see cref="WebDavResponseMessage"/> whose <see cref="LockToken"/> should be retrieved.</param> /// <returns>The <see cref="LockToken"/> of the <see cref="WebDavResponseMessage"/> or null if the WebDavResponseMessage does not contain a lock token.</returns> public static async Task <LockToken> GetLockTokenFromWebDavResponseMessage(WebDavResponseMessage responseMessage) { // 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 codedUrl)) { return(new LockToken(codedUrl.AbsoluteUri)); } } // If lock token was not submitted by response header, it should be found in the response content. try { var prop = await WebDavResponseContentParser.ParsePropResponseContentAsync(responseMessage.Content); var href = prop.LockDiscovery.ActiveLock[0].LockToken.Href; if (AbsoluteUri.TryParse(href, out var absoluteUri)) { return(new LockToken(absoluteUri)); } } catch (Exception) { return(null); } return(null); }
public void UT_WebDavResponseMessage_Transparently_Wrap_Headers() { const string expected = "Jetty(9.3.9.v20160517)"; var responseToWrap = new HttpResponseMessage(); responseToWrap.Headers.TryAddWithoutValidation("Server", expected); var wrapper = new WebDavResponseMessage(responseToWrap); var actual = wrapper.Headers.GetValues("Server").FirstOrDefault(); Assert.AreEqual(expected, actual); }
public async Task UIT_WebDavClient_Get_WithFileWithUnknownExtension() { using (var client = CreateWebDavClientWithDebugHttpMessageHandler()) { var testFile = UriHelper.CombineUrl(webDavRootFolder, TestFileUnknownExtension, true); WebDavResponseMessage response = null; // Put file. using (var fileStream = File.OpenRead(TestFileUnknownExtension)) { var content = new StreamContent(fileStream); response = await client.PutAsync(testFile, content); } var putResponseSuccess = response.IsSuccessStatusCode; // Head response = await client.HeadAsync(testFile); var headResponseSuccess = response.IsSuccessStatusCode; // Get file. response = await client.GetAsync(testFile); var getResponseSuccess = response.IsSuccessStatusCode; var responseContentStringGet = await response.Content.ReadAsStringAsync(); // Delete file. response = await client.DeleteAsync(testFile); var deleteResponseSuccess = response.IsSuccessStatusCode; Assert.IsTrue(putResponseSuccess); Assert.IsTrue(headResponseSuccess); Assert.IsTrue(getResponseSuccess); Assert.AreEqual("This a a file with unknown extension.", responseContentStringGet); Assert.IsTrue(deleteResponseSuccess); } }
/// <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 async Task <ActiveLock> GetActiveLockFromWebDavResponseMessage(WebDavResponseMessage responseMessage) { if (responseMessage == null) { throw new ArgumentNullException(nameof(responseMessage)); } var prop = await WebDavResponseContentParser.ParsePropResponseContentAsync(responseMessage.Content); 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); }
public void UIT_WebDavClient_Get() { using (var client = CreateWebDavClientWithDebugHttpMessageHandler()) { var testFile = UriHelper.CombineUrl(webDavRootFolder, TestFile, true); WebDavResponseMessage response = null; // Put file. using (var fileStream = File.OpenRead(TestFile)) { var content = new StreamContent(fileStream); response = client.PutAsync(testFile, content).Result; } var putResponseSuccess = response.IsSuccessStatusCode; // Head response = client.HeadAsync(testFile).Result; var headResponseSuccess = response.IsSuccessStatusCode; // Get file. response = client.GetAsync(testFile).Result; var getResponseSuccess = response.IsSuccessStatusCode; var responseContentStringGet = response.Content.ReadAsStringAsync().Result; // Delete file. response = client.DeleteAsync(testFile).Result; var deleteResponseSuccess = response.IsSuccessStatusCode; Assert.IsTrue(putResponseSuccess); Assert.IsTrue(headResponseSuccess); Assert.IsTrue(getResponseSuccess); Assert.AreEqual("This is a test file for WebDAV.", responseContentStringGet); Assert.IsTrue(deleteResponseSuccess); } }