/
http.coffee
417 lines (283 loc) · 10.4 KB
/
http.coffee
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#
# http.coffee : anode wrapper around Node.js 'http' module
#
# (C) 2011 Tristan Slominski
#
anode = require '../lib/anode'
nodehttp = require 'http'
#
# Anode wrapper around the http.clientRequest
#
clientRequest_beh = anode.beh 'request'
'cust, #abort' : ->
@request.abort()
if @cust # ack requested
@send( @, '#abort' ).to @cust
'cust, #end, [data], [encoding]' : ->
@request.end @data, @encoding
if @cust # ack requested
@send( @, '#end' ).to @cust
'cust, #setNoDelay, noDelay' : ->
@request.setNoDelay @noDelay
if @cust # ack requested
@send( @, '#setNoDelay' ).to @cust
'cust, #setSocketKeepAlive, enable, [initialDelay]' : ->
@request.setSocketKeepAlive @enable, @initialDelay
if @cust # ack requested
@send( @, '#setSocketKeepAlive' ).to @cust
'cust, #setTimeout, timeout' : ->
@request.setTimeout @timeout
if @cust # ack requested
@send( @, '#setTimeout' ).to @cust
'cust, #write, chunk, [encoding]' : ->
@request.write chunk, @encoding
if @cust # ack requested
@send( @, '#write' ).to @cust
#
# Anode wrapper around the http.clientResponse
#
clientResponse_beh = anode.beh '_cust', 'response'
'cust, #headers' : ->
@send( @, '#headers', @response.headers ).to @cust
'cust, #httpVersion' : ->
@send( @, '#httpVersion', @response.httpVersion ).to @cust
'cust, #pause' : ->
@response.pause()
if @cust # ack requested
@send( @, '#pause' ).to @cust
'cust, #resume' : ->
@response.resume()
if @cust # ack requested
@send( @, '#resume' ).to @cust
'cust, #setEncoding, [encoding]' : ->
@response.setEncoding( @encoding )
if @cust # ack requested
@send( @, '#setEncoding' ).to @cust
'cust, #statusCode' : ->
@send( @, '#statusCode', @response.statusCode ).to @cust
'cust, #trailers' : ->
@send( @, '#trailers', @response.trailers ).to @cust
'$response, #close, err' : ->
@send( @, '#close', @err ).to @_cust
'$response, #data, chunk' : ->
@send( @, '#data', @chunk ).to @_cust
'$response, #end' : ->
@send( @, '#end' ).to @_cust
#
# Initialized http server beh wrapped around a created server
#
initializedServer_beh = anode.beh 'server',
# stops the server from accepting new connections
'cust, #close' : ->
@server.close()
if @cust # ack requested
@send( @, '#close' ).to @cust
#
# Actor wrapper around the request object that attaches the actor identity
# with each message for distinction between messages from different
# simultaneous requests
#
request_beh = anode.beh '_cust', 'req'
# attempt to match the explicit $req first before trying generic 'cust'
'$req, #close' : ->
@send( @, '#close' ).to @_cust
'$req, #data, chunk' : ->
@send( @, '#data', @chunk ).to @_cust
'$req, #end' : ->
@send( @, '#end' ).to @_cust
'cust, #connection' : ->
# TODO: implement net.Socket as actor
@send( @, '#connection', @req.connection ).to @cust
# headers
'cust, #headers' : ->
@send( @, '#headers', @req.headers ).to @cust
'cust, #httpVersion' : ->
@send( @, '#httpVersion', @req.httpVersion ).to @cust
# the request method as a string
'cust, #method' : ->
@send( @, '#method', @req.method ).to @cust
# pauses request from emitting events
'cust, #pause' : ->
@req.pause()
if @cust # ack requested
@send( @, '#pause' ).to @cust
'cust, #resume' : ->
@req.resume()
if @cust # ack requested
@send( @, '#resume' ).to @cust
'cust, #setEncoding, encoding' : ->
@req.setEncoding @encoding
if @cust # ack requested
@send( @, '#setEncoding' ).to @cust
# trailers
'cust, #trailers' : ->
@send( @, '#trailers', @req.trailers ).to @cust
# requested URL string
'cust, #url' : ->
@send( @, '#url', @req.url ).to @_cust
#
# An actor wrapper for the response object
#
response_beh = anode.beh 'res'
'cust, #addTrailers, headers' : ->
@res.addTrailers @headers
if @cust # ack requested
@send( @, '#addTrailers' ).to @cust
'cust, #end, [data], [encoding]' : ->
@res.end @data, @encoding
if @cust # ack requested
@send( @, '#end' ).to @cust
'cust, #getHeader, name' : ->
@send( @, '#header', @res.getHeader @name ).to @cust
'cust, #removeHeader, name' : ->
@res.removeHeader @name
if @cust # ack requested
@send( @, '#removeHeader' ). to @cust
'cust, #setHeader, name, value' : ->
@res.setHeader @name, @value
if @cust # ack requested
@send( @, '#setHeader' ).to @cust
'cust, #statusCode, statusCode' : ->
@res.statusCode @statusCode
if @cust # ack requested
@send( @, '#statusCode' ).to @cust
'cust, #write, chunk, [encoding]' : ->
@res.write @chunk, @encoding
if @cust # ack requested
@send( @, '#write' ).to @cust
'cust, #writeContinue' : ->
@res.writeContinue()
if @cust # ack requested
@send( @, '#writeContinue' ).to @cust
'cust, #writeHead, statusCode, [reasonPhrase], [headers]' : ->
@res.writeHead @statusCode, @reasonPhrase, @headers
if @cust # ack requested
@send( @, '#writeHead' ).to @cust
#
# An http client beh that will execute client requests
#
client_beh = anode.beh
# get convenience method, will create a request, set method to 'GET'
# and automatically send '#end' message to request actor
'cust, #get, options' : ->
# set method to GET
@options.method = 'GET'
# local aliases for use inside closures
__httpClient = @
__cust = @cust
__send = @send
clientRequest = nodehttp.request @options
request_actor = @create clientRequest_beh clientRequest
# all clientRequest event bindings need to happen before nextTick
clientRequest.on 'response', ( response ) ->
response_actor = __create clientResponse_beh __cust, response
response.on 'data', ( chunk ) ->
__send( response, '#data', chunk ).to response_actor
response.on 'end', ->
__send( response, '#end' ).to response_actor
response.on 'close', ( err ) ->
__send( response, '#close', err ).to response_actor
__send( __httpClient, '#response', response_actor ).to __cust
clientRequest.on 'socket', ( socket ) ->
# TODO
clientRequest.on 'upgrade', ( response, socket, head ) ->
# TODO
clientRequest.on 'continue', ->
__send( __httpClient, '#continue' ).to __cust
# create 'callback' ack actor to execute once the 'get' request has been
# sent, it matches this specific request_actor instance via $_request
if @cust # ack requested
ack = @create anode.beh( '_request'
'$_request, #end' : ->
__send( __httpClient, '#get', @_request ).to __cust
)( request_actor )
ack = if ack then ack else undefined
# send '#end' to request actor, executing the request, but give it
# 'ack' actor ( if ack requested ) to listen for acknowledgment once '#end'
# is executed, at which point, 'ack' will send an acknowledgment to the
# customer of original '#get' request
@send( ack, '#end' ).to request_actor
# creates the request client
'cust, #request, options' : ->
# local aliases for use inside closures
__httpClient = @
__cust = @cust
__send = @send
clientRequest = nodehttp.request @options
request_actor = @create clientRequest_beh clientRequest
# all clientRequest event bindings need to happen before nextTick
clientRequest.on 'response', ( response ) ->
response_actor = __create clientResponse_beh __cust, response
response.on 'data', ( chunk ) ->
__send( response, '#data', chunk ).to response_actor
response.on 'end', ->
__send( response, '#end' ).to response_actor
response.on 'close', ( err ) ->
__send( response, '#close', err ).to response_actor
__send( __httpClient, '#response', response_actor ).to __cust
clientRequest.on 'socket', ( socket ) ->
# TODO
clientRequest.on 'upgrade', ( response, socket, head ) ->
# TODO
clientRequest.on 'continue', ->
__send( __httpClient, '#continue' ).to __cust
@send( @, '#request', request_actor ).to @cust
#
# An uninitialized http server beh that will lazy-initialize the server
# and then become initialized http server beh
#
uninitializedServer_beh = anode.beh
# creates the server and attempts to listen on given port and host
'cust, #listen, port, [host]' : ->
# local aliases for use inside closures
__create = @create
__cust = @cust
__host = @host
__http = @
__port = @port
__send = @send
server = nodehttp.createServer()
# all server event bindings need to happen before nextTick
# on 'request'
server.on 'request', ( req, res ) ->
request_actor = __create request_beh __cust, req
response_actor = __create response_beh res
# all request binding needs to happen here before nextTick
req.on 'data', ( chunk ) ->
__send( req, '#data', chunk ).to request_actor
req.on 'end', ->
__send( req, '#end' ).to request_actor
req.on 'close', ->
__send( req, '#close' ).to request_actor
__send( __http, '#request', request_actor, response_actor ).to __cust
# on 'connection'
server.on 'connection', ( socket ) ->
# TODO
# on 'close'
server.on 'close', ->
# TODO
# on 'checkContinue'
server.on 'checkContinue', ( req, res ) ->
# TODO
# on 'upgrade'
server.on 'upgrade', ( request, socket, head ) ->
# TODO
# on clientError
server.on 'clientError', ( exception ) ->
# TODO
# start listening
if @cust # ack requested
__callback = ->
__send( __http, '#listen' ).to __cust
@host = if @host then @host else undefined
__callback = if __callback then __callback else undefined
server.listen @port, @host, __callback
@become initializedServer_beh server
#
# The request behavior for making http requests
#
exports.client_beh = client_beh
#
# The server behavior for serving http requests
#
exports.server_beh = uninitializedServer_beh