Skip to content

Commit

Permalink
expose status and contentLength
Browse files Browse the repository at this point in the history
  • Loading branch information
luopengift committed Apr 24, 2024
1 parent 8aaa70c commit 6dfcf94
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
9 changes: 5 additions & 4 deletions response_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,27 @@ import (
"net/http"
)

// ResponseWriter wrap `http.ResponseWriter` interface.
type ResponseWriter struct {
http.ResponseWriter
wroteHeader bool
statusCode int
contentLength int64
StatusCode int
ContentLength int64
}

func (w *ResponseWriter) WriteHeader(statusCode int) {
if w.wroteHeader {
return
}
w.wroteHeader = true
w.statusCode = statusCode
w.StatusCode = statusCode
w.ResponseWriter.WriteHeader(statusCode)
}

func (w *ResponseWriter) Write(buf []byte) (int, error) {
w.WriteHeader(http.StatusOK)
n, err := w.ResponseWriter.Write(buf)
w.contentLength += int64(n)
w.ContentLength += int64(n)
return n, err
}

Expand Down
4 changes: 2 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ var ErrHandler = func(err string, code int) http.Handler {
})
}

// WarpHttpHandler warp `http.Handler`.
func WarpHttpHandler(next http.Handler) func(http.Handler) http.Handler {
// WarpHandler warp `http.Handler`.
func WarpHandler(next http.Handler) func(http.Handler) http.Handler {
return func(http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
Expand Down
4 changes: 2 additions & 2 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
)

// WarpRoundTripper warp `http.RoundTripper`.
func WarpRoundTripper(h http.RoundTripper) func(http.RoundTripper) http.RoundTripper {
return func(next http.RoundTripper) http.RoundTripper {
func WarpRoundTripper(next http.RoundTripper) func(http.RoundTripper) http.RoundTripper {
return func(http.RoundTripper) http.RoundTripper {
return RoundTripperFunc(func(r *http.Request) (*http.Response, error) {
return next.RoundTrip(r)
})
Expand Down

0 comments on commit 6dfcf94

Please sign in to comment.