Skip to content

Commit

Permalink
Add hooks for request filtering before deserialization or finding the…
Browse files Browse the repository at this point in the history
… path (ie before anything else has been processed)
  • Loading branch information
Andrew Robinson committed Aug 29, 2012
1 parent efbe987 commit 5dc59a5
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -45,3 +45,5 @@ src/ServiceStack.MonoTouch/.DS_Store
NuGet/
build/
*.DS_Store

*.dotCover
3 changes: 3 additions & 0 deletions src/ServiceStack.ServiceInterface/Testing/BasicAppHost.cs
Expand Up @@ -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>();
Expand Down Expand Up @@ -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; }
Expand Down
3 changes: 3 additions & 0 deletions src/ServiceStack.ServiceInterface/Testing/TestAppHost.cs
Expand Up @@ -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>();
Expand Down Expand Up @@ -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; }
Expand Down
8 changes: 8 additions & 0 deletions src/ServiceStack/WebHost.Endpoints/AppHostBase.cs
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions src/ServiceStack/WebHost.Endpoints/EndpointHost.cs
Expand Up @@ -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; }
Expand All @@ -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>();
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions src/ServiceStack/WebHost.Endpoints/IAppHost.cs
Expand Up @@ -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>
Expand Down
2 changes: 2 additions & 0 deletions src/ServiceStack/WebHost.Endpoints/RestHandler.cs
Expand Up @@ -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);
Expand Down
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tests/ServiceStack.ServiceHost.Tests/Formats/ViewTests.cs
Expand Up @@ -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; }
Expand Down

5 comments on commit 5dc59a5

@mythz
Copy link
Member

@mythz mythz commented on 5dc59a5 Aug 30, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link

@awr awr commented on 5dc59a5 Aug 31, 2012 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mythz
Copy link
Member

@mythz mythz commented on 5dc59a5 Aug 31, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Andrew,

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

Could that do what you need?

@awr
Copy link

@awr awr commented on 5dc59a5 Sep 4, 2012 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mythz
Copy link
Member

@mythz mythz commented on 5dc59a5 Sep 4, 2012 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.