eventmachine 0.12.10-x86-mswin32-60 → 1.0.0.beta.2-x86-mswin32-60
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile +1 -0
- data/README +80 -81
- data/Rakefile +7 -370
- data/docs/COPYING +60 -60
- data/docs/ChangeLog +211 -211
- data/docs/DEFERRABLES +246 -133
- data/docs/EPOLL +141 -141
- data/docs/GNU +281 -281
- data/docs/INSTALL +13 -13
- data/docs/KEYBOARD +42 -38
- data/docs/LEGAL +25 -25
- data/docs/LIGHTWEIGHT_CONCURRENCY +130 -70
- data/docs/PURE_RUBY +75 -75
- data/docs/RELEASE_NOTES +94 -94
- data/docs/SMTP +4 -2
- data/docs/SPAWNED_PROCESSES +148 -89
- data/docs/TODO +8 -8
- data/eventmachine.gemspec +19 -26
- data/examples/ex_channel.rb +42 -42
- data/examples/ex_queue.rb +2 -2
- data/examples/ex_tick_loop_array.rb +15 -0
- data/examples/ex_tick_loop_counter.rb +32 -0
- data/examples/helper.rb +1 -1
- data/ext/binder.cpp +0 -1
- data/ext/cmain.cpp +36 -25
- data/ext/ed.cpp +104 -113
- data/ext/ed.h +24 -30
- data/ext/em.cpp +349 -283
- data/ext/em.h +25 -29
- data/ext/eventmachine.h +5 -4
- data/ext/extconf.rb +58 -49
- data/ext/fastfilereader/extconf.rb +5 -3
- data/ext/fastfilereader/mapper.cpp +214 -214
- data/ext/fastfilereader/mapper.h +59 -59
- data/ext/fastfilereader/rubymain.cpp +127 -127
- data/ext/kb.cpp +1 -3
- data/ext/page.cpp +107 -107
- data/ext/page.h +51 -51
- data/ext/pipe.cpp +9 -11
- data/ext/project.h +12 -8
- data/ext/rubymain.cpp +138 -104
- data/java/.classpath +8 -8
- data/java/.project +17 -17
- data/java/src/com/rubyeventmachine/EmReactor.java +1 -0
- data/java/src/com/rubyeventmachine/EmReactorException.java +40 -40
- data/lib/em/buftok.rb +138 -138
- data/lib/em/callback.rb +25 -25
- data/lib/em/channel.rb +1 -1
- data/lib/em/connection.rb +6 -1
- data/lib/em/deferrable.rb +16 -2
- data/lib/em/file_watch.rb +53 -53
- data/lib/em/future.rb +61 -61
- data/lib/em/iterator.rb +270 -0
- data/lib/em/messages.rb +66 -66
- data/lib/em/process_watch.rb +43 -43
- data/lib/em/protocols.rb +1 -1
- data/lib/em/protocols/header_and_content.rb +138 -138
- data/lib/em/protocols/httpclient.rb +267 -262
- data/lib/em/protocols/line_protocol.rb +28 -0
- data/lib/em/protocols/memcache.rb +322 -322
- data/lib/em/protocols/postgres3.rb +247 -247
- data/lib/em/protocols/saslauth.rb +175 -175
- data/lib/em/protocols/smtpserver.rb +640 -547
- data/lib/em/protocols/stomp.rb +200 -200
- data/lib/em/protocols/tcptest.rb +52 -52
- data/lib/{pr_eventmachine.rb → em/pure_ruby.rb} +1013 -1022
- data/lib/em/queue.rb +1 -0
- data/lib/em/spawnable.rb +85 -85
- data/lib/em/streamer.rb +130 -130
- data/lib/em/tick_loop.rb +85 -0
- data/lib/em/timers.rb +2 -1
- data/lib/em/version.rb +1 -1
- data/lib/eventmachine.rb +40 -84
- data/lib/jeventmachine.rb +2 -1
- data/lib/rubyeventmachine.rb +2 -0
- data/setup.rb +1585 -1585
- data/tasks/doc.rake +30 -0
- data/tasks/package.rake +85 -0
- data/tasks/test.rake +6 -0
- data/tests/client.crt +31 -31
- data/tests/client.key +51 -51
- data/tests/test_attach.rb +13 -3
- data/tests/test_basic.rb +60 -95
- data/tests/test_channel.rb +3 -2
- data/tests/test_defer.rb +49 -47
- data/tests/test_deferrable.rb +35 -0
- data/tests/test_error_handler.rb +35 -35
- data/tests/test_errors.rb +82 -82
- data/tests/test_exc.rb +55 -55
- data/tests/test_file_watch.rb +49 -49
- data/tests/test_futures.rb +198 -198
- data/tests/test_handler_check.rb +36 -36
- data/tests/test_hc.rb +190 -218
- data/tests/test_httpclient.rb +227 -218
- data/tests/test_httpclient2.rb +3 -2
- data/tests/test_inactivity_timeout.rb +3 -3
- data/tests/test_kb.rb +60 -60
- data/tests/test_ltp.rb +13 -5
- data/tests/test_ltp2.rb +317 -317
- data/tests/test_next_tick.rb +1 -1
- data/tests/test_object_protocol.rb +36 -36
- data/tests/test_pending_connect_timeout.rb +2 -2
- data/tests/test_process_watch.rb +50 -48
- data/tests/test_proxy_connection.rb +52 -0
- data/tests/test_pure.rb +134 -125
- data/tests/test_queue.rb +44 -44
- data/tests/test_running.rb +42 -42
- data/tests/test_sasl.rb +72 -72
- data/tests/test_send_file.rb +251 -242
- data/tests/test_servers.rb +76 -76
- data/tests/test_smtpclient.rb +83 -83
- data/tests/test_smtpserver.rb +85 -85
- data/tests/test_spawn.rb +322 -322
- data/tests/test_ssl_methods.rb +49 -49
- data/tests/test_ssl_verify.rb +82 -82
- data/tests/test_tick_loop.rb +59 -0
- data/tests/test_timers.rb +13 -15
- data/tests/test_ud.rb +36 -36
- data/tests/testem.rb +31 -31
- metadata +66 -51
- data/ext/cplusplus.cpp +0 -202
- data/ext/emwin.cpp +0 -300
- data/ext/emwin.h +0 -94
- data/ext/epoll.cpp +0 -26
- data/ext/epoll.h +0 -25
- data/ext/eventmachine_cpp.h +0 -96
- data/ext/files.cpp +0 -94
- data/ext/files.h +0 -65
- data/ext/sigs.cpp +0 -89
- data/ext/sigs.h +0 -32
- data/java/src/com/rubyeventmachine/application/Application.java +0 -194
- data/java/src/com/rubyeventmachine/application/Connection.java +0 -74
- data/java/src/com/rubyeventmachine/application/ConnectionFactory.java +0 -37
- data/java/src/com/rubyeventmachine/application/DefaultConnectionFactory.java +0 -46
- data/java/src/com/rubyeventmachine/application/PeriodicTimer.java +0 -38
- data/java/src/com/rubyeventmachine/application/Timer.java +0 -54
- data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +0 -109
- data/java/src/com/rubyeventmachine/tests/ConnectTest.java +0 -148
- data/java/src/com/rubyeventmachine/tests/EMTest.java +0 -80
- data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +0 -53
- data/java/src/com/rubyeventmachine/tests/TestServers.java +0 -75
- data/java/src/com/rubyeventmachine/tests/TestTimers.java +0 -90
- data/lib/evma.rb +0 -32
- data/lib/evma/callback.rb +0 -32
- data/lib/evma/container.rb +0 -75
- data/lib/evma/factory.rb +0 -77
- data/lib/evma/protocol.rb +0 -87
- data/lib/evma/reactor.rb +0 -48
- data/web/whatis +0 -7
@@ -1,263 +1,268 @@
|
|
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
|
-
#
|
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
|
-
# content-length
|
195
|
-
@content_length
|
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
|
-
|
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
|
+
# Allow custom HTTP headers, e.g. SOAPAction
|
146
|
+
args[:custom_headers].each do |k,v|
|
147
|
+
req << "#{k}: #{v}"
|
148
|
+
end if args[:custom_headers]
|
149
|
+
|
150
|
+
# Basic-auth stanza contributed by Matt Murphy.
|
151
|
+
if args[:basic_auth]
|
152
|
+
basic_auth_string = ["#{args[:basic_auth][:username]}:#{args[:basic_auth][:password]}"].pack('m').strip.gsub(/\n/,'')
|
153
|
+
req << "Authorization: Basic #{basic_auth_string}"
|
154
|
+
end
|
155
|
+
|
156
|
+
req << ""
|
157
|
+
reqstring = req.map {|l| "#{l}\r\n"}.join
|
158
|
+
send_data reqstring
|
159
|
+
|
160
|
+
if verb == "POST" || verb == "PUT"
|
161
|
+
send_data postcontent
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
|
166
|
+
def receive_data data
|
167
|
+
while data and data.length > 0
|
168
|
+
case @read_state
|
169
|
+
when :base
|
170
|
+
# Perform any per-request initialization here and don't consume any data.
|
171
|
+
@data = ""
|
172
|
+
@headers = []
|
173
|
+
@content_length = nil # not zero
|
174
|
+
@content = ""
|
175
|
+
@status = nil
|
176
|
+
@read_state = :header
|
177
|
+
@connection_close = nil
|
178
|
+
when :header
|
179
|
+
ary = data.split( /\r?\n/m, 2 )
|
180
|
+
if ary.length == 2
|
181
|
+
data = ary.last
|
182
|
+
if ary.first == ""
|
183
|
+
if (@content_length and @content_length > 0) || @connection_close
|
184
|
+
@read_state = :content
|
185
|
+
else
|
186
|
+
dispatch_response
|
187
|
+
@read_state = :base
|
188
|
+
end
|
189
|
+
else
|
190
|
+
@headers << ary.first
|
191
|
+
if @headers.length == 1
|
192
|
+
parse_response_line
|
193
|
+
elsif ary.first =~ /\Acontent-length:\s*/i
|
194
|
+
# Only take the FIRST content-length header that appears,
|
195
|
+
# which we can distinguish because @content_length is nil.
|
196
|
+
# TODO, it's actually a fatal error if there is more than one
|
197
|
+
# content-length header, because the caller is presumptively
|
198
|
+
# a bad guy. (There is an exploit that depends on multiple
|
199
|
+
# content-length headers.)
|
200
|
+
@content_length ||= $'.to_i
|
201
|
+
elsif ary.first =~ /\Aconnection:\s*close/i
|
202
|
+
@connection_close = true
|
203
|
+
end
|
204
|
+
end
|
205
|
+
else
|
206
|
+
@data << data
|
207
|
+
data = ""
|
208
|
+
end
|
209
|
+
when :content
|
210
|
+
# If there was no content-length header, we have to wait until the connection
|
211
|
+
# closes. Everything we get until that point is content.
|
212
|
+
# TODO: Must impose a content-size limit, and also must implement chunking.
|
213
|
+
# Also, must support either temporary files for large content, or calling
|
214
|
+
# a content-consumer block supplied by the user.
|
215
|
+
if @content_length
|
216
|
+
bytes_needed = @content_length - @content.length
|
217
|
+
@content += data[0, bytes_needed]
|
218
|
+
data = data[bytes_needed..-1] || ""
|
219
|
+
if @content_length == @content.length
|
220
|
+
dispatch_response
|
221
|
+
@read_state = :base
|
222
|
+
end
|
223
|
+
else
|
224
|
+
@content << data
|
225
|
+
data = ""
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
|
232
|
+
# We get called here when we have received an HTTP response line.
|
233
|
+
# It's an opportunity to throw an exception or trigger other exceptional
|
234
|
+
# handling.
|
235
|
+
def parse_response_line
|
236
|
+
if @headers.first =~ /\AHTTP\/1\.[01] ([\d]{3})/
|
237
|
+
@status = $1.to_i
|
238
|
+
else
|
239
|
+
set_deferred_status :failed, {
|
240
|
+
:status => 0 # crappy way of signifying an unrecognized response. TODO, find a better way to do this.
|
241
|
+
}
|
242
|
+
close_connection
|
243
|
+
end
|
244
|
+
end
|
245
|
+
private :parse_response_line
|
246
|
+
|
247
|
+
def dispatch_response
|
248
|
+
@read_state = :base
|
249
|
+
set_deferred_status :succeeded, {
|
250
|
+
:content => @content,
|
251
|
+
:headers => @headers,
|
252
|
+
:status => @status
|
253
|
+
}
|
254
|
+
# TODO, we close the connection for now, but this is wrong for persistent clients.
|
255
|
+
close_connection
|
256
|
+
end
|
257
|
+
|
258
|
+
def unbind
|
259
|
+
if !@connected
|
260
|
+
set_deferred_status :failed, {:status => 0} # YECCCCH. Find a better way to signal no-connect/network error.
|
261
|
+
elsif (@read_state == :content and @content_length == nil)
|
262
|
+
dispatch_response
|
263
|
+
end
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
end
|
263
268
|
end
|