Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed version number, formatting

  • Loading branch information...
commit 30ff7373cb64ac5332519cc74717fa44564d5c05 1 parent 721fc0c
Dan Simpson authored August 30, 2010
6  README.markdown
Source Rendered
@@ -68,9 +68,9 @@ macros are used to configure your application server.
68 68
         EM::WebSocket::Log.debug "Connected"
69 69
       end
70 70
 
71  
-    	def on_receive msg
72  
-    	  send_message msg
73  
-    	end
  71
+      def on_receive msg
  72
+        send_message msg
  73
+      end
74 74
 
75 75
     end
76 76
 
26  em-websocket-server.gemspec 100644 → 100755
... ...
@@ -1,22 +1,22 @@
1 1
 spec = Gem::Specification.new do |s|
2  
-	s.name = "em-websocket-server"
3  
-	s.version = "0.5"
4  
-	s.date = "2010-08-30"
5  
-	s.summary = "An evented ruby websocket server built on top of EventMachine"
6  
-	s.email = "dan.simpson@gmail.com"
7  
-	s.homepage = "http://github.com/dansimpson/em-websocket-server"
8  
-	s.description = "em-websocket-server allows the creation of efficient, evented, websocket services with ruby"
9  
-	s.has_rdoc = true
10  
-	
11  
-	s.authors = ["Dan Simpson"]
12  
-	s.add_dependency("eventmachine", ">= 0.12.10")
  2
+  s.name = 'em-websocket-server'
  3
+  s.version = '0.15'
  4
+  s.date = '2010-08-30'
  5
+  s.summary = 'An evented ruby websocket server built on top of EventMachine'
  6
+  s.email = "dan.simpson@gmail.com"
  7
+  s.homepage = "http://github.com/dansimpson/em-websocket-server"
  8
+  s.description = "An evented ruby websocket server built on top of EventMachine"
13 9
 
14  
-	s.files = [
  10
+  s.authors = ["Dan Simpson"]
  11
+  s.add_dependency('eventmachine', '>= 0.12.10')
  12
+
  13
+
  14
+  s.files = [
15 15
     "README.markdown",
16 16
     "em-websocket-server.gemspec",
17 17
     "lib/em-websocket-server.rb",
18 18
     "lib/em-websocket-server/server.rb",
19 19
     "lib/em-websocket-server/request.rb",
20 20
     "lib/em-websocket-server/protocol/version76.rb"
21  
-	]
  21
+  ]
22 22
 end
4  lib/em-websocket-server.rb
@@ -5,8 +5,8 @@
5 5
 
6 6
 module EM
7 7
   module WebSocket
8  
-    Version = 0.50
9  
-    Log     = Logger.new STDOUT
  8
+    Version = 0.15
  9
+    Log = Logger.new STDOUT  
10 10
   end
11 11
 end
12 12
 
19  lib/em-websocket-server/protocol/version76.rb
... ...
@@ -1,35 +1,42 @@
1 1
 module EM
2 2
   module WebSocket
3  
-  	module Protocol
  3
+    module Protocol
4 4
       module Version76
5 5
       
  6
+        # generate protocol 76 compatible response headers
6 7
         def response
7 8
           response = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n"
8 9
           response << "Upgrade: WebSocket\r\n"
9 10
           response << "Connection: Upgrade\r\n"
10 11
           response << "Sec-WebSocket-Origin: #{origin}\r\n"
11 12
           response << "Sec-WebSocket-Location: #{scheme}://#{host}#{path}\r\n"
12  
-          
  13
+  
13 14
           if protocol
14 15
             response << "Sec-WebSocket-Protocol: #{protocol}\r\n"
15 16
           end
16  
-        
  17
+
17 18
           response << "\r\n"
18 19
           response << Digest::MD5.digest(keyset)
19 20
 
20 21
           response
21 22
         end
  23
+
  24
+        protected
22 25
         
  26
+        # generate a keyset from the 3 secure keys
23 27
         def keyset
24 28
           [:sec_websocket_key1,:sec_websocket_key2].collect { |k| 
25 29
             partify(@headers[k])
26 30
           }.push(@headers[:sec_websocket_key3]).join
27 31
         end
28  
-      
  32
+
  33
+        # decode a websocket key and create a token for
  34
+        # use in the response
  35
+        # +key+ the key value to decode
29 36
         def partify key
30 37
           nums   = key.scan(/[0-9]/).join.to_i
31 38
           spaces = key.scan(/ /).size
32  
-          
  39
+  
33 40
           raise "Key Error: #{key} has no spaces" if spaces == 0
34 41
           raise "Key Error: #{key} nums #{nums} is not an integral multiple of #{spaces}" if (nums % spaces) != 0
35 42
 
@@ -37,6 +44,6 @@ def partify key
37 44
         end
38 45
               
39 46
       end
40  
-  	end
  47
+    end
41 48
   end
42 49
 end
20  lib/em-websocket-server/request.rb
@@ -6,10 +6,15 @@ class Request
6 6
       Path    = /^GET (\/[^\s]*) HTTP\/[\d|\.]+$/.freeze
7 7
       Header  = /^([^:]+):\s*(.+)$/.freeze
8 8
 
  9
+      # create a new request object
  10
+      # +data+ the string value of the HTTP headers for parsing
9 11
       def initialize data=nil
10 12
         parse(data) if data
11 13
       end
12 14
     
  15
+      # parse the HTTP headers from an incoming
  16
+      # request into actionable information
  17
