rev 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,22 +6,15 @@
6
6
 
7
7
  require 'openssl'
8
8
 
9
- # --
10
- # Rev implements SSL by subclassing OpenSSL::SSL::SSLSocket in C
11
- # and adding implementations for non-blocking versions of the
12
- # methods it provides. Unfortunately, this relies on hacks specific
13
- # to Ruby 1.9. If you'd like to find a workaround which is compatible
14
- # with Ruby 1.8, have a look at rev_ssl.c and find out if you can
15
- # properly initialize an OpenSSL::SSL::SSLSocket.
16
- # ++
17
- if RUBY_VERSION.gsub('.', '').to_i < 190
18
- raise LoadError, "Rev::SSL not supported in this Ruby version, sorry"
19
- end
20
-
21
9
  module Rev
22
- # Monkeypatch Rev::IO to include SSL support. This can be accomplished
23
- # by extending any Rev:IO (or subclass) object with Rev::SSL after the
24
- # connection has completed, e.g.
10
+ # The easiest way to add SSL support to your Rev applications is to use
11
+ # the SSLSocket class. However, the SSL module is provided for cases where
12
+ # you've already subclassed TCPSocket and want to optionally provide
13
+ # SSL support in that class.
14
+ #
15
+ # This module monkeypatches Rev::IO to include SSL support. This can be
16
+ # accomplished by extending any Rev:IO (or subclass) object with Rev::SSL
17
+ # after the connection has completed, e.g.
25
18
  #
26
19
  # class MySocket < Rev::TCPSocket
27
20
  # def on_connect
@@ -29,19 +22,18 @@ module Rev
29
22
  # ssl_client_start
30
23
  # end
31
24
  # end
32
- #
33
- # Please be aware that SSL is only supported on Ruby 1.9 at the present time.
34
25
  #
35
26
  module SSL
36
27
  # Start SSL explicitly in client mode. After calling this, callbacks
37
28
  # will fire for checking the peer certificate (ssl_peer_cert) and
38
29
  # its validity (ssl_verify_result)
39
30
  def ssl_client_start
40
- raise "ssl already started" if @ssl_socket
31
+ raise "ssl already started" if @_ssl_socket
41
32
 
42
33
  context = respond_to?(:ssl_context) ? ssl_context : OpenSSL::SSL::SSLContext.new
43
- @ssl_socket = SSL::IO.new(@io, context)
44
- @ssl_init = proc { @ssl_socket.connect_nonblock }
34
+
35
+ @_ssl_socket = SSL::IO.new(@_io, context)
36
+ @_ssl_init = proc { @_ssl_socket.connect_nonblock }
45
37
 
46
38
  ssl_init
47
39
  end
@@ -50,10 +42,10 @@ module Rev
50
42
  # will fire for checking the peer certificate (ssl_peer_cert) and
51
43
  # its validity (ssl_verify_result)
52
44
  def ssl_server_start
53
- raise "ssl already started" if @ssl_socket
45
+ raise "ssl already started" if @_ssl_socket
54
46
 
55
- @ssl_socket = SSL::IO.new(@io, ssl_context)
56
- @ssl_init = proc { @ssl_socket.accept_nonblock }
47
+ @_ssl_socket = SSL::IO.new(@_io, ssl_context)
48
+ @_ssl_init = proc { @_ssl_socket.accept_nonblock }
57
49
 
58
50
  ssl_init
59
51
  end
@@ -64,13 +56,13 @@ module Rev
64
56
 
65
57
  def ssl_init
66
58
  begin
67
- @ssl_init.call
59
+ @_ssl_init.call
68
60
  ssl_init_complete
69
61
  rescue SSL::IO::ReadAgain
70
62
  enable unless enabled?
71
63
  rescue SSL::IO::WriteAgain
72
64
  enable_write_watcher
73
- rescue OpenSSL::SSL::SSLError, Errno::ECONNRESET
65
+ rescue OpenSSL::SSL::SSLError, Errno::ECONNRESET, Errno::EPIPE
74
66
  close
75
67
  rescue => ex
76
68
  if respond_to? :on_ssl_error
@@ -81,24 +73,25 @@ module Rev
81
73
  end
82
74
 
83
75
  def ssl_init_complete
84
- @ssl_init = nil
76
+ @_ssl_init = nil
85
77
  enable unless enabled?
86
78
 
87
- on_peer_cert(@ssl_socket.peer_cert) if respond_to? :on_peer_cert
88
- on_ssl_result(@ssl_socket.verify_result) if respond_to? :on_ssl_result
79
+ on_peer_cert(@_ssl_socket.peer_cert) if respond_to? :on_peer_cert
80
+ # FIXME Rev::SSL::IO#verify_result needs to be adapted to non-blocking
81
+ #on_ssl_result(@_ssl_socket.verify_result) if respond_to? :on_ssl_result
89
82
  on_ssl_connect if respond_to? :on_ssl_connect
90
83
  end
91
84
 
92
85
  def on_readable
93
- if @ssl_init
86
+ if @_ssl_init
94
87
  disable
