Skip to content

Fix BaseURL Path Handling #31

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

Merged
merged 2 commits into from
Apr 18, 2020
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
12 changes: 10 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
"net/http"
"net/url"
"strings"

"github.com/go-log/log"
)
Expand Down Expand Up @@ -66,6 +67,13 @@ func New(cfg *Config) (c *Client, err error) {
return nil, fmt.Errorf("unsupported protocol scheme %q", baseURL.Scheme)
}

// If baseURL has a path component, ensure it is terminated with a separator, to prevent
// url.ResolveReference from stripping the final component of the path when constructing
// request URL.
if p := baseURL.Path; p != "" && !strings.HasSuffix(p, "/") {
baseURL.Path += "/"
}

c = &Client{
BaseURL: baseURL,
AuthToken: cfg.AuthToken,
Expand All @@ -87,10 +95,10 @@ func New(cfg *Config) (c *Client, err error) {
return c, nil
}

// newRequest returns a new Request given a method, path, query, and optional body.
// newRequest returns a new Request given a method, relative path, query, and optional body.
func (c *Client) newRequest(method, path string, body io.Reader) (r *http.Request, err error) {
u := c.BaseURL.ResolveReference(&url.URL{
Path: path,
Path: strings.TrimPrefix(path, "/"), // trim leading separator as path is relative.
})

r, err = http.NewRequest(method, u.String(), body)
Expand Down
Loading