diff options
author | Péter Szilágyi <peterke@gmail.com> | 2017-11-17 20:18:46 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2017-11-17 20:25:02 +0800 |
commit | 3c6b9c5d726e6dda72f469fefd1a37b37a0a1621 (patch) | |
tree | 11447d3437a400b06c21e32f81293c5183fc660f /rpc/http.go | |
parent | c5b8569707cabe19f861cb67062c07598aff2aa1 (diff) | |
download | dexon-3c6b9c5d726e6dda72f469fefd1a37b37a0a1621.tar dexon-3c6b9c5d726e6dda72f469fefd1a37b37a0a1621.tar.gz dexon-3c6b9c5d726e6dda72f469fefd1a37b37a0a1621.tar.bz2 dexon-3c6b9c5d726e6dda72f469fefd1a37b37a0a1621.tar.lz dexon-3c6b9c5d726e6dda72f469fefd1a37b37a0a1621.tar.xz dexon-3c6b9c5d726e6dda72f469fefd1a37b37a0a1621.tar.zst dexon-3c6b9c5d726e6dda72f469fefd1a37b37a0a1621.zip |
rpc: minor cleanups to RPC PR
Diffstat (limited to 'rpc/http.go')
-rw-r--r-- | rpc/http.go | 32 |
1 files changed, 14 insertions, 18 deletions
diff --git a/rpc/http.go b/rpc/http.go index 68634e3fd..5941c0677 100644 --- a/rpc/http.go +++ b/rpc/http.go @@ -20,6 +20,7 @@ import ( "bytes" "context" "encoding/json" + "errors" "fmt" "io" "io/ioutil" @@ -151,41 +152,36 @@ func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" && r.ContentLength == 0 && r.URL.RawQuery == "" { return } - if responseCode, errorMessage := httpErrorResponse(r); responseCode != 0 { - http.Error(w, errorMessage, responseCode) + if code, err := validateRequest(r); err != nil { + http.Error(w, err.Error(), code) return } - // All checks passed, create a codec that reads direct from the request body // untilEOF and writes the response to w and order the server to process a // single request. codec := NewJSONCodec(&httpReadWriteNopCloser{r.Body, w}) defer codec.Close() - w.Header().Set("content-type", "application/json") + w.Header().Set("content-type", contentType) srv.ServeSingleRequest(codec, OptionMethodInvocation) } -// Returns a non-zero response code and error message if the request is invalid. -func httpErrorResponse(r *http.Request) (int, string) { +// 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 == "PUT" || r.Method == "DELETE" { - errorMessage := "method not allowed" - return http.StatusMethodNotAllowed, errorMessage + return http.StatusMethodNotAllowed, errors.New("method not allowed") } - if r.ContentLength > maxHTTPRequestContentLength { - errorMessage := fmt.Sprintf("content length too large (%d>%d)", r.ContentLength, maxHTTPRequestContentLength) - return http.StatusRequestEntityTooLarge, errorMessage + err := fmt.Errorf("content length too large (%d>%d)", r.ContentLength, maxHTTPRequestContentLength) + return http.StatusRequestEntityTooLarge, err } - - ct := r.Header.Get("content-type") - mt, _, err := mime.ParseMediaType(ct) + mt, _, err := mime.ParseMediaType(r.Header.Get("content-type")) if err != nil || mt != contentType { - errorMessage := fmt.Sprintf("invalid content type, only %s is supported", contentType) - return http.StatusUnsupportedMediaType, errorMessage + err := fmt.Errorf("invalid content type, only %s is supported", contentType) + return http.StatusUnsupportedMediaType, err } - - return 0, "" + return 0, nil } func newCorsHandler(srv *Server, allowedOrigins []string) http.Handler { |