Moving classes into their own files.

feature-npv2
Icedream 2014-05-15 06:52:51 +02:00
parent 20ae47ef0f
commit 05a0ee8ac0
7 changed files with 181 additions and 153 deletions

View File

@ -0,0 +1,54 @@
using System;
namespace NPSharp.Authentication
{
public class SessionAuthenticationResult
{
/// <summary>
/// true if authentication was successful, otherwise false.
/// </summary>
public bool Success { get; set; }
/// <summary>
/// Reason for the given success state. Use this especially in authentication fail cases.
/// </summary>
public string Reason { get; set; }
/// <summary>
/// If authenticated set this to the user's unique ID.
/// </summary>
public uint UserID { get; set; }
/// <summary>
/// If authenticated set this to the user's session token.
/// </summary>
public string SessionToken { get; set; }
/// <summary>
/// If authenticated set this to the actual correctly spelled username.
/// </summary>
public string UserName { get; set; }
/// <summary>
/// If authenticated set this to the user's e-mail address.
/// </summary>
public string UserMail { get; set; }
/// <summary>
/// Returns the response line as it should be sent out to the client.
/// </summary>
public override string ToString()
{
// Response will be in this syntax:
// (ok|fail)#text#userid#username#email#sessiontoken
return String.Join("#",
Success ? "ok" : "fail",
String.IsNullOrEmpty(Reason) ? (Success ? "Success" : "Unknown error") : Reason,
UserID,
string.IsNullOrEmpty(UserName) ? "Anonymous" : UserName,
string.IsNullOrEmpty(UserMail) ? "anonymous@localhost" : UserMail,
string.IsNullOrEmpty(SessionToken) ? "0" : SessionToken,
String.Empty);
}
}
}

View File

@ -134,54 +134,4 @@ namespace NPSharp.Authentication
} }
} }
} }
public class SessionAuthenticationResult
{
/// <summary>
/// true if authentication was successful, otherwise false.
/// </summary>
public bool Success { get; set; }
/// <summary>
/// Reason for the given success state. Use this especially in authentication fail cases.
/// </summary>
public string Reason { get; set; }
/// <summary>
/// If authenticated set this to the user's unique ID.
/// </summary>
public uint UserID { get; set; }
/// <summary>
/// If authenticated set this to the user's session token.
/// </summary>
public string SessionToken { get; set; }
/// <summary>
/// If authenticated set this to the actual correctly spelled username.
/// </summary>
public string UserName { get; set; }
/// <summary>
/// If authenticated set this to the user's e-mail address.
/// </summary>
public string UserMail { get; set; }
/// <summary>
/// Returns the response line as it should be sent out to the client.
/// </summary>
public override string ToString()
{
// Response will be in this syntax:
// (ok|fail)#text#userid#username#email#sessiontoken
return String.Join("#",
Success ? "ok" : "fail",
String.IsNullOrEmpty(Reason) ? (Success ? "Success" : "Unknown error") : Reason,
UserID,
string.IsNullOrEmpty(UserName) ? "Anonymous" : UserName,
string.IsNullOrEmpty(UserMail) ? "anonymous@localhost" : UserMail,
string.IsNullOrEmpty(SessionToken) ? "0" : SessionToken,
String.Empty);
}
}
} }

View File

@ -64,6 +64,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="AuthenticationResult.cs" /> <Compile Include="AuthenticationResult.cs" />
<Compile Include="Authentication\SessionAuthenticationResult.cs" />
<Compile Include="Authentication\SessionAuthenticationServer.cs" /> <Compile Include="Authentication\SessionAuthenticationServer.cs" />
<Compile Include="ClientEventArgs.cs" /> <Compile Include="ClientEventArgs.cs" />
<Compile Include="ClientEventHandler.cs" /> <Compile Include="ClientEventHandler.cs" />

View File

@ -0,0 +1,62 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using log4net;
using uhttpsharp;
using uhttpsharp.Headers;
using HttpResponse = uhttpsharp.HttpResponse;
namespace NPSharp.CommandLine.File
{
internal class NP2HTTPPublisherFileHandler : IHttpRequestHandler
{
private readonly ILog _log;
private readonly NPClient _np;
public NP2HTTPPublisherFileHandler(NPClient np)
{
_np = np;
_log = LogManager.GetLogger(GetType());
}
public Task Handle(IHttpContext context, Func<Task> next)
{
string uri = context.Request.QueryString.Any()
? null
: string.Join("/", context.Request.Uri.OriginalString.Split('/').Skip(2));
if (uri == null)
if (!context.Request.QueryString.TryGetByName("uri", out uri) || uri == null)
{
context.Response = HttpResponse.CreateWithMessage(HttpResponseCode.NotFound, "Invalid request",
context.Request.Headers.KeepAliveConnection(),
"You need to provide a <code>uri</code> parameter with the URL."
);
return Task.Factory.GetCompleted();
}
_log.InfoFormat("Requesting publisher file {0}", uri);
Task<byte[]> task = _np.GetPublisherFile(uri);
try
{
task.Wait();
}
catch
{
context.Response = HttpResponse.CreateWithMessage(HttpResponseCode.NotFound, "File not accessible",
context.Request.Headers.KeepAliveConnection(),
string.Format("<pre><tt><code>{0}</code></tt></pre>",
task.Exception == null ? "Unknown error" : task.Exception.ToString())
);
return Task.Factory.GetCompleted();
}
// Return file contents
context.Response = new HttpResponse(HttpResponseCode.Ok, MimeMapping.GetMimeMapping(uri),
new MemoryStream(task.Result), context.Request.Headers.KeepAliveConnection());
return Task.Factory.GetCompleted();
}
}
}

