Permalink
Browse files

Add hooks for request filtering before deserialization or finding the…

… path (ie before anything else has been processed)
  • Loading branch information...
1 parent efbe987 commit 5dc59a5a7a40b7be0b4d8c5b21d11901ddfdea93 Andrew Robinson committed Aug 29, 2012
View
@@ -45,3 +45,5 @@ src/ServiceStack.MonoTouch/.DS_Store
NuGet/
build/
*.DS_Store
+
+*.dotCover
@@ -15,6 +15,7 @@ public class BasicAppHost : IAppHost
public BasicAppHost()
{
this.Container = new Container();
+ this.RawRequestFilters = new List<Action<IHttpRequest, IHttpResponse>>();
this.RequestFilters = new List<Action<IHttpRequest, IHttpResponse, object>>();
this.ResponseFilters = new List<Action<IHttpRequest, IHttpResponse, object>>();
this.ViewEngines = new List<IViewEngine>();
@@ -45,6 +46,8 @@ public T TryResolve<T>()
public IContentTypeFilter ContentTypeFilters { get; set; }
+ public List<Action<IHttpRequest, IHttpResponse>> RawRequestFilters { get; set; }
+
public List<Action<IHttpRequest, IHttpResponse, object>> RequestFilters { get; set; }
public List<Action<IHttpRequest, IHttpResponse, object>> ResponseFilters { get; set; }
@@ -31,6 +31,7 @@ public TestAppHost(Funq.Container container, params Assembly[] serviceAssemblies
new ServiceManager(true, serviceAssemblies));
this.ContentTypeFilters = new HttpResponseFilter();
+ this.RawRequestFilters = new List<Action<IHttpRequest, IHttpResponse>>();
this.RequestFilters = new List<Action<IHttpRequest, IHttpResponse, object>>();
this.ResponseFilters = new List<Action<IHttpRequest, IHttpResponse, object>>();
this.ViewEngines = new List<IViewEngine>();
@@ -59,6 +60,8 @@ public T TryResolve<T>()
public IContentTypeFilter ContentTypeFilters { get; set; }
+ public List<Action<IHttpRequest, IHttpResponse>> RawRequestFilters { get; set; }
+
public List<Action<IHttpRequest, IHttpResponse, object>> RequestFilters { get; set; }
public List<Action<IHttpRequest, IHttpResponse, object>> ResponseFilters { get; set; }
@@ -164,6 +164,14 @@ public IContentTypeFilter ContentTypeFilters
}
}
+ public List<Action<IHttpRequest, IHttpResponse>> RawRequestFilters
+ {
+ get
+ {
+ return EndpointHost.RawRequestFilters;
+ }
+ }
+
public List<Action<IHttpRequest, IHttpResponse, object>> RequestFilters
{
get
@@ -21,6 +21,8 @@ public class EndpointHost
public static IContentTypeFilter ContentTypeFilter { get; set; }
+ public static List<Action<IHttpRequest, IHttpResponse>> RawRequestFilters { get; private set; }
+
public static List<Action<IHttpRequest, IHttpResponse, object>> RequestFilters { get; private set; }
public static List<Action<IHttpRequest, IHttpResponse, object>> ResponseFilters { get; private set; }
@@ -42,6 +44,7 @@ public class EndpointHost
static EndpointHost()
{
ContentTypeFilter = HttpResponseFilter.Instance;
+ RawRequestFilters = new List<Action<IHttpRequest, IHttpResponse>>();
RequestFilters = new List<Action<IHttpRequest, IHttpResponse, object>>();
ResponseFilters = new List<Action<IHttpRequest, IHttpResponse, object>>();
ViewEngines = new List<IViewEngine>();
@@ -206,6 +209,22 @@ public static EndpointHostConfig Config
}
}
+ /// <summary>
+ /// Applies the raw request filters. Returns whether or not the request has been handled
+ /// and no more processing should be done.
+ /// </summary>
+ /// <returns></returns>
+ public static bool ApplyRawRequestFilters(IHttpRequest httpReq, IHttpResponse httpRes)
+ {
+ foreach (var requestFilter in RawRequestFilters)
+ {
+ requestFilter(httpReq, httpRes);
+ if (httpRes.IsClosed) break;
+ }
+
+ return httpRes.IsClosed;
+ }
+
/// <summary>
/// Applies the request filters. Returns whether or not the request has been handled
/// and no more processing should be done.
@@ -42,6 +42,11 @@ public interface IAppHost : IResolver
/// </summary>
IContentTypeFilter ContentTypeFilters { get; }
+ /// <summary>
+ /// Add Request Filters, to be applied before the dto is deserialized
+ /// </summary>
+ List<Action<IHttpRequest, IHttpResponse>> RawRequestFilters { get; }
+
/// <summary>
/// Add Request Filters
/// </summary>
@@ -44,6 +44,8 @@ public override void ProcessRequest(IHttpRequest httpReq, IHttpResponse httpRes,
var responseContentType = EndpointHost.Config.DefaultContentType;
try
{
+ if (EndpointHost.ApplyRawRequestFilters(httpReq, httpRes)) return;
+
var restPath = GetRestPath(httpReq.HttpMethod, httpReq.PathInfo);
if (restPath == null)
throw new NotSupportedException("No RestPath found for: " + httpReq.HttpMethod + " " + httpReq.PathInfo);
@@ -354,6 +354,14 @@ public IContentTypeFilter ContentTypeFilters
}
}
+ public List<Action<IHttpRequest, IHttpResponse>> RawRequestFilters
+ {
+ get
+ {
+ return EndpointHost.RawRequestFilters;
+ }
+ }
+
public List<Action<IHttpRequest, IHttpResponse, object>> RequestFilters
{
get
@@ -78,6 +78,8 @@ public T TryResolve<T>()
public IContentTypeFilter ContentTypeFilters { get; set; }
+ public List<Action<IHttpRequest, IHttpResponse>> RawRequestFilters { get; set; }
+
public List<Action<IHttpRequest, IHttpResponse, object>> RequestFilters { get; set; }
public List<Action<IHttpRequest, IHttpResponse, object>> ResponseFilters { get; set; }

5 comments on commit 5dc59a5

@mythz
Member
mythz commented on 5dc59a5 Aug 30, 2012

Hey Andrew,

Can you tell us why we need Another set of Request Filters here?

i.e. Why do you need something more than the Global Request Filters + Request Filter Attributes?

https://github.com/ServiceStack/ServiceStack/wiki/Request-and-response-filters
https://github.com/ServiceStack/ServiceStack/wiki/Filter-attributes

@awr
Contributor
awr commented on 5dc59a5 Aug 31, 2012
@mythz
Member
mythz commented on 5dc59a5 Aug 31, 2012

Hi Andrew,

Have you seen the IRequestLogger plugin?
https://github.com/ServiceStack/ServiceStack/wiki/Request-logger

Could that do what you need?

@awr
Contributor
awr commented on 5dc59a5 Sep 4, 2012
@mythz
Member
mythz commented on 5dc59a5 Sep 4, 2012
Please sign in to comment.