2024-11-15 15:37:28 +08:00
|
|
|
package rabbitmq_amqp
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2024-11-21 17:34:08 +08:00
|
|
|
// Address Creates the address for the exchange or queue following the RabbitMQ conventions.
|
|
|
|
|
// see: https://www.rabbitmq.com/docs/next/amqp#address-v2
|
|
|
|
|
func Address(exchange, key, queue *string, urlParameters *string) (string, error) {
|
|
|
|
|
if exchange == nil && queue == nil {
|
2024-11-15 15:37:28 +08:00
|
|
|
return "", errors.New("exchange or queue must be set")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
urlAppend := ""
|
2024-11-21 17:34:08 +08:00
|
|
|
if !isStringNilOrEmpty(urlParameters) {
|
|
|
|
|
urlAppend = *urlParameters
|
2024-11-15 15:37:28 +08:00
|
|
|
}
|
2024-11-21 17:34:08 +08:00
|
|
|
if !isStringNilOrEmpty(exchange) && !isStringNilOrEmpty(queue) {
|
2024-11-15 15:37:28 +08:00
|
|
|
return "", errors.New("exchange and queue cannot be set together")
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-21 17:34:08 +08:00
|
|
|
if !isStringNilOrEmpty(exchange) {
|
|
|
|
|
if !isStringNilOrEmpty(key) {
|
|
|
|
|
return "/" + exchanges + "/" + encodePathSegments(*exchange) + "/" + encodePathSegments(*key) + urlAppend, nil
|
2024-11-15 15:37:28 +08:00
|
|
|
}
|
2024-11-21 17:34:08 +08:00
|
|
|
return "/" + exchanges + "/" + encodePathSegments(*exchange) + urlAppend, nil
|
2024-11-15 15:37:28 +08:00
|
|
|
}
|
|
|
|
|
|
2024-11-21 17:34:08 +08:00
|
|
|
if queue == nil {
|
2024-11-15 15:37:28 +08:00
|
|
|
return "", nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-21 17:34:08 +08:00
|
|
|
if isStringNilOrEmpty(queue) {
|
2024-11-15 15:37:28 +08:00
|
|
|
return "", errors.New("queue must be set")
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-21 17:34:08 +08:00
|
|
|
return "/" + queues + "/" + encodePathSegments(*queue) + urlAppend, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ExchangeAddress Creates the address for the exchange
|
|
|
|
|
// See Address for more information
|
|
|
|
|
func ExchangeAddress(exchange, key *string) (string, error) {
|
|
|
|
|
return Address(exchange, key, nil, nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// QueueAddress Creates the address for the queue.
|
|
|
|
|
// See Address for more information
|
|
|
|
|
func QueueAddress(queue *string) (string, error) {
|
|
|
|
|
return Address(nil, nil, queue, nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PurgeQueueAddress Creates the address for purging the queue.
|
|
|
|
|
// See Address for more information
|
|
|
|
|
func PurgeQueueAddress(queue *string) (string, error) {
|
|
|
|
|
parameter := "/messages"
|
|
|
|
|
return Address(nil, nil, queue, ¶meter)
|
2024-11-15 15:37:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// encodePathSegments takes a string and returns its percent-encoded representation.
|
|
|
|
|
func encodePathSegments(input string) string {
|
|
|
|
|
var encoded strings.Builder
|
|
|
|
|
|
|
|
|
|
// Iterate over each character in the input string
|
|
|
|
|
for _, char := range input {
|
|
|
|
|
// Check if the character is an unreserved character (i.e., it doesn't need encoding)
|
|
|
|
|
if isUnreserved(char) {
|
|
|
|
|
encoded.WriteRune(char) // Append as is
|
|
|
|
|
} else {
|
|
|
|
|
// Encode character To %HH format
|
|
|
|
|
encoded.WriteString(fmt.Sprintf("%%%02X", char))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return encoded.String()
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-16 22:26:12 +08:00
|
|
|
//// Decode takes a percent-encoded string and returns its decoded representation.
|
|
|
|
|
//func decode(input string) (string, error) {
|
|
|
|
|
// // Use url.QueryUnescape which properly decodes percent-encoded strings
|
|
|
|
|
// decoded, err := url.QueryUnescape(input)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// return "", err
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// return decoded, nil
|
|
|
|
|
//}
|
2024-11-15 15:37:28 +08:00
|
|
|
|
|
|
|
|
// isUnreserved checks if a character is an unreserved character in percent encoding
|
|
|
|
|
// Unreserved characters are: A-Z, a-z, 0-9, -, ., _, ~
|
|
|
|
|
func isUnreserved(char rune) bool {
|
|
|
|
|
return (char >= 'A' && char <= 'Z') ||
|
|
|
|
|
(char >= 'a' && char <= 'z') ||
|
|
|
|
|
(char >= '0' && char <= '9') ||
|
|
|
|
|
char == '-' || char == '.' || char == '_' || char == '~'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func bindingPath() string {
|
|
|
|
|
return "/" + bindings
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func bindingPathWithExchangeQueueKey(toQueue bool, sourceName, destinationName, key string) string {
|
|
|
|
|
sourceNameEncoded := encodePathSegments(sourceName)
|
|
|
|
|
destinationNameEncoded := encodePathSegments(destinationName)
|
|
|
|
|
keyEncoded := encodePathSegments(key)
|
|
|
|
|
destinationType := "dste"
|
|
|
|
|
if toQueue {
|
|
|
|
|
destinationType = "dstq"
|
|
|
|
|
}
|
|
|
|
|
format := "/%s/src=%s;%s=%s;key=%s;args="
|
|
|
|
|
return fmt.Sprintf(format, bindings, sourceNameEncoded, destinationType, destinationNameEncoded, keyEncoded)
|
2025-01-16 22:26:12 +08:00
|
|
|
}
|
2024-11-15 15:37:28 +08:00
|
|
|
|
2025-01-16 22:26:12 +08:00
|
|
|
func validateAddress(address string) bool {
|
|
|
|
|
return strings.HasPrefix(address, fmt.Sprintf("/%s/", exchanges)) || strings.HasPrefix(address, fmt.Sprintf("/%s/", queues))
|
2024-11-15 15:37:28 +08:00
|
|
|
}
|