-
Notifications
You must be signed in to change notification settings - Fork 6
/
response_writer.go
63 lines (54 loc) · 1.31 KB
/
response_writer.go
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
57
58
59
60
61
62
63
package coraza
import (
"io"
"github.com/corazawaf/coraza/v2"
"github.com/corazawaf/coraza/v2/types"
"github.com/gin-gonic/gin"
)
type responseWriter struct {
gin.ResponseWriter
tx *coraza.Transaction
headersProcessed bool
size int
}
func (w responseWriter) Write(b []byte) (n int, err error) {
if it := w.processResponseHeaders(); it != nil {
// transaction was interrupted :(
return
}
w.WriteHeaderNow()
n, err = w.tx.ResponseBodyBuffer.Write(b)
w.size += n
return
}
func (w *responseWriter) WriteString(s string) (n int, err error) {
if it := w.processResponseHeaders(); it != nil {
// transaction was interrupted :(
return
}
w.WriteHeaderNow()
n, err = io.WriteString(w.tx.ResponseBodyBuffer, s)
w.size += n
return
}
func (w *responseWriter) processResponseHeaders() *types.Interruption {
if w.headersProcessed || w.tx.Interruption != nil {
return w.tx.Interruption
}
w.headersProcessed = true
for k, vv := range w.ResponseWriter.Header() {
for _, v := range vv {
w.tx.AddResponseHeader(k, v)
}
}
return w.tx.ProcessResponseHeaders(w.ResponseWriter.Status(), "http/1.1")
}
func (w *responseWriter) Status() int {
if w.tx.Interruption != nil {
return w.tx.Interruption.Status
}
return w.ResponseWriter.Status()
}
func (w *responseWriter) Size() int {
return w.size
}