Welcome to mirror list, hosted at ThFree Co, Russian Federation.

countingresponsewriter.go « helper « internal « workhorse - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a79d51d4c6ac26061b4f2332a91cf29525bc60d5 (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
49
50
51
52
53
54
55
56
package helper

import (
	"net/http"
)

type CountingResponseWriter interface {
	http.ResponseWriter
	Count() int64
	Status() int
}

type countingResponseWriter struct {
	rw     http.ResponseWriter
	status int
	count  int64
}

func NewCountingResponseWriter(rw http.ResponseWriter) CountingResponseWriter {
	return &countingResponseWriter{rw: rw}
}

func (c *countingResponseWriter) Header() http.Header {
	return c.rw.Header()
}

func (c *countingResponseWriter) Write(data []byte) (int, error) {
	if c.status == 0 {
		c.WriteHeader(http.StatusOK)
	}

	n, err := c.rw.Write(data)
	c.count += int64(n)
	return n, err
}

func (c *countingResponseWriter) WriteHeader(status int) {
	if c.status != 0 {
		return
	}

	c.status = status
	c.rw.WriteHeader(status)
}

// Count returns the number of bytes written to the ResponseWriter. This
// function is not thread-safe.
func (c *countingResponseWriter) Count() int64 {
	return c.count
}

// Status returns the first HTTP status value that was written to the
// ResponseWriter. This function is not thread-safe.
func (c *countingResponseWriter) Status() int {
	return c.status
}