@@ -10,16 +10,11 @@ import (
1010)
1111
1212var (
13- // ErrResponseUnauthorized returned when user has no session or is not authorized to execute method
14- ErrResponseUnauthorized = JSONRPCError {Code : - 32000 , Message : "Unauthorized" }
15- // ErrNoPrivilege user does not have the privilege to view resource
16- ErrNoPrivilege = JSONRPCError {Code : - 32098 , Message : "No privilege" }
17-
1813 parseErrorResponse = JSONRPCResponse {JSONRPC : "2.0" , Error : & JSONRPCError {Code : - 32700 , Message : "Parse error" }}
1914 invalidRequestResponse = JSONRPCResponse {JSONRPC : "2.0" , Error : & JSONRPCError {Code : - 32600 , Message : "Invalid Request" }}
2015 methodNotFoundResponse = JSONRPCResponse {JSONRPC : "2.0" , Error : & JSONRPCError {Code : - 32601 , Message : "Method not found" }}
2116 invalidParamsResponse = JSONRPCResponse {JSONRPC : "2.0" , Error : & JSONRPCError {Code : - 32602 , Message : "Invalid params" }}
22- unauthorizedResponse = JSONRPCResponse {JSONRPC : "2.0" , Error : & ErrResponseUnauthorized }
17+ // unauthorizedResponse = JSONRPCResponse{JSONRPC: "2.0", Error: &ErrResponseUnauthorized}
2318 // User-defined codes from -32000 to -32099
2419 emptyResponse = JSONRPCResponse {JSONRPC : "2.0" , Error : & JSONRPCError {Code : - 32099 , Message : "" }}
2520)
@@ -58,9 +53,9 @@ func (e JSONRPCError) Error() string {
5853// JSONRPCResponse A response
5954type JSONRPCResponse struct {
6055 JSONRPC string `json:"jsonrpc"`
61- ID interface {} `json:"id"`
56+ ID any `json:"id"`
6257 Error * JSONRPCError `json:"error,omitempty"`
63- Result interface {} `json:"result,omitempty"`
58+ Result any `json:"result,omitempty"`
6459}
6560
6661// Handler which implements the JSONRPC 2.0 specification at https://www.jsonrpc.org/specification
@@ -113,7 +108,7 @@ func (handler Handler) ProcessRPC(w http.ResponseWriter, r *http.Request) {
113108 if ok {
114109 response = handler .execRPCRequest (w , r , sess , rpcreq )
115110 } else {
116- w .WriteHeader (http .StatusForbidden )
111+ w .WriteHeader (http .StatusUnauthorized )
117112 return
118113 }
119114 } else {
@@ -126,19 +121,19 @@ func (handler Handler) ProcessRPC(w http.ResponseWriter, r *http.Request) {
126121
127122func (handler Handler ) execRPCRequest (w http.ResponseWriter , r * http.Request , session WebSession , req JSONRPCRequest ) JSONRPCResponse {
128123 if req .JSONRPC != "2.0" || req .Method == nil || len (* req .Method ) == 0 || req .ID == nil {
129- return emptyResponse
124+ return invalidRequestResponse
130125 }
131126 targetMethod := reflect .ValueOf (handler .API ).MethodByName (* req .Method )
132127 if reflect .Invalid == targetMethod .Kind () {
133- return emptyResponse
128+ return methodNotFoundResponse
134129 }
135130 in := make ([]reflect.Value , targetMethod .Type ().NumIn ())
136131 if targetMethod .Type ().NumIn () < len (req .Params ) {
137132 return invalidParamsResponse
138133 }
139134
140135 filled := 0
141- for paramIdx := 0 ; paramIdx < targetMethod .Type ().NumIn (); paramIdx ++ {
136+ for paramIdx := range targetMethod .Type ().NumIn () {
142137 if reflect .TypeOf (w ).AssignableTo (targetMethod .Type ().In (paramIdx )) {
143138 in [paramIdx ] = reflect .ValueOf (w )
144139 filled ++
@@ -253,7 +248,7 @@ func (handler Handler) execRPCRequest(w http.ResponseWriter, r *http.Request, se
253248 return rpcResponse
254249}
255250
256- func collectReturns (apiResp interface {} ) (interface {} , * JSONRPCError ) {
251+ func collectReturns (apiResp any ) (any , * JSONRPCError ) {
257252 switch resp := apiResp .(type ) {
258253 case JSONRPCError :
259254 return nil , & resp
@@ -264,7 +259,7 @@ func collectReturns(apiResp interface{}) (interface{}, *JSONRPCError) {
264259 return nil , & rpcErr
265260 default :
266261 if apiResp == nil {
267- return "OK " , nil
262+ return "" , nil
268263 }
269264 return apiResp , nil
270265 }
0 commit comments