crusher-eventmachine 0.12.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. data/README +81 -0
  2. data/Rakefile +370 -0
  3. data/docs/COPYING +60 -0
  4. data/docs/ChangeLog +211 -0
  5. data/docs/DEFERRABLES +246 -0
  6. data/docs/EPOLL +141 -0
  7. data/docs/GNU +281 -0
  8. data/docs/INSTALL +13 -0
  9. data/docs/KEYBOARD +42 -0
  10. data/docs/LEGAL +25 -0
  11. data/docs/LIGHTWEIGHT_CONCURRENCY +130 -0
  12. data/docs/PURE_RUBY +75 -0
  13. data/docs/RELEASE_NOTES +94 -0
  14. data/docs/SMTP +4 -0
  15. data/docs/SPAWNED_PROCESSES +148 -0
  16. data/docs/TODO +8 -0
  17. data/eventmachine.gemspec +40 -0
  18. data/examples/ex_channel.rb +43 -0
  19. data/examples/ex_queue.rb +2 -0
  20. data/examples/ex_tick_loop_array.rb +15 -0
  21. data/examples/ex_tick_loop_counter.rb +32 -0
  22. data/examples/helper.rb +2 -0
  23. data/ext/binder.cpp +124 -0
  24. data/ext/binder.h +46 -0
  25. data/ext/cmain.cpp +852 -0
  26. data/ext/cplusplus.cpp +202 -0
  27. data/ext/ed.cpp +1884 -0
  28. data/ext/ed.h +418 -0
  29. data/ext/em.cpp +2377 -0
  30. data/ext/em.h +239 -0
  31. data/ext/emwin.cpp +300 -0
  32. data/ext/emwin.h +94 -0
  33. data/ext/epoll.cpp +26 -0
  34. data/ext/epoll.h +25 -0
  35. data/ext/eventmachine.h +124 -0
  36. data/ext/eventmachine_cpp.h +96 -0
  37. data/ext/extconf.rb +150 -0
  38. data/ext/fastfilereader/extconf.rb +85 -0
  39. data/ext/fastfilereader/mapper.cpp +214 -0
  40. data/ext/fastfilereader/mapper.h +59 -0
  41. data/ext/fastfilereader/rubymain.cpp +127 -0
  42. data/ext/files.cpp +94 -0
  43. data/ext/files.h +65 -0
  44. data/ext/kb.cpp +79 -0
  45. data/ext/page.cpp +107 -0
  46. data/ext/page.h +51 -0
  47. data/ext/pipe.cpp +347 -0
  48. data/ext/project.h +157 -0
  49. data/ext/rubymain.cpp +1215 -0
  50. data/ext/sigs.cpp +89 -0
  51. data/ext/sigs.h +32 -0
  52. data/ext/ssl.cpp +460 -0
  53. data/ext/ssl.h +94 -0
  54. data/java/.classpath +8 -0
  55. data/java/.project +17 -0
  56. data/java/src/com/rubyeventmachine/EmReactor.java +571 -0
  57. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  58. data/java/src/com/rubyeventmachine/EventableChannel.java +69 -0
  59. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +189 -0
  60. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +364 -0
  61. data/java/src/com/rubyeventmachine/application/Application.java +194 -0
  62. data/java/src/com/rubyeventmachine/application/Connection.java +74 -0
  63. data/java/src/com/rubyeventmachine/application/ConnectionFactory.java +37 -0
  64. data/java/src/com/rubyeventmachine/application/DefaultConnectionFactory.java +46 -0
  65. data/java/src/com/rubyeventmachine/application/PeriodicTimer.java +38 -0
  66. data/java/src/com/rubyeventmachine/application/Timer.java +54 -0
  67. data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +109 -0
  68. data/java/src/com/rubyeventmachine/tests/ConnectTest.java +148 -0
  69. data/java/src/com/rubyeventmachine/tests/EMTest.java +80 -0
  70. data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +53 -0
  71. data/java/src/com/rubyeventmachine/tests/TestServers.java +75 -0
  72. data/java/src/com/rubyeventmachine/tests/TestTimers.java +90 -0
  73. data/lib/em/buftok.rb +138 -0
  74. data/lib/em/callback.rb +26 -0
  75. data/lib/em/channel.rb +57 -0
  76. data/lib/em/connection.rb +569 -0
  77. data/lib/em/deferrable.rb +206 -0
  78. data/lib/em/file_watch.rb +54 -0
  79. data/lib/em/future.rb +61 -0
  80. data/lib/em/iterator.rb +270 -0
  81. data/lib/em/messages.rb +66 -0
  82. data/lib/em/process_watch.rb +44 -0
  83. data/lib/em/processes.rb +119 -0
  84. data/lib/em/protocols.rb +36 -0
  85. data/lib/em/protocols/header_and_content.rb +138 -0
  86. data/lib/em/protocols/httpclient.rb +276 -0
  87. data/lib/em/protocols/httpclient2.rb +590 -0
  88. data/lib/em/protocols/line_and_text.rb +125 -0
  89. data/lib/em/protocols/linetext2.rb +161 -0
  90. data/lib/em/protocols/memcache.rb +323 -0
  91. data/lib/em/protocols/object_protocol.rb +45 -0
  92. data/lib/em/protocols/postgres3.rb +247 -0
  93. data/lib/em/protocols/saslauth.rb +175 -0
  94. data/lib/em/protocols/smtpclient.rb +357 -0
  95. data/lib/em/protocols/smtpserver.rb +640 -0
  96. data/lib/em/protocols/socks4.rb +66 -0
  97. data/lib/em/protocols/stomp.rb +200 -0
  98. data/lib/em/protocols/tcptest.rb +53 -0
  99. data/lib/em/pure_ruby.rb +1019 -0
  100. data/lib/em/queue.rb +62 -0
  101. data/lib/em/spawnable.rb +85 -0
  102. data/lib/em/streamer.rb +130 -0
  103. data/lib/em/tick_loop.rb +85 -0
  104. data/lib/em/timers.rb +57 -0
  105. data/lib/em/version.rb +3 -0
  106. data/lib/eventmachine.rb +1540 -0
  107. data/lib/evma.rb +32 -0
  108. data/lib/evma/callback.rb +32 -0
  109. data/lib/evma/container.rb +75 -0
  110. data/lib/evma/factory.rb +77 -0
  111. data/lib/evma/protocol.rb +87 -0
  112. data/lib/evma/reactor.rb +48 -0
  113. data/lib/jeventmachine.rb +257 -0
  114. data/setup.rb +1585 -0
  115. data/tasks/cpp.rake_example +77 -0
  116. data/tests/client.crt +31 -0
  117. data/tests/client.key +51 -0
  118. data/tests/test_attach.rb +136 -0
  119. data/tests/test_basic.rb +249 -0
  120. data/tests/test_channel.rb +63 -0
  121. data/tests/test_connection_count.rb +35 -0
  122. data/tests/test_defer.rb +49 -0
  123. data/tests/test_deferrable.rb +35 -0
  124. data/tests/test_epoll.rb +160 -0
  125. data/tests/test_error_handler.rb +35 -0
  126. data/tests/test_errors.rb +82 -0
  127. data/tests/test_exc.rb +55 -0
  128. data/tests/test_file_watch.rb +49 -0
  129. data/tests/test_futures.rb +198 -0
  130. data/tests/test_get_sock_opt.rb +30 -0
  131. data/tests/test_handler_check.rb +37 -0
  132. data/tests/test_hc.rb +190 -0
  133. data/tests/test_httpclient.rb +227 -0
  134. data/tests/test_httpclient2.rb +154 -0
  135. data/tests/test_inactivity_timeout.rb +50 -0
  136. data/tests/test_kb.rb +60 -0
  137. data/tests/test_ltp.rb +190 -0
  138. data/tests/test_ltp2.rb +317 -0
  139. data/tests/test_next_tick.rb +133 -0
  140. data/tests/test_object_protocol.rb +37 -0
  141. data/tests/test_pause.rb +70 -0
  142. data/tests/test_pending_connect_timeout.rb +48 -0
  143. data/tests/test_process_watch.rb +50 -0
  144. data/tests/test_processes.rb +128 -0
  145. data/tests/test_proxy_connection.rb +144 -0
  146. data/tests/test_pure.rb +134 -0
  147. data/tests/test_queue.rb +44 -0
  148. data/tests/test_running.rb +42 -0
  149. data/tests/test_sasl.rb +72 -0
  150. data/tests/test_send_file.rb +251 -0
  151. data/tests/test_servers.rb +76 -0
  152. data/tests/test_smtpclient.rb +83 -0
  153. data/tests/test_smtpserver.rb +85 -0
  154. data/tests/test_spawn.rb +322 -0
  155. data/tests/test_ssl_args.rb +79 -0
  156. data/tests/test_ssl_methods.rb +50 -0
  157. data/tests/test_ssl_verify.rb +82 -0
  158. data/tests/test_tick_loop.rb +59 -0
  159. data/tests/test_timers.rb +160 -0
  160. data/tests/test_ud.rb +36 -0
  161. data/tests/testem.rb +31 -0
  162. metadata +251 -0
