1
1
from .entities import BindingSpecification
2
2
3
3
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
+
4
24
def exchange_address (exchange_name : str , routing_key : str = "" ) -> str :
5
25
if routing_key == "" :
6
- path = "/exchanges/" + exchange_name
26
+ path = "/exchanges/" + encode_path_segment ( exchange_name )
7
27
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
+ )
9
34
10
35
return path
11
36
12
37
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 )
15
40
16
41
return path
17
42
18
43
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"
21
46
22
47
return path
23
48
@@ -33,12 +58,12 @@ def binding_path_with_exchange_queue(bind_specification: BindingSpecification) -
33
58
"/bindings"
34
59
+ "/"
35
60
+ "src="
36
- + bind_specification .source_exchange
61
+ + encode_path_segment ( bind_specification .source_exchange )
37
62
+ ";"
38
63
+ "dstq="
39
- + bind_specification .destination_queue
64
+ + encode_path_segment ( bind_specification .destination_queue )
40
65
+ ";key="
41
- + bind_specification .binding_key
66
+ + encode_path_segment ( bind_specification .binding_key )
42
67
+ ";args="
43
68
)
44
69
return binding_path_wth_exchange_queue_key
0 commit comments