Skip to content

Commit 1051282

Browse files
DusliaFGYFFFF
authored andcommitted
feat: URI host's priority is higher than host header
1 parent 7d63572 commit 1051282

3 files changed

Lines changed: 52 additions & 1 deletion

File tree

pkg/protocol/http1/req/request.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func write(req *protocol.Request, w network.Writer, usingProxy bool) error {
171171
return errRequestHostRequired
172172
}
173173

174-
if len(req.Header.Host()) == 0 {
174+
if len(req.Header.Host()) == 0 || req.UseURIHost {
175175
req.Header.SetHostBytes(host)
176176
}
177177

pkg/protocol/http1/req/request_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,3 +1487,50 @@ func testRequestBodyStreamWithTrailer(t *testing.T, body []byte, disableNormaliz
14871487
}
14881488
}
14891489
}
1490+
1491+
func TestURIHostPriority(t *testing.T) {
1492+
t.Parallel()
1493+
1494+
// normal case
1495+
var req protocol.Request
1496+
req.Header.SetHost("foobar.com")
1497+
req.SetRequestURI("http://foobarhost.com")
1498+
req.ParseURI()
1499+
var w bytes.Buffer
1500+
zw := netpoll.NewWriter(&w)
1501+
if err := Write(&req, zw); err != nil {
1502+
t.Fatalf("unexpected error: %s", err)
1503+
}
1504+
if err := zw.Flush(); err != nil {
1505+
t.Fatalf("unexpected error: %s", err)
1506+
}
1507+
1508+
var req1 protocol.Request
1509+
zr := mock.NewZeroCopyReader(w.String())
1510+
if err := Read(&req1, zr); err != nil {
1511+
t.Fatalf("unexpected error: %s", err)
1512+
}
1513+
assert.DeepEqual(t, "foobar.com", string(req1.Host()))
1514+
1515+
// uri higher priority case
1516+
var reqURIHighPriority protocol.Request
1517+
reqURIHighPriority.Header.SetHost("foobar.com")
1518+
reqURIHighPriority.SetRequestURI("http://foobarhost.com")
1519+
reqURIHighPriority.ParseURI()
1520+
reqURIHighPriority.UseURIHost = true
1521+
var bw bytes.Buffer
1522+
zw = netpoll.NewWriter(&bw)
1523+
if err := Write(&reqURIHighPriority, zw); err != nil {
1524+
t.Fatalf("unexpected error: %s", err)
1525+
}
1526+
if err := zw.Flush(); err != nil {
1527+
t.Fatalf("unexpected error: %s", err)
1528+
}
1529+
1530+
var req1URIHighPriority protocol.Request
1531+
zr = mock.NewZeroCopyReader(bw.String())
1532+
if err := Read(&req1URIHighPriority, zr); err != nil {
1533+
t.Fatalf("unexpected error: %s", err)
1534+
}
1535+
assert.DeepEqual(t, "foobarhost.com", string(req1URIHighPriority.Host()))
1536+
}

pkg/protocol/request.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ type Request struct {
109109
multipartFiles []*File
110110
multipartFields []*MultipartField
111111

112+
// UseURIHost uses URI host as host header. Ignore origin host header
113+
UseURIHost bool
114+
112115
// Request level options, service discovery options etc.
113116
options *config.RequestOptions
114117
}
@@ -190,6 +193,7 @@ func (req *Request) resetSkipHeaderAndConn() {
190193
req.parsedURI = false
191194
req.parsedPostArgs = false
192195
req.postArgs.Reset()
196+
req.UseURIHost = false
193197
}
194198

195199
func (req *Request) ResetSkipHeader() {

0 commit comments

Comments
 (0)