dmitri.shuralyov.com/website/gido

Rename responseWriter{header,bytes} to {header,bytes}ResponseWriter.

This is a more consistent naming scheme. The detail comes first, the
interface name comes last. Similar to limitedWriter, nopCloser, etc.
dmitshur committed 6 years ago commit 08f99819a87ffdf2fdaccc331a2780115e520f48
Showing partial commit. Full Commit
Collapse all
util.go
@@ -21,11 +21,11 @@ type errorHandler struct {
		GetAuthenticated(context.Context) (users.User, error)
	} // May be nil if there's no users service.
}

func (h *errorHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	rw := &responseWriterHeader{ResponseWriter: w}
	rw := &headerResponseWriter{ResponseWriter: w}
	err := h.handler(rw, req)
	if err == nil {
		// Do nothing.
		return
	}
@@ -88,34 +88,34 @@ func (h *errorHandler) getAuthenticated(ctx context.Context) (users.User, error)
		return users.User{}, errors.New("no users service")
	}
	return h.users.GetAuthenticated(ctx)
}

// responseWriterHeader wraps a real http.ResponseWriter and captures
// headerResponseWriter wraps a real http.ResponseWriter and captures
// whether or not the header has been written.
type responseWriterHeader struct {
type headerResponseWriter struct {
	http.ResponseWriter

	WroteHeader bool // Write or WriteHeader was called.
}

func (rw *responseWriterHeader) Write(p []byte) (n int, err error) {
func (rw *headerResponseWriter) Write(p []byte) (n int, err error) {
	rw.WroteHeader = true
	return rw.ResponseWriter.Write(p)
}
func (rw *responseWriterHeader) WriteHeader(code int) {
func (rw *headerResponseWriter) WriteHeader(code int) {
	rw.WroteHeader = true
	rw.ResponseWriter.WriteHeader(code)
}

// top adds some instrumentation on top of Handler.
type top struct{ Handler http.Handler }

func (t top) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	path := req.URL.Path
	started := time.Now()
	rw := &responseWriterBytes{ResponseWriter: w}
	rw := &bytesResponseWriter{ResponseWriter: w}
	t.Handler.ServeHTTP(rw, req)
	fmt.Printf("TIMING: %s: %v\n", path, time.Since(started))
	if path != req.URL.Path {
		log.Printf("warning: req.URL.Path was modified from %v to %v\n", path, req.URL.Path)
	}
@@ -128,19 +128,19 @@ func (t top) ServeHTTP(w http.ResponseWriter, req *http.Request) {
func haveType(w http.ResponseWriter) bool {
	_, ok := w.Header()["Content-Type"]
	return ok
}

// responseWriterBytes wraps a real http.ResponseWriter and captures
// bytesResponseWriter wraps a real http.ResponseWriter and captures
// whether any bytes were written.
type responseWriterBytes struct {
type bytesResponseWriter struct {
	http.ResponseWriter

	WroteBytes bool // Whether non-zero bytes have been written.
}

func (rw *responseWriterBytes) Write(p []byte) (n int, err error) {
func (rw *bytesResponseWriter) Write(p []byte) (n int, err error) {
	if len(p) > 0 {
		rw.WroteBytes = true
	}
	return rw.ResponseWriter.Write(p)
}