@@ -0,0 +1,66 @@
1
+ #--
2
+ #
3
+ # Author:: Francis Cianfrocca (gmail: blackhedd)
4
+ # Homepage:: http://rubyeventmachine.com
5
+ # Date:: 16 Jul 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
+ =begin
27
+
28
+ Message Routing in EventMachine.
29
+
30
+ The goal here is to enable "routing points," objects that can send and receive
31
+ "messages," which are delimited streams of bytes. The boundaries of a message
32
+ are preserved as it passes through the reactor system.
33
+
34
+ There will be several module methods defined in EventMachine to create route-point
35
+ objects (which will probably have a base class of EventMachine::MessageRouter
36
+ until someone suggests a better name).
37
+
38
+ As with I/O objects, routing objects will receive events by having the router
39
+ core call methods on them. And of course user code can and will define handlers
40
+ to deal with events of interest.
41
+
42
+ The message router base class only really needs a receive_message method. There will
43
+ be an EM module-method to send messages, in addition to the module methods to create
44
+ the various kinds of message receivers.
45
+
46
+ The simplest kind of message receiver object can receive messages by being named
47
+ explicitly in a parameter to EM#send_message. More sophisticated receivers can define
48
+ pub-sub selectors and message-queue names. And they can also define channels for
49
+ route-points in other processes or even on other machines.
50
+
51
+ A message is NOT a marshallable entity. Rather, it's a chunk of flat content more like
52
+ an Erlang message. Initially, all content submitted for transmission as a message will
53
+ have the to_s method called on it. Eventually, we'll be able to transmit certain structured
54
+ data types (XML and YAML documents, Structs within limits) and have them reconstructed
55
+ on the other end.
56
+
57
+ A fundamental goal of the message-routing capability is to interoperate seamlessly with
58
+ external systems, including non-Ruby systems like ActiveMQ. We will define various protocol
59
+ handlers for things like Stomp and possibly AMQP, but these will be wrapped up and hidden
60
+ from the users of the basic routing capability.
61
+
62
+ As with Erlang, a critical goal is for programs that are built to use message-passing to work
63
+ WITHOUT CHANGE when the code is re-based on a multi-process system.
64
+
65
+ =end
66
+
@@ -0,0 +1,44 @@
1
+ module EventMachine
2
+
3
+ # This is subclassed from EventMachine::Connection for use with the process monitoring API. Read the
4
+ # documentation on the instance methods of this class, and for a full explanation see EventMachine.watch_process.
5
+ class ProcessWatch < Connection
6
+ # :stopdoc:
7
+ Cfork = 'fork'.freeze
8
+ Cexit = 'exit'.freeze
9
+ # :startdoc:
10
+
11
+ def receive_data(data) # :nodoc:
12
+ case data
13
+ when Cfork
14
+ process_forked
15
+ when Cexit
16
+ process_exited
17
+ end
18
+ end
19
+
20
+ # Returns the pid that EventMachine::watch_process was originally called with.
21
+ def pid
22
+ @pid
23
+ end
24
+
25
+ # Should be redefined with the user's custom callback that will be fired when the prcess is forked.
26
+ #
27
+ # There is currently not an easy way to get the pid of the forked child.
28
+ def process_forked
29
+ end
30
+
31
+ # Should be redefined with the user's custom callback that will be fired when the process exits.
32
+ #
33
+ # stop_watching is called automatically after this callback
34
+ def process_exited
35
+ end
36
+
37
+ # Discontinue monitoring of the process.
38
+ # This will be called automatically when a process dies. User code may call it as well.
39
+ def stop_watching
40
+ EventMachine::unwatch_pid(@signature)
41
+ end
42
+ end
43
+
44
+ end
@@ -0,0 +1,119 @@
1
+ #--
2
+ #
3
+ # Author:: Francis Cianfrocca (gmail: blackhedd)
4
+ # Homepage:: http://rubyeventmachine.com
5
+ # Date:: 13 Dec 07
6
+ #
7
+ # See EventMachine and EventMachine::Connection for documentation and
8
+ # usage examples.
9
+ #
10
+ #----------------------------------------------------------------------------
11
+ #
12
+ # Copyright (C) 2006-08 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
+
27
+ module EventMachine
28
+
29
+ # EM::DeferrableChildProcess is a sugaring of a common use-case
30
+ # involving EM::popen.
31
+ # Call the #open method on EM::DeferrableChildProcess, passing
32
+ # a command-string. #open immediately returns an EM::Deferrable
33
+ # object. It also schedules the forking of a child process, which
34
+ # will execute the command passed to #open.
35
+ # When the forked child terminates, the Deferrable will be signalled
36
+ # and execute its callbacks, passing the data that the child process
37
+ # wrote to stdout.
38
+ #
39
+ class DeferrableChildProcess < EventMachine::Connection
40
+ include EventMachine::Deferrable
41
+
42
+ def initialize # :nodoc:
43
+ super
44
+ @data = []
45
+ end
46
+
47
+ # Sugars a common use-case involving forked child processes.
48
+ # #open takes a String argument containing an shell command
49
+ # string (including arguments if desired). #open immediately
50
+ # returns an EventMachine::Deferrable object, without blocking.
51
+ #
52
+ # It also invokes EventMachine#popen to run the passed-in
53
+ # command in a forked child process.
54
+ #
55
+ # When the forked child terminates, the Deferrable that
56
+ # #open calls its callbacks, passing the data returned
57
+ # from the child process.
58
+ #
59
+ def self.open cmd
60
+ EventMachine.popen( cmd, DeferrableChildProcess )
61
+ end
62
+
63
+ def receive_data data # :nodoc:
64
+ @data << data
65
+ end
66
+
67
+ def unbind # :nodoc:
68
+ succeed( @data.join )
69
+ end
70
+ end
71
+
72
+ class SystemCmd < EventMachine::Connection # :nodoc:
73
+ def initialize cb
74
+ @cb = cb
75
+ @output = []
76
+ end
77
+ def receive_data data
78
+ @output << data
79
+ end
80
+ def unbind
81
+ @cb.call @output.join(''), get_status if @cb
82
+ end
83
+ end
84
+
85
+ # EM::system is a simple wrapper for EM::popen. It is similar to Kernel::system, but requires a
86
+ # single string argument for the command and performs no shell expansion.
87
+ #
88
+ # The block or proc passed to EM::system is called with two arguments: the output generated by the command,
89
+ # and a Process::Status that contains information about the command's execution.
90
+ #
91
+ # EM.run{
92
+ # EM.system('ls'){ |output,status| puts output if status.exitstatus == 0 }
93
+ # }
94
+ #
95
+ # You can also supply an additional proc to send some data to the process:
96
+ #
97
+ # EM.run{
98
+ # EM.system('sh', proc{ |process|
99
+ # process.send_data("echo hello\n")
100
+ # process.send_data("exit\n")
101
+ # }, proc{ |out,status|
102
+ # puts(out)
103
+ # })
104
+ # }
105
+ #
106
+ # Like EventMachine.popen, EventMachine.system currently does not work on windows.
107
+ # It returns the pid of the spawned process.
108
+ def EventMachine::system cmd, *args, &cb
109
+ cb ||= args.pop if args.last.is_a? Proc
110
+ init = args.pop if args.last.is_a? Proc
111
+
112
+ # merge remaining arguments into the command
113
+ cmd = ([cmd] + args.map{|a|a.to_s.dump}).join(' ')
114
+
115
+ EM.get_subprocess_pid(EM.popen(cmd, SystemCmd, cb) do |c|
116
+ init[c] if init
117
+ end.signature)
118
+ end
119
+ end
@@ -0,0 +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
+ # - LineProtocol, 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
@@ -0,0 +1,138 @@
1
+ #--
2
+ #
3
+ # Author:: Francis Cianfrocca (gmail: blackhedd)
4
+ # Homepage:: http://rubyeventmachine.com
5
+ # Date:: 15 Nov 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
+ # class RequestHandler < EM::P::HeaderAndContentProtocol
32
+ # def receive_request headers, content
33
+ # p [:request, headers, content]
34
+ # end
35
+ # end
36
+ #
37
+ # EM.run{
38
+ # EM.start_server 'localhost', 80, RequestHandler
39
+ # }
40
+ #
41
+ #--
42
+ # Originally, this subclassed LineAndTextProtocol, which in
43
+ # turn relies on BufferedTokenizer, which doesn't gracefully
44
+ # handle the transitions between lines and binary text.
45
+ # Changed 13Sep08 by FCianfrocca.
46
+ class HeaderAndContentProtocol < Connection
47
+ include LineText2
48
+
49
+ ContentLengthPattern = /Content-length:\s*(\d+)/i
50
+
51
+ def initialize *args
52
+ super
53
+ init_for_request
54
+ end
55
+
56
+ def receive_line line
57
+ case @hc_mode
58
+ when :discard_blanks
59
+ unless line == ""
60
+ @hc_mode = :headers
61
+ receive_line line
62
+ end
63
+ when :headers
64
+ if line == ""
65
+ raise "unrecognized state" unless @hc_headers.length > 0
66
+ if respond_to?(:receive_headers)
67
+ receive_headers @hc_headers
68
+ end
69
+ # @hc_content_length will be nil, not 0, if there was no content-length header.
70
+ if @hc_content_length.to_i > 0
71
+ set_binary_mode @hc_content_length
72
+ else
73
+ dispatch_request
74
+ end
75
+ else
76
+ @hc_headers << line
77
+ if ContentLengthPattern =~ line
78
+ # There are some attacks that rely on sending multiple content-length
79
+ # headers. This is a crude protection, but needs to become tunable.
80
+ raise "extraneous content-length header" if @hc_content_length
81
+ @hc_content_length = $1.to_i
82
+ end
83
+ if @hc_headers.length == 1 and respond_to?(:receive_first_header_line)
84
+ receive_first_header_line line
85
+ end
86
+ end
87
+ else
88
+ raise "internal error, unsupported mode"
89
+ end
90
+ end
91
+
92
+ def receive_binary_data text
93
+ @hc_content = text
94
+ dispatch_request
95
+ end
96
+
97
+ def dispatch_request
98
+ if respond_to?(:receive_request)
99
+ receive_request @hc_headers, @hc_content
100
+ end
101
+ init_for_request
102
+ end
103
+ private :dispatch_request
104
+
105
+ def init_for_request
106
+ @hc_mode = :discard_blanks
107
+ @hc_headers = []
108
+ # originally was @hc_headers ||= []; @hc_headers.clear to get a performance
109
+ # boost, but it's counterproductive because a subclassed handler will have to
110
+ # call dup to use the header array we pass in receive_headers.
111
+
112
+ @hc_content_length = nil
113
+ @hc_content = ""
114
+ end
115
+ private :init_for_request
116
+
117
+ # Basically a convenience method. We might create a subclass that does this
118
+ # automatically. But it's such a performance killer.
119
+ def headers_2_hash hdrs
120
+ self.class.headers_2_hash hdrs
121
+ end
122
+
123
+ class << self
124
+ def headers_2_hash hdrs
125
+ hash = {}
126
+ hdrs.each {|h|
127
+ if /\A([^\s:]+)\s*:\s*/ =~ h
128
+ tail = $'.dup
129
+ hash[ $1.downcase.gsub(/-/,"_").intern ] = tail
130
+ end
131
+ }
132
+ hash
133
+ end
134
+ end
135
+
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,276 @@
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
+
27
+
28
+ module EventMachine
29
+ module Protocols
30
+
31
+ # === Usage
32
+ #
33
+ # EventMachine.run {
34
+ # http = EventMachine::Protocols::HttpClient.request(
35
+ # :host => server,
36
+ # :port => 80,
37
+ # :request => "/index.html",
38
+ # :query_string => "parm1=value1&parm2=value2"
39
+ # )
40
+ # http.callback {|response|
41
+ # puts response[:status]
42
+ # puts response[:headers]
43
+ # puts response[:content]
44
+ # }
45
+ # }
46
+ #--
47
+ # TODO:
48
+ # Add streaming so we can support enormous POSTs. Current max is 20meg.
49
+ # Timeout for connections that run too long or hang somewhere in the middle.
50
+ # Persistent connections (HTTP/1.1), may need a associated delegate object.
51
+ # DNS: Some way to cache DNS lookups for hostnames we connect to. Ruby's
52
+ # DNS lookups are unbelievably slow.
53
+ # HEAD requests.
54
+ # Chunked transfer encoding.
55
+ # Convenience methods for requests. get, post, url, etc.
56
+ # SSL.
57
+ # Handle status codes like 304, 100, etc.
58
+ # Refactor this code so that protocol errors all get handled one way (an exception?),
59
+ # instead of sprinkling set_deferred_status :failed calls everywhere.
60
+ class HttpClient < Connection
61
+ include EventMachine::Deferrable
62
+
63
+ MaxPostContentLength = 20 * 1024 * 1024
64
+
65
+ # === Arg list
66
+ # :host => 'ip/dns', :port => fixnum, :verb => 'GET', :request => 'path',
67
+ # :basic_auth => {:username => '', :password => ''}, :content => 'content',
68
+ # :contenttype => 'text/plain', :query_string => '', :host_header => '',
69
+ # :cookie => '', :ssl => false
70
+ def self.request( args = {} )
71
+ args[:port] ||= args[:ssl] ? 443 : 80
72
+ EventMachine.connect( args[:host], args[:port], self, args )
73
+ end
74
+
75
+ def initialize(args)
76
+ @args = args
77
+ end
78
+
79
+ def post_init
80
+ start_tls if @args[:ssl]
81
+ @start_time = Time.now
82
+ @data = ""
83
+ @read_state = :base
84
+ end
85
+
86
+ # We send the request when we get a connection.
87
+ # AND, we set an instance variable to indicate we passed through here.
88
+ # That allows #unbind to know whether there was a successful connection.
89
+ # NB: This naive technique won't work when we have to support multiple
90
+ # requests on a single connection.
91
+ def connection_completed
92
+ return if @args[:ssl]
93
+ @connected = true
94
+ send_request @args
95
+ end
96
+
97
+ def ssl_handshake_completed
98
+ @connected = true
99
+ send_request @args
100
+ end
101
+
102
+ def send_request args
103
+ args[:verb] ||= args[:method] # Support :method as an alternative to :verb.
104
+ args[:verb] ||= :get # IS THIS A GOOD IDEA, to default to GET if nothing was specified?
105
+
106
+ verb = args[:verb].to_s.upcase
107
+ unless ["GET", "POST", "PUT", "DELETE", "HEAD"].include?(verb)
108
+ set_deferred_status :failed, {:status => 0} # TODO, not signalling the error type
109
+ return # NOTE THE EARLY RETURN, we're not sending any data.
110
+ end
111
+
112
+ request = args[:request] || "/"
113
+ unless request[0,1] == "/"
114
+ request = "/" + request
115
+ end
116
+
117
+ qs = args[:query_string] || ""
118
+ if qs.length > 0 and qs[0,1] != '?'
119
+ qs = "?" + qs
120
+ end
121
+
122
+ version = args[:version] || "1.1"
123
+
124
+ # Allow an override for the host header if it's not the connect-string.
125
+ host = args[:host_header] || args[:host] || "_"
126
+ # For now, ALWAYS tuck in the port string, although we may want to omit it if it's the default.
127
+ port = args[:port]
128
+
129
+ # POST items.
130
+ postcontenttype = args[:contenttype] || "application/octet-stream"
131
+ postcontent = args[:content] || ""
132
+ raise "oversized content in HTTP POST" if postcontent.length > MaxPostContentLength
133
+
134
+ # ESSENTIAL for the request's line-endings to be CRLF, not LF. Some servers misbehave otherwise.
135
+ # TODO: We ASSUME the caller wants to send a 1.1 request. May not be a good assumption.
136
+ req = [
137
+ "#{verb} #{request}#{qs} HTTP/#{version}",
138
+ "Host: #{host}:#{port}",
139
+ "User-agent: Ruby EventMachine",
140
+ ]
141
+
142
+ if verb == "POST" || verb == "PUT"
143
+ req << "Content-type: #{postcontenttype}"
144
+ req << "Content-length: #{postcontent.length}"
145
+ end
146
+
147
+ # TODO, this cookie handler assumes it's getting a single, semicolon-delimited string.
148
+ # Eventually we will want to deal intelligently with arrays and hashes.
149
+ if args[:cookie]
150
+ req << "Cookie: #{args[:cookie]}"
151
+ end
152
+
153
+ # Allow custom HTTP headers, e.g. SOAPAction
154
+ args[:custom_headers].each do |k,v|
155
+ req << "#{k}: #{v}"
156
+ end if args[:custom_headers]
157
+
158
+ # Basic-auth stanza contributed by Matt Murphy.
159
+ if args[:basic_auth]
160
+ basic_auth_string = ["#{args[:basic_auth][:username]}:#{args[:basic_auth][:password]}"].pack('m').strip.gsub(/\n/,'')
161
+ req << "Authorization: Basic #{basic_auth_string}"
162
+ end
163
+
164
+ req << ""
165
+ reqstring = req.map {|l| "#{l}\r\n"}.join
166
+ send_data reqstring
167
+
168
+ if verb == "POST" || verb == "PUT"
169
+ send_data postcontent
170
+ end
171
+ end
172
+
173
+
174
+ def receive_data data
175
+ while data and data.length > 0
176
+ case @read_state
177
+ when :base
178
+ # Perform any per-request initialization here and don't consume any data.
179
+ @data = ""
180
+ @headers = []
181
+ @content_length = nil # not zero
182
+ @content = ""
183
+ @status = nil
184
+ @read_state = :header
185
+ @connection_close = nil
186
+ when :header
187
+ ary = data.split( /\r?\n/m, 2 )
188
+ if ary.length == 2
189
+ data = ary.last
190
+ if ary.first == ""
191
+ if (@content_length and @content_length > 0) || @connection_close
192
+ @read_state = :content
193
+ else
194
+ dispatch_response
195
+ @read_state = :base
196
+ end
197
+ else
198
+ @headers << ary.first
199
+ if @headers.length == 1
200
+ parse_response_line
201
+ elsif ary.first =~ /\Acontent-length:\s*/i
202
+ # Only take the FIRST content-length header that appears,
203
+ # which we can distinguish because @content_length is nil.
204
+ # TODO, it's actually a fatal error if there is more than one
205
+ # content-length header, because the caller is presumptively
206
+ # a bad guy. (There is an exploit that depends on multiple
207
+ # content-length headers.)
208
+ @content_length ||= $'.to_i
209
+ elsif ary.first =~ /\Aconnection:\s*close/i
210
+ @connection_close = true
211
+ end
212
+ end
213
+ else
214
+ @data << data
215
+ data = ""
216
+ end
217
+ when :content
218
+ # If there was no content-length header, we have to wait until the connection
219
+ # closes. Everything we get until that point is content.
220
+ # TODO: Must impose a content-size limit, and also must implement chunking.
221
+ # Also, must support either temporary files for large content, or calling
222
+ # a content-consumer block supplied by the user.
223
+ if @content_length
224
+ bytes_needed = @content_length - @content.length
225
+ @content += data[0, bytes_needed]
226
+ data = data[bytes_needed..-1] || ""
227
+ if @content_length == @content.length
228
+ dispatch_response
229
+ @read_state = :base
230
+ end
231
+ else
232
+ @content << data
233
+ data = ""
234
+ end
235
+ end
236
+ end
237
+ end
238
+
239
+
240
+ # We get called here when we have received an HTTP response line.
241
+ # It's an opportunity to throw an exception or trigger other exceptional
242
+ # handling.
243
+ def parse_response_line
244
+ if @headers.first =~ /\AHTTP\/1\.[01] ([\d]{3})/
245
+ @status = $1.to_i
246
+ else
247
+ set_deferred_status :failed, {
248
+ :status => 0 # crappy way of signifying an unrecognized response. TODO, find a better way to do this.
249
+ }
250
+ close_connection
251
+ end
252
+ end
253
+ private :parse_response_line
254
+
255
+ def dispatch_response
256
+ @read_state = :base
257
+ set_deferred_status :succeeded, {
258
+ :content => @content,
259
+ :headers => @headers,
260
+ :status => @status
261
+ }
262
+ # TODO, we close the connection for now, but this is wrong for persistent clients.
263
+ close_connection
264
+ end
265
+
266
+ def unbind
267
+ if !@connected
268
+ set_deferred_status :failed, {:status => 0} # YECCCCH. Find a better way to signal no-connect/network error.
269
+ elsif (@read_state == :content and @content_length == nil)
270
+ dispatch_response
271
+ end
272
+ end
273
+ end
274
+
275
+ end
276
+ end