package main
import (
"context"
"fmt"
"io"
"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) {
cookies := strings.Split(r.Header.Values("cookie")[0], "; ")
keep := []string{"name2", "name3", "name6"}
result := FilterExcept(cookies, keep)
r.Header.Set("cookie", result)
resp, err := r.Send(ctx, BackendName)
if err != nil {
w.WriteHeader(fsthttp.StatusBadGateway)
fmt.Fprintln(w, err.Error())
return
}
w.Header().Reset(resp.Header)
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
})
}
func FilterExcept(input []string, keep []string) string {
var result string
for _, val := range keep {
for index, element := range input {
if strings.Contains(element, val) {
if index < len(input)-1 {
result += element + "; "
} else {
result += element
}
}
}
}
return result
}