Skip to content
Browse files

Merging conflict.

  • Loading branch information...
2 parents 2afb526 + 12cabea commit 771f26eb4c0a6262e018728aba18b5be5ef0e752 @gimite committed
Showing with 51 additions and 24 deletions.
  1. +13 −18 README.txt
  2. +38 −6 lib/web_socket.rb
View
31 README.txt
@@ -15,28 +15,23 @@
Server:
- # Runs the server at port 10081.
- server = WebSocketServer.new("ws://example.com:10081")
+ # Runs the server at port 10081. It allows connections whose origin is example.com.
+ server = WebSocketServer.new("ws://example.com:10081", :accepted_domains => ["example.com"])
server.run() do |ws|
# The block is called for each connection.
- # Checks origin (host name of the Web page the Web Socket is used).
- if ws.origin == "http://example.com"
- # Checks requested path.
- if ws.path == "/"
- # Call ws.handshake() without argument first.
- ws.handshake()
- # Receives one message from the client as String.
- while data = ws.receive()
- puts(data)
- # Sends the message to the client.
- ws.send(data)
- end
- else
- # You can call ws.handshake() with argument to return error status.
- ws.handshake("404 Not Found")
+ # Checks requested path.
+ if ws.path == "/"
+ # Call ws.handshake() without argument first.
+ ws.handshake()
+ # Receives one message from the client as String.
+ while data = ws.receive()
+ puts(data)
+ # Sends the message to the client.
+ ws.send(data)
end
else
- ws.handshake("403 Forbidden")
+ # You can call ws.handshake() with argument to return error status.
+ ws.handshake("404 Not Found")
end
end
View
44 lib/web_socket.rb
@@ -50,6 +50,10 @@ def initialize(arg, params = {})
end
@path = $1
read_header()
+ if !@server.accepted_origin?(self.origin)
+ raise(WebSocket::Error, "unaccepted origin: %s (server.accepted_domains = %p)" %
+ [self.origin, @server.accepted_domains])
+ end
@handshaked = false
else # client
@@ -171,22 +175,23 @@ class WebSocketServer
def initialize(uri, params = {})
@uri = uri.is_a?(String) ? URI.parse(uri) : uri
- port = params[:port] || @uri.port || 80
+ @port = params[:port] || @uri.port || 80
+ @accepted_domains = params[:accepted_domains] || [@uri.host]
if params[:host]
- @tcp_server = TCPServer.open(params[:host], port)
+ @tcp_server = TCPServer.open(params[:host], @port)
else
- @tcp_server = TCPServer.open(port)
+ @tcp_server = TCPServer.open(@port)
end
end
- attr_reader(:tcp_server, :uri)
+ attr_reader(:tcp_server, :uri, :port, :accepted_domains)
def run(&block)
while true
Thread.start(accept()) do |s|
begin
ws = create_web_socket(s)
- yield(ws)
+ yield(ws) if ws
rescue => ex
print_backtrace(ex)
ensure
@@ -203,8 +208,21 @@ def accept()
return @tcp_server.accept()
end
+ def accepted_origin?(origin)
+ domain = URI.parse(origin).host
+ return @accepted_domains.any?(){ |d| File.fnmatch(d, domain) }
+ end
+
def create_web_socket(socket)
- return WebSocket.new(socket, :server => self)
+ ch = socket.getc()
+ if ch == ?<
+ # This is Flash socket policy file request, not an actual Web Socket connection.
+ send_flash_socket_policy_file(socket)
+ return nil
+ else
+ socket.ungetc(ch)
+ return WebSocket.new(socket, :server => self)
+ end
end
private
@@ -215,6 +233,20 @@ def print_backtrace(ex)
$stderr.printf(" %s\n", s)
end
end
+
+ # Handles Flash socket policy file request sent when web-socket-js is used:
+ # http://github.com/gimite/web-socket-js/tree/master
+ def send_flash_socket_policy_file(socket)
+ socket.puts('<?xml version="1.0"?>')
+ socket.puts('<!DOCTYPE cross-domain-policy SYSTEM ' +
+ '"http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">')
+ socket.puts('<cross-domain-policy>')
+ for domain in @accepted_domains
+ socket.puts("<allow-access-from domain=\"#{domain}\" to-ports=\"#{@port}\"/>")
+ end
+ socket.puts('</cross-domain-policy>')
+ socket.close()
+ end
end

0 comments on commit 771f26e

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