/Cryptography/SHA1.cs |
@@ -5,7 +5,9 @@ |
/////////////////////////////////////////////////////////////////////////// |
|
using System; |
using System.IO; |
using System.Text; |
using System.Threading.Tasks; |
|
namespace wasSharpNET.Cryptography |
{ |
@@ -20,9 +22,44 @@ |
return BitConverter.ToString(sha1.ComputeHash(data)).Replace("-", ""); |
} |
|
|
public static string ToHex(this System.Security.Cryptography.SHA1 sha1, string data) |
{ |
return BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(data))).Replace("-", ""); |
} |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
/////////////////////////////////////////////////////////////////////////// |
/// <summary> |
/// Compute the SHA1 hash of an array of bytes and copy the result to the output stream as a hexadecimal string. |
/// </summary> |
/// <param name="sha1">the SHA1 object to use</param> |
/// <param name="data">the data to hash</param> |
/// <param name="outStream">the output stream</param> |
public static async Task CopyToAsync(this System.Security.Cryptography.SHA1 sha1, byte[] data, Stream outStream) |
{ |
var buffer = new char[1]; |
using (MemoryStream SHA1OutputStream = new MemoryStream()) |
{ |
using (StringReader SHA1InputStream = new StringReader(BitConverter.ToString(sha1.ComputeHash(data)).Replace("-", ""))) |
{ |
int count; |
while ((count = await SHA1InputStream.ReadAsync(buffer, 0, 1)) != 0) |
{ |
switch (buffer[0]) |
{ |
case '-': |
continue; |
default: |
SHA1OutputStream.WriteByte((byte)buffer[0]); |
break; |
} |
} |
} |
SHA1OutputStream.Position = 0L; |
await SHA1OutputStream.CopyToAsync(outStream); |
} |
} |
} |
} |
/Network/HTTP/HTTPServer.cs |
@@ -14,43 +14,54 @@ |
public abstract class HTTPServer |
{ |
private int activeRequests; |
private readonly HttpListener HTTPListener = new HttpListener(); |
private readonly HttpListener HTTPListener = new HttpListener() |
{ |
AuthenticationSchemes = AuthenticationSchemes.None |
}; |
private int processedRequests; |
|
private readonly ManualResetEvent stopEvent = new ManualResetEvent(false); |
private readonly AutoResetEvent StopServerEvent = new AutoResetEvent(false); |
private readonly AutoResetEvent ServerStoppedEvent = new AutoResetEvent(false); |
|
public HashSet<string> Prefixes { get; set; } = new HashSet<string>(); |
|
public AuthenticationSchemes AuthenticationSchemes { get; set; } = AuthenticationSchemes.None; |
|
public bool IsRunning => HTTPListener.IsListening; |
|
public bool Start() |
public AuthenticationSchemes AuthenticationSchemes |
{ |
foreach (var prefix in Prefixes) |
get |
{ |
if (!HTTPListener.Prefixes.Contains(prefix)) |
HTTPListener.Prefixes.Add(prefix); |
return HTTPListener.AuthenticationSchemes; |
} |
set |
{ |
HTTPListener.AuthenticationSchemes = value; |
} |
} |
|
// Set up HTTP listener. |
HTTPListener.AuthenticationSchemes = AuthenticationSchemes; |
public bool IsRunning => HTTPListener.IsListening; |
|
public bool Start(IEnumerable<string> prefixes) |
{ |
// Do not start the HTTP server if it is already running. |
if (HTTPListener.IsListening) |
return false; |
|
// Add all prefixes. |
foreach (var prefix in prefixes) |
{ |
HTTPListener.Prefixes.Add(prefix); |
} |
|
// Do not bomb if the client disconnects. |
HTTPListener.IgnoreWriteExceptions = true; |
HTTPListener.Start(); |
var state = new HTTPServerCallbackState(HTTPListener); |
ThreadPool.QueueUserWorkItem(Listen, state); |
ThreadPool.QueueUserWorkItem(Listen, new HTTPServerCallbackState(HTTPListener)); |
return true; |
} |
|
public bool Stop() |
{ |
return stopEvent.Set(); |
StopServerEvent.Set(); |
ServerStoppedEvent.WaitOne(); |
HTTPListener.Prefixes.Clear(); |
return true; |
} |
|
private void Listen(object state) |
@@ -60,7 +71,7 @@ |
while (callbackState.Listener.IsListening) |
{ |
callbackState.Listener.BeginGetContext(ContextCallback, callbackState); |
var n = WaitHandle.WaitAny(new WaitHandle[] {callbackState.ContextRetrieved, stopEvent}); |
var n = WaitHandle.WaitAny(new WaitHandle[] {callbackState.ContextRetrieved, StopServerEvent}); |
|
if (n.Equals(1)) |
{ |
@@ -68,6 +79,8 @@ |
break; |
} |
} |
|
ServerStoppedEvent.Set(); |
} |
|
public abstract void ProcessHTTPContext(HttpListenerContext context); |