You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

31 lines
719 B

  1. package handlers
  2. import (
  3. "net/http"
  4. )
  5. // This interface can be implemented by an augmented ResponseWriter, so that
  6. // it doesn't hide other augmented writers in the chain.
  7. type WrapWriter interface {
  8. http.ResponseWriter
  9. WrappedWriter() http.ResponseWriter
  10. }
  11. // Helper function to retrieve a specific ResponseWriter.
  12. func GetResponseWriter(w http.ResponseWriter,
  13. predicate func(http.ResponseWriter) bool) (http.ResponseWriter, bool) {
  14. for {
  15. // Check if this writer is the one we're looking for
  16. if w != nil && predicate(w) {
  17. return w, true
  18. }
  19. // If it is a WrapWriter, move back the chain of wrapped writers
  20. ww, ok := w.(WrapWriter)
  21. if !ok {
  22. return nil, false
  23. }
  24. w = ww.WrappedWriter()
  25. }
  26. }