Async hot hdr fix (#604)

* fn: for async hot requests ensure/fix content-length/type

* fn: added tests for FromModel for content type/length

* fn: restrict the content-length fix to async in FromModel()
This commit is contained in:
Tolga Ceylan
2017-12-15 14:32:25 -08:00
committed by Reed Allman
parent 070a70f62f
commit 419298e1c0
3 changed files with 129 additions and 27 deletions

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"
@@ -205,11 +206,31 @@ func FromModel(mCall *models.Call) CallOpt {
return func(a *agent, c *call) error {
c.Call = mCall
// NOTE this adds content length based on payload length
req, err := http.NewRequest(c.Method, c.URL, strings.NewReader(c.Payload))
if err != nil {
return err
}
// HACK: only applies to async below, for async we need to restore
// content-length and content-type of the original request, which are
// derived from Payload and original content-type which now is in
// Fn_header_content_type
if c.Type == models.TypeAsync {
// Hoist original request content type into async request
if req.Header.Get("Content-Type") == "" {
content_type, ok := c.EnvVars["Fn_header_content_type"]
if ok {
req.Header.Set("Content-Type", content_type)
}
}
// Ensure content-length in async requests for protocol/http DumpRequestTo()
if req.ContentLength == -1 || req.Header.Get("Content-Length") == "" {
req.ContentLength = int64(len(c.Payload))
req.Header.Set("Content-Length", strconv.FormatInt(int64(len(c.Payload)), 10))
}
}
for k, v := range c.EnvVars {
// TODO if we don't store env as []string headers are messed up
req.Header.Set(k, v)