Skip to content

Commit

Permalink
feat: URI host's priority is higher than host header
Browse files Browse the repository at this point in the history
  • Loading branch information
Duslia committed May 30, 2024
1 parent b7cbc9d commit db6a1c9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
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

0 comments on commit db6a1c9

Please sign in to comment.