Newer
Older
c := &Codec{}
c.Reset(w, r)
return c
}
func (c *Codec) Reset(w http.ResponseWriter, r *http.Request) {
c.r = r
c.w = w
c.msgs = make(chan *serverutil.Bundle, 1)
c.errCh = make(chan httpError, 1)
ctx := c.r.Context()
func (c *Codec) peerInfo() {
c.i.Transport = "http"
c.i.RemoteAddr = c.r.RemoteAddr
Version: c.r.Proto,
UserAgent: c.r.UserAgent(),
Host: c.r.Host,
Headers: c.r.Header.Clone(),
c.i.HTTP.Origin = c.r.Header.Get("X-Real-Ip")
if c.i.HTTP.Origin == "" {
c.i.HTTP.Origin = c.r.Header.Get("X-Forwarded-For")
if c.i.HTTP.Origin == "" {
c.i.HTTP.Origin = c.r.Header.Get("Origin")
func (r *Codec) doReadGet() (msg *serverutil.Bundle, err error) {
params, _ := url.QueryUnescape(r.r.URL.Query().Get("params"))
param := []byte(params)
if pb, err := base64.URLEncoding.DecodeString(params); err == nil {
param = pb
}
id := r.r.URL.Query().Get("id")
if id == "" {
id = "1"
}
Method: method_up,
Params: param,
}},
Batch: false,
}, nil
func (r *Codec) doReadRPC() (msg *serverutil.Bundle, err error) {
}
id := r.r.URL.Query().Get("id")
if id == "" {
id = "1"
}
data, err := io.ReadAll(r.r.Body)
if err != nil {
return nil, err
}
Method: method_up,
Params: data,
}},
Batch: false,
}, nil
}
func (r *Codec) doReadPost() (msg *serverutil.Bundle, err error) {
data, err := io.ReadAll(r.r.Body)
if err != nil {
return nil, err
}
return serverutil.ParseBundle(data), nil
// validateRequest returns a non-zero response code and error message if the
// request is invalid.
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 > maxRequestContentLength {
err := fmt.Errorf("content length too large (%d>%d)", r.ContentLength, maxRequestContentLength)
return http.StatusRequestEntityTooLarge, err
// Allow OPTIONS (regardless of content-type)
if r.Method == http.MethodOptions {
return 0, nil
}
// Check content-type
if mt, _, err := mime.ParseMediaType(r.Header.Get("content-type")); err == nil {
for _, accepted := range acceptedContentTypes {
if accepted == mt {
return 0, nil
}
}
// Invalid content-type ignored for now
return 0, nil
//err := fmt.Errorf("invalid content type, only %s is supported", contentType)
//return http.StatusUnsupportedMediaType, err
}
func (c *Codec) doRead() {
code, err := ValidateRequest(c.r)
}
if err != nil {
c.errCh <- httpError{
code: http.StatusInternalServerError,
err: err,
}
return
}
c.msgs <- data
}()
func (c *Codec) ReadBatch(ctx context.Context) ([]*jsonrpc.Message, bool, error) {
func (c *Codec) Flush() error {
defer c.cn()
err := c.wr.Flush()
if err != nil {
}
// Closed returns a channel which is closed when the connection is closed.
func (c *Codec) Closed() <-chan struct{} {
return c.ctx.Done()
}
// RemoteAddr returns the peer address of the connection.
func (c *Codec) RemoteAddr() string {