Skip to content

Commit 8bf45ff

Browse files
committed
additional cleanup
1 parent f05fc4d commit 8bf45ff

3 files changed

Lines changed: 10 additions & 9 deletions

File tree

rpc/http.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,6 @@ func (s *Server) newHTTPServerConn(r *http.Request, w http.ResponseWriter) Serve
284284
return httpWrite(ctx, w, buf, isError)
285285
}
286286

287-
dec := json.NewDecoder(conn)
288-
dec.UseNumber()
289-
290287
// The body holds one message, so it can be read in one go and checked once.
291288
readFrame := func() ([]byte, error) {
292289
hint := 0
@@ -304,7 +301,7 @@ func (s *Server) newHTTPServerConn(r *http.Request, w http.ResponseWriter) Serve
304301
}
305302
return frame, nil
306303
}
307-
return newFuncCodec(conn, encodeMsg, encodeBatch, dec.Decode, readFrame)
304+
return newFuncCodec(conn, encodeMsg, encodeBatch, nil, readFrame)
308305
}
309306

310307
// readAllBody reads r to the end, sizing the buffer from the hint when there is

rpc/json.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ func NewFuncCodec(conn deadlineCloser, encodeMsg encodeMsgFunc, encodeBatch enco
226226
}
227227

228228
// newFuncCodec is NewFuncCodec with the frame reader the built in transports use.
229+
// A transport with a frame reader never calls decode, so it may be nil.
229230
func newFuncCodec(conn deadlineCloser, encodeMsg encodeMsgFunc, encodeBatch encodeBatchFunc, decode decodeFunc, readFrame readFrameFunc) *jsonCodec {
230231
codec := &jsonCodec{
231232
closeCh: make(chan interface{}),
@@ -346,12 +347,15 @@ func (c *jsonCodec) readMessage() (json.RawMessage, error) {
346347
return nil, err
347348
}
348349
if !json.Valid(frame) {
349-
// Decode the broken message to report where it went wrong.
350+
// Decode the broken message to report where it went wrong. Unmarshal
351+
// checks syntax the same way Valid does, so it fails here too. The
352+
// fallback only guards against the two ever disagreeing.
350353
var rawmsg json.RawMessage
351-
if err := json.Unmarshal(frame, &rawmsg); err != nil {
352-
return nil, err
354+
err := json.Unmarshal(frame, &rawmsg)
355+
if err == nil {
356+
err = errors.New("invalid JSON request")
353357
}
354-
return nil, errors.New("invalid JSON request")
358+
return nil, err
355359
}
356360
return frame, nil
357361
}

rpc/websocket.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ func newWebsocketCodec(conn *websocket.Conn, host string, req http.Header, readL
308308
return frame, err
309309
}
310310
wc := &websocketCodec{
311-
jsonCodec: newFuncCodec(conn, encodeMsg, encodeBatch, conn.ReadJSON, readFrame),
311+
jsonCodec: newFuncCodec(conn, encodeMsg, encodeBatch, nil, readFrame),
312312
conn: conn,
313313
pingReset: make(chan struct{}, 1),
314314
pongReceived: make(chan struct{}),

0 commit comments

Comments
 (0)