From c6d1e761796bb16e4a3b00b10f0a04674069e9d4 Mon Sep 17 00:00:00 2001 From: rainu Date: Thu, 10 Aug 2023 14:14:14 +0200 Subject: [PATCH] move to location of target url from query to path --- README.md | 8 ++++---- internal/http/controller/pre.go | 9 ++++++++- test/setup_test.go | 2 +- test/validation_test.go | 10 +++++----- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 5d50f86..641a2b3 100644 --- a/README.md +++ b/README.md @@ -55,8 +55,8 @@ docker run -p 8080:8080 -e "CREDENTIALS=user:secret" ghcr.io/rainu/r-ray:main After that you can make any request: ```bash -# url -> query encoded url. For example: https://github.com/rainu/r-ray -curl -v -u "user:secret" localhost:8080/?url=https%3A%2F%2Fgithub.com%2Frainu%2Fr-ray +# path encoded url. For example: https://github.com/rainu/r-ray +curl -v -u "user:secret" localhost:8080/https%3A%2F%2Fgithub.com%2Frainu%2Fr-ray ``` # Documentation @@ -86,7 +86,7 @@ curl -v -u "user:secret" localhost:8080/?url=https%3A%2F%2Fgithub.com%2Frainu%2F +--------+ +-------+ +--------+ ``` -* The target url must be given in the `url`-query parameter. This query parameter should be encoded correctly! +* The target url must be given in the path. This path should be encoded correctly! * The same http-method will be used for target request as the clients request is. * All header which have the prefix from _REQUEST_HEADER_PREFIX_ (default **R-**) will be transferred to the url's target (without the prefix). * If the header from _FORWARD_REQUEST_HEADER_ (default **R-Forward-Request-Header**) is sent, all request headers which match the regular expression, will be transferred to the target too @@ -95,7 +95,7 @@ curl -v -u "user:secret" localhost:8080/?url=https%3A%2F%2Fgithub.com%2Frainu%2F ``` -> POST /?url=http://target.com/ HTTP/1.1 +> POST /http://target.com/ HTTP/1.1 > User-Agent: fancy-client > R-Accept: money > R-Forward-Request-Header: user-.* diff --git a/internal/http/controller/pre.go b/internal/http/controller/pre.go index 0995d2f..9315a7b 100644 --- a/internal/http/controller/pre.go +++ b/internal/http/controller/pre.go @@ -9,7 +9,14 @@ import ( ) func (p *proxy) validateRequest(ctx *context) bool { - parsedUrl, err := url.ParseRequestURI(ctx.request.URL.Query().Get("url")) + targetUrl, err := url.PathUnescape(strings.TrimPrefix(ctx.request.RequestURI, "/")) + if err != nil { + ctx.response.WriteHeader(http.StatusBadRequest) + writeError(ctx.response, fmt.Errorf("invalid url: %w", err)) + return false + } + + parsedUrl, err := url.ParseRequestURI(targetUrl) if err != nil { ctx.response.WriteHeader(http.StatusBadRequest) writeError(ctx.response, fmt.Errorf("invalid url: %w", err)) diff --git a/test/setup_test.go b/test/setup_test.go index 888d728..b97adc1 100644 --- a/test/setup_test.go +++ b/test/setup_test.go @@ -77,7 +77,7 @@ func freePort() int { func appUrl(path string) string { return fmt.Sprintf( - "%s?url=%s", + "%s%s", appBaseUrl, url.QueryEscape(testServer.URL+path), ) diff --git a/test/validation_test.go b/test/validation_test.go index 953f99c..b30abaa 100644 --- a/test/validation_test.go +++ b/test/validation_test.go @@ -18,16 +18,16 @@ func TestValidationBadUrl(t *testing.T) { expectedErr: `{"error":"invalid url: parse \"\": empty url"}`, }, { - url: appBaseUrl + "?url=", - expectedErr: `{"error":"invalid url: parse \"\": empty url"}`, + url: appBaseUrl + "//", + expectedErr: `{"error":"invalid url"}`, }, { - url: appBaseUrl + "?url=/", + url: appBaseUrl + "/brOken", expectedErr: `{"error":"invalid url"}`, }, { - url: appBaseUrl + "?url=brOken", - expectedErr: `{"error":"invalid url: parse \"brOken\": invalid URI for request"}`, + url: appBaseUrl + "http://host:port/test", + expectedErr: `{"error":"invalid url: parse \"http://host:port/test\": invalid port \":port\" after host"}`, }, }