grim/hgkeeper

Add a caching layer to the hgweb portion. This should take some strain off of mercurial anf our cpu quota
package hgweb
import (
"bytes"
"io"
"net/http"
)
type responseWriter struct {
w http.ResponseWriter
wroteHeader bool
statusCode int
written int64
multiWriter io.Writer
cachedBody *bytes.Buffer
}
var _ http.ResponseWriter = (*responseWriter)(nil)
func newResponseWriter(w http.ResponseWriter) *responseWriter {
rw := &responseWriter{
w: w,
cachedBody: &bytes.Buffer{},
}
rw.multiWriter = io.MultiWriter(w, rw.cachedBody)
return rw
}
func (w *responseWriter) Header() http.Header {
return w.w.Header()
}
func (w *responseWriter) Write(data []byte) (int, error) {
if !w.wroteHeader {
w.wroteHeader = true
w.statusCode = http.StatusOK
}
l, err := w.multiWriter.Write(data)
w.written += int64(l)
return l, err
}
func (w *responseWriter) WriteHeader(statusCode int) {
if !w.wroteHeader {
w.wroteHeader = true
w.statusCode = statusCode
}
w.w.WriteHeader(w.statusCode)
}
func (w *responseWriter) StatusCode() int {
return w.statusCode
}
func (w *responseWriter) Written() int64 {
return w.written
}
func (w *responseWriter) Body() []byte {
return w.cachedBody.Bytes()
}