eventmachine 0.12.8-x86-mswin32-60 → 0.12.10-x86-mswin32-60
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +14 -13
- data/Rakefile +374 -264
- data/eventmachine.gemspec +4 -5
- data/ext/binder.cpp +125 -126
- data/ext/binder.h +46 -48
- data/ext/cmain.cpp +184 -42
- data/ext/cplusplus.cpp +202 -202
- data/ext/ed.cpp +242 -81
- data/ext/ed.h +39 -22
- data/ext/em.cpp +127 -108
- data/ext/em.h +27 -18
- data/ext/emwin.cpp +3 -3
- data/ext/eventmachine.h +49 -38
- data/ext/eventmachine_cpp.h +96 -96
- data/ext/extconf.rb +147 -132
- data/ext/fastfilereader/extconf.rb +82 -76
- data/ext/project.h +151 -140
- data/ext/rubymain.cpp +222 -103
- data/ext/ssl.cpp +460 -460
- data/ext/ssl.h +94 -94
- data/java/src/com/rubyeventmachine/EmReactor.java +570 -423
- data/java/src/com/rubyeventmachine/EventableChannel.java +69 -57
- data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +189 -171
- data/java/src/com/rubyeventmachine/EventableSocketChannel.java +364 -244
- data/java/src/com/rubyeventmachine/{Application.java → application/Application.java} +194 -200
- data/java/src/com/rubyeventmachine/{Connection.java → application/Connection.java} +74 -74
- data/java/src/com/rubyeventmachine/{ConnectionFactory.java → application/ConnectionFactory.java} +36 -36
- data/java/src/com/rubyeventmachine/{DefaultConnectionFactory.java → application/DefaultConnectionFactory.java} +46 -46
- data/java/src/com/rubyeventmachine/{PeriodicTimer.java → application/PeriodicTimer.java} +38 -38
- data/java/src/com/rubyeventmachine/{Timer.java → application/Timer.java} +54 -54
- data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +109 -108
- data/java/src/com/rubyeventmachine/tests/ConnectTest.java +148 -146
- data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +53 -53
- data/java/src/com/rubyeventmachine/tests/TestServers.java +75 -74
- data/java/src/com/rubyeventmachine/tests/TestTimers.java +90 -89
- data/lib/em/connection.rb +71 -12
- data/lib/em/deferrable.rb +191 -186
- data/lib/em/protocols.rb +36 -35
- data/lib/em/protocols/httpclient2.rb +590 -582
- data/lib/em/protocols/line_and_text.rb +125 -126
- data/lib/em/protocols/linetext2.rb +161 -160
- data/lib/em/protocols/object_protocol.rb +45 -39
- data/lib/em/protocols/smtpclient.rb +357 -331
- data/lib/em/protocols/socks4.rb +66 -0
- data/lib/em/queue.rb +60 -60
- data/lib/em/timers.rb +56 -55
- data/lib/em/version.rb +1 -1
- data/lib/eventmachine.rb +125 -169
- data/lib/jeventmachine.rb +257 -142
- data/tasks/{cpp.rake → cpp.rake_example} +76 -76
- data/tests/test_attach.rb +125 -100
- data/tests/test_basic.rb +1 -2
- data/tests/test_connection_count.rb +34 -44
- data/tests/test_epoll.rb +0 -2
- data/tests/test_get_sock_opt.rb +30 -0
- data/tests/test_httpclient2.rb +3 -3
- data/tests/test_inactivity_timeout.rb +21 -1
- data/tests/test_ltp.rb +182 -188
- data/tests/test_next_tick.rb +0 -2
- data/tests/test_pause.rb +70 -0
- data/tests/test_pending_connect_timeout.rb +48 -0
- data/tests/test_ssl_args.rb +78 -67
- data/tests/test_timers.rb +162 -141
- metadata +13 -11
- data/tasks/project.rake +0 -79
- data/tasks/tests.rake +0 -193
data/lib/em/protocols.rb
CHANGED
@@ -1,35 +1,36 @@
|
|
1
|
-
module EventMachine
|
2
|
-
# This module contains various protocol implementations, including:
|
3
|
-
# - HttpClient and HttpClient2
|
4
|
-
# - Stomp
|
5
|
-
# - Memcache
|
6
|
-
# - SmtpClient and SmtpServer
|
7
|
-
# - SASLauth and SASLauthclient
|
8
|
-
# - LineAndTextProtocol and LineText2
|
9
|
-
# - HeaderAndContentProtocol
|
10
|
-
# - Postgres3
|
11
|
-
# - ObjectProtocol
|
12
|
-
#
|
13
|
-
# The protocol implementations live in separate files in the protocols/ subdirectory,
|
14
|
-
# but are auto-loaded when they are first referenced in your application.
|
15
|
-
#
|
16
|
-
# EventMachine::Protocols is also aliased to EM::P for easier usage.
|
17
|
-
#
|
18
|
-
module Protocols
|
19
|
-
# TODO : various autotools are completely useless with the lack of naming
|
20
|
-
# convention, we need to correct that!
|
21
|
-
autoload :TcpConnectTester, 'em/protocols/tcptest'
|
22
|
-
autoload :HttpClient, 'em/protocols/httpclient'
|
23
|
-
autoload :HttpClient2, 'em/protocols/httpclient2'
|
24
|
-
autoload :LineAndTextProtocol, 'em/protocols/line_and_text'
|
25
|
-
autoload :HeaderAndContentProtocol, 'em/protocols/header_and_content'
|
26
|
-
autoload :LineText2, 'em/protocols/linetext2'
|
27
|
-
autoload :Stomp, 'em/protocols/stomp'
|
28
|
-
autoload :SmtpClient, 'em/protocols/smtpclient'
|
29
|
-
autoload :SmtpServer, 'em/protocols/smtpserver'
|
30
|
-
autoload :SASLauth, 'em/protocols/saslauth'
|
31
|
-
autoload :Memcache, 'em/protocols/memcache'
|
32
|
-
autoload :Postgres3, 'em/protocols/postgres3'
|
33
|
-
autoload :ObjectProtocol, 'em/protocols/object_protocol'
|
34
|
-
|
35
|
-
end
|
1
|
+
module EventMachine
|
2
|
+
# This module contains various protocol implementations, including:
|
3
|
+
# - HttpClient and HttpClient2
|
4
|
+
# - Stomp
|
5
|
+
# - Memcache
|
6
|
+
# - SmtpClient and SmtpServer
|
7
|
+
# - SASLauth and SASLauthclient
|
8
|
+
# - LineAndTextProtocol and LineText2
|
9
|
+
# - HeaderAndContentProtocol
|
10
|
+
# - Postgres3
|
11
|
+
# - ObjectProtocol
|
12
|
+
#
|
13
|
+
# The protocol implementations live in separate files in the protocols/ subdirectory,
|
14
|
+
# but are auto-loaded when they are first referenced in your application.
|
15
|
+
#
|
16
|
+
# EventMachine::Protocols is also aliased to EM::P for easier usage.
|
17
|
+
#
|
18
|
+
module Protocols
|
19
|
+
# TODO : various autotools are completely useless with the lack of naming
|
20
|
+
# convention, we need to correct that!
|
21
|
+
autoload :TcpConnectTester, 'em/protocols/tcptest'
|
22
|
+
autoload :HttpClient, 'em/protocols/httpclient'
|
23
|
+
autoload :HttpClient2, 'em/protocols/httpclient2'
|
24
|
+
autoload :LineAndTextProtocol, 'em/protocols/line_and_text'
|
25
|
+
autoload :HeaderAndContentProtocol, 'em/protocols/header_and_content'
|
26
|
+
autoload :LineText2, 'em/protocols/linetext2'
|
27
|
+
autoload :Stomp, 'em/protocols/stomp'
|
28
|
+
autoload :SmtpClient, 'em/protocols/smtpclient'
|
29
|
+
autoload :SmtpServer, 'em/protocols/smtpserver'
|
30
|
+
autoload :SASLauth, 'em/protocols/saslauth'
|
31
|
+
autoload :Memcache, 'em/protocols/memcache'
|
32
|
+
autoload :Postgres3, 'em/protocols/postgres3'
|
33
|
+
autoload :ObjectProtocol, 'em/protocols/object_protocol'
|
34
|
+
autoload :Socks4, 'em/protocols/socks4'
|
35
|
+
end
|
36
|
+
end
|
@@ -1,582 +1,590 @@
|
|
1
|
-
#--
|
2
|
-
#
|
3
|
-
# Author:: Francis Cianfrocca (gmail: blackhedd)
|
4
|
-
# Homepage:: http://rubyeventmachine.com
|
5
|
-
# Date:: 16 July 2006
|
6
|
-
#
|
7
|
-
# See EventMachine and EventMachine::Connection for documentation and
|
8
|
-
# usage examples.
|
9
|
-
#
|
10
|
-
#----------------------------------------------------------------------------
|
11
|
-
#
|
12
|
-
# Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
|
13
|
-
# Gmail: blackhedd
|
14
|
-
#
|
15
|
-
# This program is free software; you can redistribute it and/or modify
|
16
|
-
# it under the terms of either: 1) the GNU General Public License
|
17
|
-
# as published by the Free Software Foundation; either version 2 of the
|
18
|
-
# License, or (at your option) any later version; or 2) Ruby's License.
|
19
|
-
#
|
20
|
-
# See the file COPYING for complete licensing information.
|
21
|
-
#
|
22
|
-
#---------------------------------------------------------------------------
|
23
|
-
#
|
24
|
-
#
|
25
|
-
|
26
|
-
module EventMachine
|
27
|
-
module Protocols
|
28
|
-
|
29
|
-
# === Usage
|
30
|
-
#
|
31
|
-
# EM.run{
|
32
|
-
# conn = EM::Protocols::HttpClient2.connect 'google.com', 80
|
33
|
-
#
|
34
|
-
# req = conn.get('/')
|
35
|
-
# req.callback{ |response|
|
36
|
-
# p(response.status)
|
37
|
-
# p(response.headers)
|
38
|
-
# p(response.content)
|
39
|
-
# }
|
40
|
-
# }
|
41
|
-
class HttpClient2 < Connection
|
42
|
-
include LineText2
|
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
|
-
if
|
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
|
-
def
|
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
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
def
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
#
|
494
|
-
#
|
495
|
-
#
|
496
|
-
#
|
497
|
-
#
|
498
|
-
#
|
499
|
-
#
|
500
|
-
#
|
501
|
-
#
|
502
|
-
#
|
503
|
-
#
|
504
|
-
#
|
505
|
-
#
|
506
|
-
#
|
507
|
-
#
|
508
|
-
#
|
509
|
-
#
|
510
|
-
#
|
511
|
-
#
|
512
|
-
#
|
513
|
-
#
|
514
|
-
#
|
515
|
-
#
|
516
|
-
#
|
517
|
-
#
|
518
|
-
#
|
519
|
-
#
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
#
|
562
|
-
#
|
563
|
-
|
564
|
-
#
|
565
|
-
#
|
566
|
-
#
|
567
|
-
#
|
568
|
-
#
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
1
|
+
#--
|
2
|
+
#
|
3
|
+
# Author:: Francis Cianfrocca (gmail: blackhedd)
|
4
|
+
# Homepage:: http://rubyeventmachine.com
|
5
|
+
# Date:: 16 July 2006
|
6
|
+
#
|
7
|
+
# See EventMachine and EventMachine::Connection for documentation and
|
8
|
+
# usage examples.
|
9
|
+
#
|
10
|
+
#----------------------------------------------------------------------------
|
11
|
+
#
|
12
|
+
# Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
|
13
|
+
# Gmail: blackhedd
|
14
|
+
#
|
15
|
+
# This program is free software; you can redistribute it and/or modify
|
16
|
+
# it under the terms of either: 1) the GNU General Public License
|
17
|
+
# as published by the Free Software Foundation; either version 2 of the
|
18
|
+
# License, or (at your option) any later version; or 2) Ruby's License.
|
19
|
+
#
|
20
|
+
# See the file COPYING for complete licensing information.
|
21
|
+
#
|
22
|
+
#---------------------------------------------------------------------------
|
23
|
+
#
|
24
|
+
#
|
25
|
+
|
26
|
+
module EventMachine
|
27
|
+
module Protocols
|
28
|
+
|
29
|
+
# === Usage
|
30
|
+
#
|
31
|
+
# EM.run{
|
32
|
+
# conn = EM::Protocols::HttpClient2.connect 'google.com', 80
|
33
|
+
#
|
34
|
+
# req = conn.get('/')
|
35
|
+
# req.callback{ |response|
|
36
|
+
# p(response.status)
|
37
|
+
# p(response.headers)
|
38
|
+
# p(response.content)
|
39
|
+
# }
|
40
|
+
# }
|
41
|
+
class HttpClient2 < Connection
|
42
|
+
include LineText2
|
43
|
+
|
44
|
+
def initialize
|
45
|
+
@authorization = nil
|
46
|
+
@closed = nil
|
47
|
+
@requests = nil
|
48
|
+
end
|
49
|
+
|
50
|
+
class Request # :nodoc:
|
51
|
+
include Deferrable
|
52
|
+
|
53
|
+
attr_reader :version
|
54
|
+
attr_reader :status
|
55
|
+
attr_reader :header_lines
|
56
|
+
attr_reader :headers
|
57
|
+
attr_reader :content
|
58
|
+
attr_reader :internal_error
|
59
|
+
|
60
|
+
def initialize conn, args
|
61
|
+
@conn = conn
|
62
|
+
@args = args
|
63
|
+
@header_lines = []
|
64
|
+
@headers = {}
|
65
|
+
@blanks = 0
|
66
|
+
@chunk_trailer = nil
|
67
|
+
@chunking = nil
|
68
|
+
end
|
69
|
+
|
70
|
+
def send_request
|
71
|
+
az = @args[:authorization] and az = "Authorization: #{az}\r\n"
|
72
|
+
|
73
|
+
r = [
|
74
|
+
"#{@args[:verb]} #{@args[:uri]} HTTP/#{@args[:version] || "1.1"}\r\n",
|
75
|
+
"Host: #{@args[:host_header] || "_"}\r\n",
|
76
|
+
az || "",
|
77
|
+
"\r\n"
|
78
|
+
]
|
79
|
+
@conn.send_data r.join
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
#--
|
84
|
+
#
|
85
|
+
def receive_line ln
|
86
|
+
if @chunk_trailer
|
87
|
+
receive_chunk_trailer(ln)
|
88
|
+
elsif @chunking
|
89
|
+
receive_chunk_header(ln)
|
90
|
+
else
|
91
|
+
receive_header_line(ln)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
#--
|
96
|
+
#
|
97
|
+
def receive_chunk_trailer ln
|
98
|
+
if ln.length == 0
|
99
|
+
@conn.pop_request
|
100
|
+
succeed(self)
|
101
|
+
else
|
102
|
+
p "Received chunk trailer line"
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
#--
|
107
|
+
# Allow up to ten blank lines before we get a real response line.
|
108
|
+
# Allow no more than 100 lines in the header.
|
109
|
+
#
|
110
|
+
def receive_header_line ln
|
111
|
+
if ln.length == 0
|
112
|
+
if @header_lines.length > 0
|
113
|
+
process_header
|
114
|
+
else
|
115
|
+
@blanks += 1
|
116
|
+
if @blanks > 10
|
117
|
+
@conn.close_connection
|
118
|
+
end
|
119
|
+
end
|
120
|
+
else
|
121
|
+
@header_lines << ln
|
122
|
+
if @header_lines.length > 100
|
123
|
+
@internal_error = :bad_header
|
124
|
+
@conn.close_connection
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
#--
|
130
|
+
# Cf RFC 2616 pgh 3.6.1 for the format of HTTP chunks.
|
131
|
+
#
|
132
|
+
def receive_chunk_header ln
|
133
|
+
if ln.length > 0
|
134
|
+
chunksize = ln.to_i(16)
|
135
|
+
if chunksize > 0
|
136
|
+
@conn.set_text_mode(ln.to_i(16))
|
137
|
+
else
|
138
|
+
@content = @content ? @content.join : ''
|
139
|
+
@chunk_trailer = true
|
140
|
+
end
|
141
|
+
else
|
142
|
+
# We correctly come here after each chunk gets read.
|
143
|
+
# p "Got A BLANK chunk line"
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
147
|
+
|
148
|
+
|
149
|
+
#--
|
150
|
+
# We get a single chunk. Append it to the incoming content and switch back to line mode.
|
151
|
+
#
|
152
|
+
def receive_chunked_text text
|
153
|
+
# p "RECEIVED #{text.length} CHUNK"
|
154
|
+
(@content ||= []) << text
|
155
|
+
end
|
156
|
+
|
157
|
+
|
158
|
+
#--
|
159
|
+
# TODO, inefficient how we're handling this. Part of it is done so as to
|
160
|
+
# make sure we don't have problems in detecting chunked-encoding, content-length,
|
161
|
+
# etc.
|
162
|
+
#
|
163
|
+
HttpResponseRE = /\AHTTP\/(1.[01]) ([\d]{3})/i
|
164
|
+
ClenRE = /\AContent-length:\s*(\d+)/i
|
165
|
+
ChunkedRE = /\ATransfer-encoding:\s*chunked/i
|
166
|
+
ColonRE = /\:\s*/
|
167
|
+
|
168
|
+
def process_header
|
169
|
+
unless @header_lines.first =~ HttpResponseRE
|
170
|
+
@conn.close_connection
|
171
|
+
@internal_error = :bad_request
|
172
|
+
end
|
173
|
+
@version = $1.dup
|
174
|
+
@status = $2.dup.to_i
|
175
|
+
|
176
|
+
clen = nil
|
177
|
+
chunks = nil
|
178
|
+
@header_lines.each_with_index do |e,ix|
|
179
|
+
if ix > 0
|
180
|
+
hdr,val = e.split(ColonRE,2)
|
181
|
+
(@headers[hdr.downcase] ||= []) << val
|
182
|
+
end
|
183
|
+
|
184
|
+
if clen == nil and e =~ ClenRE
|
185
|
+
clen = $1.dup.to_i
|
186
|
+
end
|
187
|
+
if e =~ ChunkedRE
|
188
|
+
chunks = true
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
if clen
|
193
|
+
# If the content length is zero we should not call set_text_mode,
|
194
|
+
# because a value of zero will make it wait forever, hanging the
|
195
|
+
# connection. Just return success instead, with empty content.
|
196
|
+
if clen == 0 then
|
197
|
+
@content = ""
|
198
|
+
@conn.pop_request
|
199
|
+
succeed(self)
|
200
|
+
else
|
201
|
+
@conn.set_text_mode clen
|
202
|
+
end
|
203
|
+
elsif chunks
|
204
|
+
@chunking = true
|
205
|
+
else
|
206
|
+
# Chunked transfer, multipart, or end-of-connection.
|
207
|
+
# For end-of-connection, we need to go the unbind
|
208
|
+
# method and suppress its desire to fail us.
|
209
|
+
p "NO CLEN"
|
210
|
+
p @args[:uri]
|
211
|
+
p @header_lines
|
212
|
+
@internal_error = :unsupported_clen
|
213
|
+
@conn.close_connection
|
214
|
+
end
|
215
|
+
end
|
216
|
+
private :process_header
|
217
|
+
|
218
|
+
|
219
|
+
def receive_text text
|
220
|
+
@chunking ? receive_chunked_text(text) : receive_sized_text(text)
|
221
|
+
end
|
222
|
+
|
223
|
+
#--
|
224
|
+
# At the present time, we only handle contents that have a length
|
225
|
+
# specified by the content-length header.
|
226
|
+
#
|
227
|
+
def receive_sized_text text
|
228
|
+
@content = text
|
229
|
+
@conn.pop_request
|
230
|
+
succeed(self)
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
# Make a connection to a remote HTTP server.
|
235
|
+
# Can take either a pair of arguments (which will be interpreted as
|
236
|
+
# a hostname/ip-address and a port), or a hash.
|
237
|
+
# If the arguments are a hash, then supported values include:
|
238
|
+
# :host => a hostname or ip-address
|
239
|
+
# :port => a port number
|
240
|
+
# :ssl => true to enable ssl
|
241
|
+
def self.connect *args
|
242
|
+
if args.length == 2
|
243
|
+
args = {:host=>args[0], :port=>args[1]}
|
244
|
+
else
|
245
|
+
args = args.first
|
246
|
+
end
|
247
|
+
|
248
|
+
h,prt,ssl = args[:host], Integer(args[:port]), (args[:tls] || args[:ssl])
|
249
|
+
conn = EM.connect( h, prt, self )
|
250
|
+
conn.start_tls if ssl
|
251
|
+
conn.set_default_host_header( h, prt, ssl )
|
252
|
+
conn
|
253
|
+
end
|
254
|
+
|
255
|
+
# Get a url
|
256
|
+
#
|
257
|
+
# req = conn.get(:uri => '/')
|
258
|
+
# req.callback{|response| puts response.content }
|
259
|
+
#
|
260
|
+
def get args
|
261
|
+
if args.is_a?(String)
|
262
|
+
args = {:uri=>args}
|
263
|
+
end
|
264
|
+
args[:verb] = "GET"
|
265
|
+
request args
|
266
|
+
end
|
267
|
+
|
268
|
+
# Post to a url
|
269
|
+
#
|
270
|
+
# req = conn.post('/data')
|
271
|
+
# req.callback{|response| puts response.content }
|
272
|
+
#--
|
273
|
+
# XXX there's no way to supply a POST body.. wtf?
|
274
|
+
def post args
|
275
|
+
if args.is_a?(String)
|
276
|
+
args = {:uri=>args}
|
277
|
+
end
|
278
|
+
args[:verb] = "POST"
|
279
|
+
request args
|
280
|
+
end
|
281
|
+
|
282
|
+
# :stopdoc:
|
283
|
+
|
284
|
+
#--
|
285
|
+
# Compute and remember a string to be used as the host header in HTTP requests
|
286
|
+
# unless the user overrides it with an argument to #request.
|
287
|
+
#
|
288
|
+
def set_default_host_header host, port, ssl
|
289
|
+
if (ssl and port != 443) or (!ssl and port != 80)
|
290
|
+
@host_header = "#{host}:#{port}"
|
291
|
+
else
|
292
|
+
@host_header = host
|
293
|
+
end
|
294
|
+
end
|
295
|
+
|
296
|
+
|
297
|
+
def post_init
|
298
|
+
super
|
299
|
+
@connected = EM::DefaultDeferrable.new
|
300
|
+
end
|
301
|
+
|
302
|
+
def connection_completed
|
303
|
+
super
|
304
|
+
@connected.succeed
|
305
|
+
end
|
306
|
+
|
307
|
+
#--
|
308
|
+
# All pending requests, if any, must fail.
|
309
|
+
# We might come here without ever passing through connection_completed
|
310
|
+
# in case we can't connect to the server. We'll also get here when the
|
311
|
+
# connection closes (either because the server closes it, or we close it
|
312
|
+
# due to detecting an internal error or security violation).
|
313
|
+
# In either case, run down all pending requests, if any, and signal failure
|
314
|
+
# on them.
|
315
|
+
#
|
316
|
+
# Set and remember a flag (@closed) so we can immediately fail any
|
317
|
+
# subsequent requests.
|
318
|
+
#
|
319
|
+
def unbind
|
320
|
+
super
|
321
|
+
@closed = true
|
322
|
+
(@requests || []).each {|r| r.fail}
|
323
|
+
end
|
324
|
+
|
325
|
+
def request args
|
326
|
+
args[:host_header] = @host_header unless args.has_key?(:host_header)
|
327
|
+
args[:authorization] = @authorization unless args.has_key?(:authorization)
|
328
|
+
r = Request.new self, args
|
329
|
+
if @closed
|
330
|
+
r.fail
|
331
|
+
else
|
332
|
+
(@requests ||= []).unshift r
|
333
|
+
@connected.callback {r.send_request}
|
334
|
+
end
|
335
|
+
r
|
336
|
+
end
|
337
|
+
|
338
|
+
def receive_line ln
|
339
|
+
if req = @requests.last
|
340
|
+
req.receive_line ln
|
341
|
+
else
|
342
|
+
p "??????????"
|
343
|
+
p ln
|
344
|
+
end
|
345
|
+
|
346
|
+
end
|
347
|
+
def receive_binary_data text
|
348
|
+
@requests.last.receive_text text
|
349
|
+
end
|
350
|
+
|
351
|
+
#--
|
352
|
+
# Called by a Request object when it completes.
|
353
|
+
#
|
354
|
+
def pop_request
|
355
|
+
@requests.pop
|
356
|
+
end
|
357
|
+
|
358
|
+
# :startdoc:
|
359
|
+
end
|
360
|
+
|
361
|
+
|
362
|
+
=begin
|
363
|
+
class HttpClient2x < Connection
|
364
|
+
include LineText2
|
365
|
+
|
366
|
+
# TODO: Make this behave appropriate in case a #connect fails.
|
367
|
+
# Currently, this produces no errors.
|
368
|
+
|
369
|
+
# Make a connection to a remote HTTP server.
|
370
|
+
# Can take either a pair of arguments (which will be interpreted as
|
371
|
+
# a hostname/ip-address and a port), or a hash.
|
372
|
+
# If the arguments are a hash, then supported values include:
|
373
|
+
# :host => a hostname or ip-address;
|
374
|
+
# :port => a port number
|
375
|
+
#--
|
376
|
+
# TODO, support optional encryption arguments like :ssl
|
377
|
+
def self.connect *args
|
378
|
+
if args.length == 2
|
379
|
+
args = {:host=>args[0], :port=>args[1]}
|
380
|
+
else
|
381
|
+
args = args.first
|
382
|
+
end
|
383
|
+
|
384
|
+
h,prt = args[:host],Integer(args[:port])
|
385
|
+
EM.connect( h, prt, self, h, prt )
|
386
|
+
end
|
387
|
+
|
388
|
+
|
389
|
+
#--
|
390
|
+
# Sugars a connection that makes a single request and then
|
391
|
+
# closes the connection. Matches the behavior and the arguments
|
392
|
+
# of the original implementation of class HttpClient.
|
393
|
+
#
|
394
|
+
# Intended primarily for back compatibility, but the idiom
|
395
|
+
# is probably useful so it's not deprecated.
|
396
|
+
# We return a Deferrable, as did the original implementation.
|
397
|
+
#
|
398
|
+
# Because we're improving the way we deal with errors and exceptions
|
399
|
+
# (specifically, HTTP response codes other than 2xx will trigger the
|
400
|
+
# errback rather than the callback), this may break some existing code.
|
401
|
+
#
|
402
|
+
def self.request args
|
403
|
+
c = connect args
|
404
|
+
end
|
405
|
+
|
406
|
+
#--
|
407
|
+
# Requests can be pipelined. When we get a request, add it to the
|
408
|
+
# front of a queue as an array. The last element of the @requests
|
409
|
+
# array is always the oldest request received. Each element of the
|
410
|
+
# @requests array is a two-element array consisting of a hash with
|
411
|
+
# the original caller's arguments, and an initially-empty Ostruct
|
412
|
+
# containing the data we retrieve from the server's response.
|
413
|
+
# Maintain the instance variable @current_response, which is the response
|
414
|
+
# of the oldest pending request. That's just to make other code a little
|
415
|
+
# easier. If the variable doesn't exist when we come here, we're
|
416
|
+
# obviously the first request being made on the connection.
|
417
|
+
#
|
418
|
+
# The reason for keeping this method private (and requiring use of the
|
419
|
+
# convenience methods #get, #post, #head, etc) is to avoid the small
|
420
|
+
# performance penalty of canonicalizing the verb.
|
421
|
+
#
|
422
|
+
def request args
|
423
|
+
d = EventMachine::DefaultDeferrable.new
|
424
|
+
|
425
|
+
if @closed
|
426
|
+
d.fail
|
427
|
+
return d
|
428
|
+
end
|
429
|
+
|
430
|
+
o = OpenStruct.new
|
431
|
+
o.deferrable = d
|
432
|
+
(@requests ||= []).unshift [args, o]
|
433
|
+
@current_response ||= @requests.last.last
|
434
|
+
@connected.callback {
|
435
|
+
az = args[:authorization] and az = "Authorization: #{az}\r\n"
|
436
|
+
|
437
|
+
r = [
|
438
|
+
"#{args[:verb]} #{args[:uri]} HTTP/#{args[:version] || "1.1"}\r\n",
|
439
|
+
"Host: #{args[:host_header] || @host_header}\r\n",
|
440
|
+
az || "",
|
441
|
+
"\r\n"
|
442
|
+
]
|
443
|
+
p r
|
444
|
+
send_data r.join
|
445
|
+
}
|
446
|
+
o.deferrable
|
447
|
+
end
|
448
|
+
private :request
|
449
|
+
|
450
|
+
def get args
|
451
|
+
if args.is_a?(String)
|
452
|
+
args = {:uri=>args}
|
453
|
+
end
|
454
|
+
args[:verb] = "GET"
|
455
|
+
request args
|
456
|
+
end
|
457
|
+
|
458
|
+
def initialize host, port
|
459
|
+
super
|
460
|
+
@host_header = "#{host}:#{port}"
|
461
|
+
end
|
462
|
+
def post_init
|
463
|
+
super
|
464
|
+
@connected = EM::DefaultDeferrable.new
|
465
|
+
end
|
466
|
+
|
467
|
+
|
468
|
+
def connection_completed
|
469
|
+
super
|
470
|
+
@connected.succeed
|
471
|
+
end
|
472
|
+
|
473
|
+
#--
|
474
|
+
# Make sure to throw away any leftover incoming data if we've
|
475
|
+
# been closed due to recognizing an error.
|
476
|
+
#
|
477
|
+
# Generate an internal error if we get an unreasonable number of
|
478
|
+
# header lines. It could be malicious.
|
479
|
+
#
|
480
|
+
def receive_line ln
|
481
|
+
p ln
|
482
|
+
return if @closed
|
483
|
+
|
484
|
+
if ln.length > 0
|
485
|
+
(@current_response.headers ||= []).push ln
|
486
|
+
abort_connection if @current_response.headers.length > 100
|
487
|
+
else
|
488
|
+
process_received_headers
|
489
|
+
end
|
490
|
+
end
|
491
|
+
|
492
|
+
#--
|
493
|
+
# We come here when we've seen all the headers for a particular request.
|
494
|
+
# What we do next depends on the response line (which should be the
|
495
|
+
# first line in the header set), and whether there is content to read.
|
496
|
+
# We may transition into a text-reading state to read content, or
|
497
|
+
# we may abort the connection, or we may go right back into parsing
|
498
|
+
# responses for the next response in the chain.
|
499
|
+
#
|
500
|
+
# We make an ASSUMPTION that the first line is an HTTP response.
|
501
|
+
# Anything else produces an error that aborts the connection.
|
502
|
+
# This may not be enough, because it may be that responses to pipelined
|
503
|
+
# requests will come with a blank-line delimiter.
|
504
|
+
#
|
505
|
+
# Any non-2xx response will be treated as a fatal error, and abort the
|
506
|
+
# connection. We will set up the status and other response parameters.
|
507
|
+
# TODO: we will want to properly support 1xx responses, which some versions
|
508
|
+
# of IIS copiously generate.
|
509
|
+
# TODO: We need to give the option of not aborting the connection with certain
|
510
|
+
# non-200 responses, in order to work with NTLM and other authentication
|
511
|
+
# schemes that work at the level of individual connections.
|
512
|
+
#
|
513
|
+
# Some error responses will get sugarings. For example, we'll return the
|
514
|
+
# Location header in the response in case of a 301/302 response.
|
515
|
+
#
|
516
|
+
# Possible dispositions here:
|
517
|
+
# 1) No content to read (either content-length is zero or it's a HEAD request);
|
518
|
+
# 2) Switch to text mode to read a specific number of bytes;
|
519
|
+
# 3) Read a chunked or multipart response;
|
520
|
+
# 4) Read till the server closes the connection.
|
521
|
+
#
|
522
|
+
# Our reponse to the client can be either to wait till all the content
|
523
|
+
# has been read and then to signal caller's deferrable, or else to signal
|
524
|
+
# it when we finish the processing the headers and then expect the caller
|
525
|
+
# to have given us a block to call as the content comes in. And of course
|
526
|
+
# the latter gets stickier with chunks and multiparts.
|
527
|
+
#
|
528
|
+
HttpResponseRE = /\AHTTP\/(1.[01]) ([\d]{3})/i
|
529
|
+
ClenRE = /\AContent-length:\s*(\d+)/i
|
530
|
+
def process_received_headers
|
531
|
+
abort_connection unless @current_response.headers.first =~ HttpResponseRE
|
532
|
+
@current_response.version = $1.dup
|
533
|
+
st = $2.dup
|
534
|
+
@current_response.status = st.to_i
|
535
|
+
abort_connection unless st[0,1] == "2"
|
536
|
+
|
537
|
+
clen = nil
|
538
|
+
@current_response.headers.each do |e|
|
539
|
+
if clen == nil and e =~ ClenRE
|
540
|
+
clen = $1.dup.to_i
|
541
|
+
end
|
542
|
+
end
|
543
|
+
|
544
|
+
if clen
|
545
|
+
set_text_mode clen
|
546
|
+
end
|
547
|
+
end
|
548
|
+
private :process_received_headers
|
549
|
+
|
550
|
+
|
551
|
+
def receive_binary_data text
|
552
|
+
@current_response.content = text
|
553
|
+
@current_response.deferrable.succeed @current_response
|
554
|
+
@requests.pop
|
555
|
+
@current_response = (@requests.last || []).last
|
556
|
+
set_line_mode
|
557
|
+
end
|
558
|
+
|
559
|
+
|
560
|
+
|
561
|
+
# We've received either a server error or an internal error.
|
562
|
+
# Close the connection and abort any pending requests.
|
563
|
+
#--
|
564
|
+
# When should we call close_connection? It will cause #unbind
|
565
|
+
# to be fired. Should the user expect to see #unbind before
|
566
|
+
# we call #receive_http_error, or the other way around?
|
567
|
+
#
|
568
|
+
# Set instance variable @closed. That's used to inhibit further
|
569
|
+
# processing of any inbound data after an error has been recognized.
|
570
|
+
#
|
571
|
+
# We shouldn't have to worry about any leftover outbound data,
|
572
|
+
# because we call close_connection (not close_connection_after_writing).
|
573
|
+
# That ensures that any pipelined requests received after an error
|
574
|
+
# DO NOT get streamed out to the server on this connection.
|
575
|
+
# Very important. TODO, write a unit-test to establish that behavior.
|
576
|
+
#
|
577
|
+
def abort_connection
|
578
|
+
close_connection
|
579
|
+
@closed = true
|
580
|
+
@current_response.deferrable.fail( @current_response )
|
581
|
+
end
|
582
|
+
|
583
|
+
|
584
|
+
#------------------------
|
585
|
+
# Below here are user-overridable methods.
|
586
|
+
|
587
|
+
end
|
588
|
+
=end
|
589
|
+
end
|
590
|
+
end
|