Permalink
Browse files

fix resource filtering in host-based dispatch rules

It looks like webmachine_router:remove_resource/1 never worked on
host-based dispatch lines because it was attempting to match {Host,
Path}, instead of {Host, [Path, ...]}.  This patch changes the filter to
delve into those per-host dispatch lists to apply the filter correctly
there as well.
  • Loading branch information...
1 parent 1c814dc commit d0cdee30e467a6cd0182f2c11263482bc41d8f53 Bryan Fink committed May 6, 2012
Showing with 16 additions and 18 deletions.
  1. +16 −18 src/webmachine_router.erl
View
@@ -95,8 +95,7 @@ handle_call(get_routes, _From, State) ->
{reply, get_dispatch_list(), State};
handle_call({remove_resource, Resource}, _From, State) ->
- DL = [D || D <- get_dispatch_list(),
- filter_by_resource(D, Resource)],
+ DL = filter_by_resource(Resource, get_dispatch_list()),
{reply, set_dispatch_list(DL), State};
handle_call({remove_route, Route}, _From, State) ->
@@ -129,22 +128,21 @@ code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%% Internal functions
-filter_by_resource({_, {_, Resource, _}}, Resource) ->
- false;
-filter_by_resource({_, {_, _, _}}, _Resource) ->
- true;
-filter_by_resource({_, Resource, _}, Resource) ->
- false;
-filter_by_resource({_, _, _}, _Resource) ->
- true;
-filter_by_resource({_,_,Resource,_}, Resource) ->
- false;
-filter_by_resource({_,_,_,_}, _Resource) ->
- true;
-filter_by_resource({_,{_,_,Resource,_}}, Resource) ->
- false;
-filter_by_resource({_,{_,_,_,_}}, _Resource) ->
- true.
+
+%% @doc Remove any dispatch rule that directs requests to `Resource'
+filter_by_resource(Resource, Dispatch) ->
+ lists:foldr(filter_by_resource(Resource), [], Dispatch).
+
+filter_by_resource(Resource) ->
+ fun({_, R, _}, Acc) when R == Resource -> % basic dispatch
+ Acc;
+ ({_, _, R, _}, Acc) when R == Resource -> % guarded dispatch
+ Acc;
+ ({Host, Disp}, Acc) -> % host-based dispatch
+ [{Host, filter_by_resource(Resource, Disp)}|Acc];
+ (Other, Acc) -> % dispatch not mentioning this resource
+ [Other|Acc]
+ end.
get_dispatch_list() ->
case application:get_env(webmachine, dispatch_list) of

0 comments on commit d0cdee3

Please sign in to comment.