Skip to content

Commit 0a93a5e

Browse files
author
liaojianqi.rd
committed
fix get host from req.header
1 parent 6653abe commit 0a93a5e

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

config.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ func (cors *cors) applyCors(c *gin.Context) {
3636
// request is not a CORS request
3737
return
3838
}
39-
host := c.Request.Header.Get("Host")
39+
// go/net/http//request.go
40+
// For incoming requests, the Host header is promoted to the
41+
// Request.Host field and removed from the Header map.
42+
host := c.Request.Host
4043
if origin == "http://"+host || origin == "https://"+host {
4144
// request is not a CORS request but have origin header.
4245
// for example, use fetch api

cors_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@ func performRequest(r http.Handler, method, origin string) *httptest.ResponseRec
4242

4343
func performRequestWithHeaders(r http.Handler, method, origin string, headers map[string]string) *httptest.ResponseRecorder {
4444
req, _ := http.NewRequest(method, "/", nil)
45+
// go/net/http//request.go
46+
// For incoming requests, the Host header is promoted to the
47+
// Request.Host field and removed from the Header map.
4548
for k, v := range headers {
49+
if k == "Host" {
50+
req.Host = v
51+
continue
52+
}
4653
req.Header.Set(k, v)
4754
}
4855
if len(origin) > 0 {

0 commit comments

Comments
 (0)