libwebsocket 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.6 / 2012-11-18
4
+
5
+ - Migrate to websocket gem
6
+ - Remove most of deprecated and not working code
7
+ - Frame migration is last thing that prevents this gem to support drafts 00-17(RFC)
8
+
3
9
  ## 0.1.5 / 2012-07-27
4
10
 
5
11
  - Fixed problem caused by invalid draft 10 support
data/README.md CHANGED
@@ -3,6 +3,8 @@
3
3
  A WebSocket message parser/constructor. It is not a server and is not meant to
4
4
  be one. It can be used in any server, event loop etc.
5
5
 
6
+ **Note:** this implementation is supporting only hixie -75 and -76 drafts. Current development of newer drafts are handled within [websocket-ruby](https://github.com/imanel/websocket-ruby) gem and if you are starting new project it will be best to use it instead of libwebsocket. However, if you are already using libwebsocket then you don't need to hurry with transition - this gem will use websocket-rack to support newer drafts.
7
+
6
8
  ## Server handshake
7
9
 
8
10
  h = LibWebSocket::OpeningHandshake::Server.new
@@ -7,7 +7,7 @@ class WebSocket
7
7
  @hs ||= LibWebSocket::OpeningHandshake::Client.new(:url => url, :version => params[:version])
8
8
  @frame ||= LibWebSocket::Frame.new
9
9
 
10
- @socket = TCPSocket.new(@hs.url.host, @hs.url.port || 80)
10
+ @socket = TCPSocket.new(@hs.controller.host, @hs.controller.port || 80)
11
11
 
12
12
  @socket.write(@hs.to_s)
13
13
  @socket.flush
data/lib/libwebsocket.rb CHANGED
@@ -1,15 +1,10 @@
1
+ require 'websocket'
1
2
  # Client/server WebSocket message and frame parser/constructor. This module does
2
3
  # not provide a WebSocket server or client, but is made for using in http servers
3
4
  # or clients to provide WebSocket support.
4
5
  module LibWebSocket
5
6
 
6
- autoload :Cookie, "#{File.dirname(__FILE__)}/libwebsocket/cookie"
7
7
  autoload :Frame, "#{File.dirname(__FILE__)}/libwebsocket/frame"
8
8
  autoload :OpeningHandshake, "#{File.dirname(__FILE__)}/libwebsocket/opening_handshake"
9
- autoload :Message, "#{File.dirname(__FILE__)}/libwebsocket/message"
10
- autoload :Request, "#{File.dirname(__FILE__)}/libwebsocket/request"
11
- autoload :Response, "#{File.dirname(__FILE__)}/libwebsocket/response"
12
- autoload :Stateful, "#{File.dirname(__FILE__)}/libwebsocket/stateful"
13
- autoload :URL, "#{File.dirname(__FILE__)}/libwebsocket/url"
14
9
 
15
10
  end
@@ -5,23 +5,21 @@ module LibWebSocket
5
5
  autoload :Client, "#{File.dirname(__FILE__)}/opening_handshake/client"
6
6
  autoload :Server, "#{File.dirname(__FILE__)}/opening_handshake/server"
7
7
 
8
- attr_accessor :secure, :error
8
+ extend Forwardable
9
9
 
10
- # Convert all hash keys to instance variables.
11
- def initialize(hash = {})
12
- hash.each do |k,v|
13
- instance_variable_set("@#{k}",v)
14
- end
15
- end
10
+ def_delegator :controller, :secure
11
+ def_delegator :controller, :error
12
+ def_delegator :controller, :to_s
13
+ def_delegator :controller, :finished?, :done?
16
14
 
17
- # WebSocket request object.
18
- def req
19
- @req ||= Request.new
15
+ def controller
16
+ @controller
20
17
  end
21
18
 
22
- # WebSocket response object.
23
- def res
24
- @res ||= Response.new
19
+ def parse(opts)
20
+ @controller << opts
21
+ return false if @controller.error
22
+ return true
25
23
  end
26
24
 
27
25
  end
@@ -34,94 +34,10 @@ module LibWebSocket
34
34
  # h.done? # Returns true
35
35
  class Client < OpeningHandshake
36
36
 
37
- attr_accessor :url
37
+ def_delegator :controller, :uri, :url
38
38
 
39
39
  def initialize(hash = {})
40
- super
41
-
42
- self.set_url(self.url) if self.url
43
- end
44
-
45
- # Set or get WebSocket url.
46
- # @see LibWebSocket::URL#initialize
47
- # @example
48
- # handshake.url = 'ws://example.com/demo'
49
- def url=(val)
50
- self.set_url(val)
51
-
52
- return self
53
- end
54
-
55
- # Parse server response
56
- # @example
57
- # h.parse \<<EOF
58
- # HTTP/1.1 101 WebSocket Protocol Handshake
59
- # Upgrade: WebSocket
60
- # Connection: Upgrade
61
- # Sec-WebSocket-Origin: http://example.com
62
- # Sec-WebSocket-Location: ws://example.com/demo
63
- #
64
- # fQJ,fN/4F4!~K~MH
65
- # EOF
66
- def parse(opts)
67
- req = self.req
68
- res = self.res
69
-
70
- unless res.done?
71
- unless res.parse(opts)
72
- self.error = res.error
73
- return
74
- end
75
-
76
- if res.done?
77
- if req.version > 75 && req.checksum != res.checksum
78
- self.error = 'Checksum is wrong.'
79
- return
80
- end
81
- end
82
- end
83
-
84
- return true
85
- end
86
-
87
- # Check if response is correct
88
- def done?
89
- res.done?
90
- end
91
-
92
- # Create request
93
- # @example
94
- # h.to_s # GET /demo HTTP/1.1
95
- # # Upgrade: WebSocket
96
- # # Connection: Upgrade
97
- # # Host: example.com
98
- # # Origin: http://example.com
99
- # # Sec-WebSocket-Key1: 18x 6]8vM;54 *(5: { U1]8 z [ 8
100
- # # Sec-WebSocket-Key2: 1_ tx7X d < nw 334J702) 7]o}` 0
101
- # #
102
- # # Tm[K T2u
103
- def to_s
104
- req.to_s
105
- end
106
-
107
- protected
108
-
109
- def build_url
110
- LibWebSocket::URL.new
111
- end
112
-
113
- def set_url(url)
114
- @url = self.build_url.parse(url) unless url.is_a?(LibWebSocket::URL)
115
-
116
- req = self.req
117
-
118
- host = @url.host
119
- host += ':' + @url.port if @url.port
120
- req.host = host
121
-
122
- req.resource_name = @url.resource_name
123
-
124
- return self
40
+ @controller = ::WebSocket::Handshake::Client.new({:version => 76}.merge(hash))
125
41
  end
126
42
 
127
43
  end
@@ -34,79 +34,8 @@ module LibWebSocket
34
34
  # # fQJ,fN/4F4!~K~MH
35
35
  class Server < OpeningHandshake
36
36
 
37
- # Parse client request
38
- # @example
39
- # h.parse \<<EOF
40
- # GET /demo HTTP/1.1
41
- # Upgrade: WebSocket
42
- # Connection: Upgrade
43
- # Host: example.com
44
- # Origin: http://example.com
45
- # Sec-WebSocket-Key1: 18x 6]8vM;54 *(5: { U1]8 z [ 8
46
- # Sec-WebSocket-Key2: 1_ tx7X d < nw 334J702) 7]o}` 0
47
- #
48
- # Tm[K T2u
49
- # EOF
50
- def parse(opts)
51
- req = self.req
52
- res = self.res
53
-
54
- unless req.done?
55
- unless req.parse(opts)
56
- self.error = req.error
57
- return
58
- end
59
-
60
- if req.done?
61
- res.version = req.version
62
- res.host = req.host
63
-
64
- # res.secure = req.secure
65
- res.resource_name = req.resource_name
66
- res.origin = req.origin
67
-
68
- if req.version > 75
69
- res.number1 = req.number1
70
- res.number2 = req.number2
71
- res.challenge = req.challenge
72
- end
73
- end
74
- end
75
-
76
- return true
77
- end
78
-
79
- # Check if request is correct and done
80
- def done?
81
- req.done?
82
- end
83
-
84
- # Create response in string format
85
- # @example
86
- # h.to_s # HTTP/1.1 101 WebSocket Protocol Handshake
87
- # # Upgrade: WebSocket
88
- # # Connection: Upgrade
89
- # # Sec-WebSocket-Origin: http://example.com
90
- # # Sec-WebSocket-Location: ws://example.com/demo
91
- # #
92
- # # fQJ,fN/4F4!~K~MH
93
- def to_s
94
- res.to_s
95
- end
96
-
97
- # Create response in rack format
98
- # @example
99
- # h.to_rack # [ 101,
100
- # # {
101
- # # 'Upgrade' => 'WebSocket'
102
- # # 'Connection' => 'Upgrade'
103
- # # 'Sec-WebSocket-Origin' => 'http://example.com'
104
- # # 'Sec-WebSocket-Location' => 'ws://example.com/demo'
105
- # # 'Content-Length' => 16
106
- # # },
107
- # # [ 'fQJ,fN/4F4!~K~MH' ] ]
108
- def to_rack
109
- res.to_rack
37
+ def initialize(hash = {})
38
+ @controller = ::WebSocket::Handshake::Server.new({:version => 76}.merge(hash))
110
39
  end
111
40
 
112
41
  end
@@ -1,3 +1,3 @@
1
1
  module LibWebSocket
2
- VERSION = '0.1.5'
2
+ VERSION = '0.1.6'
3
3
  end
data/libwebsocket.gemspec CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |s|
12
12
  s.summary = %q{Universal Ruby library to handle WebSocket protocol}
13
13
  s.description = %q{Universal Ruby library to handle WebSocket protocol}
14
14
 
15
- s.add_dependency 'addressable'
15
+ s.add_dependency 'websocket'
16
16
  s.add_development_dependency 'rake'
17
17
 
18
18
  s.files = `git ls-files`.split("\n")
@@ -3,40 +3,25 @@ require 'test_helper'
3
3
  class TestServerOpeningHandshake < Test::Unit::TestCase
4
4
 
5
5
  def test_to_s
6
- h = LibWebSocket::OpeningHandshake::Client.new
7
- h.url = 'ws://example.com/demo?param=true&another=hello'
8
-
9
- # Mocking
10
- h.req.key1 = "18x 6]8vM;54 *(5: { U1]8 z [ 8"
11
- h.req.key2 = "1_ tx7X d < nw 334J702) 7]o}` 0"
12
- h.req.challenge = "Tm[K T2u"
6
+ h = LibWebSocket::OpeningHandshake::Client.new(:url => 'ws://example.com/demo?param=true&another=hello')
13
7
 
14
8
  assert_equal h.to_s, "GET /demo?param=true&another=hello HTTP/1.1\x0d\x0a" +
15
9
  "Upgrade: WebSocket\x0d\x0a" +
16
10
  "Connection: Upgrade\x0d\x0a" +
17
11
  "Host: example.com\x0d\x0a" +
18
- "Origin: http://example.com\x0d\x0a" +
19
- "Sec-WebSocket-Key1: 18x 6]8vM;54 *(5: { U1]8 z [ 8\x0d\x0a" +
20
- "Sec-WebSocket-Key2: 1_ tx7X d < nw 334J702) 7]o}` 0\x0d\x0a" +
21
- "Content-Length: 8\x0d\x0a" +
22
- "\x0d\x0aTm[K T2u"
12
+ "Sec-WebSocket-Key1: #{h.controller.key1}\x0d\x0a" +
13
+ "Sec-WebSocket-Key2: #{h.controller.key2}\x0d\x0a" +
14
+ "\x0d\x0a#{h.controller.key3}"
23
15
 
24
16
  h = LibWebSocket::OpeningHandshake::Client.new(:url => 'ws://example.com')
25
17
 
26
- # Mocking
27
- h.req.key1 = "18x 6]8vM;54 *(5: { U1]8 z [ 8"
28
- h.req.key2 = "1_ tx7X d < nw 334J702) 7]o}` 0"
29
- h.req.challenge = "Tm[K T2u"
30
-
31
18
  assert_equal h.to_s, "GET / HTTP/1.1\x0d\x0a" +
32
19
  "Upgrade: WebSocket\x0d\x0a" +
33
20
  "Connection: Upgrade\x0d\x0a" +
34
21
  "Host: example.com\x0d\x0a" +
35
- "Origin: http://example.com\x0d\x0a" +
36
- "Sec-WebSocket-Key1: 18x 6]8vM;54 *(5: { U1]8 z [ 8\x0d\x0a" +
37
- "Sec-WebSocket-Key2: 1_ tx7X d < nw 334J702) 7]o}` 0\x0d\x0a" +
38
- "Content-Length: 8\x0d\x0a" +
39
- "\x0d\x0aTm[K T2u"
22
+ "Sec-WebSocket-Key1: #{h.controller.key1}\x0d\x0a" +
23
+ "Sec-WebSocket-Key2: #{h.controller.key2}\x0d\x0a" +
24
+ "\x0d\x0a#{h.controller.key3}"
40
25
 
41
26
  assert !h.done?
42
27
  assert h.parse('')
@@ -56,9 +41,9 @@ class TestServerOpeningHandshake < Test::Unit::TestCase
56
41
  assert h.parse(message)
57
42
  assert !h.error
58
43
 
59
- h = LibWebSocket::OpeningHandshake::Client.new
60
- assert !h.parse("HTTP/1.0 foo bar\x0d\x0a")
61
- assert_equal 'Wrong response line', h.error
44
+ h = LibWebSocket::OpeningHandshake::Client.new(:url => "ws://example.com")
45
+ assert !h.parse("HTTP/1.0 foo bar\x0d\x0a\x0d\x0a")
46
+ assert_equal :invalid_header, h.error
62
47
  end
63
48
 
64
49
  end
@@ -32,8 +32,8 @@ class TestServerOpeningHandshake < Test::Unit::TestCase
32
32
  assert_nil h.error
33
33
 
34
34
  h = LibWebSocket::OpeningHandshake::Server.new
35
- assert_nil h.parse("GET /demo\x0d\x0a")
36
- assert_equal 'Wrong request line', h.error
35
+ h.parse("GET /demo\x0d\x0a\x0d\x0a")
36
+ assert_equal :invalid_header, h.error
37
37
  end
38
38
 
39
39
  end
metadata CHANGED
@@ -1,60 +1,55 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: libwebsocket
3
- version: !ruby/object:Gem::Version
4
- hash: 17
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.6
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 1
9
- - 5
10
- version: 0.1.5
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Bernard Potocki
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-07-27 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
21
- name: addressable
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
12
+ date: 2012-11-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: websocket
16
+ requirement: !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 3
29
- segments:
30
- - 0
31
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
32
22
  type: :runtime
33
- version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
35
- name: rake
36
23
  prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
38
33
  none: false
39
- requirements:
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- hash: 3
43
- segments:
44
- - 0
45
- version: "0"
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
46
38
  type: :development
47
- version_requirements: *id002
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
48
46
  description: Universal Ruby library to handle WebSocket protocol
49
- email:
47
+ email:
50
48
  - bernard.potocki@imanel.org
51
49
  executables: []
52
-
53
50
  extensions: []
54
-
55
51
  extra_rdoc_files: []
56
-
57
- files:
52
+ files:
58
53
  - .gitignore
59
54
  - .travis.yml
60
55
  - CHANGELOG.md
@@ -63,84 +58,49 @@ files:
63
58
  - Rakefile
64
59
  - examples/eventmachine_server.rb
65
60
  - examples/plain_client.rb
66
- - examples/thin_server.rb
67
61
  - lib/libwebsocket.rb
68
- - lib/libwebsocket/cookie.rb
69
- - lib/libwebsocket/cookie/request.rb
70
- - lib/libwebsocket/cookie/response.rb
71
62
  - lib/libwebsocket/frame.rb
72
- - lib/libwebsocket/message.rb
73
63
  - lib/libwebsocket/opening_handshake.rb
74
64
  - lib/libwebsocket/opening_handshake/client.rb
75
65
  - lib/libwebsocket/opening_handshake/server.rb
76
- - lib/libwebsocket/request.rb
77
- - lib/libwebsocket/response.rb
78
- - lib/libwebsocket/stateful.rb
79
- - lib/libwebsocket/url.rb
80
66
  - lib/libwebsocket/version.rb
81
67
  - libwebsocket.gemspec
82
- - test/libwebsocket/cookie/request.rb
83
- - test/libwebsocket/cookie/response.rb
84
68
  - test/libwebsocket/opening_handshake/test_client.rb
85
69
  - test/libwebsocket/opening_handshake/test_server.rb
86
- - test/libwebsocket/test_cookie.rb
87
70
  - test/libwebsocket/test_frame.rb
88
- - test/libwebsocket/test_message.rb
89
- - test/libwebsocket/test_request_75.rb
90
- - test/libwebsocket/test_request_76.rb
91
- - test/libwebsocket/test_request_common.rb
92
- - test/libwebsocket/test_response_75.rb
93
- - test/libwebsocket/test_response_76.rb
94
- - test/libwebsocket/test_response_common.rb
95
- - test/libwebsocket/test_url.rb
96
71
  - test/test_helper.rb
97
72
  homepage: http://github.com/imanel/libwebsocket
98
73
  licenses: []
99
-
100
74
  post_install_message:
101
75
  rdoc_options: []
102
-
103
- require_paths:
76
+ require_paths:
104
77
  - lib
105
- required_ruby_version: !ruby/object:Gem::Requirement
78
+ required_ruby_version: !ruby/object:Gem::Requirement
106
79
  none: false
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- hash: 3
111
- segments:
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ segments:
112
85
  - 0
113
- version: "0"
114
- required_rubygems_version: !ruby/object:Gem::Requirement
86
+ hash: -4506301570578533171
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
88
  none: false
116
- requirements:
117
- - - ">="
118
- - !ruby/object:Gem::Version
119
- hash: 3
120
- segments:
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ segments:
121
94
  - 0
122
- version: "0"
95
+ hash: -4506301570578533171
123
96
  requirements: []
124
-
125
97
  rubyforge_project:
126
98
  rubygems_version: 1.8.24
127
99
  signing_key:
128
100
  specification_version: 3
129
101
  summary: Universal Ruby library to handle WebSocket protocol
130
- test_files:
131
- - test/libwebsocket/cookie/request.rb
132
- - test/libwebsocket/cookie/response.rb
102
+ test_files:
133
103
  - test/libwebsocket/opening_handshake/test_client.rb
134
104
  - test/libwebsocket/opening_handshake/test_server.rb
135
- - test/libwebsocket/test_cookie.rb
136
105
  - test/libwebsocket/test_frame.rb
137
- - test/libwebsocket/test_message.rb
138
- - test/libwebsocket/test_request_75.rb
139
- - test/libwebsocket/test_request_76.rb
140
- - test/libwebsocket/test_request_common.rb
141
- - test/libwebsocket/test_response_75.rb
142
- - test/libwebsocket/test_response_76.rb
143
- - test/libwebsocket/test_response_common.rb
144
- - test/libwebsocket/test_url.rb
145
106
  - test/test_helper.rb
146
- has_rdoc: