Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions http/jsonrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,13 @@ func (s *Server) parseParams(method string, array []interface{}) util.Params {

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
//JSON RPC commands should be POSTs
if r.Method != "POST" {
log.Warn("HTTP JSON RPC Handle - Method!=\"POST\"")
http.Error(w, "JSON RPC procotol only allows POST method",
if r.Method != http.MethodPost {
http.Error(w, "JSON RPC protocol only allows POST method",
http.StatusMethodNotAllowed)
return
}

if r.Header["Content-Type"][0] != "application/json" {
log.Warn("HTTP JSON RPC Handle - Content-Type: ",
r.Header["Content-Type"][0], " not supported")
http.Error(w, "need content type to be application/json",
http.StatusUnsupportedMediaType)
return
Expand All @@ -158,7 +155,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err != nil {
log.Warn("HTTP JSON RPC Handle - json.Unmarshal: ", err)
resp.error(w, http.StatusBadRequest, ParseError,
fmt.Sprintf("json parse failed: %s",err))
fmt.Sprintf("json parse failed: %s", err))
return
}

Expand Down
20 changes: 10 additions & 10 deletions http/restful/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,27 @@ func (s *Server) write(w http.ResponseWriter, data []byte) {
}

func (s *Server) response(w http.ResponseWriter, result interface{}, err error) {
resp := Response{Result: result}
resp := Response{
Result: result,
Error: 0,
Desc: "Success",
}

code := 0
message := "Success"
if err != nil {
switch e := err.(type) {
case *util.Error:
code = e.Code
message = e.Message
resp.Error = e.Code
resp.Desc = e.Message

default:
code = http.StatusInternalServerError
message = err.Error()
resp.Error = http.StatusInternalServerError
resp.Desc = err.Error()
}
}
resp.Error = code
resp.Desc = message

data, err := json.Marshal(resp)
if err != nil {
log.Fatal("HTTP Handle - json.Marshal: %v", err)
log.Fatalf("HTTP Handle - json.Marshal: %v", err)
return
}
s.write(w, data)
Expand Down