Skip to content

Commit 3274db1

Browse files
committed
Merge pull request bnb-chain#2169 from karalabe/fix-http-rpc-method
rpc: allow RPC requests on GET too
2 parents a50bccc + 72c255c commit 3274db1

1 file changed

Lines changed: 17 additions & 23 deletions

File tree

‎rpc/http.go‎

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,19 @@ package rpc
1818

1919
import (
2020
"bufio"
21+
"bytes"
22+
"encoding/json"
23+
"errors"
2124
"fmt"
2225
"io"
26+
"io/ioutil"
2327
"net"
2428
"net/http"
29+
"net/url"
2530
"strconv"
2631
"strings"
27-
"time"
28-
29-
"errors"
3032
"sync"
31-
32-
"bytes"
33-
"encoding/json"
34-
"io/ioutil"
35-
"net/url"
33+
"time"
3634

3735
"github.com/ethereum/go-ethereum/logger"
3836
"github.com/ethereum/go-ethereum/logger/glog"
@@ -44,8 +42,8 @@ const (
4442
)
4543

4644
var (
47-
httpServerMu sync.Mutex // prevent concurrent access to the httpListener and httpServer
48-
httpListener net.Listener // listener for the http server
45+
httpServerMu sync.Mutex // prevent concurrent access to the httpListener and httpServer
46+
httpListener net.Listener // listener for the http server
4947
httpRPCServer *Server // the node can only start 1 HTTP RPC server instance
5048
)
5149

@@ -57,8 +55,7 @@ var (
5755
type httpMessageStream struct {
5856
conn net.Conn // TCP connection
5957
rw *bufio.ReadWriter // buffered where HTTP requests/responses are read/written from/to
60-
currentReq *http.Request // pending request, codec can pass in a too small buffer for a single read
61-
// we need to keep track of the current requests if it was not read at once
58+
currentReq *http.Request // pending request, codec can pass in a too small buffer for a single read we need to keep track of the current requests if it was not read at once
6259
payloadBytesRead int64 // number of bytes which are read from the current request
6360
allowedOrigins *set.Set // allowed CORS domains
6461
origin string // origin of this connection/request
@@ -147,7 +144,7 @@ func (h *httpMessageStream) Read(buf []byte) (n int, err error) {
147144
continue
148145
}
149146

150-
if strings.EqualFold(h.currentReq.Method, "POST") {
147+
if strings.EqualFold(h.currentReq.Method, "GET") || strings.EqualFold(h.currentReq.Method, "POST") {
151148
n, err := h.currentReq.Body.Read(buf)
152149
h.payloadBytesRead += int64(n)
153150

@@ -164,12 +161,9 @@ func (h *httpMessageStream) Read(buf []byte) (n int, err error) {
164161
h.currentReq = nil
165162
h.payloadBytesRead = 0
166163
}
167-
168164
// partial read of body
169165
return n, err
170166
}
171-
172-
h.currentReq = nil
173167
return 0, fmt.Errorf("unsupported HTTP method '%s'", h.currentReq.Method)
174168
}
175169
}
@@ -212,17 +206,17 @@ func httpTimestamp(t time.Time) []byte {
212206
t = t.UTC()
213207
yy, mm, dd := t.Date()
214208
hh, mn, ss := t.Clock()
215-
day := days[3 * t.Weekday():]
216-
mon := months[3 * (mm - 1):]
209+
day := days[3*t.Weekday():]
210+
mon := months[3*(mm-1):]
217211

218212
return append(b,
219213
day[0], day[1], day[2], ',', ' ',
220-
byte('0' + dd / 10), byte('0' + dd % 10), ' ',
214+
byte('0'+dd/10), byte('0'+dd%10), ' ',
221215
mon[0], mon[1], mon[2], ' ',
222-
byte('0' + yy / 1000), byte('0' + (yy / 100) % 10), byte('0' + (yy / 10) % 10), byte('0' + yy % 10), ' ',
223-
byte('0' + hh / 10), byte('0' + hh % 10), ':',
224-
byte('0' + mn / 10), byte('0' + mn % 10), ':',
225-
byte('0' + ss / 10), byte('0' + ss % 10), ' ',
216+
byte('0'+yy/1000), byte('0'+(yy/100)%10), byte('0'+(yy/10)%10), byte('0'+yy%10), ' ',
217+
byte('0'+hh/10), byte('0'+hh%10), ':',
218+
byte('0'+mn/10), byte('0'+mn%10), ':',
219+
byte('0'+ss/10), byte('0'+ss%10), ' ',
226220
'G', 'M', 'T')
227221
}
228222

0 commit comments

Comments
 (0)