astaxie/beego — pull request #897 by chrisport (2014-10-31 14:50:21+00:00)
Title: Bug/Improvement: Delay WriteHeader to enable Handler to change header/statuscode

### Problem

**Headers and statuscode** are already written when a handler (e.g. 404-handler) is called, therefore the author of a handler cannot change these.

### Changes made

I wrote a wrapper for ResponseWriter which doesn't forward writeHeader on its inner http.ResponseWriter immediately. Instead it stores the status code until Write or SendHeader is called. Therefore it is possible to call WriteHeader multiple times and the last one will be sent.
Additionaly SendHeader is called in a deferred method of ServeHTTP (executed AFTER recoverPanic is executed) to ensure that a Header is written.

Here a visualization of the flow in case of Exception, Abort and Panic:
![flow](https://cloud.githubusercontent.com/assets/6203829/4862458/c5142bb6-610c-11e4-9567-b3ba59d11586.png)