- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttpserver.go
More file actions
Latest commit
212 lines (173 loc) · 4.48 KB
/
Copy pathhttpserver.go
File metadata and controls
212 lines (173 loc) · 4.48 KB
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
// Copyright (c) 2010 William R. Conant, WillConant.com
// Use of this source code is governed by the MIT licence:
// http://www.opensource.org/licenses/mit-license.php
package msglite
import (
"bytes"
"fmt"
"json"
"net"
"os"
)
consthttpTimeout=180
typeHttpServerstruct {
exchange*Exchange
exchangeToAddressstring
listener net.Listener
quitChanchanbool
}
funcNewHttpServer(exchange*Exchange, networkstring, laddrstring, exchangeToAddressstring) (server*HttpServer) {
server=new(HttpServer)
server.exchange=exchange
server.exchangeToAddress=exchangeToAddress
server.quitChan=make(chanbool)
varerr os.Error
server.listener, err=net.Listen(network, laddr)
iferr!=nil {
panic(err)
}
ifnetwork=="unix" {
os.Chmod(laddr, 0777)
}
return
}
func (server*HttpServer) Run() {
connChan:=make(chan net.Conn)
gofunc() {
for {
conn, err:=server.listener.Accept()
iferr!=nil {
panic(fmt.Sprintf("error accepting connection: %v", err))
}
connChan<-conn
}
}()
for {
select {
casenetConn:=<-connChan:
goserver.handle(netConn)
case<-server.quitChan:
server.listener.Close()
return
}
}
}
func (server*HttpServer) Quit() {
server.quitChan<-true
}
funcdoError(conn net.Conn, err os.Error) {
varb bytes.Buffer
b.WriteString("HTTP/1.0 500 Internal Server Error\r\n")
b.WriteString("Content-Type: text/plain\r\n")
b.WriteString("Connection: close\r\n\r\n")
b.WriteString(fmt.Sprintf("An Error Has Occurred\n%v\n", err))
conn.Write(b.Bytes())
conn.Close()
}
func (server*HttpServer) handle(conn net.Conn) {
req, err:=readHttpRequest(conn)
iferr!=nil {
doError(conn, err)
return
}
replyMsg, err:=server.relayRequest(req)
iferr!=nil {
doError(conn, err)
return
}
ifreplyMsg==nil {
doError(conn, os.NewError("reply timed out"))
return
}
server.relayReply(replyMsg, conn)
}
func (server*HttpServer) relayRequest(req*httpRequest) (*Message, os.Error) {
bodyAddr:=server.exchange.GenerateUnusedAddress()
replyAddr:=server.exchange.GenerateUnusedAddress()
envMap:=make(map[string]interface{})
envMap["method"] =req.method
envMap["url"] =req.url
envMap["protocol"] =req.protocol
envMap["headers"] =req.headers
envMap["bodyAddr"] =bodyAddr
json, err:=json.Marshal(envMap)
iferr!=nil {
panic(err)
}
server.exchange.Send(string(json), httpTimeout, server.exchangeToAddress, replyAddr)
server.exchange.Send(string(req.body), httpTimeout, bodyAddr, "")
returnserver.exchange.Ready(httpTimeout, []string{replyAddr}), nil
}
func (server*HttpServer) relayReply(replyMsg*Message, conn net.Conn) {
varerr os.Error
varreply []interface{}
err=json.Unmarshal([]byte(replyMsg.Body), &reply)
iferr!=nil {
goto BadReply
}
status, ok:=reply[0].(string)
if!ok {
err=os.NewError("first item of reply wasn't a string")
goto BadReply
}
st, ok:=statusText[status]
if!ok {
err=&badStringError{"not a valid status code", status}
goto BadReply
}
headers, ok:=reply[1].([]interface{})
if!ok {
err=os.NewError("second item of reply wasn't a []interface{}")
goto BadReply
}
iflen(headers) %2!=0 {
err=os.NewError("headers array had odd number of items")
goto BadReply
}
varrespBuffer bytes.Buffer
respBuffer.WriteString("HTTP/1.0 "+status+" "+st+"\r\n")
fori:=0; i<len(headers); i+=2 {
kStr, ok:=headers[i].(string)
if!ok {
err=os.NewError("header key wasn't a string")
goto BadReply
}
kStr=canonicalHeaderKey(kStr)
vStr, ok:=headers[i+1].(string)
if!ok {
err=os.NewError("header value wasn't a string")
goto BadReply
}
// there is almost certainly a whole list of headers I should ignore from the worker
ifkStr!="Connection" {
respBuffer.WriteString(kStr+": "+vStr+"\r\n")
}
}
respBuffer.WriteString("Connection: close\r\n\r\n")
_, err=conn.Write(respBuffer.Bytes())
iferr!=nil {
// couldn't do the actual writing, just give up
conn.Close()
return
}
for {
switchreplyBodyMsg:=server.exchange.Ready(httpTimeout, []string{replyMsg.ToAddress}); {
casereplyBodyMsg==nil:
// we really expected a message here... this is busted
conn.Close()
return
casereplyBodyMsg.Body=="":
// an empty message indicates we're all done
conn.Close()
return
default:
_, err=conn.Write([]byte(replyBodyMsg.Body))
iferr!=nil {
conn.Close()
return
}
}
}
BadReply:
doError(conn, err)
}