View File

@ -0,0 +1,62 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using log4net;
using uhttpsharp;
using uhttpsharp.Headers;
using HttpResponse = uhttpsharp.HttpResponse;
namespace NPSharp.CommandLine.File
{
internal class NP2HTTPUserFileHandler : IHttpRequestHandler
{
private readonly ILog _log;
private readonly NPClient _np;
public NP2HTTPUserFileHandler(NPClient np)
{
_np = np;
_log = LogManager.GetLogger(GetType());
}
public Task Handle(IHttpContext context, Func<Task> next)
{
string uri = context.Request.QueryString.Any()
? null
: string.Join("/", context.Request.Uri.OriginalString.Split('/').Skip(2));
if (uri == null)
if (!context.Request.QueryString.TryGetByName("uri", out uri) || uri == null)
{
context.Response = HttpResponse.CreateWithMessage(HttpResponseCode.NotFound, "Invalid request",
context.Request.Headers.KeepAliveConnection(),
"You need to provide a <code>uri</code> parameter with the URL."
);
return Task.Factory.GetCompleted();
}
_log.InfoFormat("Requesting user file {0}", uri);
Task<byte[]> task = _np.GetUserFile(uri);
try
{
task.Wait();
}
catch
{
context.Response = HttpResponse.CreateWithMessage(HttpResponseCode.NotFound, "File not accessible",
context.Request.Headers.KeepAliveConnection(),
string.Format("<pre><tt><code>{0}</code></tt></pre>",
task.Exception == null ? "Unknown error" : task.Exception.ToString())
);
return Task.Factory.GetCompleted();
}
// Return file contents
context.Response = new HttpResponse(HttpResponseCode.Ok, MimeMapping.GetMimeMapping(uri),
new MemoryStream(task.Result), context.Request.Headers.KeepAliveConnection());
return Task.Factory.GetCompleted();
}
}
}

View File

@ -1,11 +1,8 @@
using System; using System;
using System.IO;
using System.Linq;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Web;
using log4net; using log4net;
using log4net.Appender; using log4net.Appender;
using log4net.Config; using log4net.Config;
@ -154,104 +151,4 @@ namespace NPSharp.CommandLine.File
} }
} }
} }
internal class NP2HTTPUserFileHandler : IHttpRequestHandler
{
private readonly ILog _log;
private readonly NPClient _np;
public NP2HTTPUserFileHandler(NPClient np)
{
_np = np;
_log = LogManager.GetLogger(GetType());
}
public Task Handle(IHttpContext context, Func<Task> next)
{
string uri = context.Request.QueryString.Any()
? null
: string.Join("/", context.Request.Uri.OriginalString.Split('/').Skip(2));
if (uri == null)
if (!context.Request.QueryString.TryGetByName("uri", out uri) || uri == null)
{
context.Response = HttpResponse.CreateWithMessage(HttpResponseCode.NotFound, "Invalid request",
context.Request.Headers.KeepAliveConnection(),
"You need to provide a <code>uri</code> parameter with the URL."
);
return Task.Factory.GetCompleted();
}
_log.InfoFormat("Requesting user file {0}", uri);
Task<byte[]> task = _np.GetUserFile(uri);
try
{
task.Wait();
}
catch
{
context.Response = HttpResponse.CreateWithMessage(HttpResponseCode.NotFound, "File not accessible",
context.Request.Headers.KeepAliveConnection(),
string.Format("<pre><tt><code>{0}</code></tt></pre>",
task.Exception == null ? "Unknown error" : task.Exception.ToString())
);
return Task.Factory.GetCompleted();
}
// Return file contents
context.Response = new HttpResponse(HttpResponseCode.Ok, MimeMapping.GetMimeMapping(uri),
new MemoryStream(task.Result), context.Request.Headers.KeepAliveConnection());
return Task.Factory.GetCompleted();
}
}
internal class NP2HTTPPublisherFileHandler : IHttpRequestHandler
{
private readonly ILog _log;
private readonly NPClient _np;
public NP2HTTPPublisherFileHandler(NPClient np)
{
_np = np;
_log = LogManager.GetLogger(GetType());
}
public Task Handle(IHttpContext context, Func<Task> next)
{
string uri = context.Request.QueryString.Any()
? null
: string.Join("/", context.Request.Uri.OriginalString.Split('/').Skip(2));
if (uri == null)
if (!context.Request.QueryString.TryGetByName("uri", out uri) || uri == null)
{
context.Response = HttpResponse.CreateWithMessage(HttpResponseCode.NotFound, "Invalid request",
context.Request.Headers.KeepAliveConnection(),
"You need to provide a <code>uri</code> parameter with the URL."
);
return Task.Factory.GetCompleted();
}
_log.InfoFormat("Requesting publisher file {0}", uri);
Task<byte[]> task = _np.GetPublisherFile(uri);
try
{
task.Wait();
}
catch
{
context.Response = HttpResponse.CreateWithMessage(HttpResponseCode.NotFound, "File not accessible",
context.Request.Headers.KeepAliveConnection(),
string.Format("<pre><tt><code>{0}</code></tt></pre>",
task.Exception == null ? "Unknown error" : task.Exception.ToString())
);
return Task.Factory.GetCompleted();
}
// Return file contents
context.Response = new HttpResponse(HttpResponseCode.Ok, MimeMapping.GetMimeMapping(uri),
new MemoryStream(task.Result), context.Request.Headers.KeepAliveConnection());
return Task.Factory.GetCompleted();
}
}
} }

View File

@ -53,6 +53,8 @@
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="NP2HTTPPublisherFileHandler.cs" />
<Compile Include="NP2HTTPUserFileHandler.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>