1// largely adapted from https://github.com/gorilla/handlers/blob/master/handlers.go
2// to add logging of request duration as last value (and drop referrer)
10 "github.com/pusher/oauth2_proxy/logger"
13// responseLogger is wrapper of http.ResponseWriter that keeps track of its HTTP status
15type responseLogger struct {
23// Header returns the ResponseWriter's Header
24func (l *responseLogger) Header() http.Header {
29func (l *responseLogger) Hijack() (rwc net.Conn, buf *bufio.ReadWriter, err error) {
30 if hj, ok := l.w.(http.Hijacker); ok {
33 return nil, nil, errors.New("http.Hijacker is not available on writer")
36// ExtractGAPMetadata extracts and removes GAP headers from the ResponseWriter's
38func (l *responseLogger) ExtractGAPMetadata() {
39 upstream := l.w.Header().Get("GAP-Upstream-Address")
42 l.w.Header().Del("GAP-Upstream-Address")
44 authInfo := l.w.Header().Get("GAP-Auth")
47 l.w.Header().Del("GAP-Auth")
51// Write writes the response using the ResponseWriter
52func (l *responseLogger) Write(b []byte) (int, error) {
54 // The status will be StatusOK if WriteHeader has not been called yet
55 l.status = http.StatusOK
57 l.ExtractGAPMetadata()
58 size, err := l.w.Write(b)
63// WriteHeader writes the status code for the Response
64func (l *responseLogger) WriteHeader(s int) {
65 l.ExtractGAPMetadata()
70// Status returns the response status code
71func (l *responseLogger) Status() int {
75// Size returns teh response size
76func (l *responseLogger) Size() int {
80func (l *responseLogger) Flush() {
81 if flusher, ok := l.w.(http.Flusher); ok {
86// loggingHandler is the http.Handler implementation for LoggingHandlerTo and its friends
87type loggingHandler struct {
91// LoggingHandler provides an http.Handler which logs requests to the HTTP server
92func LoggingHandler(h http.Handler) http.Handler {
93 return loggingHandler{
98func (h loggingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
101 responseLogger := &responseLogger{w: w}
102 h.handler.ServeHTTP(responseLogger, req)
103 logger.PrintReq(responseLogger.authInfo, responseLogger.upstream, req, url, t, responseLogger.Status(), responseLogger.Size())
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list