wasDAVClient

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 4  →  ?path2? @ 5
/wasDAVClient/Client.cs
@@ -1,4 +1,11 @@
using System;
///////////////////////////////////////////////////////////////////////////
// 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.Collections.Generic;
using System.IO;
using System.Linq;
@@ -47,8 +54,7 @@
private string _encodedBasePath;
private string _server;
 
 
public Client(NetworkCredential credential = null)
public Client(ICredentials credential = null)
{
var handler = new HttpClientHandler();
 
@@ -64,12 +70,10 @@
handler.PreAuthenticate = true;
}
 
_client = new HttpClient(handler);
_client.Timeout = TimeSpan.FromSeconds(Timeout);
_client = new HttpClient(handler) {Timeout = TimeSpan.FromSeconds(Timeout)};
_client.DefaultRequestHeaders.ExpectContinue = false;
 
_uploadClient = new HttpClient(handler);
_uploadClient.Timeout = TimeSpan.FromSeconds(Timeout);
_uploadClient = new HttpClient(handler) {Timeout = TimeSpan.FromSeconds(Timeout)};
_uploadClient.DefaultRequestHeaders.ExpectContinue = false;
}
 
@@ -81,11 +85,7 @@
public string Server
{
get { return _server; }
set
{
value = value.TrimEnd('/');
_server = value;
}
set { _server = value.TrimEnd('/'); }
}
 
/// <summary>
@@ -171,31 +171,29 @@
 
var listUrl = listUri.ToString();
 
var result = new List<Item>(items.Count());
foreach (var item in items)
return items.AsParallel().Select(async item =>
{
// If it's not a collection, add it to the result
if (!item.IsCollection)
switch (!item.IsCollection)
{
result.Add(item);
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;
}
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;
 
return null;
}).Select(o => o.Result).OfType<Item>();
}
}
finally
{
if (response != null)
response.Dispose();
response?.Dispose();
}
}
 
@@ -205,8 +203,7 @@
/// <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 = "/")
{
var listUri = await GetServerUrl(path, true).ConfigureAwait(false);
return await Get(listUri.Uri, path).ConfigureAwait(false);
return await Get((await GetServerUrl(path, true).ConfigureAwait(false)).Uri).ConfigureAwait(false);
}
 
/// <summary>
@@ -215,8 +212,7 @@
/// <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 = "/")
{
var listUri = await GetServerUrl(path, false).ConfigureAwait(false);
return await Get(listUri.Uri, path).ConfigureAwait(false);
return await Get((await GetServerUrl(path, false).ConfigureAwait(false)).Uri).ConfigureAwait(false);
}
 
 
@@ -224,13 +220,12 @@
/// 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, string path)
private async Task<Item> Get(Uri listUri)
{
// 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
@@ -244,7 +239,7 @@
(int) response.StatusCode != HttpStatusCode_MultiStatus)
{
throw new wasDAVException((int) response.StatusCode,
string.Format("Failed retrieving item/folder (Status Code: {0})", response.StatusCode));
$"Failed retrieving item/folder (Status Code: {response.StatusCode})");
}
 
using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
@@ -261,8 +256,7 @@
}
finally
{
if (response != null)
response.Dispose();
response?.Dispose();
}
}
 
@@ -313,8 +307,7 @@
}
finally
{
if (response != null)
response.Dispose();
response?.Dispose();
}
}
 
@@ -322,8 +315,8 @@
/// <summary>
/// Create a directory on the server
/// </summary>
/// <param name="remotePath">Destination path of the directory on the server</param>
/// <param name="name"></param>
/// <param name="remotePath">Destination path of the directory on the server.</param>
/// <param name="name">The name of the folder to create.</param>
public async Task<bool> CreateDir(string remotePath, string name)
{
// Should not have a trailing slash.
@@ -350,21 +343,18 @@
}
finally
{
if (response != null)
response.Dispose();
response?.Dispose();
}
}
 
public async Task DeleteFolder(string href)
{
var listUri = await GetServerUrl(href, true).ConfigureAwait(false);
await Delete(listUri.Uri).ConfigureAwait(false);
await Delete((await GetServerUrl(href, true).ConfigureAwait(false)).Uri).ConfigureAwait(false);
}
 
public async Task DeleteFile(string href)
{
var listUri = await GetServerUrl(href, false).ConfigureAwait(false);
await Delete(listUri.Uri).ConfigureAwait(false);
await Delete((await GetServerUrl(href, false).ConfigureAwait(false)).Uri).ConfigureAwait(false);
}
 
 
@@ -382,19 +372,19 @@
public async Task<bool> MoveFolder(string srcFolderPath, string dstFolderPath)
{
// Should have a trailing slash.
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);
return
await
Move((await GetServerUrl(srcFolderPath, true).ConfigureAwait(false)).Uri,
(await GetServerUrl(dstFolderPath, true).ConfigureAwait(false)).Uri).ConfigureAwait(false);
}
 
