Skip to content

Commit

Permalink
fix up some redirect helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
jtolio committed Jan 12, 2017
1 parent c8344fc commit 6d0cd4e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 17 deletions.
6 changes: 3 additions & 3 deletions whfatal/fatal.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ type fatalBehavior func(w http.ResponseWriter, r *http.Request)
// handler, wherr.HandleWith handlers, and a few other handlers. Otherwise,
// the wrapper will be one of the things interrupted by Fatal calls.
func Catch(h http.Handler) http.Handler {
return whroute.HandlerFunc(h,
return whmon.MonitorResponse(whroute.HandlerFunc(h,
func(w http.ResponseWriter, r *http.Request) {
rw := whmon.WrapResponseWriter(w)
rw := w.(whmon.ResponseWriter)
defer func() {
rec := recover()
if rec == nil {
Expand All @@ -48,7 +48,7 @@ func Catch(h http.Handler) http.Handler {
}
}()
h.ServeHTTP(rw, r)
})
}))
}

// Redirect is like whredir.Redirect but panics so all additional request
Expand Down
6 changes: 3 additions & 3 deletions whlog/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ var (
// whmon's ResponseWriter to keep track of activity. whfatal.Catch should be
// placed *inside* if applicable. whlog.Default makes a good default logger.
func LogResponses(logger Loggerf, h http.Handler) http.Handler {
return whroute.HandlerFunc(h,
return whmon.MonitorResponse(whroute.HandlerFunc(h,
func(w http.ResponseWriter, r *http.Request) {
method, requestURI := r.Method, r.RequestURI
rw := whmon.WrapResponseWriter(w)
rw := w.(whmon.ResponseWriter)
start := time.Now()

defer func() {
Expand All @@ -46,7 +46,7 @@ func LogResponses(logger Loggerf, h http.Handler) http.Handler {

logger(`%s %#v %d %d %d %v`, method, requestURI, code,
r.ContentLength, rw.Written(), time.Since(start))
})
}))
}

// LogRequests takes a Handler and makes it log requests (prior to request
Expand Down
6 changes: 3 additions & 3 deletions whmon/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ type ResponseWriter interface {
func MonitorResponse(h http.Handler) http.Handler {
return whroute.HandlerFunc(h,
func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(WrapResponseWriter(w), r)
h.ServeHTTP(wrapResponseWriter(w), r)
})
}

// WrapResponseWriter will wrap an http.ResponseWriter with the instrumentation
// wrapResponseWriter will wrap an http.ResponseWriter with the instrumentation
// to turn it into a whmon.ResponseWriter. An http.ResponseWriter must be
// turned into a whmon.ResponseWriter before being used. It's much better
// to use MonitorResponse instead of WrapResponseWriter.
func WrapResponseWriter(w http.ResponseWriter) ResponseWriter {
func wrapResponseWriter(w http.ResponseWriter) ResponseWriter {
if ww, ok := w.(ResponseWriter); ok {
// don't do it if we already have the methods we need
return ww
Expand Down
53 changes: 45 additions & 8 deletions whredir/redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ package whredir // import "gopkg.in/webhelp.v1/whredir"

import (
"net/http"
"net/url"
"strings"

"gopkg.in/webhelp.v1/wherr"
"gopkg.in/webhelp.v1/whmux"
"gopkg.in/webhelp.v1/whroute"
)
Expand Down Expand Up @@ -64,13 +67,17 @@ var _ whroute.Lister = RedirectHandlerFunc(nil)
func RequireHTTPS(handler http.Handler) http.Handler {
return whroute.HandlerFunc(handler,
func(w http.ResponseWriter, r *http.Request) {
if r.URL.Scheme != "https" {
u := *r.URL
u.Scheme = "https"
Redirect(w, r, u.String())
} else {
if r.URL.Scheme == "https" {
handler.ServeHTTP(w, r)
return
}
u, err := url.ParseRequestURI(r.RequestURI)
if err != nil {
wherr.Handle(w, r, err)
return
}
u.Scheme = "https"
Redirect(w, r, u.String())
})
}

Expand All @@ -82,9 +89,39 @@ func RequireHost(host string, handler http.Handler) http.Handler {
}
return whmux.Host{
host: handler,
"*": RedirectHandlerFunc(func(r *http.Request) string {
u := *r.URL
"*": http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u, err := url.ParseRequestURI(r.RequestURI)
if err != nil {
wherr.Handle(w, r, err)
return
}
u.Host = host
return u.String()
Redirect(w, r, u.String())
})}
}

// RequireTrailingSlash makes sure all handled paths have a trailing slash.
// This helps with relative URLs for other resources.
func RequireTrailingSlash(h http.Handler) http.Handler {
return whroute.HandlerFunc(h,
func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/") {
h.ServeHTTP(w, r)
return
}

u, err := url.ParseRequestURI(r.RequestURI)
if err != nil {
wherr.Handle(w, r, err)
return
}

if strings.HasSuffix(u.Path, "/") {
h.ServeHTTP(w, r)
return
}

u.Path += "/"
Redirect(w, r, u.String())
})
}

0 comments on commit 6d0cd4e

Please sign in to comment.