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

fix get host from req.header #39

Open
wants to merge 1 commit into
base: master
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
5 changes: 4 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ func (cors *cors) applyCors(c *gin.Context) {
// request is not a CORS request
return
}
host := c.Request.Header.Get("Host")
// go/net/http/request.go
// For incoming requests, the Host header is promoted to the
// Request.Host field and removed from the Header map.
host := c.Request.Host
if origin == "http://"+host || origin == "https://"+host {
// request is not a CORS request but have origin header.
// for example, use fetch api
Expand Down
7 changes: 7 additions & 0 deletions cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ func performRequest(r http.Handler, method, origin string) *httptest.ResponseRec

func performRequestWithHeaders(r http.Handler, method, origin string, headers map[string]string) *httptest.ResponseRecorder {
req, _ := http.NewRequest(method, "/", nil)
// go/net/http/request.go
// For incoming requests, the Host header is promoted to the
// Request.Host field and removed from the Header map.
for k, v := range headers {
if k == "Host" {
req.Host = v
continue
}
req.Header.Set(k, v)
}
if len(origin) > 0 {
Expand Down