10 "github.com/justinas/alice"
11 middlewareapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
12 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
15// NewRequestLogger returns middleware which logs requests
16// It uses a custom ResponseWriter to track status code & response size details
17func NewRequestLogger() alice.Constructor {
21func requestLogger(next http.Handler) http.Handler {
22 return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
23 startTime := time.Now()
26 responseLogger := &loggingResponse{ResponseWriter: rw}
27 next.ServeHTTP(responseLogger, req)
29 scope := middlewareapi.GetRequestScope(req)
30 // If scope is nil, this will panic.
31 // A scope should always be injected before this handler is called.
38 responseLogger.Status(),
39 responseLogger.Size(),
44func getUser(scope *middlewareapi.RequestScope) string {
45 session := scope.Session
47 if session.Email != "" {
55// loggingResponse is a custom http.ResponseWriter that allows tracking certain
56// details for request logging.
57type loggingResponse struct {
64// Write writes the response using the ResponseWriter
65func (r *loggingResponse) Write(b []byte) (int, error) {
67 // The status will be StatusOK if WriteHeader has not been called yet
68 r.status = http.StatusOK
70 size, err := r.ResponseWriter.Write(b)
75// WriteHeader writes the status code for the Response
76func (r *loggingResponse) WriteHeader(s int) {
77 r.ResponseWriter.WriteHeader(s)
81// Hijack implements the `http.Hijacker` interface that actual ResponseWriters
82// implement to support websockets
83func (r *loggingResponse) Hijack() (net.Conn, *bufio.ReadWriter, error) {
84 if hj, ok := r.ResponseWriter.(http.Hijacker); ok {
87 return nil, nil, errors.New("http.Hijacker is not available on writer")
90// Flush sends any buffered data to the client. Implements the `http.Flusher`
92func (r *loggingResponse) Flush() {
93 if flusher, ok := r.ResponseWriter.(http.Flusher); ok {
95 // The status will be StatusOK if WriteHeader has not been called yet
96 r.status = http.StatusOK
102// Status returns the response status code
103func (r *loggingResponse) Status() int {
107// Size returns the response size
108func (r *loggingResponse) Size() int {
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