diff options
author | Gabriel A. Giovanini <mail@gabrielgio.me> | 2024-03-22 18:28:31 +0100 |
---|---|---|
committer | Gabriel A. Giovanini <mail@gabrielgio.me> | 2024-03-22 18:28:31 +0100 |
commit | b29ad0afa15f8f34f9825cadc31ee97559ebcfd7 (patch) | |
tree | 2f470576755d3b11ce1c2137053f1ac35badeaae /ext.go | |
parent | b3d0af2de29711abfe6da373786d365d9a6de198 (diff) | |
download | jnfilter-b29ad0afa15f8f34f9825cadc31ee97559ebcfd7.tar.gz jnfilter-b29ad0afa15f8f34f9825cadc31ee97559ebcfd7.tar.bz2 jnfilter-b29ad0afa15f8f34f9825cadc31ee97559ebcfd7.zip |
feat: Add metricsv0.1.4
Diffstat (limited to 'ext.go')
-rw-r--r-- | ext.go | 75 |
1 files changed, 75 insertions, 0 deletions
@@ -0,0 +1,75 @@ +package main + +import ( + "io" + "net/http" +) + +type ResponseWriter interface { + http.ResponseWriter + Status() int +} + +type responseWriter struct { + http.ResponseWriter + pendingStatus int + status int + size int +} + +func NewResponseWriter(rw http.ResponseWriter) ResponseWriter { + return &responseWriter{ + ResponseWriter: rw, + } +} + +func (rw *responseWriter) WriteHeader(s int) { + if rw.Written() { + return + } + + rw.pendingStatus = s + + if rw.Written() { + return + } + + rw.status = s + rw.ResponseWriter.WriteHeader(s) +} + +func (rw *responseWriter) Write(b []byte) (int, error) { + if !rw.Written() { + // The status will be StatusOK if WriteHeader has not been called yet + rw.WriteHeader(http.StatusOK) + } + size, err := rw.ResponseWriter.Write(b) + rw.size += size + return size, err +} + +func (rw *responseWriter) ReadFrom(r io.Reader) (n int64, err error) { + if !rw.Written() { + // The status will be StatusOK if WriteHeader has not been called yet + rw.WriteHeader(http.StatusOK) + } + n, err = io.Copy(rw.ResponseWriter, r) + rw.size += int(n) + return +} + +func (rw *responseWriter) Unwrap() http.ResponseWriter { + return rw.ResponseWriter +} + +func (rw *responseWriter) Status() int { + if rw.Written() { + return rw.status + } + + return rw.pendingStatus +} + +func (rw *responseWriter) Written() bool { + return rw.status != 0 +} |