~ubuntu-branches/ubuntu/saucy/rabbitmq-server/saucy-proposed

« back to all changes in this revision

Viewing changes to plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_binding.erl

  • Committer: Package Import Robot
  • Author(s): Emile Joubert
  • Date: 2012-11-19 11:42:31 UTC
  • mfrom: (0.2.18) (0.1.32 sid)
  • Revision ID: package-import@ubuntu.com-20121119114231-hvapkn4akng09etr
Tags: 3.0.0-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
-export([init/1, resource_exists/2, to_json/2,
20
20
         content_types_provided/2, content_types_accepted/2,
21
 
         is_authorized/2, allowed_methods/2, accept_content/2,
22
 
         delete_resource/2]).
 
21
         is_authorized/2, allowed_methods/2, delete_resource/2,
 
22
         args_hash/1]).
23
23
 
24
24
-include("rabbit_mgmt.hrl").
25
25
-include_lib("webmachine/include/webmachine.hrl").
35
35
   {[{"application/json", accept_content}], ReqData, Context}.
36
36
 
37
37
allowed_methods(ReqData, Context) ->
38
 
    {['HEAD', 'GET', 'PUT', 'DELETE'], ReqData, Context}.
 
38
    {['HEAD', 'GET', 'DELETE'], ReqData, Context}.
39
39
 
40
40
resource_exists(ReqData, Context) ->
41
41
    Binding = binding(ReqData),
56
56
                           ReqData, Context)
57
57
                 end).
58
58
 
59
 
accept_content(ReqData, Context) ->
60
 
    MethodName = case rabbit_mgmt_util:destination_type(ReqData) of
61
 
                     exchange -> 'exchange.bind';
62
 
                     queue    -> 'queue.bind'
63
 
                 end,
64
 
    sync_resource(MethodName, ReqData, Context).
65
 
 
66
59
delete_resource(ReqData, Context) ->
67
60
    MethodName = case rabbit_mgmt_util:destination_type(ReqData) of
68
61
                     exchange -> 'exchange.unbind';
82
75
                     Dest = rabbit_mgmt_util:id(destination, ReqData),
83
76
                     DestType = rabbit_mgmt_util:destination_type(ReqData),
84
77
                     Props = rabbit_mgmt_util:id(props, ReqData),
85
 
                     case rabbit_mgmt_format:unpack_binding_props(Props) of
 
78
                     SName = rabbit_misc:r(VHost, exchange, Source),
 
79
                     DName = rabbit_misc:r(VHost, DestType, Dest),
 
80
                     case unpack(SName, DName, Props) of
86
81
                         {bad_request, Str} ->
87
82
                             {bad_request, Str};
88
83
                         {Key, Args} ->
89
 
                             SName = rabbit_misc:r(VHost, exchange, Source),
90
 
                             DName = rabbit_misc:r(VHost, DestType, Dest),
91
84
                             #binding{ source      = SName,
92
85
                                       destination = DName,
93
86
                                       key         = Key,
95
88
                     end
96
89
    end.
97
90
 
 
91
unpack(Src, Dst, Props) ->
 
92
    case rabbit_mgmt_format:tokenise(binary_to_list(Props)) of
 
93
        ["~"]          -> {<<>>, []};
 
94
        [Key]          -> {unquote(Key), []};
 
95
        ["~", ArgsEnc] -> lookup(<<>>, ArgsEnc, Src, Dst);
 
96
        [Key, ArgsEnc] -> lookup(unquote(Key), ArgsEnc, Src, Dst);
 
97
        _              -> {bad_request, {too_many_tokens, Props}}
 
98
    end.
 
99
 
 
100
lookup(RoutingKey, ArgsEnc, Src, Dst) ->
 
101
    lookup(RoutingKey, unquote(ArgsEnc),
 
102
           rabbit_binding:list_for_source_and_destination(Src, Dst)).
 
103
 
 
104
lookup(_RoutingKey, _Hash, []) ->
 
105
    {bad_request, "binding not found"};
 
106
lookup(RoutingKey, Hash, [#binding{args = Args} | Rest]) ->
 
107
    case args_hash(Args) =:= Hash of
 
108
        true  -> {RoutingKey, Args};
 
109
        false -> lookup(RoutingKey, Hash, Rest)
 
110
    end.
 
111
 
 
112
args_hash(Args) ->
 
113
    list_to_binary(rabbit_misc:base64url(erlang:md5(term_to_binary(Args)))).
 
114
 
 
115
unquote(Name) ->
 
116
    list_to_binary(mochiweb_util:unquote(Name)).
 
117
 
98
118
with_binding(ReqData, Context, Fun) ->
99
119
    case binding(ReqData) of
100
120
        {bad_request, Reason} ->