Skip to content

Instantly share code, notes, and snippets.

@alexellis
Created April 24, 2018 19:19
Show Gist options
  • Save alexellis/ba1c3b0fdde166a937810c262d4378df to your computer and use it in GitHub Desktop.
Save alexellis/ba1c3b0fdde166a937810c262d4378df to your computer and use it in GitHub Desktop.
pass_request_patch.go
diff --git a/executor/http_runner.go b/executor/http_runner.go
index 5ae8c80..1e308ac 100644
--- a/executor/http_runner.go
+++ b/executor/http_runner.go
@@ -96,11 +96,14 @@ func (f *HTTPFunctionRunner) Start() error {
// Run a function with a long-running process with a HTTP protocol for communication
func (f *HTTPFunctionRunner) Run(req FunctionRequest, contentLengthint64, r *http.Request, w http.ResponseWriter) error {
- request, _ := http.NewRequest(r.Method, f.UpstreamURL.String(), r.Body)
- for h := range r.Header {
- request.Header.Set(h, r.Header.Get(h))
- }
+ urlUpstream := f.UpstreamURL.String() + r.RequestURI
+
+ request, _ := http.NewRequest(r.Method, urlUpstream, r.Body)
+
+ copyHeaders(request.Header, &r.Header)
+
ctx, cancel := context.WithTimeout(context.Background(), f.ExecTimeout)
+
defer cancel()
res, err := f.Client.Do(request.WithContext(ctx))
@@ -133,9 +136,7 @@ func (f *HTTPFunctionRunner) Run(req FunctionRequest, contentLength int64, r *ht
return err
}
- for h := range res.Header {
- w.Header().Set(h, res.Header.Get(h))
- }
+ copyHeaders(w.Header(), &res.Header)
w.WriteHeader(res.StatusCode)
if res.Body != nil {
@@ -152,6 +153,14 @@ func (f *HTTPFunctionRunner) Run(req FunctionRequest, contentLength int64, r *ht
return nil
}
+func copyHeaders(destination http.Header, source *http.Header) {
+ for k, v := range *source {
+ vClone := make([]string, len(v))
+ copy(vClone, v)
+ (destination)[k] = vClone
+ }
+}
+
func makeProxyClient(dialTimeout time.Duration) *http.Client {
proxyClient := http.Client{
Transport: &http.Transport{
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment