wasDAVClient

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 5  →  ?path2? @ 4
/wasDAVClient/Client.cs
@@ -1,11 +1,4 @@
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
// rights of fair usage, the disclaimer and warranty conditions. //
///////////////////////////////////////////////////////////////////////////
// Originally based on: WebDAV .NET client by Sergey Kazantsev
 
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
@@ -54,7 +47,8 @@
private string _encodedBasePath;
private string _server;
 
public Client(ICredentials credential = null)
 
public Client(NetworkCredential credential = null)
{
var handler = new HttpClientHandler();
 
@@ -70,10 +64,12 @@
handler.PreAuthenticate = true;
}
 
_client = new HttpClient(handler) {Timeout = TimeSpan.FromSeconds(Timeout)};
_client = new HttpClient(handler);
_client.Timeout = TimeSpan.FromSeconds(Timeout);
_client.DefaultRequestHeaders.ExpectContinue = false;
 
_uploadClient = new HttpClient(handler) {Timeout = TimeSpan.FromSeconds(Timeout)};
_uploadClient = new HttpClient(handler);
_uploadClient.Timeout = TimeSpan.FromSeconds(Timeout);
_uploadClient.DefaultRequestHeaders.ExpectContinue = false;
}
 
@@ -85,7 +81,11 @@
public string Server
{
get { return _server; }
set { _server = value.TrimEnd('/'); }
set
{
value = value.TrimEnd('/');
_server = value;
}
}
 
/// <summary>
@@ -171,29 +171,31 @@
 
var listUrl = listUri.ToString();
 
return items.AsParallel().Select(async item =>
var result = new List<Item>(items.Count());
foreach (var item in items)
{
switch (!item.IsCollection)
// If it's not a collection, add it to the result
if (!item.IsCollection)
{
case true:
return item;
default:
// If it's not the requested parent folder, add it to the result
if (!string.Equals((await GetServerUrl(item.Href, true).ConfigureAwait(false)).ToString(),
listUrl, StringComparison.CurrentCultureIgnoreCase))
{
return item;
}
break;
result.Add(item);
}
 
return null;
}).Select(o => o.Result).OfType<Item>();
else
{
// If it's not the requested parent folder, add it to the result
var fullHref = await GetServerUrl(item.Href, true).ConfigureAwait(false);
if (!string.Equals(fullHref.ToString(), listUrl, StringComparison.CurrentCultureIgnoreCase))
{
result.Add(item);
}
}
}
return result;
}
}
finally
{
response?.Dispose();
if (response != null)
response.Dispose();
}
}
 
@@ -203,7 +205,8 @@
/// <returns>A list of files (entries without a trailing slash) and directories (entries with a trailing slash)</returns>
public async Task<Item> GetFolder(string path = "/")
{
return await Get((await GetServerUrl(path, true).ConfigureAwait(false)).Uri).ConfigureAwait(false);
var listUri = await GetServerUrl(path, true).ConfigureAwait(false);
return await Get(listUri.Uri, path).ConfigureAwait(false);
}
 
/// <summary>
@@ -212,7 +215,8 @@
/// <returns>A list of files (entries without a trailing slash) and directories (entries with a trailing slash)</returns>
public async Task<Item> GetFile(string path = "/")
{
return await Get((await GetServerUrl(path, false).ConfigureAwait(false)).Uri).ConfigureAwait(false);
var listUri = await GetServerUrl(path, false).ConfigureAwait(false);
return await Get(listUri.Uri, path).ConfigureAwait(false);
}
 
 
@@ -220,12 +224,13 @@
/// List all files present on the server.
/// </summary>
/// <returns>A list of files (entries without a trailing slash) and directories (entries with a trailing slash)</returns>
private async Task<Item> Get(Uri listUri)
private async Task<Item> Get(Uri listUri, string path)
{
// Depth header: http://webdav.org/specs/rfc4918.html#rfc.section.9.1.4
IDictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("Depth", "0");
 
 
HttpResponseMessage response = null;
 
try
@@ -239,7 +244,7 @@
(int) response.StatusCode != HttpStatusCode_MultiStatus)
{
throw new wasDAVException((int) response.StatusCode,
$"Failed retrieving item/folder (Status Code: {response.StatusCode})");
string.Format("Failed retrieving item/folder (Status Code: {0})", response.StatusCode));
}
 
using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
@@ -256,7 +261,8 @@
}
finally
{
response?.Dispose();
if (response != null)
response.Dispose();
}
}
 
