From 528356000fe62ce4bd9079e3e7de5945a579cc38 Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Thu, 20 Sep 2012 10:34:43 -0700 Subject: [PATCH] some tweaks --- README | 2 +- src/xx-http-server-basic.go | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/README b/README index da99594..da03188 100644 --- a/README +++ b/README @@ -1,7 +1,7 @@ = next get web presence in good enough shape to point to from blog post gbe-web - staging site, behind basic auth + staging basic auth basic design / brand favicon google analytics diff --git a/src/xx-http-server-basic.go b/src/xx-http-server-basic.go index c8b43bb..531d110 100644 --- a/src/xx-http-server-basic.go +++ b/src/xx-http-server-basic.go @@ -9,7 +9,7 @@ import ( type Authenticator func(string, string) bool -func CheckAuth(r *http.Request, auth Authenticator) bool { +func testAuth(r *http.Request, auth Authenticator) bool { s := strings.SplitN(r.Header.Get("Authorization"), " ", 2) if len(s) != 2 || s[0] != "Basic" { return false @@ -25,30 +25,31 @@ func CheckAuth(r *http.Request, auth Authenticator) bool { return auth(pair[0], pair[1]) } -func RequireAuth(w http.ResponseWriter, r *http.Request) { +func requireAuth(w http.ResponseWriter, r *http.Request) { w.Header().Set("WWW-Authenticate", `Basic realm="private"`) w.WriteHeader(401) w.Write([]byte("401 Unauthorized\n")) } -func WithAuth(h http.HandlerFunc, a Authenticator) http.HandlerFunc { +func wrapAuth(h http.HandlerFunc, a Authenticator) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - if CheckAuth(r, a) { + if testAuth(r, a) { h(w, r) } else { - RequireAuth(w, r) + requireAuth(w, r) } } } -func handle(w http.ResponseWriter, r *http.Request) { +func hello(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Hello secret world!") } func main() { - authenticator := func(_, password string) bool { + checkPassword := func(_, password string) bool { return password == "supersecret" } - http.HandleFunc("/", WithAuth(handle, authenticator)) - http.ListenAndServe(":5000", nil) + handler1 := http.HanderFunc(hello) + handler2 := wrapAuth(handler1, checkPassword) + http.ListenAndServe(":5000", handler2) }