From 994a47b02e18b277109c43f9cdad31e39921263a Mon Sep 17 00:00:00 2001 From: swirl Date: Sun, 8 Aug 2021 21:33:52 -0400 Subject: [PATCH] decode url in query-string GET todo: install target for nginx and systemd files --- index.html | 6 +++++- main.go | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index 7c27197..121d9d7 100644 --- a/index.html +++ b/index.html @@ -44,7 +44,11 @@ A Minimal, SQLite-Backed URL Shortener | $ curl {{.URL}}/ddg?https://duckduckgo.com | {{.URL}}/ddg | -| 5. Deleting a short link +| 5. You can also pass an encoded URL: +| $ curl {{.URL}}?https%3A%2F%2Fduckduckgo.com +| {{.URL}}/1dc4cb37e81de396 +| +| 6. Deleting a short link | $ TMP=$(mktemp) | $ # temp file will store header | $ LINK=$(curl -sS {{.URL}} -d https://duckduckgo.com -D $TMP) diff --git a/main.go b/main.go index f0369cd..2eb164b 100644 --- a/main.go +++ b/main.go @@ -171,7 +171,11 @@ func (c controller) ServeHTTP(rw http.ResponseWriter, r *http.Request) { case http.MethodGet: st := strings.TrimRight(r.URL.Path, "/") - rq := r.URL.RawQuery + rq, err := url.QueryUnescape(r.URL.RawQuery) + if err != nil { + c.Err(rw, r, err) + return + } if rq != "" { u, err := url.Parse(rq) if err != nil {