Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
62bc179b
Unverified
Commit
62bc179b
authored
Mar 13, 2018
by
Péter Szilágyi
Committed by
GitHub
Mar 13, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #16310 from karalabe/websocket-request-limits
rpc: enforce the 128KB request limits on websockets too
parents
6a2d2869
555f42cf
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
66 additions
and
22 deletions
+66
-22
http.go
rpc/http.go
+5
-5
http_test.go
rpc/http_test.go
+1
-1
json.go
rpc/json.go
+31
-15
websocket.go
rpc/websocket.go
+29
-1
No files found.
rpc/http.go
View file @
62bc179b
...
...
@@ -27,16 +27,16 @@ import (
"mime"
"net"
"net/http"
"strings"
"sync"
"time"
"github.com/rs/cors"
"strings"
)
const
(
contentType
=
"application/json"
max
HTTP
RequestContentLength
=
1024
*
128
contentType
=
"application/json"
maxRequestContentLength
=
1024
*
128
)
var
nullAddr
,
_
=
net
.
ResolveTCPAddr
(
"tcp"
,
"127.0.0.1:0"
)
...
...
@@ -182,8 +182,8 @@ func validateRequest(r *http.Request) (int, error) {
if
r
.
Method
==
http
.
MethodPut
||
r
.
Method
==
http
.
MethodDelete
{
return
http
.
StatusMethodNotAllowed
,
errors
.
New
(
"method not allowed"
)
}
if
r
.
ContentLength
>
max
HTTP
RequestContentLength
{
err
:=
fmt
.
Errorf
(
"content length too large (%d>%d)"
,
r
.
ContentLength
,
max
HTTP
RequestContentLength
)
if
r
.
ContentLength
>
maxRequestContentLength
{
err
:=
fmt
.
Errorf
(
"content length too large (%d>%d)"
,
r
.
ContentLength
,
maxRequestContentLength
)
return
http
.
StatusRequestEntityTooLarge
,
err
}
mt
,
_
,
err
:=
mime
.
ParseMediaType
(
r
.
Header
.
Get
(
"content-type"
))
...
...
rpc/http_test.go
View file @
62bc179b
...
...
@@ -32,7 +32,7 @@ func TestHTTPErrorResponseWithPut(t *testing.T) {
}
func
TestHTTPErrorResponseWithMaxContentLength
(
t
*
testing
.
T
)
{
body
:=
make
([]
rune
,
max
HTTP
RequestContentLength
+
1
)
body
:=
make
([]
rune
,
maxRequestContentLength
+
1
)
testHTTPErrorResponse
(
t
,
http
.
MethodPost
,
contentType
,
string
(
body
),
http
.
StatusRequestEntityTooLarge
)
}
...
...
rpc/json.go
View file @
62bc179b
...
...
@@ -76,13 +76,13 @@ type jsonNotification struct {
// jsonCodec reads and writes JSON-RPC messages to the underlying connection. It
// also has support for parsing arguments and serializing (result) objects.
type
jsonCodec
struct
{
closer
sync
.
Once
// close closed channel once
closed
chan
interface
{}
// closed on Close
decMu
sync
.
Mutex
// guards d
d
*
json
.
Decoder
// decodes incoming reques
ts
encMu
sync
.
Mutex
// guards e
e
*
json
.
Encoder
// encodes response
s
rw
io
.
ReadWriteCloser
// connection
closer
sync
.
Once
// close closed channel once
closed
chan
interface
{}
// closed on Close
decMu
sync
.
Mutex
// guards the decoder
d
ecode
func
(
v
interface
{})
error
// decoder to allow multiple transpor
ts
encMu
sync
.
Mutex
// guards the encoder
e
ncode
func
(
v
interface
{})
error
// encoder to allow multiple transport
s
rw
io
.
ReadWriteCloser
// connection
}
func
(
err
*
jsonError
)
Error
()
string
{
...
...
@@ -96,11 +96,29 @@ func (err *jsonError) ErrorCode() int {
return
err
.
Code
}
// NewJSONCodec creates a new RPC server codec with support for JSON-RPC 2.0
// NewCodec creates a new RPC server codec with support for JSON-RPC 2.0 based
// on explicitly given encoding and decoding methods.
func
NewCodec
(
rwc
io
.
ReadWriteCloser
,
encode
,
decode
func
(
v
interface
{})
error
)
ServerCodec
{
return
&
jsonCodec
{
closed
:
make
(
chan
interface
{}),
encode
:
encode
,
decode
:
decode
,
rw
:
rwc
,
}
}
// NewJSONCodec creates a new RPC server codec with support for JSON-RPC 2.0.
func
NewJSONCodec
(
rwc
io
.
ReadWriteCloser
)
ServerCodec
{
d
:=
json
.
NewDecoder
(
rwc
)
d
.
UseNumber
()
return
&
jsonCodec
{
closed
:
make
(
chan
interface
{}),
d
:
d
,
e
:
json
.
NewEncoder
(
rwc
),
rw
:
rwc
}
enc
:=
json
.
NewEncoder
(
rwc
)
dec
:=
json
.
NewDecoder
(
rwc
)
dec
.
UseNumber
()
return
&
jsonCodec
{
closed
:
make
(
chan
interface
{}),
encode
:
enc
.
Encode
,
decode
:
dec
.
Decode
,
rw
:
rwc
,
}
}
// isBatch returns true when the first non-whitespace characters is '['
...
...
@@ -123,14 +141,12 @@ func (c *jsonCodec) ReadRequestHeaders() ([]rpcRequest, bool, Error) {
defer
c
.
decMu
.
Unlock
()
var
incomingMsg
json
.
RawMessage
if
err
:=
c
.
d
.
D
ecode
(
&
incomingMsg
);
err
!=
nil
{
if
err
:=
c
.
decode
(
&
incomingMsg
);
err
!=
nil
{
return
nil
,
false
,
&
invalidRequestError
{
err
.
Error
()}
}
if
isBatch
(
incomingMsg
)
{
return
parseBatchRequest
(
incomingMsg
)
}
return
parseRequest
(
incomingMsg
)
}
...
...
@@ -338,7 +354,7 @@ func (c *jsonCodec) Write(res interface{}) error {
c
.
encMu
.
Lock
()
defer
c
.
encMu
.
Unlock
()
return
c
.
e
.
E
ncode
(
res
)
return
c
.
encode
(
res
)
}
// Close the underlying connection
...
...
rpc/websocket.go
View file @
62bc179b
...
...
@@ -17,8 +17,10 @@
package
rpc
import
(
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net"
"net/http"
...
...
@@ -32,6 +34,23 @@ import (
"gopkg.in/fatih/set.v0"
)
// websocketJSONCodec is a custom JSON codec with payload size enforcement and
// special number parsing.
var
websocketJSONCodec
=
websocket
.
Codec
{
// Marshal is the stock JSON marshaller used by the websocket library too.
Marshal
:
func
(
v
interface
{})
([]
byte
,
byte
,
error
)
{
msg
,
err
:=
json
.
Marshal
(
v
)
return
msg
,
websocket
.
TextFrame
,
err
},
// Unmarshal is a specialized unmarshaller to properly convert numbers.
Unmarshal
:
func
(
msg
[]
byte
,
payloadType
byte
,
v
interface
{})
error
{
dec
:=
json
.
NewDecoder
(
bytes
.
NewReader
(
msg
))
dec
.
UseNumber
()
return
dec
.
Decode
(
v
)
},
}
// WebsocketHandler returns a handler that serves JSON-RPC to WebSocket connections.
//
// allowedOrigins should be a comma-separated list of allowed origin URLs.
...
...
@@ -40,7 +59,16 @@ func (srv *Server) WebsocketHandler(allowedOrigins []string) http.Handler {
return
websocket
.
Server
{
Handshake
:
wsHandshakeValidator
(
allowedOrigins
),
Handler
:
func
(
conn
*
websocket
.
Conn
)
{
srv
.
ServeCodec
(
NewJSONCodec
(
conn
),
OptionMethodInvocation
|
OptionSubscriptions
)
// Create a custom encode/decode pair to enforce payload size and number encoding
conn
.
MaxPayloadBytes
=
maxRequestContentLength
encoder
:=
func
(
v
interface
{})
error
{
return
websocketJSONCodec
.
Send
(
conn
,
v
)
}
decoder
:=
func
(
v
interface
{})
error
{
return
websocketJSONCodec
.
Receive
(
conn
,
v
)
}
srv
.
ServeCodec
(
NewCodec
(
conn
,
encoder
,
decoder
),
OptionMethodInvocation
|
OptionSubscriptions
)
},
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment