Merge a13a37f695
into b8498c564d
This commit is contained in:
commit
a4328d237a
@ -531,6 +531,7 @@ type Hook struct {
|
|||||||
ResponseMessage string `json:"response-message,omitempty"`
|
ResponseMessage string `json:"response-message,omitempty"`
|
||||||
ResponseHeaders ResponseHeaders `json:"response-headers,omitempty"`
|
ResponseHeaders ResponseHeaders `json:"response-headers,omitempty"`
|
||||||
CaptureCommandOutput bool `json:"include-command-output-in-response,omitempty"`
|
CaptureCommandOutput bool `json:"include-command-output-in-response,omitempty"`
|
||||||
|
StreamCommandOutput bool `json:"stream-command-output,omitempty"`
|
||||||
CaptureCommandOutputOnError bool `json:"include-command-output-in-response-on-error,omitempty"`
|
CaptureCommandOutputOnError bool `json:"include-command-output-in-response-on-error,omitempty"`
|
||||||
PassEnvironmentToCommand []Argument `json:"pass-environment-to-command,omitempty"`
|
PassEnvironmentToCommand []Argument `json:"pass-environment-to-command,omitempty"`
|
||||||
PassArgumentsToCommand []Argument `json:"pass-arguments-to-command,omitempty"`
|
PassArgumentsToCommand []Argument `json:"pass-arguments-to-command,omitempty"`
|
||||||
|
45
webhook.go
45
webhook.go
@ -6,6 +6,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
@ -67,6 +68,19 @@ var (
|
|||||||
pidFile *pidfile.PIDFile
|
pidFile *pidfile.PIDFile
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type flushWriter struct {
|
||||||
|
f http.Flusher
|
||||||
|
w io.Writer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fw *flushWriter) Write(p []byte) (n int, err error) {
|
||||||
|
n, err = fw.w.Write(p)
|
||||||
|
if fw.f != nil {
|
||||||
|
fw.f.Flush()
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func matchLoadedHook(id string) *hook.Hook {
|
func matchLoadedHook(id string) *hook.Hook {
|
||||||
for _, hooks := range loadedHooksFromFiles {
|
for _, hooks := range loadedHooksFromFiles {
|
||||||
if hook := hooks.Match(id); hook != nil {
|
if hook := hooks.Match(id); hook != nil {
|
||||||
@ -535,8 +549,10 @@ func hookHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Header().Set(responseHeader.Name, responseHeader.Value)
|
w.Header().Set(responseHeader.Name, responseHeader.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
if matchedHook.CaptureCommandOutput {
|
if matchedHook.StreamCommandOutput {
|
||||||
response, err := handleHook(matchedHook, rid, &headers, &query, &payload, &body)
|
handleHook(matchedHook, rid, &headers, &query, &payload, &body, w)
|
||||||
|
} else if matchedHook.CaptureCommandOutput {
|
||||||
|
response, err := handleHook(matchedHook, rid, &headers, &query, &payload, &body, nil)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
@ -554,7 +570,7 @@ func hookHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
fmt.Fprint(w, response)
|
fmt.Fprint(w, response)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
go handleHook(matchedHook, rid, &headers, &query, &payload, &body)
|
go handleHook(matchedHook, rid, &headers, &query, &payload, &body, nil)
|
||||||
|
|
||||||
// Check if a success return code is configured for the hook
|
// Check if a success return code is configured for the hook
|
||||||
if matchedHook.SuccessHttpResponseCode != 0 {
|
if matchedHook.SuccessHttpResponseCode != 0 {
|
||||||
@ -577,7 +593,7 @@ func hookHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
fmt.Fprint(w, "Hook rules were not satisfied.")
|
fmt.Fprint(w, "Hook rules were not satisfied.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleHook(h *hook.Hook, rid string, headers, query, payload *map[string]interface{}, body *[]byte) (string, error) {
|
func handleHook(h *hook.Hook, rid string, headers, query, payload *map[string]interface{}, body *[]byte, w http.ResponseWriter) (string, error) {
|
||||||
var errors []error
|
var errors []error
|
||||||
|
|
||||||
// check the command exists
|
// check the command exists
|
||||||
@ -646,13 +662,32 @@ func handleHook(h *hook.Hook, rid string, headers, query, payload *map[string]in
|
|||||||
|
|
||||||
log.Printf("[%s] executing %s (%s) with arguments %q and environment %s using %s as cwd\n", rid, h.ExecuteCommand, cmd.Path, cmd.Args, envs, cmd.Dir)
|
log.Printf("[%s] executing %s (%s) with arguments %q and environment %s using %s as cwd\n", rid, h.ExecuteCommand, cmd.Path, cmd.Args, envs, cmd.Dir)
|
||||||
|
|
||||||
out, err := cmd.CombinedOutput()
|
var out []byte
|
||||||
|
|
||||||
|
if w != nil {
|
||||||
|
log.Printf("[%s] command output will be streamed to response", rid)
|
||||||
|
|
||||||
|
// Implementation from https://play.golang.org/p/PpbPyXbtEs
|
||||||
|
// as described in https://stackoverflow.com/questions/19292113/not-buffered-http-responsewritter-in-golang
|
||||||
|
fw := flushWriter{w: w}
|
||||||
|
if f, ok := w.(http.Flusher); ok {
|
||||||
|
fw.f = f
|
||||||
|
}
|
||||||
|
cmd.Stderr = &fw
|
||||||
|
cmd.Stdout = &fw
|
||||||
|
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
log.Printf("[%s] error occurred: %+v\n", rid, err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
out, err = cmd.CombinedOutput()
|
||||||
|
|
||||||
log.Printf("[%s] command output: %s\n", rid, out)
|
log.Printf("[%s] command output: %s\n", rid, out)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[%s] error occurred: %+v\n", rid, err)
|
log.Printf("[%s] error occurred: %+v\n", rid, err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for i := range files {
|
for i := range files {
|
||||||
if files[i].File != nil {
|
if files[i].File != nil {
|
||||||
|
@ -53,7 +53,7 @@ func TestStaticParams(t *testing.T) {
|
|||||||
b := &bytes.Buffer{}
|
b := &bytes.Buffer{}
|
||||||
log.SetOutput(b)
|
log.SetOutput(b)
|
||||||
|
|
||||||
_, err = handleHook(spHook, "test", &spHeaders, &map[string]interface{}{}, &map[string]interface{}{}, &[]byte{})
|
_, err = handleHook(spHook, "test", &spHeaders, &map[string]interface{}{}, &map[string]interface{}{}, &[]byte{}, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unexpected error: %v\n", err)
|
t.Fatalf("Unexpected error: %v\n", err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user