Fix/rpcdaemon ws upgrade (#3490)

* Added a method to `cmd/rpcdaemon/cli/config.go` to check header of incoming http request for a ws upgrade request

* Added the testing of the 'GET /' request for ws to the devnet tool

* Fixed lint errors
This commit is contained in:
leonardchinonso 2022-02-12 12:40:19 +01:00 committed by GitHub
parent 8d7eca012a
commit aca755ab43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 1 deletions

View File

@ -0,0 +1,18 @@
package commands
import (
"github.com/ledgerwatch/erigon/cmd/devnettest/requests"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(mockRequestCmd)
}
var mockRequestCmd = &cobra.Command{
Use: "mock",
Short: "Mocks a request on the devnet",
Run: func(cmd *cobra.Command, args []string) {
requests.MockGetRequest(reqId)
},
}

View File

@ -0,0 +1,16 @@
package requests
import "fmt"
func MockGetRequest(reqId int) {
reqGen := initialiseRequestGenerator(reqId)
res := reqGen.Get()
if res.Err != nil {
fmt.Printf("error: %v\n", res.Err)
return
}
fmt.Printf("OK\n")
}

View File

@ -1,7 +1,9 @@
package requests
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"time"
@ -34,6 +36,39 @@ func initialiseRequestGenerator(reqId int) *RequestGenerator {
return &reqGen
}
func (req *RequestGenerator) Get() rpctest.CallResult {
start := time.Now()
res := rpctest.CallResult{
RequestID: req.reqID,
}
resp, err := http.Get(erigonUrl)
if err != nil {
res.Took = time.Since(start)
res.Err = err
return res
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
res.Took = time.Since(start)
res.Err = errors.New("bad request")
return res
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
res.Took = time.Since(start)
res.Err = err
return res
}
res.Response = body
res.Took = time.Since(start)
res.Err = err
return res
}
func (req *RequestGenerator) Erigon(method, body string, response interface{}) rpctest.CallResult {
return req.call(erigonUrl, method, body, response)
}

View File

@ -9,6 +9,7 @@ import (
"net"
"net/http"
"path"
"strings"
"time"
"github.com/ledgerwatch/erigon-lib/gointerfaces"
@ -498,13 +499,19 @@ func StartRpcServer(ctx context.Context, cfg Flags, rpcAPI []rpc.API) error {
return nil
}
// isWebsocket checks the header of a http request for a websocket upgrade request.
func isWebsocket(r *http.Request) bool {
return strings.ToLower(r.Header.Get("Upgrade")) == "websocket" &&
strings.Contains(strings.ToLower(r.Header.Get("Connection")), "upgrade")
}
func createHandler(cfg Flags, apiList []rpc.API, httpHandler http.Handler, wsHandler http.Handler) http.Handler {
var handler http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// adding a healthcheck here
if health.ProcessHealthcheckIfNeeded(w, r, apiList) {
return
}
if cfg.WebsocketEnabled && wsHandler != nil && r.Method == "GET" {
if cfg.WebsocketEnabled && wsHandler != nil && isWebsocket(r) {
wsHandler.ServeHTTP(w, r)
return
}