mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Revisiting request body processing
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -25,6 +26,23 @@ func (p *JSONProtocol) IsStreamable() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
type RequestEncoder struct {
|
||||
*json.Encoder
|
||||
}
|
||||
|
||||
func (re *RequestEncoder) EncodeRequest(rq *http.Request) error {
|
||||
bb := new(bytes.Buffer)
|
||||
_, err := bb.ReadFrom(rq.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer bb.Reset()
|
||||
return re.Encode(JSONIO{
|
||||
Headers: rq.Header,
|
||||
Body: bb.String(),
|
||||
})
|
||||
}
|
||||
|
||||
func (h *JSONProtocol) DumpJSON(w io.Writer, req *http.Request) error {
|
||||
_, err := io.WriteString(h.in, `{`)
|
||||
if err != nil {
|
||||
@@ -68,7 +86,9 @@ func (h *JSONProtocol) DumpJSON(w io.Writer, req *http.Request) error {
|
||||
}
|
||||
|
||||
func (h *JSONProtocol) Dispatch(w io.Writer, req *http.Request) error {
|
||||
err := h.DumpJSON(w, req)
|
||||
ce := RequestEncoder{json.NewEncoder(h.in)}
|
||||
err := ce.EncodeRequest(req)
|
||||
//err := h.DumpJSON(w, req)
|
||||
if err != nil {
|
||||
return respondWithError(
|
||||
w, fmt.Errorf("unable to write JSON into STDIN: %s", err.Error()))
|
||||
|
||||
Reference in New Issue
Block a user