Fixed bug where root syntax wouldn't match in the RouteMatcher. Added WithHeader extension. Added preprocess and postprocess route events so we can control routes at a global level easier.
This commit is contained in:
parent
bf1b66e565
commit
bf254489b8
@ -173,5 +173,19 @@ namespace MontoyaTech.Rest.Net
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a header for a given response.
|
||||
/// </summary>
|
||||
/// <param name="response"></param>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <returns>This response.</returns>
|
||||
public static HttpListenerResponse WithHeader(this HttpListenerResponse response, string name, string value)
|
||||
{
|
||||
response.AddHeader(name, value);
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@
|
||||
<AssemblyName>MontoyaTech.Rest.Net</AssemblyName>
|
||||
<RootNamespace>MontoyaTech.Rest.Net</RootNamespace>
|
||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||
<Version>1.0.3</Version>
|
||||
<Version>1.0.5</Version>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json">
|
||||
|
@ -29,6 +29,16 @@ namespace MontoyaTech.Rest.Net
|
||||
/// </summary>
|
||||
public ushort Port = 8081;
|
||||
|
||||
/// <summary>
|
||||
/// An event to preprocess routes before the route is executed.
|
||||
/// </summary>
|
||||
public event RoutePreprocess PreprocessEvent;
|
||||
|
||||
/// <summary>
|
||||
/// An event to postprocess routes before the route response is returned.
|
||||
/// </summary>
|
||||
public event RoutePostprocess PostprocessEvent;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new RouteListener with the default port number.
|
||||
/// </summary>
|
||||
@ -111,16 +121,34 @@ namespace MontoyaTech.Rest.Net
|
||||
handled = true;
|
||||
close = this.Routes[i].CloseResponse;
|
||||
|
||||
var context = new RouteContext(ctx.Request, ctx.Response);
|
||||
|
||||
//Preprocess the route context, if it returns false, then we have to not invoke the route.
|
||||
try
|
||||
{
|
||||
if (this.PreprocessEvent != null && !this.PreprocessEvent.Invoke(context))
|
||||
break;
|
||||
}
|
||||
catch { }
|
||||
|
||||
//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);
|
||||
this.Routes[i].Invoke(context, arguments);
|
||||
}
|
||||
catch
|
||||
{
|
||||
ctx.Response.WithStatus(HttpStatusCode.InternalServerError);
|
||||
}
|
||||
|
||||
//Post process the route context.
|
||||
try
|
||||
{
|
||||
if (this.PostprocessEvent != null)
|
||||
this.PostprocessEvent.Invoke(context);
|
||||
}
|
||||
catch { }
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -41,8 +41,11 @@ namespace MontoyaTech.Rest.Net
|
||||
var urlSegments = url.Split('/').Where(segment => segment.Length > 0).Select(segment => segment.Trim()).ToArray();
|
||||
var syntaxSegments = syntax.Split('/').Where(segment => segment.Length > 0).Select(segment => segment.Trim()).ToArray();
|
||||
|
||||
//If we have no url segments, and we have no syntax segments, this is a root match which is fine.
|
||||
if (urlSegments.Length == 0 && syntaxSegments.Length == 0)
|
||||
return true;
|
||||
//If we have no syntax segments this is not a match.
|
||||
if (syntaxSegments.Length == 0)
|
||||
else if (syntaxSegments.Length == 0)
|
||||
return false;
|
||||
|
||||
//If we have segments and the url does not then this may not be a match.
|
||||
|
14
Rest.Net/Rest.Net/RoutePostprocess.cs
Normal file
14
Rest.Net/Rest.Net/RoutePostprocess.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MontoyaTech.Rest.Net
|
||||
{
|
||||
/// <summary>
|
||||
/// A route post process delegate that can be used to process the response from routes.
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public delegate void RoutePostprocess(RouteContext context);
|
||||
}
|
16
Rest.Net/Rest.Net/RoutePreprocess.cs
Normal file
16
Rest.Net/Rest.Net/RoutePreprocess.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MontoyaTech.Rest.Net
|
||||
{
|
||||
/// <summary>
|
||||
/// A route preprocess delegate that can be used to process requests before they reach a route
|
||||
/// and reject them if needed.
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public delegate bool RoutePreprocess(RouteContext context);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user