@@ -33,6 +33,7 @@ import (
33
33
)
34
34
35
35
const (
36
+ contentType = "application/json"
36
37
maxHTTPRequestContentLength = 1024 * 128
37
38
)
38
39
@@ -69,8 +70,8 @@ func DialHTTP(endpoint string) (*Client, error) {
69
70
if err != nil {
70
71
return nil , err
71
72
}
72
- req .Header .Set ("Content-Type" , "application/json" )
73
- req .Header .Set ("Accept" , "application/json" )
73
+ req .Header .Set ("Content-Type" , contentType )
74
+ req .Header .Set ("Accept" , contentType )
74
75
75
76
initctx := context .Background ()
76
77
return newClient (initctx , func (context.Context ) (net.Conn , error ) {
@@ -150,21 +151,11 @@ func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
150
151
if r .Method == "GET" && r .ContentLength == 0 && r .URL .RawQuery == "" {
151
152
return
152
153
}
153
- // For meaningful requests, validate it's size and content type
154
- if r .ContentLength > maxHTTPRequestContentLength {
155
- http .Error (w ,
156
- fmt .Sprintf ("content length too large (%d>%d)" , r .ContentLength , maxHTTPRequestContentLength ),
157
- http .StatusRequestEntityTooLarge )
158
- return
159
- }
160
- ct := r .Header .Get ("content-type" )
161
- mt , _ , err := mime .ParseMediaType (ct )
162
- if err != nil || mt != "application/json" {
163
- http .Error (w ,
164
- "invalid content type, only application/json is supported" ,
165
- http .StatusUnsupportedMediaType )
154
+ if responseCode , errorMessage := httpErrorResponse (r ); responseCode != 0 {
155
+ http .Error (w , errorMessage , responseCode )
166
156
return
167
157
}
158
+
168
159
// All checks passed, create a codec that reads direct from the request body
169
160
// untilEOF and writes the response to w and order the server to process a
170
161
// single request.
@@ -175,6 +166,28 @@ func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
175
166
srv .ServeSingleRequest (codec , OptionMethodInvocation )
176
167
}
177
168
169
+ // Returns a non-zero response code and error message if the request is invalid.
170
+ func httpErrorResponse (r * http.Request ) (int , string ) {
171
+ if r .Method == "PUT" || r .Method == "DELETE" {
172
+ errorMessage := "method not allowed"
173
+ return http .StatusMethodNotAllowed , errorMessage
174
+ }
175
+
176
+ if r .ContentLength > maxHTTPRequestContentLength {
177
+ errorMessage := fmt .Sprintf ("content length too large (%d>%d)" , r .ContentLength , maxHTTPRequestContentLength )
178
+ return http .StatusRequestEntityTooLarge , errorMessage
179
+ }
180
+
181
+ ct := r .Header .Get ("content-type" )
182
+ mt , _ , err := mime .ParseMediaType (ct )
183
+ if err != nil || mt != contentType {
184
+ errorMessage := fmt .Sprintf ("invalid content type, only %s is supported" , contentType )
185
+ return http .StatusUnsupportedMediaType , errorMessage
186
+ }
187
+
188
+ return 0 , ""
189
+ }
190
+
178
191
func newCorsHandler (srv * Server , allowedOrigins []string ) http.Handler {
179
192
// disable CORS support if user has not specified a custom CORS configuration
180
193
if len (allowedOrigins ) == 0 {
0 commit comments