Skip to content

Response.Flush does not mark the response as committed #3240

Open

Description

@vishr

Problem

echo.Response.Flush calls the underlying writer's Flush without going through Response.WriteHeader first. If nothing has been written yet, net/http sends an implicit 200 OK and the headers, but Response.Committed stays false. Three things go wrong after that:

  • A later Write calls WriteHeader again, and net/http logs http: superfluous response.WriteHeader call.
  • Before hooks run after the headers are already on the wire, so any headers they add are lost.
  • If the handler then returns an error, DefaultHTTPErrorHandler thinks the response is uncommitted and writes an error body under the 200 that was already sent.

Reproduce

e.GET("/", func(c *echo.Context) error {
	if err := http.NewResponseController(c.Response()).Flush(); err != nil {
		return err
	}
	return echo.ErrBadRequest
})

The client gets 200 followed by {"message":"Bad Request"}, and the request logger records the wrong status.

Expected

When the response isn't committed yet, Flush should commit it through WriteHeader with the current status (default 200). That way Before hooks run and Committed is accurate.

Found while reviewing #3239.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions