validateRequest returns a non-zero response code and error message if the request is invalid.
(r *http.Request)
| 202 | // validateRequest returns a non-zero response code and error message if the |
| 203 | // request is invalid. |
| 204 | func validateRequest(r *http.Request) (int, error) { |
| 205 | if r.Method == http.MethodPut || r.Method == http.MethodDelete { |
| 206 | return http.StatusMethodNotAllowed, errors.New("method not allowed") |
| 207 | } |
| 208 | if r.ContentLength > maxRequestContentLength { |
| 209 | err := fmt.Errorf("content length too large (%d>%d)", r.ContentLength, maxRequestContentLength) |
| 210 | return http.StatusRequestEntityTooLarge, err |
| 211 | } |
| 212 | mt, _, err := mime.ParseMediaType(r.Header.Get("content-type")) |
| 213 | if r.Method != http.MethodOptions && (err != nil || mt != contentType) { |
| 214 | err := fmt.Errorf("invalid content type, only %s is supported", contentType) |
| 215 | return http.StatusUnsupportedMediaType, err |
| 216 | } |
| 217 | return 0, nil |
| 218 | } |
| 219 | |
| 220 | func newCorsHandler(srv *Server, allowedOrigins []string) http.Handler { |
| 221 | // disable CORS support if user has not specified a custom CORS configuration |