github.com/ethw3/go-ethereuma@v0.0.0-20221013053120-c14602a4c23c/rpc/http.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package rpc
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"encoding/json"
    23  	"errors"
    24  	"fmt"
    25  	"io"
    26  	"mime"
    27  	"net/http"
    28  	"net/url"
    29  	"sync"
    30  	"time"
    31  )
    32  
    33  const (
    34  	maxRequestContentLength = 1024 * 1024 * 5
    35  	contentType             = "application/json"
    36  )
    37  
    38  // https://www.jsonrpc.org/historical/json-rpc-over-http.html#id13
    39  var acceptedContentTypes = []string{contentType, "application/json-rpc", "application/jsonrequest"}
    40  
    41  type httpConn struct {
    42  	client    *http.Client
    43  	url       string
    44  	closeOnce sync.Once
    45  	closeCh   chan interface{}
    46  	mu        sync.Mutex // protects headers
    47  	headers   http.Header
    48  }
    49  
    50  // httpConn implements ServerCodec, but it is treated specially by Client
    51  // and some methods don't work. The panic() stubs here exist to ensure
    52  // this special treatment is correct.
    53  
    54  func (hc *httpConn) writeJSON(context.Context, interface{}) error {
    55  	panic("writeJSON called on httpConn")
    56  }
    57  
    58  func (hc *httpConn) peerInfo() PeerInfo {
    59  	panic("peerInfo called on httpConn")
    60  }
    61  
    62  func (hc *httpConn) remoteAddr() string {
    63  	return hc.url
    64  }
    65  
    66  func (hc *httpConn) readBatch() ([]*jsonrpcMessage, bool, error) {
    67  	<-hc.closeCh
    68  	return nil, false, io.EOF
    69  }
    70  
    71  func (hc *httpConn) close() {
    72  	hc.closeOnce.Do(func() { close(hc.closeCh) })
    73  }
    74  
    75  func (hc *httpConn) closed() <-chan interface{} {
    76  	return hc.closeCh
    77  }
    78  
    79  // HTTPTimeouts represents the configuration params for the HTTP RPC server.
    80  type HTTPTimeouts struct {
    81  	// ReadTimeout is the maximum duration for reading the entire
    82  	// request, including the body.
    83  	//
    84  	// Because ReadTimeout does not let Handlers make per-request
    85  	// decisions on each request body's acceptable deadline or
    86  	// upload rate, most users will prefer to use
    87  	// ReadHeaderTimeout. It is valid to use them both.
    88  	ReadTimeout time.Duration
    89  
    90  	// ReadHeaderTimeout is the amount of time allowed to read
    91  	// request headers. The connection's read deadline is reset
    92  	// after reading the headers and the Handler can decide what
    93  	// is considered too slow for the body. If ReadHeaderTimeout
    94  	// is zero, the value of ReadTimeout is used. If both are
    95  	// zero, there is no timeout.
    96  	ReadHeaderTimeout time.Duration
    97  
    98  	// WriteTimeout is the maximum duration before timing out
    99  	// writes of the response. It is reset whenever a new
   100  	// request's header is read. Like ReadTimeout, it does not
   101  	// let Handlers make decisions on a per-request basis.
   102  	WriteTimeout time.Duration
   103  
   104  	// IdleTimeout is the maximum amount of time to wait for the
   105  	// next request when keep-alives are enabled. If IdleTimeout
   106  	// is zero, the value of ReadTimeout is used. If both are
   107  	// zero, ReadHeaderTimeout is used.
   108  	IdleTimeout time.Duration
   109  }
   110  
   111  // DefaultHTTPTimeouts represents the default timeout values used if further
   112  // configuration is not provided.
   113  var DefaultHTTPTimeouts = HTTPTimeouts{
   114  	ReadTimeout:       30 * time.Second,
   115  	ReadHeaderTimeout: 30 * time.Second,
   116  	WriteTimeout:      30 * time.Second,
   117  	IdleTimeout:       120 * time.Second,
   118  }
   119  
   120  // DialHTTPWithClient creates a new RPC client that connects to an RPC server over HTTP
   121  // using the provided HTTP Client.
   122  func DialHTTPWithClient(endpoint string, client *http.Client) (*Client, error) {
   123  	// Sanity check URL so we don't end up with a client that will fail every request.
   124  	_, err := url.Parse(endpoint)
   125  	if err != nil {
   126  		return nil, err
   127  	}
   128  
   129  	initctx := context.Background()
   130  	headers := make(http.Header, 2)
   131  	headers.Set("accept", contentType)
   132  	headers.Set("content-type", contentType)
   133  	return newClient(initctx, func(context.Context) (ServerCodec, error) {
   134  		hc := &httpConn{
   135  			client:  client,
   136  			headers: headers,
   137  			url:     endpoint,
   138  			closeCh: make(chan interface{}),
   139  		}
   140  		return hc, nil
   141  	})
   142  }
   143  
   144  // DialHTTP creates a new RPC client that connects to an RPC server over HTTP.
   145  func DialHTTP(endpoint string) (*Client, error) {
   146  	return DialHTTPWithClient(endpoint, new(http.Client))
   147  }
   148  
   149  func (c *Client) sendHTTP(ctx context.Context, op *requestOp, msg interface{}) error {
   150  	hc := c.writeConn.(*httpConn)
   151  	respBody, err := hc.doRequest(ctx, msg)
   152  	if err != nil {
   153  		return err
   154  	}
   155  	defer respBody.Close()
   156  
   157  	var respmsg jsonrpcMessage
   158  	if err := json.NewDecoder(respBody).Decode(&respmsg); err != nil {
   159  		return err
   160  	}
   161  	op.resp <- &respmsg
   162  	return nil
   163  }
   164  
   165  func (c *Client) sendBatchHTTP(ctx context.Context, op *requestOp, msgs []*jsonrpcMessage) error {
   166  	hc := c.writeConn.(*httpConn)
   167  	respBody, err := hc.doRequest(ctx, msgs)
   168  	if err != nil {
   169  		return err
   170  	}
   171  	defer respBody.Close()
   172  	var respmsgs []jsonrpcMessage
   173  	if err := json.NewDecoder(respBody).Decode(&respmsgs); err != nil {
   174  		return err
   175  	}
   176  	for i := 0; i < len(respmsgs); i++ {
   177  		op.resp <- &respmsgs[i]
   178  	}
   179  	return nil
   180  }
   181  
   182  func (hc *httpConn) doRequest(ctx context.Context, msg interface{}) (io.ReadCloser, error) {
   183  	body, err := json.Marshal(msg)
   184  	if err != nil {
   185  		return nil, err
   186  	}
   187  	req, err := http.NewRequestWithContext(ctx, "POST", hc.url, io.NopCloser(bytes.NewReader(body)))
   188  	if err != nil {
   189  		return nil, err
   190  	}
   191  	req.ContentLength = int64(len(body))
   192  	req.GetBody = func() (io.ReadCloser, error) { return io.NopCloser(bytes.NewReader(body)), nil }
   193  
   194  	// set headers
   195  	hc.mu.Lock()
   196  	req.Header = hc.headers.Clone()
   197  	hc.mu.Unlock()
   198  
   199  	// do request
   200  	resp, err := hc.client.Do(req)
   201  	if err != nil {
   202  		return nil, err
   203  	}
   204  	if resp.StatusCode < 200 || resp.StatusCode >= 300 {
   205  		var buf bytes.Buffer
   206  		var body []byte
   207  		if _, err := buf.ReadFrom(resp.Body); err == nil {
   208  			body = buf.Bytes()
   209  		}
   210  
   211  		return nil, HTTPError{
   212  			Status:     resp.Status,
   213  			StatusCode: resp.StatusCode,
   214  			Body:       body,
   215  		}
   216  	}
   217  	return resp.Body, nil
   218  }
   219  
   220  // httpServerConn turns a HTTP connection into a Conn.
   221  type httpServerConn struct {
   222  	io.Reader
   223  	io.Writer
   224  	r *http.Request
   225  }
   226  
   227  func newHTTPServerConn(r *http.Request, w http.ResponseWriter) ServerCodec {
   228  	body := io.LimitReader(r.Body, maxRequestContentLength)
   229  	conn := &httpServerConn{Reader: body, Writer: w, r: r}
   230  	return NewCodec(conn)
   231  }
   232  
   233  // Close does nothing and always returns nil.
   234  func (t *httpServerConn) Close() error { return nil }
   235  
   236  // RemoteAddr returns the peer address of the underlying connection.
   237  func (t *httpServerConn) RemoteAddr() string {
   238  	return t.r.RemoteAddr
   239  }
   240  
   241  // SetWriteDeadline does nothing and always returns nil.
   242  func (t *httpServerConn) SetWriteDeadline(time.Time) error { return nil }
   243  
   244  // ServeHTTP serves JSON-RPC requests over HTTP.
   245  func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
   246  	// Permit dumb empty requests for remote health-checks (AWS)
   247  	if r.Method == http.MethodGet && r.ContentLength == 0 && r.URL.RawQuery == "" {
   248  		w.WriteHeader(http.StatusOK)
   249  		return
   250  	}
   251  	if code, err := validateRequest(r); err != nil {
   252  		http.Error(w, err.Error(), code)
   253  		return
   254  	}
   255  
   256  	// Create request-scoped context.
   257  	connInfo := PeerInfo{Transport: "http", RemoteAddr: r.RemoteAddr}
   258  	connInfo.HTTP.Version = r.Proto
   259  	connInfo.HTTP.Host = r.Host
   260  	connInfo.HTTP.Origin = r.Header.Get("Origin")
   261  	connInfo.HTTP.UserAgent = r.Header.Get("User-Agent")
   262  	ctx := r.Context()
   263  	ctx = context.WithValue(ctx, peerInfoContextKey{}, connInfo)
   264  
   265  	// All checks passed, create a codec that reads directly from the request body
   266  	// until EOF, writes the response to w, and orders the server to process a
   267  	// single request.
   268  	w.Header().Set("content-type", contentType)
   269  	codec := newHTTPServerConn(r, w)
   270  	defer codec.close()
   271  	s.serveSingleRequest(ctx, codec)
   272  }
   273  
   274  // validateRequest returns a non-zero response code and error message if the
   275  // request is invalid.
   276  func validateRequest(r *http.Request) (int, error) {
   277  	if r.Method == http.MethodPut || r.Method == http.MethodDelete {
   278  		return http.StatusMethodNotAllowed, errors.New("method not allowed")
   279  	}
   280  	if r.ContentLength > maxRequestContentLength {
   281  		err := fmt.Errorf("content length too large (%d>%d)", r.ContentLength, maxRequestContentLength)
   282  		return http.StatusRequestEntityTooLarge, err
   283  	}
   284  	// Allow OPTIONS (regardless of content-type)
   285  	if r.Method == http.MethodOptions {
   286  		return 0, nil
   287  	}
   288  	// Check content-type
   289  	if mt, _, err := mime.ParseMediaType(r.Header.Get("content-type")); err == nil {
   290  		for _, accepted := range acceptedContentTypes {
   291  			if accepted == mt {
   292  				return 0, nil
   293  			}
   294  		}
   295  	}
   296  	// Invalid content-type
   297  	err := fmt.Errorf("invalid content type, only %s is supported", contentType)
   298  	return http.StatusUnsupportedMediaType, err
   299  }