forked from ghetzel/diecast
/
actions_steps_respond.go
41 lines (31 loc) · 1.03 KB
/
actions_steps_respond.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package diecast
import (
"net/http"
"github.com/PerformLine/go-stockutil/maputil"
)
// [type=respond] Configure the HTTP response headers and status.
// -------------------------------------------------------------------------------------------------
type RespondStep struct{}
func (self *RespondStep) Perform(config *StepConfig, w http.ResponseWriter, req *http.Request, prev *StepConfig) (interface{}, error) {
var opts = maputil.M(config.Data)
var status = int(opts.Int(`status`))
var data = prev.Output
config.logstep("prev=%v input=%T", prev, data)
if headers := opts.Map(`headers`); len(headers) > 0 {
for k, v := range headers {
w.Header().Set(k.String(), v.String())
}
}
if redirect := opts.String(`redirect`); redirect != `` {
w.Header().Set(`Location`, redirect)
if status >= 300 && status < 400 {
w.WriteHeader(status)
} else {
w.WriteHeader(http.StatusTemporaryRedirect)
}
} else if status > 0 {
w.WriteHeader(status)
}
// TODO: check for 204/205 status and empty the body out
return data, nil
}