1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00
puma--puma/lib/puma/minissl.rb

141 lines
2.5 KiB
Ruby
Raw Normal View History

2013-03-18 19:20:17 -04:00
module Puma
module MiniSSL
class Socket
def initialize(socket, engine)
@socket = socket
@engine = engine
end
2012-08-22 19:53:25 -04:00
2013-03-18 19:20:17 -04:00
def to_io
@socket
end
2012-08-22 19:53:25 -04:00
2013-03-18 19:20:17 -04:00
def readpartial(size)
while true
output = @engine.read
return output if output
2012-08-22 19:53:25 -04:00
2013-03-18 19:20:17 -04:00
data = @socket.readpartial(size)
@engine.inject(data)
output = @engine.read
2012-08-23 01:34:10 -04:00
2013-03-18 19:20:17 -04:00
return output if output
2012-08-23 01:34:10 -04:00
2013-03-18 19:20:17 -04:00
while neg_data = @engine.extract
@socket.write neg_data
end
2012-08-23 01:34:10 -04:00
end
end
2013-03-18 19:20:17 -04:00
def read_nonblock(size)
while true
output = @engine.read
return output if output
2012-08-23 01:34:10 -04:00
2013-03-18 19:20:17 -04:00
data = @socket.read_nonblock(size)
2012-08-23 01:34:10 -04:00
2013-03-18 19:20:17 -04:00
@engine.inject(data)
output = @engine.read
2012-08-22 19:53:25 -04:00
2013-03-18 19:20:17 -04:00
return output if output
2012-08-22 19:53:25 -04:00
2013-03-18 19:20:17 -04:00
while neg_data = @engine.extract
@socket.write neg_data
end
2012-08-22 19:53:25 -04:00
end
end
2013-03-18 19:20:17 -04:00
def write(data)
need = data.bytesize
2012-08-22 19:53:25 -04:00
2013-03-18 19:20:17 -04:00
while true
wrote = @engine.write data
enc = @engine.extract
2012-08-22 19:53:25 -04:00
while enc
@socket.write enc
enc = @engine.extract
2013-03-18 19:20:17 -04:00
end
2012-08-22 19:53:25 -04:00
2013-03-18 19:20:17 -04:00
need -= wrote
2012-08-22 19:53:25 -04:00
return data.bytesize if need == 0
2012-08-22 19:53:25 -04:00
2013-03-18 19:20:17 -04:00
data = data[need..-1]
end
2012-08-22 19:53:25 -04:00
end
2013-03-18 19:20:17 -04:00
alias_method :syswrite, :write
2012-09-10 11:50:43 -04:00
2013-03-18 19:20:17 -04:00
def flush
@socket.flush
end
2012-08-23 00:43:40 -04:00
2013-03-18 19:20:17 -04:00
def close
@socket.close
end
2012-08-23 00:43:40 -04:00
2013-03-18 19:20:17 -04:00
def peeraddr
@socket.peeraddr
end
2012-08-23 00:43:40 -04:00
end
2012-08-22 19:53:25 -04:00
2013-03-18 19:20:17 -04:00
class Context
attr_accessor :verify_mode
2013-03-18 19:20:17 -04:00
attr_reader :key
attr_reader :cert
2013-03-18 19:20:17 -04:00
def key=(key)
raise ArgumentError, "No such key file '#{key}'" unless File.exist? key
@key = key
end
2013-03-18 19:20:17 -04:00
def cert=(cert)
raise ArgumentError, "No such cert file '#{cert}'" unless File.exist? cert
@cert = cert
end
end
2012-08-23 00:43:40 -04:00
2013-03-18 19:20:17 -04:00
VERIFY_NONE = 0
VERIFY_PEER = 1
2012-08-23 00:43:40 -04:00
2013-03-18 19:20:17 -04:00
#if defined?(JRUBY_VERSION)
2012-08-23 22:50:01 -04:00
#class Engine
2013-03-18 19:20:17 -04:00
#def self.server(key, cert)
#new(key, cert)
#end
#end
2012-08-23 22:50:01 -04:00
#end
2012-08-23 19:56:37 -04:00
2013-03-18 19:20:17 -04:00
class Server
def initialize(socket, ctx)
@socket = socket
@ctx = ctx
end
2012-08-22 19:53:25 -04:00
2013-03-18 19:20:17 -04:00
def to_io
@socket
end
2012-08-22 19:53:25 -04:00
2013-03-18 19:20:17 -04:00
def accept
io = @socket.accept
engine = Engine.server @ctx.key, @ctx.cert
2012-08-22 19:53:25 -04:00
2013-03-18 19:20:17 -04:00
Socket.new io, engine
end
2012-08-23 00:43:40 -04:00
2013-03-18 19:20:17 -04:00
def accept_nonblock
io = @socket.accept_nonblock
engine = Engine.server @ctx.key, @ctx.cert
2012-09-10 11:50:43 -04:00
2013-03-18 19:20:17 -04:00
Socket.new io, engine
end
2012-09-10 11:50:43 -04:00
2013-03-18 19:20:17 -04:00
def close
@socket.close
end
2012-08-23 00:43:40 -04:00
end
2012-08-22 19:53:25 -04:00
end
end