Skip to content

Commit

Permalink
Fix for passing body from request in text streaming mode
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
alexellis committed Jan 11, 2024
1 parent f0eb480 commit bb7b23f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
12 changes: 5 additions & 7 deletions executor/http_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,11 @@ func (f *HTTPFunctionRunner) Run(req FunctionRequest, contentLength int64, r *ht
upstreamURL += r.RequestURI
}

var body io.Reader
body := r.Body

if f.BufferHTTPBody {
reqBody, _ := io.ReadAll(r.Body)
body = bytes.NewReader(reqBody)
} else {
body = r.Body
body = io.NopCloser(bytes.NewReader(reqBody))
}

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

if r.Header.Get("Accept") == "text/event-stream" {

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

f.ReverseProxy.ServeHTTP(ww, request)
f.ReverseProxy.ServeHTTP(w, r)
done := time.Since(startedTime)

log.Printf("%s %s - %d - Bytes: %s (%.4fs)", r.Method, r.RequestURI, ww.Status(), units.HumanSize(float64(ww.BytesWritten())), done.Seconds())
Expand Down
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ func getEnvironment(r *http.Request) []string {
}

func makeHTTPRequestHandler(watchdogConfig config.WatchdogConfig, prefixLogs bool, logBufferSize int) func(http.ResponseWriter, *http.Request) {
upstreamURL, _ := url.Parse(watchdogConfig.UpstreamURL)

commandName, arguments := watchdogConfig.Process()
functionInvoker := executor.HTTPFunctionRunner{
ExecTimeout: watchdogConfig.ExecTimeout,
Expand All @@ -352,6 +354,8 @@ func makeHTTPRequestHandler(watchdogConfig config.WatchdogConfig, prefixLogs boo
LogBufferSize: logBufferSize,
ReverseProxy: &httputil.ReverseProxy{
Director: func(req *http.Request) {
req.URL.Host = upstreamURL.Host
req.URL.Scheme = "http"
},
ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
},
Expand All @@ -378,7 +382,6 @@ func makeHTTPRequestHandler(watchdogConfig config.WatchdogConfig, prefixLogs boo
req := executor.FunctionRequest{
Process: commandName,
ProcessArgs: arguments,
InputReader: r.Body,
OutputWriter: w,
}

Expand Down

0 comments on commit bb7b23f

Please sign in to comment.