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

feat: URI host's priority is higher than host header #1085

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion pkg/protocol/http1/req/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func write(req *protocol.Request, w network.Writer, usingProxy bool) error {
return errRequestHostRequired
}

if len(req.Header.Host()) == 0 {
if len(req.Header.Host()) == 0 || req.UseURIHost {
req.Header.SetHostBytes(host)
}

Expand Down
47 changes: 47 additions & 0 deletions pkg/protocol/http1/req/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1487,3 +1487,50 @@ func testRequestBodyStreamWithTrailer(t *testing.T, body []byte, disableNormaliz
}
}
}

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

// normal case
var req protocol.Request
req.Header.SetHost("foobar.com")
req.SetRequestURI("http://foobarhost.com")
req.ParseURI()
var w bytes.Buffer
zw := netpoll.NewWriter(&w)
if err := Write(&req, zw); err != nil {
t.Fatalf("unexpected error: %s", err)
}
if err := zw.Flush(); err != nil {
t.Fatalf("unexpected error: %s", err)
}

var req1 protocol.Request
zr := mock.NewZeroCopyReader(w.String())
if err := Read(&req1, zr); err != nil {
t.Fatalf("unexpected error: %s", err)
}
assert.DeepEqual(t, "foobar.com", string(req1.Host()))

// uri higher priority case
var reqURIHighPriority protocol.Request
reqURIHighPriority.Header.SetHost("foobar.com")
reqURIHighPriority.SetRequestURI("http://foobarhost.com")
reqURIHighPriority.ParseURI()
reqURIHighPriority.UseURIHost = true
var bw bytes.Buffer
zw = netpoll.NewWriter(&bw)
if err := Write(&reqURIHighPriority, zw); err != nil {
t.Fatalf("unexpected error: %s", err)
}
if err := zw.Flush(); err != nil {
t.Fatalf("unexpected error: %s", err)
}

var req1URIHighPriority protocol.Request
zr = mock.NewZeroCopyReader(bw.String())
if err := Read(&req1URIHighPriority, zr); err != nil {
t.Fatalf("unexpected error: %s", err)
}
assert.DeepEqual(t, "foobarhost.com", string(req1URIHighPriority.Host()))
}
4 changes: 4 additions & 0 deletions pkg/protocol/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ type Request struct {
multipartFiles []*File
multipartFields []*MultipartField

// UseURIHost uses URI host as host header. Ignore origin host header
UseURIHost bool

// Request level options, service discovery options etc.
options *config.RequestOptions
}
Expand Down Expand Up @@ -190,6 +193,7 @@ func (req *Request) resetSkipHeaderAndConn() {
req.parsedURI = false
req.parsedPostArgs = false
req.postArgs.Reset()
req.UseURIHost = false
}

func (req *Request) ResetSkipHeader() {
Expand Down
Loading