Пример #1
0
func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	w.Header().Set("Content-Type", "application/json")

	// Limit request size to resist DoS
	if req.ContentLength > maxHttpSizeReqLength {
		err := fmt.Errorf("Request too large")
		response := shared.NewRpcErrorResponse(-1, shared.JsonRpcVersion, -32700, err)
		sendJSON(w, &response)
		return
	}

	defer req.Body.Close()
	payload, err := ioutil.ReadAll(req.Body)
	if err != nil {
		err := fmt.Errorf("Could not read request body")
		response := shared.NewRpcErrorResponse(-1, shared.JsonRpcVersion, -32700, err)
		sendJSON(w, &response)
		return
	}

	c := h.codec.New(nil)
	var rpcReq shared.Request
	if err = c.Decode(payload, &rpcReq); err == nil {
		reply, err := h.api.Execute(&rpcReq)
		res := shared.NewRpcResponse(rpcReq.Id, rpcReq.Jsonrpc, reply, err)
		sendJSON(w, &res)
		return
	}

	var reqBatch []shared.Request
	if err = c.Decode(payload, &reqBatch); err == nil {
		resBatch := make([]*interface{}, len(reqBatch))
		resCount := 0
		for i, rpcReq := range reqBatch {
			reply, err := h.api.Execute(&rpcReq)
			if rpcReq.Id != nil { // this leaves nil entries in the response batch for later removal
				resBatch[i] = shared.NewRpcResponse(rpcReq.Id, rpcReq.Jsonrpc, reply, err)
				resCount += 1
			}
		}
		// make response omitting nil entries
		sendJSON(w, resBatch[:resCount])
		return
	}

	// invalid request
	err = fmt.Errorf("Could not decode request")
	res := shared.NewRpcErrorResponse(-1, shared.JsonRpcVersion, -32600, err)
	sendJSON(w, res)
}
Пример #2
0
func handle(id int, conn net.Conn, api shared.ExpanseApi, c codec.Codec) {
	codec := c.New(conn)

	defer func() {
		if r := recover(); r != nil {
			glog.Errorf("panic: %v\n", r)
		}
		codec.Close()
	}()

	for {
		requests, isBatch, err := codec.ReadRequest()
		if err == io.EOF {
			return
		} else if err != nil {
			glog.V(logger.Debug).Infof("Closed IPC Conn %06d recv err - %v\n", id, err)
			return
		}

		if isBatch {
			responses := make([]*interface{}, len(requests))
			responseCount := 0
			for _, req := range requests {
				res, err := api.Execute(req)
				if req.Id != nil {
					rpcResponse := shared.NewRpcResponse(req.Id, req.Jsonrpc, res, err)
					responses[responseCount] = rpcResponse
					responseCount += 1
				}
			}

			err = codec.WriteResponse(responses[:responseCount])
			if err != nil {
				glog.V(logger.Debug).Infof("Closed IPC Conn %06d send err - %v\n", id, err)
				return
			}
		} else {
			var rpcResponse interface{}
			res, err := api.Execute(requests[0])

			rpcResponse = shared.NewRpcResponse(requests[0].Id, requests[0].Jsonrpc, res, err)
			err = codec.WriteResponse(rpcResponse)
			if err != nil {
				glog.V(logger.Debug).Infof("Closed IPC Conn %06d send err - %v\n", id, err)
				return
			}
		}
	}
}
Пример #3
0
// askPassword will ask the user to supply the password for a given account
func (self *Jeth) askPassword(id interface{}, jsonrpc string, args []interface{}) bool {
	var err error
	var passwd string
	if len(args) >= 1 {
		if account, ok := args[0].(string); ok {
			fmt.Printf("Unlock account %s\n", account)
		} else {
			return false
		}
	}
	passwd, err = utils.PromptPassword("Passphrase: ", true)

	if err = self.client.Send(shared.NewRpcResponse(id, jsonrpc, passwd, err)); err != nil {
		glog.V(logger.Info).Infof("Unable to send user agent ask password response - %v\n", err)
	}

	return err == nil
}
Пример #4
0
func (self *Jeth) confirmTransaction(id interface{}, jsonrpc string, args []interface{}) bool {
	// Accept all tx which are send from this console
	return self.client.Send(shared.NewRpcResponse(id, jsonrpc, true, nil)) == nil
}
Пример #5
0
func (self *InProcClient) Recv() (interface{}, error) {
	return *shared.NewRpcResponse(self.lastId, self.lastJsonrpc, self.lastRes, self.lastErr), nil
}