aboutsummaryrefslogtreecommitdiff
path: root/buffer.go
blob: e31bebd25faa5da143145ddb68c4103596df8c9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main

// Taken from https://github.com/goods/httpbuf

import (
	"bytes"
	"net/http"
	"sync"
)

//Buffer is a type that implements http.ResponseWriter but buffers all the data
//and headers.
type Buffer struct {
	bytes.Buffer
	Status  int
	headers http.Header
	once    sync.Once
}

//Header implements the header method of http.ResponseWriter
func (b *Buffer) Header() http.Header {
	b.once.Do(func() {
		b.headers = make(http.Header)
	})
	return b.headers
}

//WriteHeader implements the WriteHeader method of http.ResponseWriter
func (b *Buffer) WriteHeader(resp int) {
	b.Status = resp
}

//Apply takes an http.ResponseWriter and calls the required methods on it to
//output the buffered headers, response code, and data. It returns the number
//of bytes written and any errors flushing.
func (b *Buffer) Apply(w http.ResponseWriter) (n int, err error) {
	if len(b.headers) > 0 {
		h := w.Header()
		for key, val := range b.headers {
			h[key] = val
		}
	}
	if b.Status > 0 {
		w.WriteHeader(b.Status)
	}
	n, err = w.Write(b.Bytes())
	return
}