-
Notifications
You must be signed in to change notification settings - Fork 1
/
response.go
173 lines (143 loc) · 3.08 KB
/
response.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//+build js,wasm
package gonreli
import (
"bufio"
"bytes"
"io"
"log"
"net"
"net/http"
"syscall/js"
"time"
)
//Response implements http.ResponseWriter
type Response struct {
hijacked bool
reqReader io.Reader
headers http.Header
js.Value
body bytes.Buffer
wroteHeader bool
}
func newResponse(v js.Value) *Response {
pr, bodyWritter := io.Pipe()
br := &bodyReader{r: v, pr: pr, writer: bodyWritter}
return &Response{Value: v, headers: http.Header{}, reqReader: br}
}
func (w *Response) Header() http.Header {
return w.headers
}
func (w *Response) Write(data []byte) (int, error) {
if !w.wroteHeader {
w.WriteHeader(200)
}
if len(data) == 0 {
return 0, nil
}
ta := js.TypedArrayOf(data)
defer ta.Release()
//todo check error
w.Call("write", jsBufferType.New(ta))
return (len(data)), nil
}
func (w *Response) WriteHeader(statusCode int) {
if !w.wroteHeader {
w.Set("statusCode", statusCode)
w.writeHeader()
}
}
//todo improve this is fragile
func (w *Response) Hijack() (net.Conn, *bufio.ReadWriter, error) {
w.hijacked = true
conn := newNodeConn(w.Value, w.reqReader)
brw := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
return conn, brw, nil
}
func (w *Response) writeHeader() {
for i := range w.headers {
w.Call("setHeader", i, w.headers[i][0])
}
w.wroteHeader = true
}
type nodeConn struct {
js.Value
r io.Reader
}
func newNodeConn(v js.Value, reader io.Reader) *nodeConn {
if !v.InstanceOf(jsSocketType) {
v = v.Get("socket")
}
var nc nodeConn
nc.r = reader
nc.Value = v
nc.Call("on", "timeout", js.NewCallback(func(args []js.Value) {
log.Println("timeout received")
nc.Call("end")
}))
return &nc
}
func (nc *nodeConn) Read(b []byte) (n int, err error) {
return nc.r.Read(b)
}
func (nc *nodeConn) Write(b []byte) (n int, err error) {
ta := js.TypedArrayOf(b)
nc.Call("write", jsBufferType.Call("from", ta))
ta.Release()
return len(b), nil
}
func (nc *nodeConn) Close() error {
nc.Call("end")
return nil
}
func (nc *nodeConn) LocalAddr() net.Addr {
addr := nc.Get("localAddress").String()
port := nc.Get("localPort").Int()
ip := net.ParseIP(addr)
return &net.TCPAddr{
IP: ip,
Port: port,
}
}
func (nc *nodeConn) RemoteAddr() net.Addr {
addr := nc.Get("remoteAddress").String()
port := nc.Get("remotePort").Int()
ip := net.ParseIP(addr)
return &net.TCPAddr{
IP: ip,
Port: port,
}
}
func (nc *nodeConn) SetDeadline(t time.Time) error {
//socket.setTimeout(3000);
var timeout time.Duration
if t.IsZero() {
timeout = 0
} else {
now := time.Now()
timeout = t.Sub(now)
}
nc.Call("setTimeout", timeout/time.Millisecond)
return nil
}
func (nc *nodeConn) SetReadDeadline(t time.Time) error {
var timeout time.Duration
if t.IsZero() {
timeout = 0
} else {
now := time.Now()
timeout = t.Sub(now)
}
nc.Call("setTimeout", timeout/time.Millisecond)
return nil
}
func (nc *nodeConn) SetWriteDeadline(t time.Time) error {
var timeout time.Duration
if t.IsZero() {
timeout = 0
} else {
now := time.Now()
timeout = t.Sub(now)
}
nc.Call("setTimeout", timeout/time.Millisecond)
return nil
}