Skip to content

Commit

Permalink
Fix "Host" header access in middleware
Browse files Browse the repository at this point in the history
Host header have special treatment, and removed from http request
headers map (recommended to use r.Host instead).
  • Loading branch information
buger committed Jan 23, 2018
1 parent 1ada344 commit 241b237
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
12 changes: 11 additions & 1 deletion coprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,18 @@ func (c *CoProcessor) ObjectFromRequest(r *http.Request) *coprocess.Object {
body = string(originalBody)
}

headers := ProtoMap(r.Header)

host := r.Host
if host == "" && r.URL != nil {
host = r.URL.Host
}
if host != "" {
headers["Host"] = host
}

miniRequestObject := &coprocess.MiniRequestObject{
Headers: ProtoMap(r.Header),
Headers: headers,
SetHeaders: map[string]string{},
DeleteHeaders: []string{},
Body: body,
Expand Down
15 changes: 14 additions & 1 deletion mw_js_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,21 @@ func (d *DynamicMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Reques
return nil, 200
}

headers := r.Header
host := r.Host
if host == "" && r.URL != nil {
host = r.URL.Host
}
if host != "" {
headers = make(http.Header)
for k, v := range r.Header {
headers[k] = v
}
headers.Set("Host", host)
}

requestData := MiniRequestObject{
Headers: r.Header,
Headers: headers,
SetHeaders: map[string]string{},
DeleteHeaders: []string{},
Body: originalBody,
Expand Down

0 comments on commit 241b237

Please sign in to comment.