Skip to content

Commit bb7b23f

Browse files
committed
Fix for passing body from request in text streaming mode
The body was not being passed through to the upstream_url when in http mode. This commit has been tested and shown to pass the body. Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent f0eb480 commit bb7b23f

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

executor/http_runner.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,11 @@ func (f *HTTPFunctionRunner) Run(req FunctionRequest, contentLength int64, r *ht
9898
upstreamURL += r.RequestURI
9999
}
100100

101-
var body io.Reader
101+
body := r.Body
102+
102103
if f.BufferHTTPBody {
103104
reqBody, _ := io.ReadAll(r.Body)
104-
body = bytes.NewReader(reqBody)
105-
} else {
106-
body = r.Body
105+
body = io.NopCloser(bytes.NewReader(reqBody))
107106
}
108107

109108
request, err := http.NewRequest(r.Method, upstreamURL, body)
@@ -130,11 +129,10 @@ func (f *HTTPFunctionRunner) Run(req FunctionRequest, contentLength int64, r *ht
130129
}
131130
defer cancel()
132131

133-
if r.Header.Get("Accept") == "text/event-stream" {
134-
132+
if strings.HasPrefix(r.Header.Get("Accept"), "text/event-stream") {
135133
ww := fhttputil.NewHttpWriteInterceptor(w)
136134

137-
f.ReverseProxy.ServeHTTP(ww, request)
135+
f.ReverseProxy.ServeHTTP(w, r)
138136
done := time.Since(startedTime)
139137

140138
log.Printf("%s %s - %d - Bytes: %s (%.4fs)", r.Method, r.RequestURI, ww.Status(), units.HumanSize(float64(ww.BytesWritten())), done.Seconds())

main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ func getEnvironment(r *http.Request) []string {
342342
}
343343

344344
func makeHTTPRequestHandler(watchdogConfig config.WatchdogConfig, prefixLogs bool, logBufferSize int) func(http.ResponseWriter, *http.Request) {
345+
upstreamURL, _ := url.Parse(watchdogConfig.UpstreamURL)
346+
345347
commandName, arguments := watchdogConfig.Process()
346348
functionInvoker := executor.HTTPFunctionRunner{
347349
ExecTimeout: watchdogConfig.ExecTimeout,
@@ -352,6 +354,8 @@ func makeHTTPRequestHandler(watchdogConfig config.WatchdogConfig, prefixLogs boo
352354
LogBufferSize: logBufferSize,
353355
ReverseProxy: &httputil.ReverseProxy{
354356
Director: func(req *http.Request) {
357+
req.URL.Host = upstreamURL.Host
358+
req.URL.Scheme = "http"
355359
},
356360
ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
357361
},
@@ -378,7 +382,6 @@ func makeHTTPRequestHandler(watchdogConfig config.WatchdogConfig, prefixLogs boo
378382
req := executor.FunctionRequest{
379383
Process: commandName,
380384
ProcessArgs: arguments,
381-
InputReader: r.Body,
382385
OutputWriter: w,
383386
}
384387

0 commit comments

Comments
 (0)