Revisiting request body processing

This commit is contained in:
Denis Makogon
2017-10-01 14:12:38 +03:00
parent 1f589d641e
commit c2ee67fb21
12 changed files with 86 additions and 45 deletions

View File

@@ -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()))