forked from kyma-project/kyma
/
middleware.go
36 lines (27 loc) · 1.23 KB
/
middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package broker
import (
"net/http"
osb "github.com/pmorie/go-open-service-broker-client/v2"
)
// OSBContextMiddleware implements Handler interface
type OSBContextMiddleware struct{}
// ServeHTTP adds content of Open Service Broker Api headers to the requests
func (OSBContextMiddleware) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
osbCtx := osbContext{
APIVersion: r.Header.Get(osb.APIVersionHeader),
OriginatingIdentity: r.Header.Get(osb.OriginatingIdentityHeader),
}
r = r.WithContext(contextWithOSB(r.Context(), osbCtx))
next(rw, r)
}
// RequireAsyncMiddleware asserts if request allows for asynchronous response
type RequireAsyncMiddleware struct{}
// ServeHTTP handling asynchronous HTTP requests in Open Service Broker Api
func (RequireAsyncMiddleware) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
if r.URL.Query().Get("accepts_incomplete") != "true" {
// message and desc as defined in https://github.com/openservicebrokerapi/servicebroker/blob/master/spec.md#response-2
writeErrorResponse(rw, http.StatusUnprocessableEntity, "AsyncRequired", "This service plan requires client support for asynchronous service operations.")
return
}
next(rw, r)
}