95
88
  ssl_init
96
89
  return
97
90
  end
98
91
 
99
92
  begin
100
- on_read @ssl_socket.read_nonblock(Rev::IO::INPUT_SIZE)
101
- rescue Errno::AGAIN, SSL::IO::ReadAgain
93
+ on_read @_ssl_socket.read_nonblock(Rev::IO::INPUT_SIZE)
94
+ rescue Errno::EAGAIN, SSL::IO::ReadAgain
102
95
  return
103
96
  rescue OpenSSL::SSL::SSLError, Errno::ECONNRESET, EOFError
104
97
  close
@@ -106,14 +99,14 @@ module Rev
106
99
  end
107
100
 
108
101
  def on_writable
109
- if @ssl_init
102
+ if @_ssl_init
110
103
  disable_write_watcher
111
104
  ssl_init
112
105
  return
113
106
  end
114
107
 
115
108
  begin
116
- nbytes = @ssl_socket.write_nonblock @write_buffer.to_str
109
+ nbytes = @_ssl_socket.write_nonblock @_write_buffer.to_str
117
110
  rescue Errno::EAGAIN, SSL::IO::WriteAgain
118
111
  return
119
112
  rescue OpenSSL::SSL::SSLError, Errno::EPIPE, Errno::ECONNRESET
@@ -121,29 +114,30 @@ module Rev
121
114
  return
122
115
  end
123
116
 
124
- @write_buffer.read(nbytes)
117
+ @_write_buffer.read(nbytes)
125
118
 
126
- if @write_buffer.empty?
119
+ if @_write_buffer.empty?
127
120
  disable_write_watcher
128
121
  on_write_complete
129
122
  end
130
123
  end
131
124
  end
132
125
 
133
- # A socket class for outgoing and incoming SSL connections. Please be aware
134
- # that SSL is only supported on Ruby 1.9 at the present time.
126
+ # A socket class for SSL connections
135
127
  class SSLSocket < TCPSocket
136
128
  # Perform a non-blocking connect to the given host and port
137
129
  def self.connect(addr, port, *args)
138
- super.instance_eval { @connecting = true; self }
130
+ sock = super
131
+ sock.instance_variable_set(:@_connecting, true)
132
+ sock
139
133
  end
140
134
 
141
135
  # Returns the OpenSSL::SSL::SSLContext for to use for the session.
142
136
  # By default no certificates will be checked. If you would like
143
137
  # any certificate checking to be performed, please override this
144
- # class and return a context loaded with the appropriate certificates.
138
+ # method and return a context loaded with the appropriate certificates.
145
139
  def ssl_context
146
- @ssl_context ||= OpenSSL::SSL::SSLContext.new
140
+ @_ssl_context ||= OpenSSL::SSL::SSLContext.new
147
141
  end
148
142
 
149
143
  # Called when SSL handshaking has successfully completed
@@ -170,15 +164,7 @@ module Rev
170
164
 
171
165
  def on_connect
172
166
  extend SSL
173
- @connecting ? ssl_client_start : ssl_server_start
174
- end
175
- end
176
-
177
- # A class for implementing SSL servers. Please be aware that SSL is only
178
- # supported on Ruby 1.9 at the present time.
179
- class SSLServer < TCPServer
180
- def initialize(host, port, klass = SSLSocket, *args, &block)
181
- super
167
+ @_connecting ? ssl_client_start : ssl_server_start
182
168
  end
183
169
  end
184
170
  end
@@ -11,6 +11,7 @@ module Rev
11
11
  # These can take a block and store it to be called when the event
12
12
  # is actually fired.
13
13
 
14
+ extend Meta
14
15
  event_callback :timer_watcher
15
16
  end
16
17
  end
@@ -2,10 +2,10 @@ require 'rubygems'
2
2
 
3
3
  GEMSPEC = Gem::Specification.new do |s|
4
4
  s.name = "rev"
5
- s.version = "0.2.0"
5
+ s.version = "0.2.1"
6
6
  s.authors = "Tony Arcieri"
7
7
  s.email = "tony@medioh.com"
8
- s.date = "2008-2-16"
8
+ s.date = "2008-4-19"
9
9
  s.summary = "Rev is a Ruby binding to the libev high performance event library"
10
10
  s.platform = Gem::Platform::RUBY
11
11
  s.required_ruby_version = '>= 1.8.6'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rev
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tony Arcieri
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-02-16 00:00:00 -07:00
12
+ date: 2008-04-19 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -33,11 +33,11 @@ files:
33
33
  - lib/rev/io_watcher.rb
34
34
  - lib/rev/listener.rb
35
35
  - lib/rev/loop.rb
36
+ - lib/rev/meta.rb
36
37
  - lib/rev/server.rb
37
38
  - lib/rev/socket.rb
38
39
  - lib/rev/ssl.rb
39
40
  - lib/rev/timer_watcher.rb
40
- - lib/rev/watcher.rb
41
41
  - lib/rev.rb
42
42
  - ext/http11_client
43
43
  - ext/http11_client/ext_help.h