Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions internal/rpc/jsonrpc/server.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2017-2024 The Decred developers
// Copyright (c) 2017-2026 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -86,7 +86,7 @@ type handler struct {
// jsonAuthFail sends a message back to the client if the http auth is rejected.
func jsonAuthFail(w http.ResponseWriter) {
w.Header().Add("WWW-Authenticate", `Basic realm="dcrwallet RPC"`)
http.Error(w, "401 Unauthorized.", http.StatusUnauthorized)
http.Error(w, "401 Unauthorized", http.StatusUnauthorized)
}

// NewServer creates a new server for serving JSON-RPC client connections,
Expand Down Expand Up @@ -538,10 +538,16 @@ func (s *Server) postClientRPC(w http.ResponseWriter, r *http.Request) {
body := http.MaxBytesReader(w, r.Body, maxRequestSize)
rpcRequest, err := io.ReadAll(body)
if err != nil {
// TODO: what if the underlying reader errored?
log.Warnf("Request from client %v exceeds maximum size", r.RemoteAddr)
http.Error(w, "413 Request Too Large.",
http.StatusRequestEntityTooLarge)
var maxBytesError *http.MaxBytesError
if errors.As(err, &maxBytesError) {
log.Warnf("Request from client %s exceeds maximum size", r.RemoteAddr)
http.Error(w, "413 Request Too Large",
http.StatusRequestEntityTooLarge)
return
}

log.Warnf("Failed to read request from client %s", r.RemoteAddr)
http.Error(w, "Request read failed", http.StatusInternalServerError)
return
}

Expand All @@ -562,7 +568,7 @@ func (s *Server) postClientRPC(w http.ResponseWriter, r *http.Request) {
}
_, err = w.Write(resp)
if err != nil {
log.Warnf("Cannot write invalid request request to "+
log.Warnf("Cannot write invalid request response to "+
"client %s: %v", r.RemoteAddr, err)
}
return
Expand Down
Loading