Skip to content

Commit

Permalink
move to location of target url from query to path
Browse files Browse the repository at this point in the history
  • Loading branch information
rainu committed Aug 10, 2023
1 parent 17d8e40 commit c6d1e76
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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%2Fgit.luolix.top%2Frainu%2Fr-ray
# path encoded url. For example: https://github.com/rainu/r-ray
curl -v -u "user:secret" localhost:8080/https%3A%2F%2Fgit.luolix.top%2Frainu%2Fr-ray
```

# Documentation
Expand Down Expand Up @@ -86,7 +86,7 @@ curl -v -u "user:secret" localhost:8080/?url=https%3A%2F%2Fgit.luolix.top%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
Expand All @@ -95,7 +95,7 @@ curl -v -u "user:secret" localhost:8080/?url=https%3A%2F%2Fgit.luolix.top%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-.*
Expand Down
9 changes: 8 additions & 1 deletion internal/http/controller/pre.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion test/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
Expand Down
10 changes: 5 additions & 5 deletions test/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}`,
},
}

Expand Down

0 comments on commit c6d1e76

Please sign in to comment.