11from .entities import BindingSpecification
22
33
4+ def encode_path_segment (input : str ) -> str :
5+ encoded_input = ""
6+ for character in input :
7+ if is_unreserved (character ):
8+ encoded_input += character
9+
10+ else :
11+ # encoded_input += "%%%02X" % character
12+ encoded_input += hex ((ord (character )))
13+
14+ return encoded_input
15+
16+
417def exchange_address (exchange_name : str , routing_key : str = "" ) -> str :
518 if routing_key == "" :
6- path = "/exchanges/" + exchange_name
19+ path = "/exchanges/" + encode_path_segment ( exchange_name )
720 else :
8- path = "/exchanges/" + exchange_name + "/" + routing_key
21+ path = (
22+ "/exchanges/"
23+ + encode_path_segment (exchange_name )
24+ + "/"
25+ + encode_path_segment (routing_key )
26+ )
927
1028 return path
1129
1230
13- def queue_address (name : str ) -> str :
14- path = "/queues/" + name
31+ def queue_address (queue_name : str ) -> str :
32+ path = "/queues/" + encode_path_segment ( queue_name )
1533
1634 return path
1735
1836
19- def purge_queue_address (name : str ) -> str :
20- path = "/queues/" + name + "/messages"
37+ def purge_queue_address (queue_name : str ) -> str :
38+ path = "/queues/" + encode_path_segment ( queue_name ) + "/messages"
2139
2240 return path
2341
@@ -28,17 +46,29 @@ def path_address() -> str:
2846 return path
2947
3048
49+ def is_unreserved (input : str ) -> bool :
50+ return (
51+ (input >= "A" and input <= "Z" )
52+ or (input >= "a" and input <= "z" )
53+ or (input >= "0" and input <= "9" )
54+ or input == "-"
55+ or input == "."
56+ or input == "_"
57+ or input == "~"
58+ )
59+
60+
3161def binding_path_with_exchange_queue (bind_specification : BindingSpecification ) -> str :
3262 binding_path_wth_exchange_queue_key = (
3363 "/bindings"
3464 + "/"
3565 + "src="
36- + bind_specification .source_exchange
66+ + encode_path_segment ( bind_specification .source_exchange )
3767 + ";"
3868 + "dstq="
39- + bind_specification .destination_queue
69+ + encode_path_segment ( bind_specification .destination_queue )
4070 + ";key="
41- + bind_specification .binding_key
71+ + encode_path_segment ( bind_specification .binding_key )
4272 + ";args="
4373 )
4474 return binding_path_wth_exchange_queue_key
0 commit comments