mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Trying to avoid buffers and write directly to pipe
this change makes Dispatch write request body and http headers directly to pipe one by one in case of non-empty request body, if not - write headers and close finalize JSON
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package protocol
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -27,27 +26,51 @@ func (p *JSONProtocol) IsStreamable() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *JSONProtocol) Dispatch(w io.Writer, req *http.Request) error {
|
func (h *JSONProtocol) Dispatch(w io.Writer, req *http.Request) error {
|
||||||
var body bytes.Buffer
|
_, err := io.WriteString(h.in, `{`)
|
||||||
if req.Body != nil {
|
|
||||||
var dest io.Writer = &body
|
|
||||||
|
|
||||||
// TODO copy w/ ctx
|
|
||||||
_, err := io.Copy(dest, req.Body)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// this shouldn't happen
|
||||||
|
return respondWithError(
|
||||||
|
w, fmt.Errorf("error reader JSON object from request body: %s", err.Error()))
|
||||||
|
}
|
||||||
|
if req.Body != nil {
|
||||||
|
_, err := io.WriteString(h.in, `"body":"`)
|
||||||
|
if err != nil {
|
||||||
|
// this shouldn't happen
|
||||||
|
return respondWithError(
|
||||||
|
w, fmt.Errorf("error reader JSON object from request body: %s", err.Error()))
|
||||||
|
}
|
||||||
|
_, err = io.CopyN(h.in, req.Body, req.ContentLength)
|
||||||
|
if err != nil {
|
||||||
|
// this shouldn't happen
|
||||||
|
return respondWithError(
|
||||||
|
w, fmt.Errorf("error reader JSON object from request body: %s", err.Error()))
|
||||||
|
}
|
||||||
|
_, err = io.WriteString(h.in, `",`)
|
||||||
|
if err != nil {
|
||||||
|
// this shouldn't happen
|
||||||
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()))
|
||||||
}
|
}
|
||||||
defer req.Body.Close()
|
defer req.Body.Close()
|
||||||
}
|
}
|
||||||
err := json.NewEncoder(h.in).Encode(&JSONIO{
|
_, err = io.WriteString(h.in, `"headers:"`)
|
||||||
Headers: req.Header,
|
if err != nil {
|
||||||
Body: body.String(),
|
// this shouldn't happen
|
||||||
})
|
return respondWithError(
|
||||||
|
w, fmt.Errorf("error reader JSON object from request body: %s", err.Error()))
|
||||||
|
}
|
||||||
|
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 respondWithError(
|
||||||
w, fmt.Errorf("error marshalling JSONInput: %s", err.Error()))
|
w, fmt.Errorf("error marshalling JSONInput: %s", err.Error()))
|
||||||
}
|
}
|
||||||
|
_, err = io.WriteString(h.in, `"}`)
|
||||||
|
if err != nil {
|
||||||
|
// this shouldn't happen
|
||||||
|
return respondWithError(
|
||||||
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user