(w http.ResponseWriter, r *http.Request)
| 77 | } |
| 78 | |
| 79 | func (this *CliRpcServer) Handler(w http.ResponseWriter, r *http.Request) { |
| 80 | resp := &common.CliRpcResponse{} |
| 81 | defer func() { |
| 82 | w.Header().Add("Access-Control-Allow-Headers", "Content-Type") |
| 83 | w.Header().Set("content-type", "application/json;charset=utf-8") |
| 84 | w.Header().Set("Access-Control-Allow-Origin", "*") |
| 85 | w.WriteHeader(http.StatusOK) |
| 86 | |
| 87 | if resp.ErrorInfo == "" { |
| 88 | resp.ErrorInfo = common.GetCLIErrorDesc(resp.ErrorCode) |
| 89 | } |
| 90 | data, err := json.Marshal(resp) |
| 91 | if err != nil { |
| 92 | log.Error("CliRpcServer json.Marshal JsonRpcResponse:%+v error:%s", resp, err) |
| 93 | return |
| 94 | } |
| 95 | _, err = w.Write(data) |
| 96 | if err != nil { |
| 97 | log.Error("CliRpcServer Write:%s error %s", data, err) |
| 98 | return |
| 99 | } |
| 100 | log.Infof("[CliRpcResponse]%s", data) |
| 101 | }() |
| 102 | |
| 103 | if r.Method != http.MethodPost { |
| 104 | resp.ErrorCode = common.CLIERR_HTTP_METHOD_INVALID |
| 105 | return |
| 106 | } |
| 107 | data, err := ioutil.ReadAll(r.Body) |
| 108 | if err != nil { |
| 109 | log.Error("CliRpcServer read body error:%s", err) |
| 110 | resp.ErrorCode = common.CLIERR_INVALID_REQUEST |
| 111 | resp.ErrorInfo = "invalid body" |
| 112 | return |
| 113 | } |
| 114 | defer r.Body.Close() |
| 115 | |
| 116 | req := &common.CliRpcRequest{} |
| 117 | err = json.Unmarshal(data, req) |
| 118 | if err != nil { |
| 119 | log.Errorf("CliRpcServer json.Unmarshal JsonRpcRequest error:%s", err) |
| 120 | resp.ErrorCode = common.CLIERR_INVALID_PARAMS |
| 121 | return |
| 122 | } |
| 123 | |
| 124 | pwd := req.Pwd |
| 125 | req.Pwd = "*" |
| 126 | logData, _ := json.Marshal(req) |
| 127 | log.Infof("[CliRpcRequest]%s", logData) |
| 128 | |
| 129 | req.Pwd = pwd |
| 130 | resp.Method = req.Method |
| 131 | resp.Qid = req.Qid |
| 132 | |
| 133 | handler := this.GetHandler(req.Method) |
| 134 | if handler == nil { |
| 135 | resp.ErrorCode = common.CLIERR_UNSUPPORT_METHOD |
| 136 | return |
nothing calls this directly
no test coverage detected