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

http.go: Request.SetURI() (Fix #1141) #1148

Merged
merged 1 commit into from Nov 8, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions http.go
Expand Up @@ -774,6 +774,20 @@ func (req *Request) URI() *URI {
return &req.uri
}

// SetURI initializes request URI
// Use this method if a single URI may be reused across multiple requests.
// Otherwise, you can just use SetRequestURI() and it will be parsed as new URI.
// The URI is copied and can be safely modified later.
func (req *Request) SetURI(newUri *URI) {
if newUri != nil {
newUri.CopyTo(&req.uri)
req.parsedURI = true
return
}
req.uri.Reset()
req.parsedURI = false
}

func (req *Request) parseURI() error {
if req.parsedURI {
return nil
Expand Down
25 changes: 25 additions & 0 deletions http_test.go
Expand Up @@ -515,6 +515,31 @@ tailfoobar`
}
}

func TestRequestSetURI(t *testing.T) {
t.Parallel()

var r Request

uri := "/foo/bar?baz"
u := &URI{}
u.Parse(nil, []byte(uri)) //nolint:errcheck
// Set request uri via SetURI()
r.SetURI(u) // copies URI
// modifying an original URI struct doesn't affect stored URI inside of request
u.SetPath("newPath")
if string(r.RequestURI()) != uri {
t.Fatalf("unexpected request uri %q. Expecting %q", r.RequestURI(), uri)
}

// Set request uri to nil just resets the URI
r.Reset()
uri = "/"
r.SetURI(nil)
if string(r.RequestURI()) != uri {
t.Fatalf("unexpected request uri %q. Expecting %q", r.RequestURI(), uri)
}
}

func TestRequestRequestURI(t *testing.T) {
t.Parallel()

Expand Down