diff --git a/Rest.Net.Example/Program.cs b/Rest.Net.Example/Program.cs index 441f67b..d9523fc 100644 --- a/Rest.Net.Example/Program.cs +++ b/Rest.Net.Example/Program.cs @@ -34,12 +34,12 @@ namespace MontoyaTech.Rest.Net.Example new Route(HttpRequestMethod.Get, "/json", Json) ); - listener.RequestPreProcessEvent += (RouteListenerContext context) => { + listener.RequestPreProcessEvent += (HttpListenerContext context) => { Console.WriteLine("Request start: " + context.Request.RawUrl); return true; }; - listener.RequestPostProcessEvent += (RouteListenerContext context) => + listener.RequestPostProcessEvent += (HttpListenerContext context) => { Console.WriteLine("Request end: " + context.Request.RawUrl); }; @@ -56,22 +56,22 @@ namespace MontoyaTech.Rest.Net.Example listener.Block(); } - public static HttpListenerResponse Status(RouteListenerContext context) + public static HttpListenerResponse Status(HttpListenerContext context) { return context.Response.WithStatus(HttpStatusCode.OK).WithText("Everything is operational. 👍"); } - public static HttpListenerResponse Add(RouteListenerContext context, double a, double b) + public static HttpListenerResponse Add(HttpListenerContext context, double a, double b) { return context.Response.WithStatus(HttpStatusCode.OK).WithText((a + b).ToString()); } - public static HttpListenerResponse Signup(RouteListenerContext context, User user) + public static HttpListenerResponse Signup(HttpListenerContext context, User user) { return context.Response.WithStatus(HttpStatusCode.OK).WithText("User:" + user.Name); } - public static HttpListenerResponse Json(RouteListenerContext context) + public static HttpListenerResponse Json(HttpListenerContext context) { return context.Response.WithStatus(HttpStatusCode.OK).WithJson(new User("Rest.Net")); } diff --git a/Rest.Net/RequestPostProcessEventHandler.cs b/Rest.Net/RequestPostProcessEventHandler.cs index 7d59212..c5176f4 100644 --- a/Rest.Net/RequestPostProcessEventHandler.cs +++ b/Rest.Net/RequestPostProcessEventHandler.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Net; namespace MontoyaTech.Rest.Net { @@ -10,5 +11,5 @@ namespace MontoyaTech.Rest.Net /// A delegate to post process requests. /// /// - public delegate void RequestPostProcessEventHandler(RouteListenerContext context); + public delegate void RequestPostProcessEventHandler(HttpListenerContext context); } diff --git a/Rest.Net/RequestPreProcessEventHandler.cs b/Rest.Net/RequestPreProcessEventHandler.cs index 600df49..cb34db5 100644 --- a/Rest.Net/RequestPreProcessEventHandler.cs +++ b/Rest.Net/RequestPreProcessEventHandler.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Net; namespace MontoyaTech.Rest.Net { @@ -11,5 +12,5 @@ namespace MontoyaTech.Rest.Net /// /// /// - public delegate bool RequestPreProcessEventHandler(RouteListenerContext context); + public delegate bool RequestPreProcessEventHandler(HttpListenerContext context); } diff --git a/Rest.Net/Rest.Net.csproj b/Rest.Net/Rest.Net.csproj index f6afe83..c3796f7 100644 --- a/Rest.Net/Rest.Net.csproj +++ b/Rest.Net/Rest.Net.csproj @@ -17,7 +17,7 @@ MontoyaTech.Rest.Net MontoyaTech.Rest.Net True - 1.1.5 + 1.1.6 Upgraded to latest version of Newtonsoft.Json Logo_Symbol_Black_Outline.png diff --git a/Rest.Net/Route.cs b/Rest.Net/Route.cs index edf9746..784f67a 100644 --- a/Rest.Net/Route.cs +++ b/Rest.Net/Route.cs @@ -25,13 +25,16 @@ namespace MontoyaTech.Rest.Net /// /// The target function to invoke if this route is invoked. /// - private Func Target; + private Func Target; /// /// Whether or not to close the response after the route is invoked. /// public bool CloseResponse = true; + /// + /// Creates a default Route. + /// internal Route() { } /// @@ -42,7 +45,7 @@ namespace MontoyaTech.Rest.Net /// /// /// - 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; @@ -58,7 +61,7 @@ namespace MontoyaTech.Rest.Net /// /// /// - 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) { } /// @@ -110,7 +113,7 @@ namespace MontoyaTech.Rest.Net /// /// /// - public virtual void Invoke(RouteListenerContext context, params string[] arguments) + public virtual void Invoke(HttpListenerContext context, params string[] arguments) { this.Target.Invoke(context); } @@ -118,9 +121,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; @@ -128,10 +131,10 @@ 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(RouteListenerContext context, params string[] arguments) + public override void Invoke(HttpListenerContext context, params string[] arguments) { this.Target.DynamicInvoke(context, RouteArgumentConverter.Convert(arguments[0])); } @@ -139,9 +142,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; @@ -149,10 +152,10 @@ 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(RouteListenerContext context, params string[] arguments) + public override void Invoke(HttpListenerContext context, params string[] arguments) { this.Target.DynamicInvoke( context, @@ -164,9 +167,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; @@ -174,10 +177,10 @@ 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(RouteListenerContext context, params string[] arguments) + public override void Invoke(HttpListenerContext context, params string[] arguments) { this.Target.DynamicInvoke( context, @@ -190,9 +193,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; @@ -200,10 +203,10 @@ 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(RouteListenerContext context, params string[] arguments) + public override void Invoke(HttpListenerContext context, params string[] arguments) { this.Target.DynamicInvoke( context, @@ -217,9 +220,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; @@ -227,10 +230,10 @@ 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(RouteListenerContext context, params string[] arguments) + public override void Invoke(HttpListenerContext context, params string[] arguments) { this.Target.DynamicInvoke( context, @@ -245,9 +248,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; @@ -255,10 +258,10 @@ 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(RouteListenerContext context, params string[] arguments) + public override void Invoke(HttpListenerContext context, params string[] arguments) { this.Target.DynamicInvoke( context, @@ -274,9 +277,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; @@ -284,10 +287,10 @@ 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(RouteListenerContext context, params string[] arguments) + public override void Invoke(HttpListenerContext context, params string[] arguments) { this.Target.DynamicInvoke( context, @@ -304,9 +307,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; @@ -314,10 +317,10 @@ 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(RouteListenerContext context, params string[] arguments) + public override void Invoke(HttpListenerContext context, params string[] arguments) { this.Target.DynamicInvoke( context, diff --git a/Rest.Net/RouteExceptionEventHandler.cs b/Rest.Net/RouteExceptionEventHandler.cs index 3b53bdc..b538d3f 100644 --- a/Rest.Net/RouteExceptionEventHandler.cs +++ b/Rest.Net/RouteExceptionEventHandler.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Text; using System.Threading.Tasks; @@ -12,5 +13,5 @@ namespace MontoyaTech.Rest.Net /// /// /// - public delegate void RouteExceptionEventHandler(Route route, RouteListenerContext context, Exception ex); + public delegate void RouteExceptionEventHandler(Route route, HttpListenerContext context, Exception ex); } diff --git a/Rest.Net/RouteListener.cs b/Rest.Net/RouteListener.cs index aaf420d..60ab12f 100644 --- a/Rest.Net/RouteListener.cs +++ b/Rest.Net/RouteListener.cs @@ -113,7 +113,7 @@ namespace MontoyaTech.Rest.Net { ThreadPool.QueueUserWorkItem((item) => { - var ctx = item as HttpListenerContext; + var context = item as HttpListenerContext; try { @@ -121,8 +121,6 @@ namespace MontoyaTech.Rest.Net bool close = true; string[] arguments = null; - var context = new RouteListenerContext(ctx.Request, ctx.Response); - //Preprocess the route context, if it returns false, then we have to not invoke the route. try { @@ -133,7 +131,7 @@ namespace MontoyaTech.Rest.Net for (int i = 0; i < this.Routes.Count && !handled; i++) { - if (this.Routes[i].Method.ToUpper() == ctx.Request.HttpMethod.ToUpper() && RouteMatcher.Matches(ctx.Request.Url.AbsolutePath, this.Routes[i].Syntax, out arguments)) + if (this.Routes[i].Method.ToUpper() == context.Request.HttpMethod.ToUpper() && RouteMatcher.Matches(context.Request.Url.AbsolutePath, this.Routes[i].Syntax, out arguments)) { handled = true; close = this.Routes[i].CloseResponse; @@ -148,7 +146,7 @@ namespace MontoyaTech.Rest.Net if (this.RouteExceptionEvent != null) this.RouteExceptionEvent.Invoke(this.Routes[i], context, ex); - ctx.Response.WithStatus(HttpStatusCode.InternalServerError); + context.Response.WithStatus(HttpStatusCode.InternalServerError); } break; @@ -164,15 +162,15 @@ namespace MontoyaTech.Rest.Net catch { } if (!handled) - ctx.Response.WithStatus(HttpStatusCode.NotFound); + context.Response.WithStatus(HttpStatusCode.NotFound); if (close) - ctx.Response.Close(); + context.Response.Close(); } catch (Exception ex) { - ctx.Response.WithStatus(HttpStatusCode.InternalServerError); - ctx.Response.Close(); + context.Response.WithStatus(HttpStatusCode.InternalServerError); + context.Response.Close(); } }, this.HttpListener.GetContext()); } diff --git a/Rest.Net/RouteListenerContext.cs b/Rest.Net/RouteListenerContext.cs deleted file mode 100644 index 00c789c..0000000 --- a/Rest.Net/RouteListenerContext.cs +++ /dev/null @@ -1,37 +0,0 @@ -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 Listener Context which includes - /// the given request and a resposne. - /// - public class RouteListenerContext - { - /// - /// The Http Request that requested this route. - /// - public HttpListenerRequest Request = null; - - /// - /// The Http Response for this route. - /// - public HttpListenerResponse Response = null; - - /// - /// Creates a new ListenerContext with a given request and response. - /// - /// - /// - public RouteListenerContext(HttpListenerRequest request, HttpListenerResponse response) - { - this.Request = request; - this.Response = response; - } - } -}