Skip to content

Commit

Permalink
fix: prevent double response sending in HTTP middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielluizsf committed Jan 31, 2025
1 parent 31424fd commit 4c3d3d7
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ func (s *Server) Listen() error {
}
errCh <- nil
}()

log.Println(banner(s.addr))
return <-errCh
}

func banner(address string) string {
logo := []string{
logo := []string{
" _ ",
" ____ (_)___ ___ ",
" / __ \\/ / __ \\/ _ \\",
Expand All @@ -185,7 +185,6 @@ func banner(address string) string {
}
}


contentWidth := max(maxLogoWidth, len(addressLine))
totalWidth := contentWidth + 4

Expand Down Expand Up @@ -219,7 +218,6 @@ func banner(address string) string {
return fmt.Sprintf("\n%s\n%s%s", top, content.String(), bottom)
}


// Shutdown gracefully stops the HTTP server, allowing any pending requests to complete.
// This method should be called when you want to stop the server from accepting new connections
// and shut it down safely without losing any ongoing requests.
Expand Down Expand Up @@ -494,7 +492,9 @@ func httpMiddleware(m Handler, next http.Handler) http.Handler {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
next.ServeHTTP(w, r)
if !res.sent {
next.ServeHTTP(w, r)
}
})
}

Expand Down Expand Up @@ -598,6 +598,7 @@ func (r *Request) Context() context.Context {
type Response struct {
res http.ResponseWriter
statusCode int
sent bool
}

// HTTP returns the HTTP response.
Expand Down Expand Up @@ -643,6 +644,7 @@ const defaultStatusCode = http.StatusOK
// It uses a defaultStatusCode if one isn't explicitly set.
func (r *Response) Send(b []byte) error {
r.writeStatus()
r.sent = true
if len(b) > 0 {
r.SetHeader("Content-Type", http.DetectContentType(b))
_, err := r.res.Write(b)
Expand All @@ -657,6 +659,7 @@ func (r *Response) Send(b []byte) error {
// into JSON format and setting the appropriate content-type and status code.
func (r *Response) JSON(data JSON) error {
r.res.Header().Add("Content-Type", "application/json")
r.sent = true
if r.invalidStatusCode() {
r.statusCode = defaultStatusCode
}
Expand All @@ -667,6 +670,7 @@ func (r *Response) JSON(data JSON) error {
// SendStatus sends the HTTP response with the specified status code.
func (r *Response) SendStatus(statusCode int) error {
r.statusCode = statusCode
r.sent = true
return &ServerError{
StatusCode: r.statusCode,
Err: errors.New(http.StatusText(r.statusCode)),
Expand Down

0 comments on commit 4c3d3d7

Please sign in to comment.