Skip to content

Commit

Permalink
do not reply with Parse Error on EOF, #16
Browse files Browse the repository at this point in the history
  • Loading branch information
powerman committed Mar 19, 2017
1 parent d10c0e9 commit f4328ab
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions jsonrpc2/http_test.go
Expand Up @@ -50,8 +50,8 @@ func TestHTTPServer(t *testing.T) {
{"POST", contentType, contentType, jNotify, http.StatusNoContent, ""},
{"POST", contentType, contentType, jSum, http.StatusOK, jRes},
{"POST", contentType, contentType, jBad, http.StatusOK, jErr},
{"POST", contentType, contentType, "", http.StatusOK, jParse},
{"POST", contentType, contentType, " ", http.StatusOK, jParse},
{"POST", contentType, contentType, "", http.StatusNoContent, ""},
{"POST", contentType, contentType, " ", http.StatusNoContent, ""},
{"POST", contentType, contentType, "{", http.StatusOK, jParse},
{"POST", contentType, contentType, `{"jsonrpc":"2.0",`, http.StatusOK, jParse},
{"POST", contentType + "; charset=utf-8", contentType, jSum, http.StatusOK, jRes},
Expand Down
8 changes: 5 additions & 3 deletions jsonrpc2/server.go
Expand Up @@ -144,9 +144,11 @@ func (c *serverCodec) ReadRequestHeader(r *rpc.Request) (err error) {
// So, try to send error reply to client before returning error.
var raw json.RawMessage
if err := c.dec.Decode(&raw); err != nil {
c.encmutex.Lock()
c.enc.Encode(serverResponse{Version: "2.0", ID: &null, Error: errParse})
c.encmutex.Unlock()
if err != io.EOF {
c.encmutex.Lock()
c.enc.Encode(serverResponse{Version: "2.0", ID: &null, Error: errParse})
c.encmutex.Unlock()
}
return err
}

Expand Down

0 comments on commit f4328ab

Please sign in to comment.