Make Dispatch cleaner

This commit is contained in:
Denis Makogon
2017-09-30 18:26:25 +03:00
parent 2250e1d08c
commit caf1488dd9

View File

@@ -25,53 +25,54 @@ func (p *JSONProtocol) IsStreamable() bool {
return true return true
} }
func (h *JSONProtocol) Dispatch(w io.Writer, req *http.Request) error { func (h *JSONProtocol) DumpJSON(w io.Writer, req *http.Request) error {
_, err := io.WriteString(h.in, `{`) _, err := io.WriteString(h.in, `{`)
if err != nil { if err != nil {
// this shouldn't happen // this shouldn't happen
return respondWithError( return err
w, fmt.Errorf("error reader JSON object from request body: %s", err.Error()))
} }
if req.Body != nil { if req.Body != nil {
_, err := io.WriteString(h.in, `"body":"`) _, err := io.WriteString(h.in, `"body":"`)
if err != nil { if err != nil {
// this shouldn't happen // this shouldn't happen
return respondWithError( return err
w, fmt.Errorf("error reader JSON object from request body: %s", err.Error()))
} }
_, err = io.Copy(h.in, req.Body) _, err = io.Copy(h.in, req.Body)
if err != nil { if err != nil {
// this shouldn't happen // this shouldn't happen
return respondWithError( return err
w, fmt.Errorf("error reader JSON object from request body: %s", err.Error()))
} }
_, err = io.WriteString(h.in, `",`) _, err = io.WriteString(h.in, `",`)
if err != nil { if err != nil {
// this shouldn't happen // this shouldn't happen
return respondWithError( return err
w, fmt.Errorf("error reader JSON object from request body: %s", err.Error()))
} }
defer req.Body.Close() defer req.Body.Close()
} }
_, err = io.WriteString(h.in, `"headers:"`) _, err = io.WriteString(h.in, `"headers:"`)
if err != nil { if err != nil {
// this shouldn't happen // this shouldn't happen
return respondWithError( return err
w, fmt.Errorf("error reader JSON object from request body: %s", err.Error()))
} }
err = json.NewEncoder(h.in).Encode(req.Header) err = json.NewEncoder(h.in).Encode(req.Header)
if err != nil { if err != nil {
// this shouldn't happen // this shouldn't happen
return respondWithError( return err
w, fmt.Errorf("error marshalling JSONInput: %s", err.Error()))
} }
_, err = io.WriteString(h.in, `"}`) _, err = io.WriteString(h.in, `"}`)
if err != nil { if err != nil {
// this shouldn't happen // this shouldn't happen
return err
}
return nil
}
func (h *JSONProtocol) Dispatch(w io.Writer, req *http.Request) error {
err := h.DumpJSON(w, req)
if err != nil {
return respondWithError( return respondWithError(
w, fmt.Errorf("error reader JSON object from request body: %s", err.Error())) w, fmt.Errorf("error reader JSON object from request body: %s", err.Error()))
} }
jout := new(JSONIO) jout := new(JSONIO)
dec := json.NewDecoder(h.out) dec := json.NewDecoder(h.out)
if err := dec.Decode(jout); err != nil { if err := dec.Decode(jout); err != nil {