-
Notifications
You must be signed in to change notification settings - Fork 0
/
responsewriter.go
136 lines (123 loc) · 2.73 KB
/
responsewriter.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package serve
import (
"bufio"
"context"
"fmt"
"io"
"net"
"net/http"
"net/http/httputil"
"strings"
"github.com/evocert/lnksnk/iorw"
)
type responseWriter struct {
ctx context.Context
chunkedWriter io.WriteCloser
wroteHeader bool
bufw *bufio.Writer
statusCode int
header http.Header
orgwtr io.Writer
req *http.Request
}
func NewResponseWriter(req *http.Request, wt io.Writer) (rspnswtr *responseWriter) {
var orgwtr io.Writer = wt
rspnswtr = &responseWriter{
header: http.Header{}, bufw: bufio.NewWriterSize(orgwtr, 65536), orgwtr: orgwtr, statusCode: 200, req: req, ctx: req.Context()}
return
}
func (w *responseWriter) Header() http.Header {
return w.header
}
func (w *responseWriter) Flush() {
if w != nil && w.bufw != nil {
w.bufw.Flush()
}
}
func (w *responseWriter) Hijack() (con net.Conn, bufrw *bufio.ReadWriter, err error) {
if w != nil {
if con, _ = w.orgwtr.(net.Conn); con != nil {
bufrw = bufio.NewReadWriter(bufio.NewReader(con), bufio.NewWriter(con))
} else {
err = fmt.Errorf("unable to hijack connection")
}
} else {
err = fmt.Errorf("unable to hijack connection")
}
return con, bufrw, err
}
func (w *responseWriter) Close() (err error) {
if w != nil {
w.Flush()
if !w.wroteHeader {
w.writeHeader()
} else {
w.wroteHeader = false
}
if w.req != nil {
w.req = nil
}
if w.chunkedWriter != nil {
w.chunkedWriter.Close()
w.chunkedWriter = nil
}
if w.orgwtr != nil {
if clswtr, _ := w.orgwtr.(io.Closer); clswtr != nil {
clswtr.Close()
}
w.orgwtr = nil
}
}
return err
}
func (w *responseWriter) writeHeader() {
if !w.wroteHeader {
w.wroteHeader = true
if w.bufw != nil {
if w.req != nil {
protoHeaderLine := fmt.Sprintf("%s %d %s\r\n", w.req.Proto, w.statusCode, http.StatusText(w.statusCode))
fmt.Fprint(w.bufw, protoHeaderLine)
ischunked := false
if len(w.header) > 0 {
for hdr, hdv := range w.header {
iorw.Fprint(w.bufw, hdr+": "+strings.Join(hdv, ";"), "\r\n")
}
}
iorw.Fprint(w.bufw, "\r\n")
w.Flush()
if ischunked {
w.chunkedWriter = httputil.NewChunkedWriter(w.orgwtr)
w.bufw.Reset(w.chunkedWriter)
}
}
}
}
}
func (w *responseWriter) Write(b []byte) (n int, err error) {
if bl := len(b); bl > 0 {
if w != nil && w.bufw != nil {
w.writeHeader()
if w.ctx != nil {
select {
case <-w.ctx.Done():
if err = w.ctx.Err(); err != nil {
return
} else {
err = fmt.Errorf("Interrupted")
return
}
default:
}
}
n, err = w.bufw.Write(b[:bl])
}
}
return n, err
}
func (w *responseWriter) WriteHeader(statusCode int) {
if w != nil {
if !w.wroteHeader {
w.statusCode = statusCode
}
}
}