rpc: use jsonw for string quoting

This commit is contained in:
Felix Lange 2026-05-19 17:40:36 +02:00
parent 469cd8d781
commit 3939e4c8f0
3 changed files with 6 additions and 53 deletions

2
go.mod
View file

@ -83,7 +83,7 @@ require (
require (
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
github.com/fjl/jsonw v0.0.0-20260519133611-b234a3a62a01 // indirect
github.com/fjl/jsonw v0.0.0-20260519153618-cb1f61b11f55 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.7 // indirect

2
go.sum
View file

@ -131,6 +131,8 @@ github.com/fjl/jsonw v0.0.0-20260518201611-f2cd7df7ef66 h1:B+iPMRxXE3dbWXwQX6Un0
github.com/fjl/jsonw v0.0.0-20260518201611-f2cd7df7ef66/go.mod h1:2KMLevM6FXEJnfhtk7naXu9vZdVfOma1GlnGdPRlumU=
github.com/fjl/jsonw v0.0.0-20260519133611-b234a3a62a01 h1:2fvSvrrQMWs3l3MY1Ot4cPzv3Iww17ha1KJwjymX+Ks=
github.com/fjl/jsonw v0.0.0-20260519133611-b234a3a62a01/go.mod h1:2KMLevM6FXEJnfhtk7naXu9vZdVfOma1GlnGdPRlumU=
github.com/fjl/jsonw v0.0.0-20260519153618-cb1f61b11f55 h1:QH056XH5XG73Mr/f0BUFwNVsbRxauNDaSF1fBnIlF2Q=
github.com/fjl/jsonw v0.0.0-20260519153618-cb1f61b11f55/go.mod h1:2KMLevM6FXEJnfhtk7naXu9vZdVfOma1GlnGdPRlumU=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=

View file

@ -27,7 +27,8 @@ import (
"strings"
"sync"
"time"
"unicode/utf8"
"github.com/fjl/jsonw"
)
const (
@ -257,7 +258,7 @@ func appendMessage(buf []byte, msg *jsonrpcMessage) []byte {
}
if msg.Method != "" {
buf = append(buf, `,"method":`...)
buf = appendQuotedString(buf, msg.Method)
buf = jsonw.AppendQuotedString(buf, msg.Method)
}
if msg.Params != nil {
buf = append(buf, `,"params":`...)
@ -288,56 +289,6 @@ func appendBatch(buf []byte, msgs []*jsonrpcMessage) []byte {
return buf
}
const hexDigits = "0123456789abcdef"
// appendQuotedString appends a JSON-quoted string to buf. Adapted
// from encoding/json appendString without HTML and JSONP safety escaping.
func appendQuotedString(buf []byte, s string) []byte {
buf = append(buf, '"')
start := 0
for i := 0; i < len(s); {
if b := s[i]; b < utf8.RuneSelf {
if b >= 0x20 && b != '\\' && b != '"' {
i++
continue
}
buf = append(buf, s[start:i]...)
switch b {
case '\\', '"':
buf = append(buf, '\\', b)
case '\b':
buf = append(buf, '\\', 'b')
case '\f':
buf = append(buf, '\\', 'f')
case '\n':
buf = append(buf, '\\', 'n')
case '\r':
buf = append(buf, '\\', 'r')
case '\t':
buf = append(buf, '\\', 't')
default:
// This encodes bytes < 0x20 except for \b, \f, \n, \r and \t.
buf = append(buf, '\\', 'u', '0', '0', hexDigits[b>>4], hexDigits[b&0xF])
}
i++
start = i
continue
}
c, size := utf8.DecodeRuneInString(s[i:])
if c == utf8.RuneError && size == 1 {
buf = append(buf, s[start:i]...)
buf = append(buf, `\ufffd`...)
i += size
start = i
continue
}
i += size
}
buf = append(buf, s[start:]...)
buf = append(buf, '"')
return buf
}
func (c *jsonCodec) peerInfo() PeerInfo {
// This returns "ipc" because all other built-in transports have a separate codec type.
return PeerInfo{Transport: "ipc", RemoteAddr: c.remote}