mirror of https://github.com/hashicorp/consul
Allow endpoints to handle OPTIONS/MethodNotFound themselves
parent
f5af8b0f03
commit
c5f0bb3711
|
@ -243,13 +243,14 @@ func (s *HTTPServer) wrap(handler endpoint, methods []string) http.HandlerFunc {
|
||||||
|
|
||||||
var obj interface{}
|
var obj interface{}
|
||||||
|
|
||||||
// respond appropriately to OPTIONS requests
|
// if this endpoint has declared methods, respond appropriately to OPTIONS requests. Otherwise let the endpoint handle that.
|
||||||
if req.Method == "OPTIONS" {
|
if req.Method == "OPTIONS" && len(methods) > 0 {
|
||||||
addAllowHeader(append([]string{"OPTIONS"}, methods...))
|
addAllowHeader(append([]string{"OPTIONS"}, methods...))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
methodFound := false
|
// if this endpoint has declared methods, check the request method. Otherwise let the endpoint handle that.
|
||||||
|
methodFound := len(methods) == 0
|
||||||
for _, method := range methods {
|
for _, method := range methods {
|
||||||
if method == req.Method {
|
if method == req.Method {
|
||||||
methodFound = true
|
methodFound = true
|
||||||
|
|
|
@ -59,7 +59,8 @@ func init() {
|
||||||
registerEndpoint("/v1/operator/autopilot/configuration", []string{"GET", "PUT"}, (*HTTPServer).OperatorAutopilotConfiguration)
|
registerEndpoint("/v1/operator/autopilot/configuration", []string{"GET", "PUT"}, (*HTTPServer).OperatorAutopilotConfiguration)
|
||||||
registerEndpoint("/v1/operator/autopilot/health", []string{"GET"}, (*HTTPServer).OperatorServerHealth)
|
registerEndpoint("/v1/operator/autopilot/health", []string{"GET"}, (*HTTPServer).OperatorServerHealth)
|
||||||
registerEndpoint("/v1/query", []string{"GET", "POST"}, (*HTTPServer).PreparedQueryGeneral)
|
registerEndpoint("/v1/query", []string{"GET", "POST"}, (*HTTPServer).PreparedQueryGeneral)
|
||||||
registerEndpoint("/v1/query/", []string{"GET", "PUT", "DELETE"}, (*HTTPServer).PreparedQuerySpecific)
|
// because prepared queries have more complex rules for allowed methods, handle that in the endpoint.
|
||||||
|
registerEndpoint("/v1/query/", []string{}, (*HTTPServer).PreparedQuerySpecific)
|
||||||
registerEndpoint("/v1/session/create", []string{"PUT"}, (*HTTPServer).SessionCreate)
|
registerEndpoint("/v1/session/create", []string{"PUT"}, (*HTTPServer).SessionCreate)
|
||||||
registerEndpoint("/v1/session/destroy/", []string{"PUT"}, (*HTTPServer).SessionDestroy)
|
registerEndpoint("/v1/session/destroy/", []string{"PUT"}, (*HTTPServer).SessionDestroy)
|
||||||
registerEndpoint("/v1/session/renew/", []string{"PUT"}, (*HTTPServer).SessionRenew)
|
registerEndpoint("/v1/session/renew/", []string{"PUT"}, (*HTTPServer).SessionRenew)
|
||||||
|
|
|
@ -228,9 +228,31 @@ func (s *HTTPServer) preparedQueryDelete(id string, resp http.ResponseWriter, re
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PreparedQuerySpecificOptions handles OPTIONS requests to prepared query endpoints.
|
||||||
|
func (s *HTTPServer) preparedQuerySpecificOptions(resp http.ResponseWriter, req *http.Request) interface{} {
|
||||||
|
path := req.URL.Path
|
||||||
|
switch {
|
||||||
|
case strings.HasSuffix(path, "/execute"):
|
||||||
|
resp.Header().Add("Allow", strings.Join([]string{"OPTIONS", "GET"}, ","))
|
||||||
|
return resp
|
||||||
|
|
||||||
|
case strings.HasSuffix(path, "/explain"):
|
||||||
|
resp.Header().Add("Allow", strings.Join([]string{"OPTIONS", "GET"}, ","))
|
||||||
|
return resp
|
||||||
|
|
||||||
|
default:
|
||||||
|
resp.Header().Add("Allow", strings.Join([]string{"OPTIONS", "GET", "PUT", "DELETE"}, ","))
|
||||||
|
return resp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// PreparedQuerySpecific handles all the prepared query requests specific to a
|
// PreparedQuerySpecific handles all the prepared query requests specific to a
|
||||||
// particular query.
|
// particular query.
|
||||||
func (s *HTTPServer) PreparedQuerySpecific(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
func (s *HTTPServer) PreparedQuerySpecific(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
||||||
|
if req.Method == "OPTIONS" {
|
||||||
|
return s.preparedQuerySpecificOptions(resp, req), nil
|
||||||
|
}
|
||||||
|
|
||||||
path := req.URL.Path
|
path := req.URL.Path
|
||||||
id := strings.TrimPrefix(path, "/v1/query/")
|
id := strings.TrimPrefix(path, "/v1/query/")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue