mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-19 22:44:24 +00:00
rpc: rework how responses are written back via HTTP (#7575)
Add writeRPCResponse and writeHTTPResponse helpers, that handle the way RPC responses are written to HTTP replies. These replace the exported helpers. Visible effects: - JSON results are now marshaled without indentation. - HTTP status codes are now normalized. - Cache control headers are no longer set. Details: - When writing a response to a URL (GET) request, do not marshal the whole JSON-RPC object into the body, only encode the result or the error object. This is a user-visible change. - Do not change the HTTP status code for RPC errors. The RPC error already reports what went wrong, the HTTP status should only report problems with the HTTP transaction itself. This is a user-visible change. - Encode JSON without indentation in POST response bodies. This is mainly cosmetic but saves quite a bit of response data. Indent is still applied to GET responses to make life easier for code examples. - Remove an obsolete TODO about reporting an HTTP error on websocket upgrade. Nothing needed to change; the upgrader already reports an error. - Report an HTTP error when starting the server loop fails. - Improve logging for encoding errors. - Log less aggressively.
This commit is contained in:
@@ -21,22 +21,18 @@ import (
|
||||
// jsonrpc calls grab the given method's function info and runs reflect.Call
|
||||
func makeJSONRPCHandler(funcMap map[string]*RPCFunc, logger log.Logger) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, hreq *http.Request) {
|
||||
fail := func(res rpctypes.RPCResponse) {
|
||||
if err := WriteRPCResponseHTTPError(w, res); err != nil {
|
||||
logger.Error("Failed writing error response", "res", res, "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
// For POST requests, reject a non-root URL path. This should not happen
|
||||
// in the standard configuration, since the wrapper checks the path.
|
||||
if hreq.URL.Path != "/" {
|
||||
fail(rpctypes.RPCInvalidRequestError(nil, fmt.Errorf("invalid path: %q", hreq.URL.Path)))
|
||||
writeRPCResponse(w, logger, rpctypes.RPCInvalidRequestError(
|
||||
nil, fmt.Errorf("invalid path: %q", hreq.URL.Path)))
|
||||
return
|
||||
}
|
||||
|
||||
b, err := io.ReadAll(hreq.Body)
|
||||
if err != nil {
|
||||
fail(rpctypes.RPCInvalidRequestError(nil, fmt.Errorf("reading request body: %w", err)))
|
||||
writeRPCResponse(w, logger, rpctypes.RPCInvalidRequestError(
|
||||
nil, fmt.Errorf("reading request body: %w", err)))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -49,7 +45,7 @@ func makeJSONRPCHandler(funcMap map[string]*RPCFunc, logger log.Logger) http.Han
|
||||
|
||||
requests, err := parseRequests(b)
|
||||
if err != nil {
|
||||
fail(rpctypes.RPCParseError(fmt.Errorf("decoding request: %w", err)))
|
||||
writeRPCResponse(w, logger, rpctypes.RPCParseError(fmt.Errorf("decoding request: %w", err)))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -98,11 +94,10 @@ func makeJSONRPCHandler(funcMap map[string]*RPCFunc, logger log.Logger) http.Han
|
||||
}
|
||||
}
|
||||
|
||||
if len(responses) > 0 {
|
||||
if wErr := WriteRPCResponseHTTP(w, responses...); wErr != nil {
|
||||
logger.Error("failed to write responses", "err", wErr)
|
||||
}
|
||||
if len(responses) == 0 {
|
||||
return
|
||||
}
|
||||
writeRPCResponse(w, logger, responses...)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user