diff --git a/MontoyaTech.Rest.Net/MontoyaTech.Rest.Net/Extensions/HttpListenerRequestExtensions.cs b/MontoyaTech.Rest.Net/MontoyaTech.Rest.Net/Extensions/HttpListenerRequestExtensions.cs deleted file mode 100644 index 8e86649..0000000 --- a/MontoyaTech.Rest.Net/MontoyaTech.Rest.Net/Extensions/HttpListenerRequestExtensions.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Text; -using System.Threading.Tasks; -using System.IO; - -namespace MontoyaTech.Rest.Net -{ - public static class HttpListenerRequestExtensions - { - public static string ReadAsString(this HttpListenerRequest request) - { - try - { - using (var input = request.InputStream) - using (var stream = new StreamReader(input)) - return stream.ReadToEnd(); - } - catch - { - return ""; - } - } - } -} diff --git a/MontoyaTech.Rest.Net.Example/MontoyaTech.Rest.Net.Example.sln b/Rest.Net.Example/Rest.Net.Example.sln similarity index 83% rename from MontoyaTech.Rest.Net.Example/MontoyaTech.Rest.Net.Example.sln rename to Rest.Net.Example/Rest.Net.Example.sln index 18c8ec8..8d840a1 100644 --- a/MontoyaTech.Rest.Net.Example/MontoyaTech.Rest.Net.Example.sln +++ b/Rest.Net.Example/Rest.Net.Example.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.0.32112.339 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MontoyaTech.Rest.Net.Example", "MontoyaTech.Rest.Net.Example\MontoyaTech.Rest.Net.Example.csproj", "{D476199D-526A-4831-866F-790676F8BC37}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rest.Net.Example", "Rest.Net.Example\Rest.Net.Example.csproj", "{D476199D-526A-4831-866F-790676F8BC37}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/MontoyaTech.Rest.Net.Example/MontoyaTech.Rest.Net.Example/App.config b/Rest.Net.Example/Rest.Net.Example/App.config similarity index 100% rename from MontoyaTech.Rest.Net.Example/MontoyaTech.Rest.Net.Example/App.config rename to Rest.Net.Example/Rest.Net.Example/App.config diff --git a/MontoyaTech.Rest.Net.Example/MontoyaTech.Rest.Net.Example/Program.cs b/Rest.Net.Example/Rest.Net.Example/Program.cs similarity index 100% rename from MontoyaTech.Rest.Net.Example/MontoyaTech.Rest.Net.Example/Program.cs rename to Rest.Net.Example/Rest.Net.Example/Program.cs diff --git a/MontoyaTech.Rest.Net.Example/MontoyaTech.Rest.Net.Example/Properties/AssemblyInfo.cs b/Rest.Net.Example/Rest.Net.Example/Properties/AssemblyInfo.cs similarity index 100% rename from MontoyaTech.Rest.Net.Example/MontoyaTech.Rest.Net.Example/Properties/AssemblyInfo.cs rename to Rest.Net.Example/Rest.Net.Example/Properties/AssemblyInfo.cs diff --git a/MontoyaTech.Rest.Net.Example/MontoyaTech.Rest.Net.Example/MontoyaTech.Rest.Net.Example.csproj b/Rest.Net.Example/Rest.Net.Example/Rest.Net.Example.csproj similarity index 91% rename from MontoyaTech.Rest.Net.Example/MontoyaTech.Rest.Net.Example/MontoyaTech.Rest.Net.Example.csproj rename to Rest.Net.Example/Rest.Net.Example/Rest.Net.Example.csproj index cebc86a..a650230 100644 --- a/MontoyaTech.Rest.Net.Example/MontoyaTech.Rest.Net.Example/MontoyaTech.Rest.Net.Example.csproj +++ b/Rest.Net.Example/Rest.Net.Example/Rest.Net.Example.csproj @@ -33,9 +33,6 @@ 4 - - ..\..\MontoyaTech.Rest.Net\MontoyaTech.Rest.Net\bin\Debug\MontoyaTech.Rest.Net.dll - @@ -52,5 +49,11 @@ + + + {03d4578f-3239-4b12-88bb-5d877c2609d6} + Rest.Net + + \ No newline at end of file diff --git a/Rest.Net.sln b/Rest.Net.sln new file mode 100644 index 0000000..86a46f8 --- /dev/null +++ b/Rest.Net.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.32112.339 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Rest.Net", "Rest.Net\Rest.Net\Rest.Net.csproj", "{03D4578F-3239-4B12-88BB-5D877C2609D6}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rest.Net.Example", "Rest.Net.Example\Rest.Net.Example\Rest.Net.Example.csproj", "{D476199D-526A-4831-866F-790676F8BC37}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {03D4578F-3239-4B12-88BB-5D877C2609D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {03D4578F-3239-4B12-88BB-5D877C2609D6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {03D4578F-3239-4B12-88BB-5D877C2609D6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {03D4578F-3239-4B12-88BB-5D877C2609D6}.Release|Any CPU.Build.0 = Release|Any CPU + {D476199D-526A-4831-866F-790676F8BC37}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D476199D-526A-4831-866F-790676F8BC37}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D476199D-526A-4831-866F-790676F8BC37}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D476199D-526A-4831-866F-790676F8BC37}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {10D7CB88-DA0F-4A05-9A76-8FF600B040DA} + EndGlobalSection +EndGlobal diff --git a/MontoyaTech.Rest.Net/MontoyaTech.Rest.Net.sln b/Rest.Net/Rest.Net.sln similarity index 85% rename from MontoyaTech.Rest.Net/MontoyaTech.Rest.Net.sln rename to Rest.Net/Rest.Net.sln index 9856c97..022102b 100644 --- a/MontoyaTech.Rest.Net/MontoyaTech.Rest.Net.sln +++ b/Rest.Net/Rest.Net.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.0.32112.339 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MontoyaTech.Rest.Net", "MontoyaTech.Rest.Net\MontoyaTech.Rest.Net.csproj", "{C885E940-05C8-43BB-B80B-02F6AAF1AE09}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rest.Net", "Rest.Net\Rest.Net.csproj", "{C885E940-05C8-43BB-B80B-02F6AAF1AE09}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/Rest.Net/Rest.Net/Extensions/HttpListenerRequestExtensions.cs b/Rest.Net/Rest.Net/Extensions/HttpListenerRequestExtensions.cs new file mode 100644 index 0000000..6b10bdf --- /dev/null +++ b/Rest.Net/Rest.Net/Extensions/HttpListenerRequestExtensions.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using System.IO; +using Newtonsoft.Json; + +namespace MontoyaTech.Rest.Net +{ + /// + /// A set of extensions to help with HttpListenerRequests. + /// + public static class HttpListenerRequestExtensions + { + /// + /// Reads the content of a HttpListenerRequest as a string and returns it. + /// + /// + /// + public static string ReadAsString(this HttpListenerRequest request) + { + try + { + using (var input = request.InputStream) + using (var stream = new StreamReader(input)) + return stream.ReadToEnd(); + } + catch + { + return ""; + } + } + + /// + /// Reads the content of a HttpListenerRequest as a json object and returns it. + /// + /// + /// + /// + public static T ReadAsJson(this HttpListenerRequest request) + { + try + { + using (var input = request.InputStream) + using (var stream = new StreamReader(input)) + return JsonConvert.DeserializeObject(stream.ReadToEnd()); + } + catch + { + return default(T); + } + } + } +} diff --git a/MontoyaTech.Rest.Net/MontoyaTech.Rest.Net/Extensions/HttpListenerResponseExtensions.cs b/Rest.Net/Rest.Net/Extensions/HttpListenerResponseExtensions.cs similarity index 54% rename from MontoyaTech.Rest.Net/MontoyaTech.Rest.Net/Extensions/HttpListenerResponseExtensions.cs rename to Rest.Net/Rest.Net/Extensions/HttpListenerResponseExtensions.cs index cea8d06..9063e35 100644 --- a/MontoyaTech.Rest.Net/MontoyaTech.Rest.Net/Extensions/HttpListenerResponseExtensions.cs +++ b/Rest.Net/Rest.Net/Extensions/HttpListenerResponseExtensions.cs @@ -9,28 +9,49 @@ using Newtonsoft.Json; namespace MontoyaTech.Rest.Net { + /// + /// A set of extensions to help with HttpListenerResponses. + /// public static class HttpListenerResponseExtensions { + /// + /// Sets the response content type to text and writes the given text to it. + /// + /// + /// + /// public static HttpListenerResponse WithText(this HttpListenerResponse response, string text) { response.ContentType = "text/plain"; - var bytes = Encoding.UTF8.GetBytes(text); + var bytes = Encoding.Unicode.GetBytes(text); response.OutputStream.Write(bytes, 0, bytes.Length); return response; } + /// + /// Sets the response content type to json and serializes the object as json and writes it. + /// + /// + /// + /// public static HttpListenerResponse WithJson(this HttpListenerResponse response, object obj) { response.ContentType = "application/json"; - var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(obj)); + var bytes = Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(obj)); response.OutputStream.Write(bytes, 0, bytes.Length); return response; } + /// + /// Sets the response content type to a file and writes the given file content to the response. + /// + /// + /// + /// public static HttpListenerResponse WithFile(this HttpListenerResponse response, string filePath) { response.ContentType = "application/octet-stream"; @@ -38,12 +59,18 @@ namespace MontoyaTech.Rest.Net response.SendChunked = true; using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) - using (var responseStream = response.OutputStream) - fileStream.CopyTo(responseStream); + using (var responseStream = response.OutputStream) + fileStream.CopyTo(responseStream); return response; } + /// + /// Sets the status code for a given response. + /// + /// + /// + /// public static HttpListenerResponse WithStatus(this HttpListenerResponse response, HttpStatusCode status) { try diff --git a/MontoyaTech.Rest.Net/MontoyaTech.Rest.Net/Extensions/StringExtensions.cs b/Rest.Net/Rest.Net/Extensions/StringExtensions.cs similarity index 100% rename from MontoyaTech.Rest.Net/MontoyaTech.Rest.Net/Extensions/StringExtensions.cs rename to Rest.Net/Rest.Net/Extensions/StringExtensions.cs diff --git a/MontoyaTech.Rest.Net/MontoyaTech.Rest.Net/HttpRequestMethod.cs b/Rest.Net/Rest.Net/HttpRequestMethod.cs similarity index 100% rename from MontoyaTech.Rest.Net/MontoyaTech.Rest.Net/HttpRequestMethod.cs rename to Rest.Net/Rest.Net/HttpRequestMethod.cs diff --git a/MontoyaTech.Rest.Net/MontoyaTech.Rest.Net/MontoyaTech.Rest.Net.csproj b/Rest.Net/Rest.Net/Rest - Backup.Net.csproj similarity index 84% rename from MontoyaTech.Rest.Net/MontoyaTech.Rest.Net/MontoyaTech.Rest.Net.csproj rename to Rest.Net/Rest.Net/Rest - Backup.Net.csproj index 01fa4b0..a23745a 100644 --- a/MontoyaTech.Rest.Net/MontoyaTech.Rest.Net/MontoyaTech.Rest.Net.csproj +++ b/Rest.Net/Rest.Net/Rest - Backup.Net.csproj @@ -11,6 +11,10 @@ MontoyaTech;Rest.Net en MIT + MontoyaTech.Rest.Net + MontoyaTech.Rest.Net + True + 1.0.* diff --git a/Rest.Net/Rest.Net/Rest.Net.csproj b/Rest.Net/Rest.Net/Rest.Net.csproj new file mode 100644 index 0000000..dc3d9c1 --- /dev/null +++ b/Rest.Net/Rest.Net/Rest.Net.csproj @@ -0,0 +1,34 @@ + + + library + net472 + True + MontoyaTech + MontoyaTech + MontoyaTech 2022 + https://code.montoyatech.com/MontoyaTech/Rest.Net + A simple C# library for creating a rest api. + MontoyaTech;Rest.Net + en + MIT + MontoyaTech.Rest.Net + MontoyaTech.Rest.Net + True + 1.0.1 + + + + 13.0.1 + + + + + + + + + + + + + \ No newline at end of file diff --git a/MontoyaTech.Rest.Net/MontoyaTech.Rest.Net/Route.cs b/Rest.Net/Rest.Net/Route.cs similarity index 55% rename from MontoyaTech.Rest.Net/MontoyaTech.Rest.Net/Route.cs rename to Rest.Net/Rest.Net/Route.cs index 150445d..e41b9eb 100644 --- a/MontoyaTech.Rest.Net/MontoyaTech.Rest.Net/Route.cs +++ b/Rest.Net/Rest.Net/Route.cs @@ -7,19 +7,41 @@ using System.Net; namespace MontoyaTech.Rest.Net { + /// + /// The outline of a Http Route. + /// public class Route { + /// + /// The http method for this route. + /// public string Method; + /// + /// The syntax of this route. + /// public string Syntax; - private Func Target; + /// + /// The target function to invoke if this route is invoked. + /// + private Func Target; + /// + /// Whether or not to close the response after the route is invoked. + /// public bool CloseResponse = true; internal Route() { } - public Route(string method, string syntax, Func target, bool closeResponse = true) + /// + /// Creates a new route with a given method, syntax, target and optional close response flag. + /// + /// + /// + /// + /// + public Route(string method, string syntax, Func target, bool closeResponse = true) { this.Method = method; this.Syntax = syntax; @@ -27,20 +49,32 @@ namespace MontoyaTech.Rest.Net this.CloseResponse = closeResponse; } - public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) + /// + /// Creates a new route with a given method, syntax, target and optional close response flag. + /// + /// + /// + /// + /// + public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) : this(method.ToString(), syntax, target, closeResponse) { } - public virtual void Invoke(HttpListenerRequest request, HttpListenerResponse response, params string[] arguments) + /// + /// Invokes this route with a context and a given set of string arguments. + /// + /// + /// + public virtual void Invoke(RouteContext context, params string[] arguments) { - this.Target.Invoke(request, response); + this.Target.Invoke(context); } } public class Route : Route { - private Func Target; + private Func Target; - public Route(string method, string syntax, Func target, bool closeResponse = true) + public Route(string method, string syntax, Func target, bool closeResponse = true) { this.Method = method; this.Syntax = syntax; @@ -48,20 +82,20 @@ namespace MontoyaTech.Rest.Net this.CloseResponse = closeResponse; } - public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) + public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) : this(method.ToString(), syntax, target, closeResponse) { } - public override void Invoke(HttpListenerRequest request, HttpListenerResponse response, params string[] arguments) + public override void Invoke(RouteContext context, params string[] arguments) { - this.Target.DynamicInvoke(request, response, RouteArgumentConverter.Convert(arguments[0])); + this.Target.DynamicInvoke(context, RouteArgumentConverter.Convert(arguments[0])); } } public class Route : Route { - private Func Target; + private Func Target; - public Route(string method, string syntax, Func target, bool closeResponse = true) + public Route(string method, string syntax, Func target, bool closeResponse = true) { this.Method = method; this.Syntax = syntax; @@ -69,25 +103,24 @@ namespace MontoyaTech.Rest.Net this.CloseResponse = closeResponse; } - public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) + public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) : this(method.ToString(), syntax, target, closeResponse) { } - public override void Invoke(HttpListenerRequest request, HttpListenerResponse response, params string[] arguments) + public override void Invoke(RouteContext context, params string[] arguments) { this.Target.DynamicInvoke( - request, - response, + context, RouteArgumentConverter.Convert(arguments[0]), RouteArgumentConverter.Convert(arguments[1]) - ); ; + ); } } public class Route : Route { - private Func Target; + private Func Target; - public Route(string method, string syntax, Func target, bool closeResponse = true) + public Route(string method, string syntax, Func target, bool closeResponse = true) { this.Method = method; this.Syntax = syntax; @@ -95,14 +128,13 @@ namespace MontoyaTech.Rest.Net this.CloseResponse = closeResponse; } - public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) + public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) : this(method.ToString(), syntax, target, closeResponse) { } - public override void Invoke(HttpListenerRequest request, HttpListenerResponse response, params string[] arguments) + public override void Invoke(RouteContext context, params string[] arguments) { this.Target.DynamicInvoke( - request, - response, + context, RouteArgumentConverter.Convert(arguments[0]), RouteArgumentConverter.Convert(arguments[1]), RouteArgumentConverter.Convert(arguments[2]) @@ -112,9 +144,9 @@ namespace MontoyaTech.Rest.Net public class Route : Route { - private Func Target; + private Func Target; - public Route(string method, string syntax, Func target, bool closeResponse = true) + public Route(string method, string syntax, Func target, bool closeResponse = true) { this.Method = method; this.Syntax = syntax; @@ -122,14 +154,13 @@ namespace MontoyaTech.Rest.Net this.CloseResponse = closeResponse; } - public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) + public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) : this(method.ToString(), syntax, target, closeResponse) { } - public override void Invoke(HttpListenerRequest request, HttpListenerResponse response, params string[] arguments) + public override void Invoke(RouteContext context, params string[] arguments) { this.Target.DynamicInvoke( - request, - response, + context, RouteArgumentConverter.Convert(arguments[0]), RouteArgumentConverter.Convert(arguments[1]), RouteArgumentConverter.Convert(arguments[2]), @@ -140,9 +171,9 @@ namespace MontoyaTech.Rest.Net public class Route : Route { - private Func Target; + private Func Target; - public Route(string method, string syntax, Func target, bool closeResponse = true) + public Route(string method, string syntax, Func target, bool closeResponse = true) { this.Method = method; this.Syntax = syntax; @@ -150,14 +181,13 @@ namespace MontoyaTech.Rest.Net this.CloseResponse = closeResponse; } - public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) + public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) : this(method.ToString(), syntax, target, closeResponse) { } - public override void Invoke(HttpListenerRequest request, HttpListenerResponse response, params string[] arguments) + public override void Invoke(RouteContext context, params string[] arguments) { this.Target.DynamicInvoke( - request, - response, + context, RouteArgumentConverter.Convert(arguments[0]), RouteArgumentConverter.Convert(arguments[1]), RouteArgumentConverter.Convert(arguments[2]), @@ -169,9 +199,9 @@ namespace MontoyaTech.Rest.Net public class Route : Route { - private Func Target; + private Func Target; - public Route(string method, string syntax, Func target, bool closeResponse = true) + public Route(string method, string syntax, Func target, bool closeResponse = true) { this.Method = method; this.Syntax = syntax; @@ -179,14 +209,13 @@ namespace MontoyaTech.Rest.Net this.CloseResponse = closeResponse; } - public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) + public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) : this(method.ToString(), syntax, target, closeResponse) { } - public override void Invoke(HttpListenerRequest request, HttpListenerResponse response, params string[] arguments) + public override void Invoke(RouteContext context, params string[] arguments) { this.Target.DynamicInvoke( - request, - response, + context, RouteArgumentConverter.Convert(arguments[0]), RouteArgumentConverter.Convert(arguments[1]), RouteArgumentConverter.Convert(arguments[2]), @@ -199,9 +228,9 @@ namespace MontoyaTech.Rest.Net public class Route : Route { - private Func Target; + private Func Target; - public Route(string method, string syntax, Func target, bool closeResponse = true) + public Route(string method, string syntax, Func target, bool closeResponse = true) { this.Method = method; this.Syntax = syntax; @@ -209,14 +238,13 @@ namespace MontoyaTech.Rest.Net this.CloseResponse = closeResponse; } - public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) + public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) : this(method.ToString(), syntax, target, closeResponse) { } - public override void Invoke(HttpListenerRequest request, HttpListenerResponse response, params string[] arguments) + public override void Invoke(RouteContext context, params string[] arguments) { this.Target.DynamicInvoke( - request, - response, + context, RouteArgumentConverter.Convert(arguments[0]), RouteArgumentConverter.Convert(arguments[1]), RouteArgumentConverter.Convert(arguments[2]), @@ -230,9 +258,9 @@ namespace MontoyaTech.Rest.Net public class Route : Route { - private Func Target; + private Func Target; - public Route(string method, string syntax, Func target, bool closeResponse = true) + public Route(string method, string syntax, Func target, bool closeResponse = true) { this.Method = method; this.Syntax = syntax; @@ -240,14 +268,13 @@ namespace MontoyaTech.Rest.Net this.CloseResponse = closeResponse; } - public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) + public Route(HttpRequestMethod method, string syntax, Func target, bool closeResponse = true) : this(method.ToString(), syntax, target, closeResponse) { } - public override void Invoke(HttpListenerRequest request, HttpListenerResponse response, params string[] arguments) + public override void Invoke(RouteContext context, params string[] arguments) { this.Target.DynamicInvoke( - request, - response, + context, RouteArgumentConverter.Convert(arguments[0]), RouteArgumentConverter.Convert(arguments[1]), RouteArgumentConverter.Convert(arguments[2]), diff --git a/MontoyaTech.Rest.Net/MontoyaTech.Rest.Net/RouteArgumentConverter.cs b/Rest.Net/Rest.Net/RouteArgumentConverter.cs similarity index 89% rename from MontoyaTech.Rest.Net/MontoyaTech.Rest.Net/RouteArgumentConverter.cs rename to Rest.Net/Rest.Net/RouteArgumentConverter.cs index 04bf308..9bb4f67 100644 --- a/MontoyaTech.Rest.Net/MontoyaTech.Rest.Net/RouteArgumentConverter.cs +++ b/Rest.Net/Rest.Net/RouteArgumentConverter.cs @@ -6,8 +6,17 @@ using System.Threading.Tasks; namespace MontoyaTech.Rest.Net { - public class RouteArgumentConverter + /// + /// A class to help convert route arguments. + /// + internal class RouteArgumentConverter { + /// + /// Converts a string to a given type if possible. Otherwise returns default of T. + /// + /// + /// + /// public static T Convert(string input) { var typeCode = Type.GetTypeCode(typeof(T)); diff --git a/Rest.Net/Rest.Net/RouteContext.cs b/Rest.Net/Rest.Net/RouteContext.cs new file mode 100644 index 0000000..c1eb3ef --- /dev/null +++ b/Rest.Net/Rest.Net/RouteContext.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Net; + +namespace MontoyaTech.Rest.Net +{ + /// + /// An outline of a Route Context which includes + /// the request and response for a route. + /// + public class RouteContext + { + /// + /// The Http Request that requested this route. + /// + public HttpListenerRequest Request = null; + + /// + /// The Http Response for this route. + /// + public HttpListenerResponse Response = null; + + /// + /// Creates a new RouteContext with a given request and response. + /// + /// + /// + public RouteContext(HttpListenerRequest request, HttpListenerResponse response) + { + this.Request = request; + this.Response = response; + } + } +} diff --git a/MontoyaTech.Rest.Net/MontoyaTech.Rest.Net/RouteListener.cs b/Rest.Net/Rest.Net/RouteListener.cs similarity index 59% rename from MontoyaTech.Rest.Net/MontoyaTech.Rest.Net/RouteListener.cs rename to Rest.Net/Rest.Net/RouteListener.cs index a728181..53c9c44 100644 --- a/MontoyaTech.Rest.Net/MontoyaTech.Rest.Net/RouteListener.cs +++ b/Rest.Net/Rest.Net/RouteListener.cs @@ -8,24 +8,49 @@ using System.Threading; namespace MontoyaTech.Rest.Net { + /// + /// The outline of a Route listener that listens for http requests and invokes + /// matching routes. + /// public class RouteListener { + /// + /// The internal http listener. + /// private HttpListener HttpListener = null; - + + /// + /// The list of routes this RouteListener is listening for. + /// public List Routes = new List(); + /// + /// The port this RouteListener is listening on. + /// public ushort Port = 8081; + /// + /// Creates a new RouteListener with the default port number. + /// public RouteListener() { } + /// + /// Creates a nwe RouteListener with a series of routes. + /// + /// The routes to listen for. public RouteListener(params Route[] routes) { this.Routes = routes.ToList(); } + /// + /// Creates a new RouteListener with a port to listen on and a series of routes. + /// + /// The port number the listener should use. + /// The routes to listen for. public RouteListener(ushort port, params Route[] routes) { this.Routes = routes.ToList(); @@ -33,12 +58,16 @@ namespace MontoyaTech.Rest.Net this.Port = port; } + /// + /// Starts this RouteListener if it's not already running. + /// public void Start() { if (this.HttpListener == null) { this.HttpListener = new HttpListener(); + //Support listening on Windows & Linux. if (Environment.OSVersion.Platform == PlatformID.Win32NT) { this.HttpListener.Prefixes.Add($"http://localhost:{this.Port}/"); @@ -55,6 +84,9 @@ namespace MontoyaTech.Rest.Net } } + /// + /// Function that does the actual listening. + /// private void Listen() { ThreadPool.QueueUserWorkItem((o) => @@ -78,7 +110,17 @@ namespace MontoyaTech.Rest.Net { handled = true; close = this.Routes[i].CloseResponse; - this.Routes[i].Invoke(ctx.Request, ctx.Response, arguments); + + //Make sure if the route fails we don't die here, just set the response to internal server error. + try + { + this.Routes[i].Invoke(new RouteContext(ctx.Request, ctx.Response), arguments); + } + catch + { + ctx.Response.WithStatus(HttpStatusCode.InternalServerError); + } + break; } } @@ -101,16 +143,29 @@ namespace MontoyaTech.Rest.Net }); } + /// + /// Stops this RouteListener if it's running. + /// public void Stop() { if (this.HttpListener != null) { - this.HttpListener.Stop(); + try + { + this.HttpListener.Stop(); - this.HttpListener = null; + this.HttpListener = null; + } + catch + { + this.HttpListener = null; + } } } + /// + /// Blocks the current thread indenfinitly while this RouteListner is running. + /// public void Block() { while (this.HttpListener != null) diff --git a/MontoyaTech.Rest.Net/MontoyaTech.Rest.Net/RouteMatcher.cs b/Rest.Net/Rest.Net/RouteMatcher.cs similarity index 98% rename from MontoyaTech.Rest.Net/MontoyaTech.Rest.Net/RouteMatcher.cs rename to Rest.Net/Rest.Net/RouteMatcher.cs index ed88b8f..5e802b9 100644 --- a/MontoyaTech.Rest.Net/MontoyaTech.Rest.Net/RouteMatcher.cs +++ b/Rest.Net/Rest.Net/RouteMatcher.cs @@ -6,6 +6,9 @@ using System.Threading.Tasks; namespace MontoyaTech.Rest.Net { + /// + /// A class to help match route syntaxs against requests. + /// public class RouteMatcher { ///