@@ -307,7 +313,8 @@
}
finally
{
response?.Dispose();
if (response != null)
response.Dispose();
}
}
 
@@ -315,8 +322,8 @@
/// <summary>
/// Create a directory on the server
/// </summary>
/// <param name="remotePath">Destination path of the directory on the server.</param>
/// <param name="name">The name of the folder to create.</param>
/// <param name="remotePath">Destination path of the directory on the server</param>
/// <param name="name"></param>
public async Task<bool> CreateDir(string remotePath, string name)
{
// Should not have a trailing slash.
@@ -343,18 +350,21 @@
}
finally
{
response?.Dispose();
if (response != null)
response.Dispose();
}
}
 
public async Task DeleteFolder(string href)
{
await Delete((await GetServerUrl(href, true).ConfigureAwait(false)).Uri).ConfigureAwait(false);
var listUri = await GetServerUrl(href, true).ConfigureAwait(false);
await Delete(listUri.Uri).ConfigureAwait(false);
}
 
public async Task DeleteFile(string href)
{
await Delete((await GetServerUrl(href, false).ConfigureAwait(false)).Uri).ConfigureAwait(false);
var listUri = await GetServerUrl(href, false).ConfigureAwait(false);
await Delete(listUri.Uri).ConfigureAwait(false);
}
 
 
@@ -372,19 +382,19 @@
public async Task<bool> MoveFolder(string srcFolderPath, string dstFolderPath)
{
// Should have a trailing slash.
return
await
Move((await GetServerUrl(srcFolderPath, true).ConfigureAwait(false)).Uri,
(await GetServerUrl(dstFolderPath, true).ConfigureAwait(false)).Uri).ConfigureAwait(false);
var srcUri = await GetServerUrl(srcFolderPath, true).ConfigureAwait(false);
var dstUri = await GetServerUrl(dstFolderPath, true).ConfigureAwait(false);
 
return await Move(srcUri.Uri, dstUri.Uri).ConfigureAwait(false);
}
 
public async Task<bool> MoveFile(string srcFilePath, string dstFilePath)
{
// Should not have a trailing slash.
return
await
Move((await GetServerUrl(srcFilePath, false).ConfigureAwait(false)).Uri,
(await GetServerUrl(dstFilePath, false).ConfigureAwait(false)).Uri).ConfigureAwait(false);
var srcUri = await GetServerUrl(srcFilePath, false).ConfigureAwait(false);
var dstUri = await GetServerUrl(dstFilePath, false).ConfigureAwait(false);
 
return await Move(srcUri.Uri, dstUri.Uri).ConfigureAwait(false);
}
 
 
@@ -392,10 +402,8 @@
{
const string requestContent = "MOVE";
 
IDictionary<string, string> headers = new Dictionary<string, string>
{
{"Destination", dstUri.ToString()}
};
IDictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("Destination", dstUri.ToString());
 
var response =
await
@@ -428,9 +436,10 @@
using (var request = new HttpRequestMessage(method, uri))
{
request.Headers.Connection.Add("Keep-Alive");
request.Headers.UserAgent.Add(!string.IsNullOrWhiteSpace(UserAgent)
? new ProductInfoHeaderValue(UserAgent, UserAgentVersion)
: new ProductInfoHeaderValue("WebDAVClient", AssemblyVersion));
if (!string.IsNullOrWhiteSpace(UserAgent))
request.Headers.UserAgent.Add(new ProductInfoHeaderValue(UserAgent, UserAgentVersion));
else
request.Headers.UserAgent.Add(new ProductInfoHeaderValue("WebDAVClient", AssemblyVersion));
 
if (headers != null)
{
@@ -464,9 +473,10 @@
using (var request = new HttpRequestMessage(method, uri))
{
request.Headers.Connection.Add("Keep-Alive");
request.Headers.UserAgent.Add(!string.IsNullOrWhiteSpace(UserAgent)
? new ProductInfoHeaderValue(UserAgent, UserAgentVersion)
: new ProductInfoHeaderValue("WebDAVClient", AssemblyVersion));
if (!string.IsNullOrWhiteSpace(UserAgent))
request.Headers.UserAgent.Add(new ProductInfoHeaderValue(UserAgent, UserAgentVersion));
else
request.Headers.UserAgent.Add(new ProductInfoHeaderValue("WebDAVClient", AssemblyVersion));
 
if (headers != null)
{
@@ -482,7 +492,8 @@
request.Content = new StreamContent(content);
}
 
return await (_uploadClient ?? _client).SendAsync(request).ConfigureAwait(false);
var client = _uploadClient ?? _client;
return await client.SendAsync(request).ConfigureAwait(false);
}
}
 
@@ -507,7 +518,7 @@
if (_encodedBasePath == null)
{
var baseUri = new UriBuilder(_server) {Path = _basePath};
var root = await Get(baseUri.Uri).ConfigureAwait(false);
var root = await Get(baseUri.Uri, null).ConfigureAwait(false);
 
_encodedBasePath = root.Href;
}
@@ -571,8 +582,10 @@
 
public void Dispose()
{
_client?.Dispose();
_uploadClient?.Dispose();
if(_client != null)
_client.Dispose();
if (_uploadClient != null)
_uploadClient.Dispose();
}
 
#endregion
/wasDAVClient/Helpers/ResponseParser.cs
@@ -1,11 +1,4 @@
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
// rights of fair usage, the disclaimer and warranty conditions. //
///////////////////////////////////////////////////////////////////////////
// Originally based on: WebDAV .NET client by Sergey Kazantsev
 
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
@@ -144,6 +137,11 @@
case "version-controlled-configuration":
reader.Skip();
break;
default:
{
var a = 0;
break;
}
}
}
else if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName.ToLower() == "response")
/wasDAVClient/Helpers/wasDAVConflictException.cs
@@ -1,10 +1,3 @@
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
// rights of fair usage, the disclaimer and warranty conditions. //
///////////////////////////////////////////////////////////////////////////
// Originally based on: WebDAV .NET client by Sergey Kazantsev
 
