good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
bor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open
bor
Commits
c8b7ea1c
Unverified
Commit
c8b7ea1c
authored
Nov 17, 2017
by
Péter Szilágyi
Committed by
GitHub
Nov 17, 2017
Browse files
Options
Downloads
Plain Diff
Merge pull request #15505 from karalabe/fix-rpc-pr
rpc: minor cleanups to RPC PR
parents
c5b85697
3c6b9c5d
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
rpc/http.go
+14
-18
14 additions, 18 deletions
rpc/http.go
rpc/http_test.go
+24
-10
24 additions, 10 deletions
rpc/http_test.go
with
38 additions
and
28 deletions
rpc/http.go
+
14
−
18
View file @
c8b7ea1c
...
@@ -20,6 +20,7 @@ import (
...
@@ -20,6 +20,7 @@ import (
"bytes"
"bytes"
"context"
"context"
"encoding/json"
"encoding/json"
"errors"
"fmt"
"fmt"
"io"
"io"
"io/ioutil"
"io/ioutil"
...
@@ -151,41 +152,36 @@ func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
...
@@ -151,41 +152,36 @@ func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if
r
.
Method
==
"GET"
&&
r
.
ContentLength
==
0
&&
r
.
URL
.
RawQuery
==
""
{
if
r
.
Method
==
"GET"
&&
r
.
ContentLength
==
0
&&
r
.
URL
.
RawQuery
==
""
{
return
return
}
}
if
responseCode
,
errorMessage
:=
httpErrorResponse
(
r
);
responseCode
!=
0
{
if
code
,
err
:=
validateRequest
(
r
);
err
!=
nil
{
http
.
Error
(
w
,
err
orMessage
,
responseC
ode
)
http
.
Error
(
w
,
err
.
Error
(),
c
ode
)
return
return
}
}
// All checks passed, create a codec that reads direct from the request body
// 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
// untilEOF and writes the response to w and order the server to process a
// single request.
// single request.
codec
:=
NewJSONCodec
(
&
httpReadWriteNopCloser
{
r
.
Body
,
w
})
codec
:=
NewJSONCodec
(
&
httpReadWriteNopCloser
{
r
.
Body
,
w
})
defer
codec
.
Close
()
defer
codec
.
Close
()
w
.
Header
()
.
Set
(
"content-type"
,
"application/json"
)
w
.
Header
()
.
Set
(
"content-type"
,
contentType
)
srv
.
ServeSingleRequest
(
codec
,
OptionMethodInvocation
)
srv
.
ServeSingleRequest
(
codec
,
OptionMethodInvocation
)
}
}
// Returns a non-zero response code and error message if the request is invalid.
// validateRequest returns a non-zero response code and error message if the
func
httpErrorResponse
(
r
*
http
.
Request
)
(
int
,
string
)
{
// request is invalid.
func
validateRequest
(
r
*
http
.
Request
)
(
int
,
error
)
{
if
r
.
Method
==
"PUT"
||
r
.
Method
==
"DELETE"
{
if
r
.
Method
==
"PUT"
||
r
.
Method
==
"DELETE"
{
errorMessage
:=
"method not allowed"
return
http
.
StatusMethodNotAllowed
,
errors
.
New
(
"method not allowed"
)
return
http
.
StatusMethodNotAllowed
,
errorMessage
}
}
if
r
.
ContentLength
>
maxHTTPRequestContentLength
{
if
r
.
ContentLength
>
maxHTTPRequestContentLength
{
err
orMessage
:=
fmt
.
Sprint
f
(
"content length too large (%d>%d)"
,
r
.
ContentLength
,
maxHTTPRequestContentLength
)
err
:=
fmt
.
Error
f
(
"content length too large (%d>%d)"
,
r
.
ContentLength
,
maxHTTPRequestContentLength
)
return
http
.
StatusRequestEntityTooLarge
,
err
orMessage
return
http
.
StatusRequestEntityTooLarge
,
err
}
}
mt
,
_
,
err
:=
mime
.
ParseMediaType
(
r
.
Header
.
Get
(
"content-type"
))
ct
:=
r
.
Header
.
Get
(
"content-type"
)
mt
,
_
,
err
:=
mime
.
ParseMediaType
(
ct
)
if
err
!=
nil
||
mt
!=
contentType
{
if
err
!=
nil
||
mt
!=
contentType
{
err
orMessage
:=
fmt
.
Sprint
f
(
"invalid content type, only %s is supported"
,
contentType
)
err
:=
fmt
.
Error
f
(
"invalid content type, only %s is supported"
,
contentType
)
return
http
.
StatusUnsupportedMediaType
,
err
orMessage
return
http
.
StatusUnsupportedMediaType
,
err
}
}
return
0
,
nil
return
0
,
""
}
}
func
newCorsHandler
(
srv
*
Server
,
allowedOrigins
[]
string
)
http
.
Handler
{
func
newCorsHandler
(
srv
*
Server
,
allowedOrigins
[]
string
)
http
.
Handler
{
...
...
This diff is collapsed.
Click to expand it.
rpc/http_test.go
+
24
−
10
View file @
c8b7ea1c
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package
rpc
package
rpc
import
(
import
(
...
@@ -8,33 +24,31 @@ import (
...
@@ -8,33 +24,31 @@ import (
)
)
func
TestHTTPErrorResponseWithDelete
(
t
*
testing
.
T
)
{
func
TestHTTPErrorResponseWithDelete
(
t
*
testing
.
T
)
{
http
ErrorResponse
Test
(
t
,
"DELETE"
,
contentType
,
""
,
http
.
StatusMethodNotAllowed
)
testHTTP
ErrorResponse
(
t
,
"DELETE"
,
contentType
,
""
,
http
.
StatusMethodNotAllowed
)
}
}
func
TestHTTPErrorResponseWithPut
(
t
*
testing
.
T
)
{
func
TestHTTPErrorResponseWithPut
(
t
*
testing
.
T
)
{
http
ErrorResponse
Test
(
t
,
"PUT"
,
contentType
,
""
,
http
.
StatusMethodNotAllowed
)
testHTTP
ErrorResponse
(
t
,
"PUT"
,
contentType
,
""
,
http
.
StatusMethodNotAllowed
)
}
}
func
TestHTTPErrorResponseWithMaxContentLength
(
t
*
testing
.
T
)
{
func
TestHTTPErrorResponseWithMaxContentLength
(
t
*
testing
.
T
)
{
body
:=
make
([]
rune
,
maxHTTPRequestContentLength
+
1
,
maxHTTPRequestContentLength
+
1
)
body
:=
make
([]
rune
,
maxHTTPRequestContentLength
+
1
,
maxHTTPRequestContentLength
+
1
)
http
ErrorResponse
Test
(
t
,
testHTTP
ErrorResponse
(
t
,
"POST"
,
contentType
,
string
(
body
),
http
.
StatusRequestEntityTooLarge
)
"POST"
,
contentType
,
string
(
body
),
http
.
StatusRequestEntityTooLarge
)
}
}
func
TestHTTPErrorResponseWithEmptyContentType
(
t
*
testing
.
T
)
{
func
TestHTTPErrorResponseWithEmptyContentType
(
t
*
testing
.
T
)
{
http
ErrorResponse
Test
(
t
,
"POST"
,
""
,
""
,
http
.
StatusUnsupportedMediaType
)
testHTTP
ErrorResponse
(
t
,
"POST"
,
""
,
""
,
http
.
StatusUnsupportedMediaType
)
}
}
func
TestHTTPErrorResponseWithValidRequest
(
t
*
testing
.
T
)
{
func
TestHTTPErrorResponseWithValidRequest
(
t
*
testing
.
T
)
{
http
ErrorResponse
Test
(
t
,
"POST"
,
contentType
,
""
,
0
)
testHTTP
ErrorResponse
(
t
,
"POST"
,
contentType
,
""
,
0
)
}
}
func
httpErrorResponseTest
(
t
*
testing
.
T
,
func
testHTTPErrorResponse
(
t
*
testing
.
T
,
method
,
contentType
,
body
string
,
expected
int
)
{
method
,
contentType
,
body
string
,
expectedResponse
int
)
{
request
:=
httptest
.
NewRequest
(
method
,
"http://url.com"
,
strings
.
NewReader
(
body
))
request
:=
httptest
.
NewRequest
(
method
,
"http://url.com"
,
strings
.
NewReader
(
body
))
request
.
Header
.
Set
(
"content-type"
,
contentType
)
request
.
Header
.
Set
(
"content-type"
,
contentType
)
if
respons
e
,
_
:=
httpErrorResponse
(
request
);
respons
e
!=
expected
Response
{
if
cod
e
,
_
:=
validateRequest
(
request
);
cod
e
!=
expected
{
t
.
Fatalf
(
"response code should be %d not %d"
,
expected
Response
,
respons
e
)
t
.
Fatalf
(
"response code should be %d not %d"
,
expected
,
cod
e
)
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment