forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
required_header.go
47 lines (42 loc) · 1.23 KB
/
required_header.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
37
38
39
40
41
42
43
44
45
46
47
package middleware
import (
"net/http"
"regexp"
"github.com/goadesign/goa"
"context"
)
// RequireHeader requires a request header to match a value pattern. If the
// header is missing or does not match then the failureStatus is the response
// (e.g. http.StatusUnauthorized). If pathPattern is nil then any path is
// included. If requiredHeaderValue is nil then any value is accepted so long as
// the header is non-empty.
func RequireHeader(
service *goa.Service,
pathPattern *regexp.Regexp,
requiredHeaderName string,
requiredHeaderValue *regexp.Regexp,
failureStatus int) goa.Middleware {
return func(h goa.Handler) goa.Handler {
return func(ctx context.Context, rw http.ResponseWriter, req *http.Request) (err error) {
if pathPattern == nil || pathPattern.MatchString(req.URL.Path) {
matched := false
headerValue := req.Header.Get(requiredHeaderName)
if len(headerValue) > 0 {
if requiredHeaderValue == nil {
matched = true
} else {
matched = requiredHeaderValue.MatchString(headerValue)
}
}
if matched {
err = h(ctx, rw, req)
} else {
err = service.Send(ctx, failureStatus, http.StatusText(failureStatus))
}
} else {
err = h(ctx, rw, req)
}
return
}
}
}