Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a helper function to extract the HTTP path from a template #52

Merged
merged 3 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package masque

import (
"fmt"
"github.com/dunglas/httpsfv"
"github.com/yosida95/uritemplate/v3"
"net/http"
"net/url"
"strconv"

"github.com/dunglas/httpsfv"
"github.com/yosida95/uritemplate/v3"
"strings"
)

// Request is the parsed CONNECT-UDP request returned from ParseRequest.
Expand Down Expand Up @@ -89,3 +89,19 @@ func ParseRequest(r *http.Request, template *uritemplate.Template) (*Request, er
}
return &Request{Target: fmt.Sprintf("%s:%d", targetHost, targetPort)}, nil
}

// PathFromTemplate extracts the HTTP path from a URI template,
// such that it can be used in a http.ServeMux.
// This is useful when the template variables are parts of the path, e.g.
// when using the default template (.well-known/masque/udp/{target_host}/{target_port}/).
func PathFromTemplate(t *uritemplate.Template) (string, error) {
u, err := url.Parse(t.Raw())
if err != nil {
return "", err
}
path := strings.ReplaceAll(strings.ReplaceAll(u.Path, "/{target_host}", ""), "/{target_port}", "")
if path != u.Path && len(path) > 0 && path[len(path)-1] != '/' {
path = path + "/"
}
return path, nil
}
36 changes: 36 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,39 @@ func TestRequestParsing(t *testing.T) {
require.Equal(t, http.StatusBadRequest, err.(*RequestParseError).HTTPStatus)
})
}

func TestPathFromTemplate(t *testing.T) {
for _, tc := range []struct{ name, template, expected, errContains string }{
{
name: "invalid URL",
template: "://localhost:1234",
errContains: "missing protocol scheme",
},
{
name: "variables as URL parameters",
template: "https://localhost:1234/masque?h={target_host}&p={target_port}",
expected: "/masque",
},
{
name: "variables in URL paths",
template: "https://localhost:1234/masque/{target_host}/{target_port}",
expected: "/masque/", // needs to have a trailing /
},
{
name: "variables in URL paths, no trailing /",
template: "https://localhost:1234/masque/{target_host}/{target_port}/",
expected: "/masque/",
},
} {
t.Run(tc.name, func(t *testing.T) {
temp := uritemplate.MustNew(tc.template)
path, err := PathFromTemplate(temp)
if tc.errContains != "" {
require.ErrorContains(t, err, tc.errContains)
} else {
require.NoError(t, err)
require.Equal(t, tc.expected, path)
}
})
}
}