Allow updating request's RequestURI and Host header via Request.URI()

This commit is contained in:
Aliaksandr Valialkin
2016-02-19 18:53:13 +02:00
parent 0ab0d45a50
commit be49d3027a
3 changed files with 24 additions and 2 deletions
+1 -1
View File
@@ -896,7 +896,7 @@ func (req *Request) onlyMultipartForm() bool {
//
// See also WriteTo.
func (req *Request) Write(w *bufio.Writer) error {
if len(req.Header.Host()) == 0 {
if len(req.Header.Host()) == 0 || req.parsedURI {
uri := req.URI()
host := uri.Host()
if len(host) == 0 {
+22
View File
@@ -11,6 +11,28 @@ import (
"testing"
)
func TestRequestUpdateURI(t *testing.T) {
var r Request
r.Header.SetHost("aaa.bbb")
r.SetRequestURI("/lkjkl/kjl")
// Modify request uri and host via URI() object and make sure
// the requestURI and Host header are properly updated
u := r.URI()
u.SetPath("/123/432.html")
u.SetHost("foobar.com")
a := u.QueryArgs()
a.Set("aaa", "bcse")
s := r.String()
if !strings.HasPrefix(s, "GET /123/432.html?aaa=bcse") {
t.Fatalf("cannot find %q in %q", "GET /123/432.html?aaa=bcse", s)
}
if strings.Index(s, "\r\nHost: foobar.com\r\n") < 0 {
t.Fatalf("cannot find %q in %q", "\r\nHost: foobar.com\r\n", s)
}
}
func TestRequestBodyStreamMultipleBodyCalls(t *testing.T) {
var r Request
+1 -1
View File
@@ -122,7 +122,7 @@ func (u *URI) Path() []byte {
// SetPath sets URI path.
func (u *URI) SetPath(path string) {
u.pathOriginal = append(u.pathOriginal, path...)
u.pathOriginal = append(u.pathOriginal[:0], path...)
u.path = normalizePath(u.path, u.pathOriginal)
}