-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.go
330 lines (311 loc) · 7.18 KB
/
net.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
Copyright 2015 Lee Boynton
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ell
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"strings"
. "github.com/boynton/ell/data"
)
var HTTPErrorKey = Intern("http-error:")
func httpClientOperation(method string, url string, headers *Struct, data *String) (*Struct, error) {
client := &http.Client{}
var bodyReader io.Reader
bodyLen := 0
if data != nil {
tmp := []byte(data.Value)
bodyLen = len(tmp)
if bodyLen > 0 {
bodyReader = bytes.NewBuffer(tmp)
}
}
req, err := http.NewRequest(method, url, bodyReader)
if err != nil {
return nil, err
}
if headers != nil {
for k, v := range headers.Bindings {
ks := StringValue(k.ToValue())
if p, ok := v.(*List); ok {
vs := p.Car.String()
req.Header.Set(ks, vs)
for p.Cdr != EmptyList {
p = p.Cdr
req.Header.Add(ks, p.Car.String())
}
} else {
req.Header.Set(ks, v.String())
}
}
}
if bodyLen > 0 {
req.Header.Set("Content-Length", fmt.Sprint(bodyLen))
}
res, err := client.Do(req)
if err == nil {
bodyBytes, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err == nil {
s := NewStruct()
Put(s, Intern("status:"), Integer(res.StatusCode))
bodyLen := len(bodyBytes)
if bodyLen > 0 {
Put(s, Intern("body:"), NewBlob(bodyBytes))
}
if len(res.Header) > 0 {
headers = NewStruct()
for k, v := range res.Header {
var values []Value
for _, val := range v {
values = append(values, NewString(val))
}
Put(headers, NewString(k), ListFromValues(values))
}
Put(s, Intern("headers:"), headers)
}
return s, nil
}
}
return nil, err
}
func httpServer(port int, handler *Function) (Value, error) {
glue := func(w http.ResponseWriter, r *http.Request) {
headers := NewStruct()
for k, v := range r.Header {
var values []Value
for _, val := range v {
values = append(values, NewString(val))
}
Put(headers, NewString(k), ListFromValues(values))
}
var body Value
method := strings.ToUpper(r.Method)
switch method {
case "POST", "PUT":
bodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(500)
w.Write([]byte("Cannot decode body for " + r.Method + " request"))
return
}
body = NewBlob(bodyBytes)
}
req, _ := MakeStruct([]Value{Intern("headers:"), headers})
if body != nil {
Put(req, Intern("body:"), body)
}
Put(req, Intern("method:"), NewString(method))
Put(req, Intern("path:"), NewString(r.URL.Path))
if r.URL.Scheme != "" {
Put(req, Intern("scheme:"), NewString(r.URL.Scheme))
}
if r.URL.User != nil {
//this is a *url.Userinfo
}
if r.URL.Host != "" {
Put(req, Intern("host:"), NewString(r.URL.Host))
}
if r.URL.RawQuery != "" {
Put(req, Intern("query:"), NewString(r.URL.RawQuery))
}
args := []Value{req}
res, err := exec(handler.code, args)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
p, ok := res.(*Struct)
if !ok {
// if !IsStruct(res) {
w.WriteHeader(500)
w.Write([]byte("Handler did not return a struct"))
return
}
headers2 := p.Get(Intern("headers:"))
body = p.Get(Intern("body:"))
status := p.Get(Intern("status:"))
if headers, ok := headers2.(*Struct); ok {
//fix: multiple values for a header
for k, v := range headers.Bindings {
ks := headerString(k.ToValue())
vs := v.String()
w.Header().Set(ks, vs)
}
}
if s, ok := body.(*String); ok {
bodylen := len(s.Value)
w.Header().Set("Content-length", fmt.Sprint(bodylen))
if status != nil {
nstatus, _ := AsIntValue(status)
if nstatus != 0 && nstatus != 200 {
w.WriteHeader(nstatus)
}
}
if bodylen > 0 {
w.Write([]byte(s.Value))
}
} else {
if status != nil {
nstatus := IntValue(status)
if nstatus != 0 && nstatus != 200 {
w.WriteHeader(nstatus)
}
}
}
}
http.HandleFunc("/", glue)
//if verbose {
fmt.Printf("[web server running at http://localhost:%d]\n", port)
//}
http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
//no way to stop it
return Null, nil
}
func headerString(obj Value) string {
switch p := obj.(type) {
case *String:
return p.Value
case *Symbol:
return p.Text
case *Keyword:
return p.Name()
default:
s, err := ToString(obj)
if err != nil {
return TypeNameOf(obj)
}
return s.Value
}
}
var ConnectionType = Intern("<connection>")
type Connection struct {
Name string
In *Channel
Out *Channel
Con net.Conn
}
func (c *Connection) Type() Value {
return ConnectionType
}
func (c *Connection) Equals(another Value) bool {
return false
}
func (c *Connection) String() string {
return "#[Connection]"
}
func NewConnection(con net.Conn, endpoint string) Value {
inchan := NewChannel(10, "input")
outchan := NewChannel(10, "output")
go tcpReader(con, inchan)
go tcpWriter(con, outchan)
name := fmt.Sprintf("connection on %s", endpoint)
return &Connection{
Name: name,
In: inchan,
Out: outchan,
Con: con,
}
}
func closeConnection(obj Value) {
if p, ok := obj.(*Connection); ok {
if p.Con != nil {
CloseChannel(p.In)
CloseChannel(p.Out)
p.Con.Close()
p.Con = nil
}
}
}
// MaxFrameSize is an arbitrary limit to the tcp server framesize, to avoid trouble
const MaxFrameSize = 1000000
func tcpReader(conn net.Conn, inchan Value) {
r := bufio.NewReader(conn)
for {
count, err := binary.ReadVarint(r)
if err != nil {
CloseChannel(inchan)
return
}
if count < 0 || count > MaxFrameSize {
println("Bad frame size: ", count)
CloseChannel(inchan)
return
}
buf := make([]byte, count, count)
cur := buf[:]
remaining := int(count)
offset := 0
for remaining > 0 {
n, err := r.Read(cur)
if err != nil {
CloseChannel(inchan)
return
}
remaining -= n
offset += n
cur = buf[offset:]
}
packet := NewBlob(buf)
ch := ChannelValue(inchan)
if ch != nil {
ch <- packet
}
}
}
func tcpWriter(con net.Conn, outchan Value) {
for {
var packet Value
ch := ChannelValue(outchan)
if ch != nil {
packet = <-ch
}
if packet == nil {
return
}
if p, ok := packet.(*String); ok {
data := []byte(p.Value)
count := len(data)
header := make([]byte, 8)
nvar := binary.PutVarint(header, int64(count))
_, er := con.Write(header[:nvar])
if er != nil {
CloseChannel(outchan)
return
}
n, err := con.Write([]byte(data))
if n != len(data) || err != nil {
CloseChannel(outchan)
return
}
}
}
}
func tcpListener(listener net.Listener, acceptChannel Value, endpoint string) (Value, error) {
for {
con, err := listener.Accept()
if err != nil {
return nil, err
}
ch := ChannelValue(acceptChannel)
if ch != nil {
ch <- NewConnection(con, endpoint)
}
}
}