+      # +data+ The header data as a string
13 18
       def parse data
14 19
 
15 20
         lines = data.split("\r\n")
@@ -41,6 +46,8 @@ def parse data
41 46
 
42 47
       end
43 48
              
  49
+      # generate a response for this request
  50
+      # may be overridden by other protocol modules
44 51
       def response
45 52
         response = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n"
46 53
         response << "Upgrade: WebSocket\r\n"
@@ -52,34 +59,47 @@ def response
52 59
       end  
53 60
     
54 61
     
  62
+      # returns a header value
  63
+      # +key+ the symbol value of the header field
55 64
       def [](key)
56 65
         @headers[key.to_sym]
57 66
       end
58 67
     
  68
+      # sets the header value
  69
+      # +key+ the header field name
  70
+      # +val+ the value of said field
59 71
       def []=(key,val)
60 72
         @headers[key.downcase.gsub(/-/,"_").to_sym] = val
61 73
       end
62 74
     
  75
+      # is the websocket connection supplying secure
  76
+      # web socket fields in the header
63 77
       def is_secure?
64 78
         @headers.has_key? :sec_websocket_key1
65 79
       end
66 80
     
  81
+      # the websocket protocol that is used
67 82
       def protocol
68 83
         @headers[:sec_websocket_protocol]
69 84
       end
70 85
     
  86
+      # the origin domain of the connected client
71 87
       def origin
72 88
         @headers[:origin]
73 89
       end
74 90
   
  91
+      # the host which the client connected to
75 92
       def host
76 93
         @headers[:host]
77 94
       end
78 95
     
  96
+      # the request path portion of the request URI
79 97
       def path
80 98
         @headers[:path]
81 99
       end
82 100
       
  101
+      # the websocket scheme, either ws, or wss for
  102
+      # a TLS secured connection
83 103
       def scheme
84 104
         "ws"
85 105
       end
37  lib/em-websocket-server/server.rb
@@ -14,33 +14,37 @@ def initialize *args
14 14
         @connected = false
15 15
       end
16 16
 
  17
+      # do not override!
17 18
       def post_init
18 19
         start_tls(self.class.tls_opts) if self.class.secure?
19 20
       end
20  
-  
  21
+
  22
+      # close the connection
21 23
       def unbind
22 24
         on_disconnect
23 25
       end
24 26
 
  27
+      # send a message to the websocket client
  28
+      # +msg+ the message the client should receive
25 29
       def send_message msg
26 30
         send_data "\x00#{msg}\xff"
27 31
       end
28 32
 
29 33
       protected
30 34
 
31  
-      #override this method
  35
+      # override
32 36
       def on_receive msg
33 37
       end
34 38
   
35  
-      #override this method
  39
+      # override this method
36 40
       def on_connect
37 41
       end
38 42
   
39  
-      #override this method
  43
+      # override this method
40 44
       def on_disconnect
41 45
       end
42 46
       
43  
-      #override this method
  47
+      # override this method
44 48
       def on_error ex
45 49
         Log.fatal ex
46 50
       end
@@ -66,7 +70,7 @@ def receive_data data
66 70
           end
67 71
         else        
68 72
           if data =~ /</
69  
-            Log.debug "Sending flash policy #{self.class.policy}"
  73
+            Log.debug "Sending flash policy #{self.class.policy_content}"
70 74
             send_data self.class.policy
71 75
             close_connection_after_writing
72 76
           else
@@ -91,6 +95,9 @@ def handshake data
91 95
     
92 96
       class << self
93 97
 
  98
+        # set the flash policy this is sent to flash clients
  99
+        # +policy+ either a string containing XML or a
  100
+        # path to a XML policy file
94 101
         def flash_policy policy
95 102
           if policy =~ /\.xml$/
96 103
               @policy = File.read(policy)
@@ -99,31 +106,47 @@ def flash_policy policy
99 106
           end
100 107
         end
101 108
         
102  
-        def policy
  109
+        # get the flash policy content
  110
+        def policy_content
103 111
           @policy || default_policy
104 112
         end
105 113
         
  114
+        # secure any instance of this connection
  115
+        # with TLS
  116
+        # +opts+ the EM specific options hash for starting
  117
+        # tls on the connection.  Important options:
  118
+        # :private_key_file
  119
+        # :cert_chain_file
106 120
         def secure opts={}
107 121
           @tls_opts = opts
108 122
         end
109 123
       
  124
+        # is the connection secured with TLS?
110 125
         def secure?
111 126
           @tls_opts != nil
112 127
         end
113 128
       
  129
+        # the TLS options used for the secured connection
114 130
         def tls_opts
115 131
           @tls_opts
116 132
         end
117 133
       
  134
+        # add a domain to the list of allowable domains
  135
+        # +domain+ the domain that is allowed eg: cnet.com
118 136
         def accept_domain domain
119 137
           @accepted = [] unless @accepted
120 138
           @accepted << domain
121 139
         end
122 140
       
  141
+        # the set of domains that the server should
  142
+        # accept the connection from.  If the list is
  143
+        # empty, the server will accept all connections
123 144
         def accept_domains
124 145
           @accepted || []
125 146
         end
126 147
         
  148
+        # the default flash policy content, which accepts
  149
+        # from all domains to all ports (maybe not a good thing)
127 150
         def default_policy
128 151
           "<?xml version=\"1.0\"?><cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\"/></cross-domain-policy>"
129 152
         end

0 notes on commit 30ff737

Please sign in to comment.
Something went wrong with that request. Please try again.