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)
13 "github.com/pusher/oauth2_proxy/pkg/logger"
16// responseLogger is wrapper of http.ResponseWriter that keeps track of its HTTP status
18type responseLogger struct {
26// Header returns the ResponseWriter's Header
27func (l *responseLogger) Header() http.Header {
32func (l *responseLogger) Hijack() (rwc net.Conn, buf *bufio.ReadWriter, err error) {
33 if hj, ok := l.w.(http.Hijacker); ok {
36 return nil, nil, errors.New("http.Hijacker is not available on writer")
39// ExtractGAPMetadata extracts and removes GAP headers from the ResponseWriter's
41func (l *responseLogger) ExtractGAPMetadata() {
42 upstream := l.w.Header().Get("GAP-Upstream-Address")
45 l.w.Header().Del("GAP-Upstream-Address")
47 authInfo := l.w.Header().Get("GAP-Auth")
50 l.w.Header().Del("GAP-Auth")
54// Write writes the response using the ResponseWriter
55func (l *responseLogger) Write(b []byte) (int, error) {
57 // The status will be StatusOK if WriteHeader has not been called yet
58 l.status = http.StatusOK
60 l.ExtractGAPMetadata()
61 size, err := l.w.Write(b)
66// WriteHeader writes the status code for the Response
67func (l *responseLogger) WriteHeader(s int) {
68 l.ExtractGAPMetadata()
73// Status returns the response status code
74func (l *responseLogger) Status() int {
78// Size returns the response size
79func (l *responseLogger) Size() int {
83// Flush sends any buffered data to the client
84func (l *responseLogger) Flush() {
85 if flusher, ok := l.w.(http.Flusher); ok {
90// loggingHandler is the http.Handler implementation for LoggingHandler
91type loggingHandler struct {
95// LoggingHandler provides an http.Handler which logs requests to the HTTP server
96func LoggingHandler(h http.Handler) http.Handler {
97 return loggingHandler{
102func (h loggingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
105 responseLogger := &responseLogger{w: w}
106 h.handler.ServeHTTP(responseLogger, req)
107 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