in openwhisk/runHandler.go [205:267]
func (ap *ActionProxy) doRun(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
sendError(w, http.StatusBadRequest, fmt.Sprintf("Error reading request body: %v", err))
return
}
Debug("done reading %d bytes", len(body))
body = bytes.Replace(body, []byte("\n"), []byte(""), -1)
// check if you have an action
if ap.theExecutor == nil {
sendError(w, http.StatusInternalServerError, "no action defined yet")
return
}
// check if the process exited
if ap.theExecutor.Exited() {
sendError(w, http.StatusInternalServerError, "command exited")
return
}
// execute the action
response, err := ap.theExecutor.Interact(body)
// check for early termination
if err != nil {
Debug("WARNING! Command exited")
ap.theExecutor = nil
sendError(w, http.StatusBadRequest, "command exited")
return
}
DebugLimit("received:", response, 120)
// check if the answer is an object map or array
if ok := isJsonObjOrArray(response); !ok {
sendError(w, http.StatusBadGateway, "The action did not return a dictionary or array.")
return
}
// write response
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(response)))
numBytesWritten, err := w.Write(response)
// flush output if possible
if f, ok := w.(http.Flusher); ok {
f.Flush()
}
// handle writing errors
if err != nil {
Debug("Error writing response: %v", err)
sendError(w, http.StatusInternalServerError, fmt.Sprintf("Error writing response: %v", err))
return
}
if numBytesWritten != len(response) {
Debug("Only wrote %d of %d bytes to response", numBytesWritten, len(response))
sendError(w, http.StatusInternalServerError, fmt.Sprintf("Only wrote %d of %d bytes to response", numBytesWritten, len(response)))
return
}
}