summaryrefslogtreecommitdiff
path: root/sample/drb/http0.rb
diff options
context:
space:
mode:
authorseki <seki@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-02-14 02:28:02 +0000
committerseki <seki@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-02-14 02:28:02 +0000
commita686414ef56dc289d28d96173c1fe5ab8ce0ac15 (patch)
treead272130eda4acb6fd95dc13919257f1629ba262 /sample/drb/http0.rb
parent27669906241b4b965ab20068f773b878c8ed2598 (diff)
import drb/sample
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@5698 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'sample/drb/http0.rb')
-rw-r--r--sample/drb/http0.rb77
1 files changed, 77 insertions, 0 deletions
diff --git a/sample/drb/http0.rb b/sample/drb/http0.rb
new file mode 100644
index 0000000000..7649925282
--- /dev/null
+++ b/sample/drb/http0.rb
@@ -0,0 +1,77 @@
+require 'drb/drb'
+require 'net/http'
+require 'uri'
+
+module DRb
+ module HTTP0
+ class StrStream
+ def initialize(str='')
+ @buf = str
+ end
+ attr_reader :buf
+
+ def read(n)
+ begin
+ return @buf[0,n]
+ ensure
+ @buf[0,n] = ''
+ end
+ end
+
+ def write(s)
+ @buf.concat s
+ end
+ end
+
+ def self.uri_option(uri, config)
+ return uri, nil
+ end
+
+ def self.open(uri, config)
+ unless /^http:/ =~ uri
+ raise(DRbBadScheme, uri) unless uri =~ /^http:/
+ raise(DRbBadURI, 'can\'t parse uri:' + uri)
+ end
+ ClientSide.new(uri, config)
+ end
+
+ class ClientSide
+ def initialize(uri, config)
+ @uri = uri
+ @res = nil
+ @config = config
+ @msg = DRbMessage.new(config)
+ @proxy = ENV['HTTP_PROXY']
+ end
+
+ def close; end
+ def alive?; false; end
+
+ def send_request(ref, msg_id, *arg, &b)
+ stream = StrStream.new
+ @msg.send_request(stream, ref, msg_id, *arg, &b)
+ @reply_stream = StrStream.new
+ post(@uri, stream.buf)
+ end
+
+ def recv_reply
+ @msg.recv_reply(@reply_stream)
+ end
+
+ def post(url, data)
+ it = URI.parse(url)
+ path = [(it.path=='' ? '/' : it.path), it.query].compact.join('?')
+ http = Net::HTTP.new(it.host, it.port)
+ sio = StrStream.new
+ http.post(path, data, {'Content-Type'=>'application/octetstream;'}) do |str|
+ sio.write(str)
+ if @config[:load_limit] < sio.buf.size
+ raise TypeError, 'too large packet'
+ end
+ end
+ @reply_stream = sio
+ end
+ end
+ end
+ DRbProtocol.add_protocol(HTTP0)
+end