summaryrefslogtreecommitdiff
path: root/http/response.go
diff options
context:
space:
mode:
Diffstat (limited to 'http/response.go')
-rw-r--r--http/response.go18
1 files changed, 15 insertions, 3 deletions
diff --git a/http/response.go b/http/response.go
index 2736c4ab..dd092353 100644
--- a/http/response.go
+++ b/http/response.go
@@ -4,6 +4,7 @@ import (
"encoding/json"
"github.com/spiral/roadrunner"
"net/http"
+ "io"
)
// Response handles PSR7 response logic.
@@ -15,7 +16,7 @@ type Response struct {
Headers map[string][]string `json:"headers"`
// associated body payload.
- body []byte
+ body interface{}
}
// NewResponse creates new response based on given roadrunner payload.
@@ -29,7 +30,7 @@ func NewResponse(p *roadrunner.Payload) (*Response, error) {
}
// Write writes response headers, status and body into ResponseWriter.
-func (r *Response) Write(w http.ResponseWriter) {
+func (r *Response) Write(w http.ResponseWriter) error {
for k, v := range r.Headers {
for _, h := range v {
w.Header().Add(k, h)
@@ -38,5 +39,16 @@ func (r *Response) Write(w http.ResponseWriter) {
}
w.WriteHeader(r.Status)
- w.Write(r.body)
+
+ if data, ok := r.body.([]byte); ok {
+ w.Write(data)
+ }
+
+ if rc, ok := r.body.(io.Reader); ok {
+ if _, err := io.Copy(w, rc); err != nil {
+ return err
+ }
+ }
+
+ return nil
}