cool.io-websocket 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/ChangeLog +23 -0
- data/README.rdoc +144 -0
- data/Rakefile +24 -0
- data/VERSION +1 -0
- data/examples/README.md +41 -0
- data/examples/echo +18 -0
- data/examples/echo.rb +31 -0
- data/examples/public/echo.css +11 -0
- data/examples/public/echo.html +62 -0
- data/examples/public/js/FABridge.js +604 -0
- data/examples/public/js/WebSocketMain.swf +0 -0
- data/examples/public/js/jquery.min.js +154 -0
- data/examples/public/js/json2.js +482 -0
- data/examples/public/js/swfobject.js +4 -0
- data/examples/public/js/web_socket.js +371 -0
- data/examples/public/shoutchat.css +63 -0
- data/examples/public/shoutchat.html +148 -0
- data/examples/rpc +26 -0
- data/examples/rpc.rb +47 -0
- data/examples/shoutchat +18 -0
- data/examples/shoutchat.rb +79 -0
- data/examples/views/rpc.erb +67 -0
- data/lib/cool.io-websocket.rb +1 -0
- data/lib/coolio/websocket.rb +243 -0
- data/lib/coolio/websocket/spec.rb +103 -0
- metadata +121 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
require 'digest/md5'
|
|
2
|
+
|
|
3
|
+
module Coolio
|
|
4
|
+
class WebSocket < TCPSocket
|
|
5
|
+
|
|
6
|
+
module Spec75
|
|
7
|
+
protected
|
|
8
|
+
|
|
9
|
+
def handshake
|
|
10
|
+
schema = (ssl? ? 'wss' : 'ws')
|
|
11
|
+
location = "#{schema}://#{@request['HTTP_HOST']}#{@request['REQUEST_URI']}"
|
|
12
|
+
|
|
13
|
+
upgrade = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n"
|
|
14
|
+
upgrade << "Upgrade: WebSocket\r\n"
|
|
15
|
+
upgrade << "Connection: Upgrade\r\n"
|
|
16
|
+
upgrade << "WebSocket-Origin: #{@request['HTTP_ORIGIN']}\r\n"
|
|
17
|
+
upgrade << "WebSocket-Location: #{location}\r\n\r\n"
|
|
18
|
+
|
|
19
|
+
write upgrade
|
|
20
|
+
|
|
21
|
+
return true # opened
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
module Spec76
|
|
26
|
+
protected
|
|
27
|
+
|
|
28
|
+
def handshake
|
|
29
|
+
@state_save = @state
|
|
30
|
+
@state = :process_challenge_response
|
|
31
|
+
|
|
32
|
+
return false # not yet opened
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def process_challenge_response
|
|
36
|
+
return false if @data.size < 8
|
|
37
|
+
|
|
38
|
+
key_3 = @data.read(8)
|
|
39
|
+
|
|
40
|
+
@state = remove_instance_variable(:@state_save)
|
|
41
|
+
|
|
42
|
+
challenge_response = do_challenge_response(
|
|
43
|
+
@request['HTTP_SEC_WEBSOCKET_KEY1'],
|
|
44
|
+
@request['HTTP_SEC_WEBSOCKET_KEY2'],
|
|
45
|
+
key_3)
|
|
46
|
+
|
|
47
|
+
schema = (ssl? ? 'wss' : 'ws')
|
|
48
|
+
location = "#{schema}://#{@request['HTTP_HOST']}#{@request['REQUEST_URI']}"
|
|
49
|
+
|
|
50
|
+
upgrade = "HTTP/1.1 101 WebSocket Protocol Handshake\r\n"
|
|
51
|
+
upgrade << "Upgrade: WebSocket\r\n"
|
|
52
|
+
upgrade << "Connection: Upgrade\r\n"
|
|
53
|
+
upgrade << "Sec-WebSocket-Location: #{location}\r\n"
|
|
54
|
+
upgrade << "Sec-WebSocket-Origin: #{@request['HTTP_ORIGIN']}\r\n"
|
|
55
|
+
if proto = @request['HTTP_SEC_WEBSOCKET_PROTOCOL']
|
|
56
|
+
# FIXME server is requed to confirm the subprotocol of the connection
|
|
57
|
+
upgrade << "Sec-WebSocket-Protocol: #{proto}\r\n"
|
|
58
|
+
end
|
|
59
|
+
upgrade << "\r\n"
|
|
60
|
+
upgrade << challenge_response
|
|
61
|
+
|
|
62
|
+
write upgrade
|
|
63
|
+
|
|
64
|
+
on_open
|
|
65
|
+
|
|
66
|
+
return true
|
|
67
|
+
|
|
68
|
+
rescue
|
|
69
|
+
on_bad_request
|
|
70
|
+
return false
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
private
|
|
74
|
+
def do_challenge_response(key_1, key_2, key_3)
|
|
75
|
+
key_number_1 = key_1.scan(/[0-9]/).join.to_i
|
|
76
|
+
key_number_2 = key_2.scan(/[0-9]/).join.to_i
|
|
77
|
+
spaces_1 = key_1.count(' ')
|
|
78
|
+
spaces_2 = key_2.count(' ')
|
|
79
|
+
|
|
80
|
+
if spaces_1 == 0 || spaces_2 == 0
|
|
81
|
+
raise RuntimeError, "invalid challenge key"
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
if key_number_1 % spaces_1 != 0 || key_number_2 % spaces_2 != 0
|
|
85
|
+
raise RuntimeError, "invalid challenge key"
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
part_1 = key_number_1 / spaces_1
|
|
89
|
+
part_2 = key_number_2 / spaces_2
|
|
90
|
+
|
|
91
|
+
challenge = [part_1, part_2].pack('NN') + key_3[0,8]
|
|
92
|
+
response = Digest::MD5.digest(challenge)
|
|
93
|
+
|
|
94
|
+
return response
|
|
95
|
+
|
|
96
|
+
rescue
|
|
97
|
+
on_bad_request
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
metadata
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: cool.io-websocket
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
prerelease: false
|
|
5
|
+
segments:
|
|
6
|
+
- 0
|
|
7
|
+
- 1
|
|
8
|
+
- 4
|
|
9
|
+
version: 0.1.4
|
|
10
|
+
platform: ruby
|
|
11
|
+
authors:
|
|
12
|
+
- FURUHASHI Sadayuki
|
|
13
|
+
- Max Justus Spransy
|
|
14
|
+
autorequire:
|
|
15
|
+
bindir: bin
|
|
16
|
+
cert_chain: []
|
|
17
|
+
|
|
18
|
+
date: 2011-01-15 00:00:00 -06:00
|
|
19
|
+
default_executable:
|
|
20
|
+
dependencies:
|
|
21
|
+
- !ruby/object:Gem::Dependency
|
|
22
|
+
name: cool.io
|
|
23
|
+
prerelease: false
|
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
26
|
+
requirements:
|
|
27
|
+
- - ">="
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
segments:
|
|
30
|
+
- 0
|
|
31
|
+
- 3
|
|
32
|
+
- 2
|
|
33
|
+
version: 0.3.2
|
|
34
|
+
type: :runtime
|
|
35
|
+
version_requirements: *id001
|
|
36
|
+
- !ruby/object:Gem::Dependency
|
|
37
|
+
name: thin
|
|
38
|
+
prerelease: false
|
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
|
40
|
+
none: false
|
|
41
|
+
requirements:
|
|
42
|
+
- - ">="
|
|
43
|
+
- !ruby/object:Gem::Version
|
|
44
|
+
segments:
|
|
45
|
+
- 1
|
|
46
|
+
- 2
|
|
47
|
+
- 7
|
|
48
|
+
version: 1.2.7
|
|
49
|
+
type: :runtime
|
|
50
|
+
version_requirements: *id002
|
|
51
|
+
description: Cool.io-WebSocket is a WebSocket server implementation based on Cool.io, a high performance event-driven I/O library for Ruby. This library conforms to WebSocket draft-75 and draft-76.
|
|
52
|
+
email: frsyuki@users.sourceforge.jp
|
|
53
|
+
executables: []
|
|
54
|
+
|
|
55
|
+
extensions: []
|
|
56
|
+
|
|
57
|
+
extra_rdoc_files:
|
|
58
|
+
- ChangeLog
|
|
59
|
+
- README.rdoc
|
|
60
|
+
files:
|
|
61
|
+
- ChangeLog
|
|
62
|
+
- README.rdoc
|
|
63
|
+
- Rakefile
|
|
64
|
+
- VERSION
|
|
65
|
+
- examples/README.md
|
|
66
|
+
- examples/echo
|
|
67
|
+
- examples/echo.rb
|
|
68
|
+
- examples/public/echo.css
|
|
69
|
+
- examples/public/echo.html
|
|
70
|
+
- examples/public/js/FABridge.js
|
|
71
|
+
- examples/public/js/WebSocketMain.swf
|
|
72
|
+
- examples/public/js/jquery.min.js
|
|
73
|
+
- examples/public/js/json2.js
|
|
74
|
+
- examples/public/js/swfobject.js
|
|
75
|
+
- examples/public/js/web_socket.js
|
|
76
|
+
- examples/public/shoutchat.css
|
|
77
|
+
- examples/public/shoutchat.html
|
|
78
|
+
- examples/rpc
|
|
79
|
+
- examples/rpc.rb
|
|
80
|
+
- examples/shoutchat
|
|
81
|
+
- examples/shoutchat.rb
|
|
82
|
+
- examples/views/rpc.erb
|
|
83
|
+
- lib/cool.io-websocket.rb
|
|
84
|
+
- lib/coolio/websocket.rb
|
|
85
|
+
- lib/coolio/websocket/spec.rb
|
|
86
|
+
has_rdoc: true
|
|
87
|
+
homepage: http://github.com/maxjustus/cool.io-websocket
|
|
88
|
+
licenses: []
|
|
89
|
+
|
|
90
|
+
post_install_message:
|
|
91
|
+
rdoc_options: []
|
|
92
|
+
|
|
93
|
+
require_paths:
|
|
94
|
+
- lib
|
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
96
|
+
none: false
|
|
97
|
+
requirements:
|
|
98
|
+
- - ">="
|
|
99
|
+
- !ruby/object:Gem::Version
|
|
100
|
+
segments:
|
|
101
|
+
- 0
|
|
102
|
+
version: "0"
|
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
|
+
none: false
|
|
105
|
+
requirements:
|
|
106
|
+
- - ">="
|
|
107
|
+
- !ruby/object:Gem::Version
|
|
108
|
+
segments:
|
|
109
|
+
- 0
|
|
110
|
+
version: "0"
|
|
111
|
+
requirements: []
|
|
112
|
+
|
|
113
|
+
rubyforge_project:
|
|
114
|
+
rubygems_version: 1.3.7
|
|
115
|
+
signing_key:
|
|
116
|
+
specification_version: 3
|
|
117
|
+
summary: WebSocket server based on Coolio
|
|
118
|
+
test_files:
|
|
119
|
+
- examples/echo.rb
|
|
120
|
+
- examples/rpc.rb
|
|
121
|
+
- examples/shoutchat.rb
|