/Collections/Utilities/CollectionExtensions.cs |
@@ -0,0 +1,57 @@ |
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
// rights of fair usage, the disclaimer and warranty conditions. // |
/////////////////////////////////////////////////////////////////////////// |
|
using System.Collections.Generic; |
using System.Linq; |
|
namespace wasSharp.Collections.Utilities |
{ |
public static class CollectionExtensions |
{ |
/// <summary> |
/// Compares two dictionaries for equality. |
/// </summary> |
/// <typeparam name="TKey">key type</typeparam> |
/// <typeparam name="TValue">value type</typeparam> |
/// <param name="dictionary">dictionary to compare</param> |
/// <param name="otherDictionary">dictionary to compare to</param> |
/// <returns>true if the dictionaries contain the same elements</returns> |
public static bool ContentEquals<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, |
IDictionary<TKey, TValue> otherDictionary) |
{ |
return |
(dictionary ?? new Dictionary<TKey, TValue>()).Count.Equals( |
(otherDictionary ?? new Dictionary<TKey, TValue>()).Count) && |
(otherDictionary ?? new Dictionary<TKey, TValue>()) |
.OrderBy(kvp => kvp.Key) |
.SequenceEqual((dictionary ?? new Dictionary<TKey, TValue>()) |
.OrderBy(kvp => kvp.Key)); |
} |
|
public static void AddOrReplace<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value) |
{ |
switch (dictionary.ContainsKey(key)) |
{ |
case true: |
dictionary[key] = value; |
break; |
default: |
dictionary.Add(key, value); |
break; |
} |
} |
|
public static void AddIfNotExists<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value) |
{ |
switch (!dictionary.ContainsKey(key)) |
{ |
case true: |
dictionary.Add(key, value); |
break; |
} |
} |
} |
} |
/Timers/Utilities/TimeExtensions.cs |
@@ -0,0 +1,35 @@ |
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
// rights of fair usage, the disclaimer and warranty conditions. // |
/////////////////////////////////////////////////////////////////////////// |
|
using System; |
|
namespace wasSharp.Timers.Utilities |
{ |
public static class TimeExtensions |
{ |
/// <summary> |
/// Convert an Unix timestamp to a DateTime structure. |
/// </summary> |
/// <param name="unixTimestamp">the Unix timestamp to convert</param> |
/// <returns>the DateTime structure</returns> |
/// <remarks>the function assumes UTC time</remarks> |
public static DateTime UnixTimestampToDateTime(this uint unixTimestamp) |
{ |
return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(unixTimestamp).ToUniversalTime(); |
} |
|
/// <summary> |
/// Convert a DateTime structure to a Unix timestamp. |
/// </summary> |
/// <param name="dateTime">the DateTime structure to convert</param> |
/// <returns>the Unix timestamp</returns> |
/// <remarks>the function assumes UTC time</remarks> |
public static uint DateTimeToUnixTimestamp(this DateTime dateTime) |
{ |
return (uint) (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds; |
} |
} |
} |
/Web/Utilities/WebExtensions.cs |
@@ -0,0 +1,94 @@ |
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
// rights of fair usage, the disclaimer and warranty conditions. // |
/////////////////////////////////////////////////////////////////////////// |
|
using System; |
using System.Linq; |
using System.Linq.Expressions; |
using System.Net; |
using System.Text.RegularExpressions; |
|
namespace wasSharp.Web.Utilities |
{ |
public static class WebExtensions |
{ |
private static readonly Func<string, string> directURIEscapeDataString = |
((Expression<Func<string, string>>) |
(data => string.Join("", Enumerable.Range(0, (data.Length + 32765)/32766).AsParallel() |
.Select(o => Uri.EscapeDataString(data.Substring(o*32766, Math.Min(32766, data.Length - o*32766)))) |
.ToArray()))).Compile(); |
|
private static readonly Func<string, string> directURIUnescapeDataString = |
((Expression<Func<string, string>>) |
(data => string.Join("", Enumerable.Range(0, (data.Length + 32765)/32766).AsParallel() |
.Select( |
o => Uri.UnescapeDataString(data.Substring(o*32766, Math.Min(32766, data.Length - o*32766)))) |
.ToArray()))).Compile(); |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary>RFC3986 URI Escapes a string</summary> |
/// <remarks> |
/// data - a string to escape |
/// </remarks> |
/// <returns>an RFC3986 escaped string</returns> |
public static string URIEscapeDataString(this string data) |
{ |
return directURIEscapeDataString(data); |
} |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary>URI unescapes an RFC3986 URI escaped string</summary> |
/// <remarks> |
/// data - a string to unescape |
/// </remarks> |
/// <returns>the resulting string</returns> |
public static string URIUnescapeDataString(this string data) |
{ |
return directURIUnescapeDataString(data); |
} |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary>RFC1738 URL Escapes a string</summary> |
/// <param name="data">a string to escape</param> |
/// <returns>an RFC1738 escaped string</returns> |
public static string URLEscapeDataString(this string data) |
{ |
return WebUtility.UrlEncode(data); |
} |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary>RFC1738 URL Unescape a string</summary> |
/// <param name="data">a string to unescape</param> |
/// <returns>an RFC1738 unescaped string</returns> |
public static string URLUnescapeDataString(this string data) |
{ |
return WebUtility.UrlDecode(data); |
} |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <param name="prefix">a HttpListener prefix</param> |
/// <returns>the port of the HttpListener</returns> |
public static long GetPortFromPrefix(this string prefix) |
{ |
var split = Regex.Replace( |
prefix, |
@"^([a-zA-Z]+:\/\/)?([^\/]+)\/.*?$", |
"$2" |
).Split(':'); |
long port; |
return split.Length <= 1 || !long.TryParse(split[1], out port) ? 80 : port; |
} |
} |
} |
/Web/wasHTTPClient.cs |
@@ -65,11 +65,10 @@ |
} |
// Add some standard headers: |
// Accept - for socially acceptable security of mod_sec |
// Accept-Encoding - since we want to use compression if possible |
switch (headers != null) |
{ |
case false: |
headers = new Dictionary<string, string> {{"Accept", @"*/*"}, {"Accept-Encoding", "gzip,defalate"}}; |
headers = new Dictionary<string, string> {{"Accept", @"*/*"}}; |
break; |
default: |
if (!headers.ContainsKey("Accept")) |
@@ -76,10 +75,6 @@ |
{ |
headers.Add("Accept", @"*/*"); |
} |
if (!headers.ContainsKey("Accept-Encoding")) |
{ |
headers.Add("Accept-Encoding", "gzip,deflate"); |
} |
break; |
} |
foreach (var header in headers) |
@@ -119,15 +114,10 @@ |
} |
// Add some standard headers: |
// Accept - for socially acceptable security of mod_sec |
// Accept-Encoding - since we want to use compression if possible |
if (!headers.ContainsKey("Accept")) |
{ |
headers.Add("Accept", @"*/*"); |
} |
if (!headers.ContainsKey("Accept-Encoding")) |
{ |
headers.Add("Accept-Encoding", "gzip,deflate"); |
} |
using (var response = await HTTPClient.SendAsync(request)) |
{ |
return response.IsSuccessStatusCode |
@@ -173,15 +163,10 @@ |
} |
// Add some standard headers: |
// Accept - for socially acceptable security of mod_sec |
// Accept-Encoding - since we want to use compression if possible |
if (!headers.ContainsKey("Accept")) |
{ |
headers.Add("Accept", @"*/*"); |
} |
if (!headers.ContainsKey("Accept-Encoding")) |
{ |
headers.Add("Accept-Encoding", "gzip,deflate"); |
} |
using (var response = await HTTPClient.SendAsync(request)) |
{ |
return response.IsSuccessStatusCode |
/wasSharp.csproj |
@@ -38,7 +38,7 @@ |
<Compile Include="BitTwiddling.cs" /> |
<Compile Include="Collections\Generic\CircularQueue.cs" /> |
<Compile Include="Collections\Specialized\ExtendedObservableCollection.cs" /> |
<Compile Include="Collections\Utilities\Extensions.cs" /> |
<Compile Include="Collections\Utilities\CollectionExtensions.cs" /> |
<Compile Include="Collections\Specialized\ObservableHashSet.cs" /> |
<Compile Include="Collections\Generic\RangeCollection.cs" /> |
<Compile Include="Collections\Generic\SerializableDictionary.cs" /> |
@@ -56,13 +56,13 @@ |
<Compile Include="Reflection.cs" /> |
<Compile Include="Strings.cs" /> |
<Compile Include="Timers\DecayingAlarm.cs" /> |
<Compile Include="Timers\Utilities\Extensions.cs" /> |
<Compile Include="Timers\Utilities\TimeExtensions.cs" /> |
<Compile Include="Timers\TimedThrottle.cs" /> |
<Compile Include="Timers\Timer.cs" /> |
<Compile Include="wasSharp.cs" /> |
<Compile Include="Properties\AssemblyInfo.cs" /> |
<Compile Include="Web\QValueParsing.cs" /> |
<Compile Include="Web\Utilities\Extensions.cs" /> |
<Compile Include="Web\Utilities\WebExtensions.cs" /> |
<Compile Include="Web\wasHTTPClient.cs" /> |
<Compile Include="XML.cs" /> |
</ItemGroup> |