auth

Paddy 2015-05-11 Parent:cf6c1f05eb21 Child:0ff23f3a4ede

165:04c8edf89e3b Go to Latest

auth/request.go

Support CORS. Add support for the OPTIONS method on certain endpoints. Automatically add headers (and respond to) OPTIONS requests. Note that these headers are full of deceit and mendacity, and we should switch to trout soon so we can actually be honest about what methods we support.

History
     1.1 --- a/request.go	Sat Apr 25 22:44:36 2015 -0400
     1.2 +++ b/request.go	Mon May 11 20:46:23 2015 -0400
     1.3 @@ -89,6 +89,15 @@
     1.4  
     1.5  func wrap(context Context, f func(w http.ResponseWriter, r *http.Request, context Context)) http.Handler {
     1.6  	return negotiate(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
     1.7 +		w.Header().Set("Access-Control-Allow-Origin", "*")
     1.8 +		w.Header().Set("Access-Control-Allow-Headers", r.Header.Get("Access-Control-Request-Headers"))
     1.9 +		w.Header().Set("Access-Control-Allow-Credentials", "true")
    1.10 +		if r.Method == "OPTIONS" {
    1.11 +			w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS")
    1.12 +			w.Header().Set("Allow", "GET, POST, PUT, DELETE, PATCH, OPTIONS")
    1.13 +			w.WriteHeader(http.StatusOK)
    1.14 +			return
    1.15 +		}
    1.16  		f(w, r, context)
    1.17  	}))
    1.18  }