smparkes-eventmachine 0.12.10
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +15 -0
- data/README +81 -0
- data/Rakefile +374 -0
- data/docs/COPYING +60 -0
- data/docs/ChangeLog +211 -0
- data/docs/DEFERRABLES +133 -0
- data/docs/EPOLL +141 -0
- data/docs/GNU +281 -0
- data/docs/INSTALL +13 -0
- data/docs/KEYBOARD +38 -0
- data/docs/LEGAL +25 -0
- data/docs/LIGHTWEIGHT_CONCURRENCY +70 -0
- data/docs/PURE_RUBY +75 -0
- data/docs/RELEASE_NOTES +94 -0
- data/docs/SMTP +2 -0
- data/docs/SPAWNED_PROCESSES +89 -0
- data/docs/TODO +8 -0
- data/eventmachine.gemspec +40 -0
- data/examples/ex_channel.rb +43 -0
- data/examples/ex_queue.rb +2 -0
- data/examples/helper.rb +2 -0
- data/ext/binder.cpp +125 -0
- data/ext/binder.h +46 -0
- data/ext/cmain.cpp +827 -0
- data/ext/cplusplus.cpp +202 -0
- data/ext/ed.cpp +1901 -0
- data/ext/ed.h +424 -0
- data/ext/em.cpp +2288 -0
- data/ext/em.h +229 -0
- data/ext/emwin.cpp +300 -0
- data/ext/emwin.h +94 -0
- data/ext/epoll.cpp +26 -0
- data/ext/epoll.h +25 -0
- data/ext/eventmachine.h +122 -0
- data/ext/eventmachine_cpp.h +96 -0
- data/ext/extconf.rb +150 -0
- data/ext/fastfilereader/extconf.rb +85 -0
- data/ext/fastfilereader/mapper.cpp +214 -0
- data/ext/fastfilereader/mapper.h +59 -0
- data/ext/fastfilereader/rubymain.cpp +127 -0
- data/ext/files.cpp +94 -0
- data/ext/files.h +65 -0
- data/ext/kb.cpp +81 -0
- data/ext/page.cpp +107 -0
- data/ext/page.h +51 -0
- data/ext/pipe.cpp +349 -0
- data/ext/project.h +156 -0
- data/ext/rubymain.cpp +1194 -0
- data/ext/sigs.cpp +89 -0
- data/ext/sigs.h +32 -0
- data/ext/ssl.cpp +460 -0
- data/ext/ssl.h +94 -0
- data/java/.classpath +8 -0
- data/java/.project +17 -0
- data/java/src/com/rubyeventmachine/EmReactor.java +570 -0
- data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
- data/java/src/com/rubyeventmachine/EventableChannel.java +69 -0
- data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +189 -0
- data/java/src/com/rubyeventmachine/EventableSocketChannel.java +364 -0
- data/java/src/com/rubyeventmachine/application/Application.java +194 -0
- data/java/src/com/rubyeventmachine/application/Connection.java +74 -0
- data/java/src/com/rubyeventmachine/application/ConnectionFactory.java +37 -0
- data/java/src/com/rubyeventmachine/application/DefaultConnectionFactory.java +46 -0
- data/java/src/com/rubyeventmachine/application/PeriodicTimer.java +38 -0
- data/java/src/com/rubyeventmachine/application/Timer.java +54 -0
- data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +109 -0
- data/java/src/com/rubyeventmachine/tests/ConnectTest.java +148 -0
- data/java/src/com/rubyeventmachine/tests/EMTest.java +80 -0
- data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +53 -0
- data/java/src/com/rubyeventmachine/tests/TestServers.java +75 -0
- data/java/src/com/rubyeventmachine/tests/TestTimers.java +90 -0
- data/lib/em/buftok.rb +138 -0
- data/lib/em/callback.rb +26 -0
- data/lib/em/channel.rb +57 -0
- data/lib/em/connection.rb +564 -0
- data/lib/em/deferrable.rb +192 -0
- data/lib/em/file_watch.rb +54 -0
- data/lib/em/future.rb +61 -0
- data/lib/em/messages.rb +66 -0
- data/lib/em/process_watch.rb +44 -0
- data/lib/em/processes.rb +119 -0
- data/lib/em/protocols/header_and_content.rb +138 -0
- data/lib/em/protocols/httpclient.rb +263 -0
- data/lib/em/protocols/httpclient2.rb +590 -0
- data/lib/em/protocols/line_and_text.rb +125 -0
- data/lib/em/protocols/linetext2.rb +161 -0
- data/lib/em/protocols/memcache.rb +323 -0
- data/lib/em/protocols/object_protocol.rb +45 -0
- data/lib/em/protocols/postgres3.rb +247 -0
- data/lib/em/protocols/saslauth.rb +175 -0
- data/lib/em/protocols/smtpclient.rb +357 -0
- data/lib/em/protocols/smtpserver.rb +547 -0
- data/lib/em/protocols/socks4.rb +66 -0
- data/lib/em/protocols/stomp.rb +200 -0
- data/lib/em/protocols/tcptest.rb +53 -0
- data/lib/em/protocols.rb +36 -0
- data/lib/em/queue.rb +61 -0
- data/lib/em/spawnable.rb +85 -0
- data/lib/em/streamer.rb +130 -0
- data/lib/em/timers.rb +56 -0
- data/lib/em/version.rb +3 -0
- data/lib/eventmachine.rb +1592 -0
- data/lib/evma/callback.rb +32 -0
- data/lib/evma/container.rb +75 -0
- data/lib/evma/factory.rb +77 -0
- data/lib/evma/protocol.rb +87 -0
- data/lib/evma/reactor.rb +48 -0
- data/lib/evma.rb +32 -0
- data/lib/jeventmachine.rb +257 -0
- data/lib/pr_eventmachine.rb +1022 -0
- data/setup.rb +1585 -0
- data/tasks/cpp.rake_example +77 -0
- data/tests/client.crt +31 -0
- data/tests/client.key +51 -0
- data/tests/test_attach.rb +126 -0
- data/tests/test_basic.rb +284 -0
- data/tests/test_channel.rb +63 -0
- data/tests/test_connection_count.rb +35 -0
- data/tests/test_defer.rb +47 -0
- data/tests/test_epoll.rb +160 -0
- data/tests/test_error_handler.rb +35 -0
- data/tests/test_errors.rb +82 -0
- data/tests/test_exc.rb +55 -0
- data/tests/test_file_watch.rb +49 -0
- data/tests/test_futures.rb +198 -0
- data/tests/test_get_sock_opt.rb +30 -0
- data/tests/test_handler_check.rb +37 -0
- data/tests/test_hc.rb +218 -0
- data/tests/test_httpclient.rb +218 -0
- data/tests/test_httpclient2.rb +153 -0
- data/tests/test_inactivity_timeout.rb +50 -0
- data/tests/test_kb.rb +60 -0
- data/tests/test_ltp.rb +182 -0
- data/tests/test_ltp2.rb +317 -0
- data/tests/test_next_tick.rb +133 -0
- data/tests/test_object_protocol.rb +37 -0
- data/tests/test_pause.rb +70 -0
- data/tests/test_pending_connect_timeout.rb +48 -0
- data/tests/test_process_watch.rb +48 -0
- data/tests/test_processes.rb +128 -0
- data/tests/test_proxy_connection.rb +92 -0
- data/tests/test_pure.rb +125 -0
- data/tests/test_queue.rb +44 -0
- data/tests/test_running.rb +42 -0
- data/tests/test_sasl.rb +72 -0
- data/tests/test_send_file.rb +242 -0
- data/tests/test_servers.rb +76 -0
- data/tests/test_smtpclient.rb +83 -0
- data/tests/test_smtpserver.rb +85 -0
- data/tests/test_spawn.rb +322 -0
- data/tests/test_ssl_args.rb +79 -0
- data/tests/test_ssl_methods.rb +50 -0
- data/tests/test_ssl_verify.rb +82 -0
- data/tests/test_timers.rb +162 -0
- data/tests/test_ud.rb +36 -0
- data/tests/testem.rb +31 -0
- data/web/whatis +7 -0
- metadata +237 -0
@@ -0,0 +1,263 @@
|
|
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 => ''
|
70
|
+
def self.request( args = {} )
|
71
|
+
args[:port] ||= 80
|
72
|
+
EventMachine.connect( args[:host], args[:port], self ) {|c|
|
73
|
+
# According to the docs, we will get here AFTER post_init is called.
|
74
|
+
c.instance_eval {@args = args}
|
75
|
+
}
|
76
|
+
end
|
77
|
+
|
78
|
+
def post_init
|
79
|
+
@start_time = Time.now
|
80
|
+
@data = ""
|
81
|
+
@read_state = :base
|
82
|
+
end
|
83
|
+
|
84
|
+
# We send the request when we get a connection.
|
85
|
+
# AND, we set an instance variable to indicate we passed through here.
|
86
|
+
# That allows #unbind to know whether there was a successful connection.
|
87
|
+
# NB: This naive technique won't work when we have to support multiple
|
88
|
+
# requests on a single connection.
|
89
|
+
def connection_completed
|
90
|
+
@connected = true
|
91
|
+
send_request @args
|
92
|
+
end
|
93
|
+
|
94
|
+
def send_request args
|
95
|
+
args[:verb] ||= args[:method] # Support :method as an alternative to :verb.
|
96
|
+
args[:verb] ||= :get # IS THIS A GOOD IDEA, to default to GET if nothing was specified?
|
97
|
+
|
98
|
+
verb = args[:verb].to_s.upcase
|
99
|
+
unless ["GET", "POST", "PUT", "DELETE", "HEAD"].include?(verb)
|
100
|
+
set_deferred_status :failed, {:status => 0} # TODO, not signalling the error type
|
101
|
+
return # NOTE THE EARLY RETURN, we're not sending any data.
|
102
|
+
end
|
103
|
+
|
104
|
+
request = args[:request] || "/"
|
105
|
+
unless request[0,1] == "/"
|
106
|
+
request = "/" + request
|
107
|
+
end
|
108
|
+
|
109
|
+
qs = args[:query_string] || ""
|
110
|
+
if qs.length > 0 and qs[0,1] != '?'
|
111
|
+
qs = "?" + qs
|
112
|
+
end
|
113
|
+
|
114
|
+
version = args[:version] || "1.1"
|
115
|
+
|
116
|
+
# Allow an override for the host header if it's not the connect-string.
|
117
|
+
host = args[:host_header] || args[:host] || "_"
|
118
|
+
# For now, ALWAYS tuck in the port string, although we may want to omit it if it's the default.
|
119
|
+
port = args[:port]
|
120
|
+
|
121
|
+
# POST items.
|
122
|
+
postcontenttype = args[:contenttype] || "application/octet-stream"
|
123
|
+
postcontent = args[:content] || ""
|
124
|
+
raise "oversized content in HTTP POST" if postcontent.length > MaxPostContentLength
|
125
|
+
|
126
|
+
# ESSENTIAL for the request's line-endings to be CRLF, not LF. Some servers misbehave otherwise.
|
127
|
+
# TODO: We ASSUME the caller wants to send a 1.1 request. May not be a good assumption.
|
128
|
+
req = [
|
129
|
+
"#{verb} #{request}#{qs} HTTP/#{version}",
|
130
|
+
"Host: #{host}:#{port}",
|
131
|
+
"User-agent: Ruby EventMachine",
|
132
|
+
]
|
133
|
+
|
134
|
+
if verb == "POST" || verb == "PUT"
|
135
|
+
req << "Content-type: #{postcontenttype}"
|
136
|
+
req << "Content-length: #{postcontent.length}"
|
137
|
+
end
|
138
|
+
|
139
|
+
# TODO, this cookie handler assumes it's getting a single, semicolon-delimited string.
|
140
|
+
# Eventually we will want to deal intelligently with arrays and hashes.
|
141
|
+
if args[:cookie]
|
142
|
+
req << "Cookie: #{args[:cookie]}"
|
143
|
+
end
|
144
|
+
|
145
|
+
# Basic-auth stanza contributed by Matt Murphy.
|
146
|
+
if args[:basic_auth]
|
147
|
+
basic_auth_string = ["#{args[:basic_auth][:username]}:#{args[:basic_auth][:password]}"].pack('m').strip.gsub(/\n/,'')
|
148
|
+
req << "Authorization: Basic #{basic_auth_string}"
|
149
|
+
end
|
150
|
+
|
151
|
+
req << ""
|
152
|
+
reqstring = req.map {|l| "#{l}\r\n"}.join
|
153
|
+
send_data reqstring
|
154
|
+
|
155
|
+
if verb == "POST" || verb == "PUT"
|
156
|
+
send_data postcontent
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
|
161
|
+
def receive_data data
|
162
|
+
while data and data.length > 0
|
163
|
+
case @read_state
|
164
|
+
when :base
|
165
|
+
# Perform any per-request initialization here and don't consume any data.
|
166
|
+
@data = ""
|
167
|
+
@headers = []
|
168
|
+
@content_length = nil # not zero
|
169
|
+
@content = ""
|
170
|
+
@status = nil
|
171
|
+
@read_state = :header
|
172
|
+
@connection_close = nil
|
173
|
+
when :header
|
174
|
+
ary = data.split( /\r?\n/m, 2 )
|
175
|
+
if ary.length == 2
|
176
|
+
data = ary.last
|
177
|
+
if ary.first == ""
|
178
|
+
if (@content_length and @content_length > 0) || @connection_close
|
179
|
+
@read_state = :content
|
180
|
+
else
|
181
|
+
dispatch_response
|
182
|
+
@read_state = :base
|
183
|
+
end
|
184
|
+
else
|
185
|
+
@headers << ary.first
|
186
|
+
if @headers.length == 1
|
187
|
+
parse_response_line
|
188
|
+
elsif ary.first =~ /\Acontent-length:\s*/i
|
189
|
+
# Only take the FIRST content-length header that appears,
|
190
|
+
# which we can distinguish because @content_length is nil.
|
191
|
+
# TODO, it's actually a fatal error if there is more than one
|
192
|
+
# content-length header, because the caller is presumptively
|
193
|
+
# a bad guy. (There is an exploit that depends on multiple
|
194
|
+
# content-length headers.)
|
195
|
+
@content_length ||= $'.to_i
|
196
|
+
elsif ary.first =~ /\Aconnection:\s*close/i
|
197
|
+
@connection_close = true
|
198
|
+
end
|
199
|
+
end
|
200
|
+
else
|
201
|
+
@data << data
|
202
|
+
data = ""
|
203
|
+
end
|
204
|
+
when :content
|
205
|
+
# If there was no content-length header, we have to wait until the connection
|
206
|
+
# closes. Everything we get until that point is content.
|
207
|
+
# TODO: Must impose a content-size limit, and also must implement chunking.
|
208
|
+
# Also, must support either temporary files for large content, or calling
|
209
|
+
# a content-consumer block supplied by the user.
|
210
|
+
if @content_length
|
211
|
+
bytes_needed = @content_length - @content.length
|
212
|
+
@content += data[0, bytes_needed]
|
213
|
+
data = data[bytes_needed..-1] || ""
|
214
|
+
if @content_length == @content.length
|
215
|
+
dispatch_response
|
216
|
+
@read_state = :base
|
217
|
+
end
|
218
|
+
else
|
219
|
+
@content << data
|
220
|
+
data = ""
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
|
227
|
+
# We get called here when we have received an HTTP response line.
|
228
|
+
# It's an opportunity to throw an exception or trigger other exceptional
|
229
|
+
# handling.
|
230
|
+
def parse_response_line
|
231
|
+
if @headers.first =~ /\AHTTP\/1\.[01] ([\d]{3})/
|
232
|
+
@status = $1.to_i
|
233
|
+
else
|
234
|
+
set_deferred_status :failed, {
|
235
|
+
:status => 0 # crappy way of signifying an unrecognized response. TODO, find a better way to do this.
|
236
|
+
}
|
237
|
+
close_connection
|
238
|
+
end
|
239
|
+
end
|
240
|
+
private :parse_response_line
|
241
|
+
|
242
|
+
def dispatch_response
|
243
|
+
@read_state = :base
|
244
|
+
set_deferred_status :succeeded, {
|
245
|
+
:content => @content,
|
246
|
+
:headers => @headers,
|
247
|
+
:status => @status
|
248
|
+
}
|
249
|
+
# TODO, we close the connection for now, but this is wrong for persistent clients.
|
250
|
+
close_connection
|
251
|
+
end
|
252
|
+
|
253
|
+
def unbind
|
254
|
+
if !@connected
|
255
|
+
set_deferred_status :failed, {:status => 0} # YECCCCH. Find a better way to signal no-connect/network error.
|
256
|
+
elsif (@read_state == :content and @content_length == nil)
|
257
|
+
dispatch_response
|
258
|
+
end
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
end
|
263
|
+
end
|