2004-02-13 21:28:02 -05:00
|
|
|
require 'drb/drb'
|
|
|
|
require 'net/http'
|
|
|
|
require 'uri'
|
|
|
|
|
|
|
|
module DRb
|
|
|
|
module HTTP0
|
|
|
|
class StrStream
|
|
|
|
def initialize(str='')
|
2013-07-20 09:41:02 -04:00
|
|
|
@buf = str
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
attr_reader :buf
|
|
|
|
|
|
|
|
def read(n)
|
2013-07-20 09:41:02 -04:00
|
|
|
begin
|
|
|
|
return @buf[0,n]
|
|
|
|
ensure
|
|
|
|
@buf[0,n] = ''
|
|
|
|
end
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def write(s)
|
2013-07-20 09:41:02 -04:00
|
|
|
@buf.concat s
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.uri_option(uri, config)
|
|
|
|
return uri, nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.open(uri, config)
|
|
|
|
unless /^http:/ =~ uri
|
2013-07-20 09:41:02 -04:00
|
|
|
raise(DRbBadScheme, uri) unless uri =~ /^http:/
|
|
|
|
raise(DRbBadURI, 'can\'t parse uri:' + uri)
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
ClientSide.new(uri, config)
|
|
|
|
end
|
|
|
|
|
|
|
|
class ClientSide
|
|
|
|
def initialize(uri, config)
|
2013-07-20 09:41:02 -04:00
|
|
|
@uri = uri
|
|
|
|
@res = nil
|
|
|
|
@config = config
|
|
|
|
@msg = DRbMessage.new(config)
|
|
|
|
@proxy = ENV['HTTP_PROXY']
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def close; end
|
|
|
|
def alive?; false; end
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2004-02-13 21:28:02 -05:00
|
|
|
def send_request(ref, msg_id, *arg, &b)
|
2013-07-20 09:41:02 -04:00
|
|
|
stream = StrStream.new
|
|
|
|
@msg.send_request(stream, ref, msg_id, *arg, &b)
|
|
|
|
@reply_stream = StrStream.new
|
|
|
|
post(@uri, stream.buf)
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2004-02-13 21:28:02 -05:00
|
|
|
def recv_reply
|
2013-07-20 09:41:02 -04:00
|
|
|
@msg.recv_reply(@reply_stream)
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def post(url, data)
|
2013-07-20 09:41:02 -04:00
|
|
|
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
|
2004-02-13 21:28:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
DRbProtocol.add_protocol(HTTP0)
|
|
|
|
end
|