websocket_parser 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e45c5d450a8c75baa97f7c1019935ece33763c44
4
- data.tar.gz: 1842fd255487115f5d66a135a073bcac3e8e3350
3
+ metadata.gz: dc4855a9ac712ce4e9122a142cd5b7c7490fc1f2
4
+ data.tar.gz: c5f772d978806e57e200e77f833f0af1d86f078d
5
5
  SHA512:
6
- metadata.gz: 8e543dd1f7299e1e3b08d95085597b523bc46adf09824254b93db9042e3970d5c2600184435457fa059e2b6b0f4675971a6539a348a8aefac84149505cb93c2e
7
- data.tar.gz: 4ec4c8e0e8c877009c6b29013e0ea9ff7745add5ba23ab75f3bda51c20d892e4dcc67733edd504ea8b8c287a2eeff5747ee45055131e0b5b680fdbb5ce9ff970
6
+ metadata.gz: 5152da6ff5e0fbd83670b892744ea26ae641b46710c62be3e405209bc3571fe6d2bfeb38d105336c99a71c5eaa3d6a308ee56d73f3441ef50be9266e11af636d
7
+ data.tar.gz: 5c20f099efa09f71e44e1e16980c0a924a30dd83e6a9cfe76048966555919a4e8c44263d7116f299458b4e01efbaa3af8a8d1d4c76e3dcefd82969385cc0b7a1
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # WebSocketParser
2
2
  [![Build Status](https://secure.travis-ci.org/afcapel/websocket_parser.png)](http://travis-ci.org/afcapel/websocket_parser)
3
+ [![Code Climate](https://codeclimate.com/github/afcapel/websocket_parser.png)](https://codeclimate.com/github/afcapel/websocket_parser)
3
4
 
4
5
  WebsocketParser is a RFC6455 compliant parser for websocket messages written in Ruby. It
5
6
  is intended to write websockets servers in Ruby, but it only handles the parsing of the
@@ -2,12 +2,19 @@ require 'digest/sha1'
2
2
  require 'base64'
3
3
 
4
4
  module WebSocket
5
- class ClientHandshake < Http::Request
5
+ class ClientHandshake
6
+
7
+ attr_reader :verb, :uri, :headers, :proxy, :body, :version
6
8
 
7
9
  def self.accept_token_for(websocket_key)
8
10
  Base64.encode64(Digest::SHA1.digest(websocket_key.strip + GUID)).strip
9
11
  end
10
12
 
13
+ def initialize(verb, uri, headers = {}, proxy = {}, body = nil, version = '1.1')
14
+ @verb, @headers, @proxy, @body, @version = verb, headers, proxy, body, version
15
+ @uri = uri.is_a?(URI) ? uri : URI(uri.to_s)
16
+ end
17
+
11
18
  def errors
12
19
  @errors ||= []
13
20
  end
@@ -18,10 +25,8 @@ module WebSocket
18
25
  return false
19
26
  end
20
27
 
21
- # Careful: Http gem changes header capitalization,
22
- # so Sec-WebSocket-Version becomes Sec-Websocket-Version
23
- if headers['Sec-Websocket-Version'].to_i != PROTOCOL_VERSION
24
- errors << "Protocol version not supported '#{headers['Sec-Websocket-Version']}'"
28
+ if websocket_version_header.to_i != PROTOCOL_VERSION
29
+ errors << "Protocol version not supported '#{websocket_version_header}'"
25
30
  return false
26
31
  end
27
32
 
@@ -33,16 +38,23 @@ module WebSocket
33
38
  end
34
39
 
35
40
  def response_headers
36
- websocket_key = headers['Sec-Websocket-Key']
37
41
  {
38
42
  'Upgrade' => 'websocket',
39
43
  'Connection' => 'Upgrade',
40
- 'Sec-WebSocket-Accept' => ClientHandshake.accept_token_for(websocket_key)
44
+ 'Sec-WebSocket-Accept' => ClientHandshake.accept_token_for(websocket_key_header)
41
45
  }
42
46
  end
43
47
 
48
+ def websocket_version_header
49
+ headers['Sec-WebSocket-Version'] || headers['Sec-Websocket-Version']
50
+ end
51
+
52
+ def websocket_key_header
53
+ headers['Sec-Websocket-Key'] || headers['Sec-WebSocket-Key']
54
+ end
55
+
44
56
  def to_data
45
- data = "#{method.to_s.upcase} #{uri.path} HTTP/#{version}#{CRLF}"
57
+ data = "#{verb.to_s.upcase} #{uri.path} HTTP/#{version}#{CRLF}"
46
58
  @headers.each do |field, value|
47
59
  data << "#{field}: #{value}#{CRLF}"
48
60
  end
@@ -1,5 +1,7 @@
1
1
  module WebSocket
2
- class ServerHandshake < Http::Response
2
+ class ServerHandshake
3
+ attr_reader :headers
4
+
3
5
  def initialize(headers = {})
4
6
  @headers = headers
5
7
  end
@@ -1,3 +1,3 @@
1
1
  module WebSocket
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
@@ -1,5 +1,3 @@
1
- require "http"
2
-
3
1
  require "websocket/version"
4
2
  require "websocket/client_handshake"
5
3
  require "websocket/server_handshake"
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require 'bundler/setup'
2
- require 'http'
3
2
  require 'websocket_parser'
4
3
 
5
4
  RSpec.configure do |config|
@@ -26,4 +26,20 @@ describe WebSocket::ClientHandshake do
26
26
  response.headers['Connection'].should eq('Upgrade')
27
27
  response.headers['Sec-WebSocket-Accept'].should eq('s3pPLMBiTxaQ9kYGzzhZRbK+xOo=')
28
28
  end
29
+
30
+ it "can be seariakized to data" do
31
+ expected_lines = [
32
+ "GET / HTTP/1.1",
33
+ "Host: server.example.com",
34
+ "Upgrade: websocket",
35
+ "Connection: Upgrade",
36
+ "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==",
37
+ "Origin: http://example.com",
38
+ "Sec-WebSocket-Protocol: chat, superchat",
39
+ "Sec-WebSocket-Version: 13",
40
+ "\r\n"
41
+ ]
42
+
43
+ client_handshake.to_data.should eq expected_lines.join("\r\n")
44
+ end
29
45
  end
@@ -15,8 +15,6 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = WebSocket::VERSION
17
17
 
18
- gem.add_runtime_dependency 'http'
19
-
20
18
  gem.add_development_dependency 'rake'
21
19
  gem.add_development_dependency 'rspec'
22
20
  end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: websocket_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alberto Fernandez-Capel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-13 00:00:00.000000000 Z
11
+ date: 2014-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: http
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: rake
29
15
  requirement: !ruby/object:Gem::Requirement