-
Notifications
You must be signed in to change notification settings - Fork 1
/
writer.go
74 lines (65 loc) · 1.55 KB
/
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
64
65
66
67
68
69
70
71
72
73
74
package rex
import (
"bufio"
"fmt"
"io"
"net"
"net/http"
)
// A responseWriter is used by rex.Context to construct a HTTP response.
type responseWriter struct {
status int
written int
headerSent bool
httpWriter http.ResponseWriter
compression io.WriteCloser
}
// Hijack lets the caller take over the connection.
func (w *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
h, ok := w.httpWriter.(http.Hijacker)
if ok {
return h.Hijack()
}
return nil, nil, fmt.Errorf("the raw response writer does not implement the http.Hijacker")
}
// Flush sends any buffered data to the client.
func (w *responseWriter) Flush() {
f, ok := w.httpWriter.(http.Flusher)
if ok {
f.Flush()
}
}
// Header returns the header map that will be sent by WriteHeader.
func (w *responseWriter) Header() http.Header {
return w.httpWriter.Header()
}
// WriteHeader sends a HTTP response header with the provided status code.
func (w *responseWriter) WriteHeader(status int) {
if !w.headerSent {
w.status = status
w.httpWriter.WriteHeader(status)
w.headerSent = true
}
}
// Write writes the data to the connection as part of an HTTP reply.
func (w *responseWriter) Write(p []byte) (n int, err error) {
if !w.headerSent {
w.headerSent = true
}
var wr io.Writer = w.httpWriter
if w.compression != nil {
wr = w.compression
}
n, err = wr.Write(p)
if n > 0 {
w.written += n
}
return
}
// Close closes the underlying connection.
func (w *responseWriter) Close() error {
if w.compression != nil {
return w.compression.Close()
}
return nil
}