using System;
using System.Runtime.Serialization;
 
/wasDAVClient/Helpers/wasDAVException.cs
@@ -1,13 +1,5 @@
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
// rights of fair usage, the disclaimer and warranty conditions. //
///////////////////////////////////////////////////////////////////////////
// Originally based on: WebDAV .NET client by Sergey Kazantsev
 
using System;
using System.Runtime.Serialization;
using System.Text;
using System.Web;
 
namespace wasDAVClient.Helpers
@@ -55,16 +47,12 @@
 
public override string ToString()
{
var sb = new StringBuilder();
sb.Append($"HttpStatusCode: {GetHttpCode()}");
sb.Append(Environment.NewLine);
sb.Append($"ErrorCode: {ErrorCode}");
sb.Append(Environment.NewLine);
sb.Append($"Message: {Message}");
sb.Append(Environment.NewLine);
sb.Append(base.ToString());
var s = string.Format("HttpStatusCode: {0}", GetHttpCode());
s += Environment.NewLine + string.Format("ErrorCode: {0}", ErrorCode);
s += Environment.NewLine + string.Format("Message: {0}", Message);
s += Environment.NewLine + base.ToString();
 
return sb.ToString();
return s;
}
}
}
/wasDAVClient/IClient.cs
@@ -1,10 +1,3 @@
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
// rights of fair usage, the disclaimer and warranty conditions. //
///////////////////////////////////////////////////////////////////////////
// Originally based on: WebDAV .NET client by Sergey Kazantsev
 
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
/wasDAVClient/Model/Item.cs
@@ -1,12 +1,5 @@
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
// rights of fair usage, the disclaimer and warranty conditions. //
///////////////////////////////////////////////////////////////////////////
// Originally based on: WebDAV .NET client by Sergey Kazantsev
using System;
 
using System;
 
namespace wasDAVClient.Model
{
public class Item
/wasDAVClient/Properties/AssemblyInfo.cs
@@ -1,5 +1,4 @@
using System.Reflection;
using System.Resources;
using System.Runtime.InteropServices;
 
// General Information about an assembly is controlled through the following
@@ -6,15 +5,14 @@
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
 
[assembly: AssemblyTitle("WebDAV Client")]
[assembly: AssemblyDescription("A C# WebDAV Client")]
[assembly: AssemblyTitle("wasDAV")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Wizardry and Steamworks")]
[assembly: AssemblyProduct("WebDAV Client")]
[assembly: AssemblyCopyright("(c) Copyright 2016, Wizardry and Steamworks")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("wasDAV")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: NeutralResourcesLanguage("en")]
 
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
@@ -37,4 +35,5 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
 
[assembly: AssemblyVersion("1.2.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]