public async Task<bool> MoveFile(string srcFilePath, string dstFilePath)
{
// Should not have a trailing slash.
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);
return
await
Move((await GetServerUrl(srcFilePath, false).ConfigureAwait(false)).Uri,
(await GetServerUrl(dstFilePath, false).ConfigureAwait(false)).Uri).ConfigureAwait(false);
}
 
 
@@ -402,8 +392,10 @@
{
const string requestContent = "MOVE";
 
IDictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("Destination", dstUri.ToString());
IDictionary<string, string> headers = new Dictionary<string, string>
{
{"Destination", dstUri.ToString()}
};
 
var response =
await
@@ -436,10 +428,9 @@
using (var request = new HttpRequestMessage(method, uri))
{
request.Headers.Connection.Add("Keep-Alive");
if (!string.IsNullOrWhiteSpace(UserAgent))
request.Headers.UserAgent.Add(new ProductInfoHeaderValue(UserAgent, UserAgentVersion));
else
request.Headers.UserAgent.Add(new ProductInfoHeaderValue("WebDAVClient", AssemblyVersion));
request.Headers.UserAgent.Add(!string.IsNullOrWhiteSpace(UserAgent)
? new ProductInfoHeaderValue(UserAgent, UserAgentVersion)
: new ProductInfoHeaderValue("WebDAVClient", AssemblyVersion));
 
if (headers != null)
{
@@ -473,10 +464,9 @@
using (var request = new HttpRequestMessage(method, uri))
{
request.Headers.Connection.Add("Keep-Alive");
if (!string.IsNullOrWhiteSpace(UserAgent))
request.Headers.UserAgent.Add(new ProductInfoHeaderValue(UserAgent, UserAgentVersion));
else
request.Headers.UserAgent.Add(new ProductInfoHeaderValue("WebDAVClient", AssemblyVersion));
request.Headers.UserAgent.Add(!string.IsNullOrWhiteSpace(UserAgent)
? new ProductInfoHeaderValue(UserAgent, UserAgentVersion)
: new ProductInfoHeaderValue("WebDAVClient", AssemblyVersion));
 
if (headers != null)
{
@@ -492,8 +482,7 @@
request.Content = new StreamContent(content);
}
 
var client = _uploadClient ?? _client;
return await client.SendAsync(request).ConfigureAwait(false);
return await (_uploadClient ?? _client).SendAsync(request).ConfigureAwait(false);
}
}
 
@@ -518,7 +507,7 @@
if (_encodedBasePath == null)
{
var baseUri = new UriBuilder(_server) {Path = _basePath};
var root = await Get(baseUri.Uri, null).ConfigureAwait(false);
var root = await Get(baseUri.Uri).ConfigureAwait(false);
 
_encodedBasePath = root.Href;
}
@@ -582,10 +571,8 @@
 
public void Dispose()
{
if(_client != null)
_client.Dispose();
if (_uploadClient != null)
_uploadClient.Dispose();
_client?.Dispose();
_uploadClient?.Dispose();
}
 
#endregion
/wasDAVClient/Helpers/ResponseParser.cs
@@ -1,4 +1,11 @@
using System;
///////////////////////////////////////////////////////////////////////////
// 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.Collections.Generic;
using System.IO;
using System.Linq;
@@ -137,11 +144,6 @@
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,3 +1,10 @@
///////////////////////////////////////////////////////////////////////////
// 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,5 +1,13 @@
///////////////////////////////////////////////////////////////////////////
// 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
@@ -47,12 +55,16 @@
 
public override string 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();
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());
 
return s;
return sb.ToString();
}
}
}
/wasDAVClient/IClient.cs
@@ -1,3 +1,10 @@
///////////////////////////////////////////////////////////////////////////
// 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,5 +1,12 @@
using System;
///////////////////////////////////////////////////////////////////////////
// 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;
 
namespace wasDAVClient.Model
{
public class Item
/wasDAVClient/Properties/AssemblyInfo.cs
@@ -1,4 +1,5 @@
using System.Reflection;
using System.Resources;
using System.Runtime.InteropServices;
 
// General Information about an assembly is controlled through the following
@@ -5,14 +6,15 @@
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
 
[assembly: AssemblyTitle("wasDAV")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyTitle("WebDAV Client")]
[assembly: AssemblyDescription("A C# WebDAV Client")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("wasDAV")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyCompany("Wizardry and Steamworks")]
[assembly: AssemblyProduct("WebDAV Client")]
[assembly: AssemblyCopyright("(c) Copyright 2016, Wizardry and Steamworks")]
[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
@@ -35,5 +37,4 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
 
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.2.*")]