Skip to content

Commit c8b7ea1

Browse files
authored
Merge pull request #15505 from karalabe/fix-rpc-pr
rpc: minor cleanups to RPC PR
2 parents c5b8569 + 3c6b9c5 commit c8b7ea1

File tree

2 files changed

+38
-28
lines changed

2 files changed

+38
-28
lines changed

rpc/http.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"context"
2222
"encoding/json"
23+
"errors"
2324
"fmt"
2425
"io"
2526
"io/ioutil"
@@ -151,41 +152,36 @@ func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
151152
if r.Method == "GET" && r.ContentLength == 0 && r.URL.RawQuery == "" {
152153
return
153154
}
154-
if responseCode, errorMessage := httpErrorResponse(r); responseCode != 0 {
155-
http.Error(w, errorMessage, responseCode)
155+
if code, err := validateRequest(r); err != nil {
156+
http.Error(w, err.Error(), code)
156157
return
157158
}
158-
159159
// All checks passed, create a codec that reads direct from the request body
160160
// untilEOF and writes the response to w and order the server to process a
161161
// single request.
162162
codec := NewJSONCodec(&httpReadWriteNopCloser{r.Body, w})
163163
defer codec.Close()
164164

165-
w.Header().Set("content-type", "application/json")
165+
w.Header().Set("content-type", contentType)
166166
srv.ServeSingleRequest(codec, OptionMethodInvocation)
167167
}
168168

169-
// Returns a non-zero response code and error message if the request is invalid.
170-
func httpErrorResponse(r *http.Request) (int, string) {
169+
// validateRequest returns a non-zero response code and error message if the
170+
// request is invalid.
171+
func validateRequest(r *http.Request) (int, error) {
171172
if r.Method == "PUT" || r.Method == "DELETE" {
172-
errorMessage := "method not allowed"
173-
return http.StatusMethodNotAllowed, errorMessage
173+
return http.StatusMethodNotAllowed, errors.New("method not allowed")
174174
}
175-
176175
if r.ContentLength > maxHTTPRequestContentLength {
177-
errorMessage := fmt.Sprintf("content length too large (%d>%d)", r.ContentLength, maxHTTPRequestContentLength)
178-
return http.StatusRequestEntityTooLarge, errorMessage
176+
err := fmt.Errorf("content length too large (%d>%d)", r.ContentLength, maxHTTPRequestContentLength)
177+
return http.StatusRequestEntityTooLarge, err
179178
}
180-
181-
ct := r.Header.Get("content-type")
182-
mt, _, err := mime.ParseMediaType(ct)
179+
mt, _, err := mime.ParseMediaType(r.Header.Get("content-type"))
183180
if err != nil || mt != contentType {
184-
errorMessage := fmt.Sprintf("invalid content type, only %s is supported", contentType)
185-
return http.StatusUnsupportedMediaType, errorMessage
181+
err := fmt.Errorf("invalid content type, only %s is supported", contentType)
182+
return http.StatusUnsupportedMediaType, err
186183
}
187-
188-
return 0, ""
184+
return 0, nil
189185
}
190186

191187
func newCorsHandler(srv *Server, allowedOrigins []string) http.Handler {

rpc/http_test.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
// Copyright 2017 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
117
package rpc
218

319
import (
@@ -8,33 +24,31 @@ import (
824
)
925

1026
func TestHTTPErrorResponseWithDelete(t *testing.T) {
11-
httpErrorResponseTest(t, "DELETE", contentType, "", http.StatusMethodNotAllowed)
27+
testHTTPErrorResponse(t, "DELETE", contentType, "", http.StatusMethodNotAllowed)
1228
}
1329

1430
func TestHTTPErrorResponseWithPut(t *testing.T) {
15-
httpErrorResponseTest(t, "PUT", contentType, "", http.StatusMethodNotAllowed)
31+
testHTTPErrorResponse(t, "PUT", contentType, "", http.StatusMethodNotAllowed)
1632
}
1733

1834
func TestHTTPErrorResponseWithMaxContentLength(t *testing.T) {
1935
body := make([]rune, maxHTTPRequestContentLength+1, maxHTTPRequestContentLength+1)
20-
httpErrorResponseTest(t,
36+
testHTTPErrorResponse(t,
2137
"POST", contentType, string(body), http.StatusRequestEntityTooLarge)
2238
}
2339

2440
func TestHTTPErrorResponseWithEmptyContentType(t *testing.T) {
25-
httpErrorResponseTest(t, "POST", "", "", http.StatusUnsupportedMediaType)
41+
testHTTPErrorResponse(t, "POST", "", "", http.StatusUnsupportedMediaType)
2642
}
2743

2844
func TestHTTPErrorResponseWithValidRequest(t *testing.T) {
29-
httpErrorResponseTest(t, "POST", contentType, "", 0)
45+
testHTTPErrorResponse(t, "POST", contentType, "", 0)
3046
}
3147

32-
func httpErrorResponseTest(t *testing.T,
33-
method, contentType, body string, expectedResponse int) {
34-
48+
func testHTTPErrorResponse(t *testing.T, method, contentType, body string, expected int) {
3549
request := httptest.NewRequest(method, "http://url.com", strings.NewReader(body))
3650
request.Header.Set("content-type", contentType)
37-
if response, _ := httpErrorResponse(request); response != expectedResponse {
38-
t.Fatalf("response code should be %d not %d", expectedResponse, response)
51+
if code, _ := validateRequest(request); code != expected {
52+
t.Fatalf("response code should be %d not %d", expected, code)
3953
}
4054
}

0 commit comments

Comments
 (0)