cramp 0.8 → 0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,40 @@
1
+ require 'rainbows'
2
+
3
+ class Rainbows::EventMachine::Client
4
+ include Cramp::Controller::WebsocketExtension
5
+
6
+ def websocket_handshake!
7
+ @state = :websocket
8
+ end
9
+
10
+ def receive_data_with_websocket(data)
11
+ case @state
12
+ when :websocket
13
+ callback = @env[WEBSOCKET_RECEIVE_CALLBACK]
14
+ callback.call(data) if callback
15
+ else
16
+ receive_data_without_websocket(data)
17
+ end
18
+ end
19
+
20
+ alias_method_chain :receive_data, :websocket
21
+ end
22
+
23
+ class Rainbows::HttpResponse
24
+ class << self
25
+
26
+ def write_with_magic(socket, rack_response, out = [])
27
+ if socket.websocket?
28
+ socket.write socket.websocket_upgrade_data
29
+ socket.websocket_handshake!
30
+
31
+ out = nil # To make sure Rainbows! doesn't send back regular HTTP headers
32
+ end
33
+
34
+ write_without_magic(socket, rack_response, out)
35
+ end
36
+
37
+ alias_method_chain :write, :magic
38
+ end
39
+
40
+ end
@@ -1,19 +1,20 @@
1
1
  require 'thin'
2
2
 
3
- class Thin::Connection
4
- WEBSOCKET_RECEIVE_CALLBACK = 'websocket.receive_callback'.freeze
3
+ silence_warnings { Thin::Server::DEFAULT_TIMEOUT = 0 }
5
4
 
5
+ class Thin::Connection
6
6
  # Called when data is received from the client.
7
7
  def receive_data(data)
8
8
  trace { data }
9
9
 
10
10
  case @serving
11
11
  when :websocket
12
- callback = @request.env[WEBSOCKET_RECEIVE_CALLBACK]
12
+ callback = @request.env[Thin::Request::WEBSOCKET_RECEIVE_CALLBACK]
13
13
  callback.call(data) if callback
14
14
  else
15
15
  if @request.parse(data)
16
16
  if @request.websocket?
17
+ @response.persistent!
17
18
  @response.websocket_upgrade_data = @request.websocket_upgrade_data
18
19
  @serving = :websocket
19
20
  end
@@ -21,7 +22,7 @@ class Thin::Connection
21
22
  process
22
23
  end
23
24
  end
24
- rescue InvalidRequest => e
25
+ rescue Thin::InvalidRequest => e
25
26
  log "!! Invalid request"
26
27
  log_error e
27
28
  close_connection
@@ -29,22 +30,7 @@ class Thin::Connection
29
30
  end
30
31
 
31
32
  class Thin::Request
32
- def websocket?
33
- @env['HTTP_CONNECTION'] == 'Upgrade' && @env['HTTP_UPGRADE'] == 'WebSocket'
34
- end
35
-
36
- # upgrade headers for websocket connections
37
- def websocket_upgrade_data
38
- location = "ws://#{@env['HTTP_HOST']}#{@env['REQUEST_PATH']}"
39
-
40
- upgrade = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n"
41
- upgrade << "Upgrade: WebSocket\r\n"
42
- upgrade << "Connection: Upgrade\r\n"
43
- upgrade << "WebSocket-Origin: #{@env['HTTP_ORIGIN']}\r\n"
44
- upgrade << "WebSocket-Location: #{location}\r\n\r\n"
45
-
46
- upgrade
47
- end
33
+ include Cramp::Controller::WebsocketExtension
48
34
  end
49
35
 
50
36
  class Thin::Response
@@ -1,5 +1,25 @@
1
1
  module Cramp
2
2
  module Controller
3
+ module WebsocketExtension
4
+ WEBSOCKET_RECEIVE_CALLBACK = 'websocket.receive_callback'.freeze
5
+
6
+ def websocket?
7
+ @env['HTTP_CONNECTION'] == 'Upgrade' && @env['HTTP_UPGRADE'] == 'WebSocket'
8
+ end
9
+
10
+ def websocket_upgrade_data
11
+ location = "ws://#{@env['HTTP_HOST']}#{@env['REQUEST_PATH']}"
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: #{@env['HTTP_ORIGIN']}\r\n"
17
+ upgrade << "WebSocket-Location: #{location}\r\n\r\n"
18
+
19
+ upgrade
20
+ end
21
+ end
22
+
3
23
  class Websocket < Abstract
4
24
 
5
25
  include PeriodicTimer
@@ -9,7 +29,7 @@ module Cramp
9
29
 
10
30
  class << self
11
31
  def backend=(backend)
12
- raise "Websocket backend #{backend} is unknown" unless [:thin].include?(backend.to_sym)
32
+ raise "Websocket backend #{backend} is unknown" unless [:thin, :rainbows].include?(backend.to_sym)
13
33
  require "cramp/controller/websocket/#{backend}_backend.rb"
14
34
  end
15
35
 
data/lib/cramp.rb CHANGED
@@ -2,11 +2,12 @@ require 'eventmachine'
2
2
  EM.epoll
3
3
 
4
4
  require 'active_support'
5
- require 'active_support/core_ext'
5
+ require 'active_support/core_ext/class/inheritable_attributes'
6
+ require 'active_support/core_ext/class/attribute_accessors'
7
+ require 'active_support/core_ext/module/aliasing'
8
+ require 'active_support/core_ext/kernel/reporting'
6
9
  require 'active_support/concern'
7
10
 
8
- require 'cramp/core_ext'
9
-
10
11
  module Cramp
11
- VERSION = '0.8'
12
+ VERSION = '0.9'
12
13
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cramp
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.8"
4
+ version: "0.9"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pratik Naik
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-10 00:00:00 +05:30
12
+ date: 2010-01-11 00:00:00 +05:30
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -100,10 +100,10 @@ files:
100
100
  - lib/cramp/controller/periodic_timer.rb
101
101
  - lib/cramp/controller/rendering.rb
102
102
  - lib/cramp/controller/test_case.rb
103
+ - lib/cramp/controller/websocket/rainbows_backend.rb
103
104
  - lib/cramp/controller/websocket/thin_backend.rb
104
105
  - lib/cramp/controller/websocket.rb
105
106
  - lib/cramp/controller.rb
106
- - lib/cramp/core_ext.rb
107
107
  - lib/cramp/model/arel_monkey_patches.rb
108
108
  - lib/cramp/model/attribute.rb
109
109
  - lib/cramp/model/attribute_methods.rb
@@ -1,4 +0,0 @@
1
- module Kernel
2
- alias :'_' :method
3
- alias :callback :method
4
- end