From 05a0ee8ac060c8a896ad1b25ff2bcb42f5f1dee9 Mon Sep 17 00:00:00 2001 From: icedream Date: Thu, 15 May 2014 06:52:51 +0200 Subject: [PATCH] Moving classes into their own files. --- .../SessionAuthenticationResult.cs | 54 +++++++++ .../SessionAuthenticationServer.cs | 50 --------- src/libnpsharp/libnpsharp.csproj | 1 + src/npfile/NP2HTTPPublisherFileHandler.cs | 62 +++++++++++ src/npfile/NP2HTTPUserFileHandler.cs | 62 +++++++++++ src/npfile/Program.cs | 103 ------------------ src/npfile/npfile.csproj | 2 + 7 files changed, 181 insertions(+), 153 deletions(-) create mode 100644 src/libnpsharp/Authentication/SessionAuthenticationResult.cs create mode 100644 src/npfile/NP2HTTPPublisherFileHandler.cs create mode 100644 src/npfile/NP2HTTPUserFileHandler.cs diff --git a/src/libnpsharp/Authentication/SessionAuthenticationResult.cs b/src/libnpsharp/Authentication/SessionAuthenticationResult.cs new file mode 100644 index 0000000..ddfd388 --- /dev/null +++ b/src/libnpsharp/Authentication/SessionAuthenticationResult.cs @@ -0,0 +1,54 @@ +using System; + +namespace NPSharp.Authentication +{ + public class SessionAuthenticationResult + { + /// + /// true if authentication was successful, otherwise false. + /// + public bool Success { get; set; } + + /// + /// Reason for the given success state. Use this especially in authentication fail cases. + /// + public string Reason { get; set; } + + /// + /// If authenticated set this to the user's unique ID. + /// + public uint UserID { get; set; } + + /// + /// If authenticated set this to the user's session token. + /// + public string SessionToken { get; set; } + + /// + /// If authenticated set this to the actual correctly spelled username. + /// + public string UserName { get; set; } + + /// + /// If authenticated set this to the user's e-mail address. + /// + public string UserMail { get; set; } + + /// + /// Returns the response line as it should be sent out to the client. + /// + 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); + } + } +} \ No newline at end of file diff --git a/src/libnpsharp/Authentication/SessionAuthenticationServer.cs b/src/libnpsharp/Authentication/SessionAuthenticationServer.cs index aba65e0..ace5a6f 100644 --- a/src/libnpsharp/Authentication/SessionAuthenticationServer.cs +++ b/src/libnpsharp/Authentication/SessionAuthenticationServer.cs @@ -134,54 +134,4 @@ namespace NPSharp.Authentication } } } - - public class SessionAuthenticationResult - { - /// - /// true if authentication was successful, otherwise false. - /// - public bool Success { get; set; } - - /// - /// Reason for the given success state. Use this especially in authentication fail cases. - /// - public string Reason { get; set; } - - /// - /// If authenticated set this to the user's unique ID. - /// - public uint UserID { get; set; } - - /// - /// If authenticated set this to the user's session token. - /// - public string SessionToken { get; set; } - - /// - /// If authenticated set this to the actual correctly spelled username. - /// - public string UserName { get; set; } - - /// - /// If authenticated set this to the user's e-mail address. - /// - public string UserMail { get; set; } - - /// - /// Returns the response line as it should be sent out to the client. - /// - 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); - } - } } \ No newline at end of file diff --git a/src/libnpsharp/libnpsharp.csproj b/src/libnpsharp/libnpsharp.csproj index 06c9c48..c8d1801 100644 --- a/src/libnpsharp/libnpsharp.csproj +++ b/src/libnpsharp/libnpsharp.csproj @@ -64,6 +64,7 @@ + diff --git a/src/npfile/NP2HTTPPublisherFileHandler.cs b/src/npfile/NP2HTTPPublisherFileHandler.cs new file mode 100644 index 0000000..03778d3 --- /dev/null +++ b/src/npfile/NP2HTTPPublisherFileHandler.cs @@ -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 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 uri parameter with the URL." + ); + return Task.Factory.GetCompleted(); + } + + _log.InfoFormat("Requesting publisher file {0}", uri); + Task task = _np.GetPublisherFile(uri); + try + { + task.Wait(); + } + catch + { + context.Response = HttpResponse.CreateWithMessage(HttpResponseCode.NotFound, "File not accessible", + context.Request.Headers.KeepAliveConnection(), + string.Format("
{0}
", + 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(); + } + } +} \ No newline at end of file diff --git a/src/npfile/NP2HTTPUserFileHandler.cs b/src/npfile/NP2HTTPUserFileHandler.cs new file mode 100644 index 0000000..1c57e60 --- /dev/null +++ b/src/npfile/NP2HTTPUserFileHandler.cs @@ -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 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 uri parameter with the URL." + ); + return Task.Factory.GetCompleted(); + } + + _log.InfoFormat("Requesting user file {0}", uri); + Task task = _np.GetUserFile(uri); + try + { + task.Wait(); + } + catch + { + context.Response = HttpResponse.CreateWithMessage(HttpResponseCode.NotFound, "File not accessible", + context.Request.Headers.KeepAliveConnection(), + string.Format("
{0}
", + 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(); + } + } +} \ No newline at end of file diff --git a/src/npfile/Program.cs b/src/npfile/Program.cs index efa0faf..f01d1b6 100644 --- a/src/npfile/Program.cs +++ b/src/npfile/Program.cs @@ -1,11 +1,8 @@ using System; -using System.IO; -using System.Linq; using System.Net; using System.Net.Sockets; using System.Threading; using System.Threading.Tasks; -using System.Web; using log4net; using log4net.Appender; 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 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 uri parameter with the URL." - ); - return Task.Factory.GetCompleted(); - } - - _log.InfoFormat("Requesting user file {0}", uri); - Task task = _np.GetUserFile(uri); - try - { - task.Wait(); - } - catch - { - context.Response = HttpResponse.CreateWithMessage(HttpResponseCode.NotFound, "File not accessible", - context.Request.Headers.KeepAliveConnection(), - string.Format("
{0}
", - 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 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 uri parameter with the URL." - ); - return Task.Factory.GetCompleted(); - } - - _log.InfoFormat("Requesting publisher file {0}", uri); - Task task = _np.GetPublisherFile(uri); - try - { - task.Wait(); - } - catch - { - context.Response = HttpResponse.CreateWithMessage(HttpResponseCode.NotFound, "File not accessible", - context.Request.Headers.KeepAliveConnection(), - string.Format("
{0}
", - 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(); - } - } } \ No newline at end of file diff --git a/src/npfile/npfile.csproj b/src/npfile/npfile.csproj index ce483ad..9686bb2 100644 --- a/src/npfile/npfile.csproj +++ b/src/npfile/npfile.csproj @@ -53,6 +53,8 @@
+ +