summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2025-02-22 09:55:42 -0500
committerRobby Zambito <contact@robbyzambito.me>2025-02-22 10:06:15 -0500
commitac1fe1f5ce37d8b1908caeeb96c98b0053952420 (patch)
treea6e275eeca3f376ea30a2c04336fa557950069ec /main.go
parent9fd787527146d3bc37c6312a3bde29ed38018bb0 (diff)
Update endpoint -> subject encoding
Use an encoding that can be safely used bidirectionally while still being easy to read. This is useful for being able to make HTTP requests from NATS, which I am working on now.
Diffstat (limited to 'main.go')
-rw-r--r--main.go33
1 files changed, 30 insertions, 3 deletions
diff --git a/main.go b/main.go
index dad6584..c54b239 100644
--- a/main.go
+++ b/main.go
@@ -7,6 +7,7 @@ import (
"log"
"net"
"net/http"
+ "net/url"
"os"
"strings"
"time"
@@ -29,6 +30,27 @@ func printHelp() {
fmt.Println(" HTTP_PORT - HTTP port to listen on (default: 8080)")
}
+// URL to NATS subject conversion
+func URLToNATS(urlPath string) (string, error) {
+ segments := strings.Split(strings.Trim(urlPath, "/"), "/")
+ for i, seg := range segments {
+ // Decode existing encoding first to prevent double-encoding
+ unescaped, err := url.PathUnescape(seg)
+ if err != nil {
+ return "", fmt.Errorf("failed to unescape segment: %w", err)
+ }
+
+ // Encode special NATS-sensitive characters
+ encoded := url.PathEscape(unescaped)
+ encoded = strings.ReplaceAll(encoded, ".", "%2E") // Critical for token separation
+ encoded = strings.ReplaceAll(encoded, "*", "%2A") // Wildcard protection
+ encoded = strings.ReplaceAll(encoded, ">", "%3E") // Wildcard protection
+
+ segments[i] = encoded
+ }
+ return strings.Join(segments, "."), nil
+}
+
func main() {
helpFlag := flag.Bool("help", false, "Display help information about available environment variables")
flag.Parse()
@@ -103,9 +125,14 @@ func main() {
domainParts := strings.ReplaceAll(host, ".", "_")
// Process path component
- path := strings.TrimPrefix(r.URL.Path, "/")
- // Replace all "." with "_" and then all "/" with ".".
- subjectPath := strings.ReplaceAll(strings.ReplaceAll(path, ".", "_"), "/", ".")
+ path := strings.TrimSuffix(strings.TrimPrefix(r.URL.Path, "/"), "/")
+ subjectPath, err := URLToNATS(path)
+ if err != nil {
+ http.Error(w, "Error converting endpoint to NATS subject", http.StatusInternalServerError)
+ log.Println("Could not convert endpoint to NATS subject", err)
+ return
+
+ }
// Build final subject
subjectBase := "http"