package main
import (
"context"
"fmt"
"io"
"regexp"
"strings"
"github.com/fastly/compute-sdk-go/fsthttp"
)
const BackendName = "origin_0"
func main() {
fsthttp.ServeFunc(func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
resp, err := r.Send(ctx, BackendName)
if err != nil {
w.WriteHeader(fsthttp.StatusBadGateway)
fmt.Fprintln(w, err.Error())
return
}
cookie := r.Header.Get("cookie")
re := regexp.MustCompile(`permission=:(.+:.+:.+):;`)
permission := re.FindStringSubmatch(cookie)
PermValues := strings.Split(permission[1], ":")
q := resp.Header.Get("require-group")
re = regexp.MustCompile(`:(.+):`)
qs := re.FindStringSubmatch(q)
QValues := strings.Split(qs[1], ":")
result := CheckIntersection(PermValues, QValues)
fmt.Println(result)
if result == "No intersection found" {
resp := &fsthttp.Response{
StatusCode: fsthttp.StatusForbidden,
Body: io.NopCloser(strings.NewReader("")),
}
w.Header().Reset(resp.Header)
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
return
}
w.Header().Reset(resp.Header)
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
})
}
func CheckIntersection(first []string, second []string) string {
var result string
for _, f := range first {
for _, s := range second {
if s == f {
result += s + " "
}
}
}
if len(result) > 0 {
return "Intersection: " + result
}else{
return "No intersection found"
}
}