11from .entities import BindingSpecification
22
33
4+ def is_unreserved (char : str ) -> bool :
5+ # According to RFC 3986, unreserved characters are A-Z, a-z, 0-9, '-', '.', '_', and '~'
6+ return char .isalnum () or char in "-._~"
7+
8+
9+ def encode_path_segment (input_string : str ) -> str :
10+ encoded = []
11+
12+ # Iterate over each character in the input string
13+ for char in input_string :
14+ # Check if the character is an unreserved character
15+ if is_unreserved (char ):
16+ encoded .append (char ) # Append as is
17+ else :
18+ # Encode character to %HH format
19+ encoded .append (f"%{ ord (char ):02X} " )
20+
21+ return "" .join (encoded )
22+
23+
424def exchange_address (exchange_name : str , routing_key : str = "" ) -> str :
525 if routing_key == "" :
6- path = "/exchanges/" + exchange_name
26+ path = "/exchanges/" + encode_path_segment ( exchange_name )
727 else :
8- path = "/exchanges/" + exchange_name + "/" + routing_key
28+ path = (
29+ "/exchanges/"
30+ + encode_path_segment (exchange_name )
31+ + "/"
32+ + encode_path_segment (routing_key )
33+ )
934
1035 return path
1136
1237
13- def queue_address (name : str ) -> str :
14- path = "/queues/" + name
38+ def queue_address (queue_name : str ) -> str :
39+ path = "/queues/" + encode_path_segment ( queue_name )
1540
1641 return path
1742
1843
19- def purge_queue_address (name : str ) -> str :
20- path = "/queues/" + name + "/messages"
44+ def purge_queue_address (queue_name : str ) -> str :
45+ path = "/queues/" + encode_path_segment ( queue_name ) + "/messages"
2146
2247 return path
2348
@@ -33,12 +58,12 @@ def binding_path_with_exchange_queue(bind_specification: BindingSpecification) -
3358 "/bindings"
3459 + "/"
3560 + "src="
36- + bind_specification .source_exchange
61+ + encode_path_segment ( bind_specification .source_exchange )
3762 + ";"
3863 + "dstq="
39- + bind_specification .destination_queue
64+ + encode_path_segment ( bind_specification .destination_queue )
4065 + ";key="
41- + bind_specification .binding_key
66+ + encode_path_segment ( bind_specification .binding_key )
4267 + ";args="
4368 )
4469 return binding_path_wth_exchange_queue_key
0 commit comments