-
Notifications
You must be signed in to change notification settings - Fork 57
/
connresponsewriter.go
82 lines (74 loc) · 1.72 KB
/
connresponsewriter.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
package httpproxy
import (
"fmt"
"io"
"net"
"net/http"
"sync"
)
// ConnResponseWriter implements http.ResponseWriter interface to use hijacked
// HTTP connection.
type ConnResponseWriter struct {
Conn net.Conn
mu sync.Mutex
err error
header http.Header
headersSent bool
}
// NewConnResponseWriter returns a new ConnResponseWriter.
func NewConnResponseWriter(conn net.Conn) *ConnResponseWriter {
return &ConnResponseWriter{Conn: conn, header: make(http.Header)}
}
// Header returns the header map that will be sent by WriteHeader.
func (c *ConnResponseWriter) Header() http.Header {
return c.header
}
// Write writes the data to the connection as part of an HTTP reply.
func (c *ConnResponseWriter) Write(body []byte) (int, error) {
c.mu.Lock()
defer c.mu.Unlock()
c.writeHeader(http.StatusOK)
if c.err != nil {
return 0, c.err
}
n, err := c.Conn.Write(body)
if err != nil {
c.err = err
}
return n, err
}
// WriteHeader sends an HTTP response header with status code.
func (c *ConnResponseWriter) WriteHeader(statusCode int) {
c.mu.Lock()
defer c.mu.Unlock()
c.writeHeader(statusCode)
}
// Close closes network connection.
func (c *ConnResponseWriter) Close() error {
return c.Conn.Close()
}
func (c *ConnResponseWriter) writeHeader(statusCode int) {
if c.err != nil {
return
}
if c.headersSent {
return
}
st := http.StatusText(statusCode)
if st != "" {
st = " " + st
}
if _, err := io.WriteString(c.Conn, fmt.Sprintf("HTTP/1.1 %d%s\r\n", statusCode, st)); err != nil {
c.err = err
return
}
if err := c.header.Write(c.Conn); err != nil {
c.err = err
return
}
if _, err := io.WriteString(c.Conn, "\r\n"); err != nil {
c.err = err
return
}
c.headersSent = true
}