Class: WebSocket::Handshake::Server
- Defined in:
- lib/websocket/handshake/server.rb
Overview
Construct or parse a server WebSocket handshake.
Instance Attribute Summary
Attributes inherited from Base
#headers, #path, #protocols, #query, #secure, #state, #version
Attributes included from ExceptionHandler
Instance Method Summary collapse
-
#<<(data) ⇒ Object
Add text of request from Client.
-
#from_hash(hash) ⇒ Object
Parse the request from hash.
-
#from_rack(env) ⇒ Object
Parse the request from a rack environment.
-
#host ⇒ String
Host of server according to client header.
-
#initialize(args = {}) ⇒ Server
constructor
Initialize new WebSocket Server.
-
#port ⇒ Integer
Port of server according to client header.
-
#should_respond? ⇒ Boolean
Should send content to client after finished parsing?.
Methods inherited from Base
#default_port, #default_port?, #finished?, #leftovers, #to_s, #uri, #valid?
Methods included from NiceInspect
Methods included from ExceptionHandler
Constructor Details
#initialize(args = {}) ⇒ Server
Initialize new WebSocket Server
44 45 46 47 |
# File 'lib/websocket/handshake/server.rb', line 44 def initialize(args = {}) super @secure ||= false end |
Instance Method Details
#<<(data) ⇒ Object
Add text of request from Client. This method will parse content immediately and update version, state and error(if neccessary)
64 65 66 67 |
# File 'lib/websocket/handshake/server.rb', line 64 def <<(data) super set_version if parse_data end |
#from_hash(hash) ⇒ Object
Parse the request from hash
113 114 115 116 117 118 119 120 121 |
# File 'lib/websocket/handshake/server.rb', line 113 def from_hash(hash) @headers = hash[:headers] || {} @path = hash[:path] || '/' @query = hash[:query] || '' @leftovers = hash[:body] set_version @state = :finished end |
#from_rack(env) ⇒ Object
Parse the request from a rack environment
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/websocket/handshake/server.rb', line 75 def from_rack(env) @headers = env.select { |key, _value| key.to_s.start_with? 'HTTP_' }.each_with_object({}) do |tuple, memo| key, value = tuple memo[key.gsub(/\AHTTP_/, '').tr('_', '-').downcase] = value end @path = env['REQUEST_PATH'] @query = env['QUERY_STRING'] set_version # Passenger is blocking on read # Unicorn doesn't support readpartial # Maybe someone is providing even plain string? # Better safe than sorry... if @version == 76 input = env['rack.input'] @leftovers = if input.respond_to?(:readpartial) input.readpartial elsif input.respond_to?(:read) input.read else input.to_s end end @state = :finished end |
#host ⇒ String
Host of server according to client header
131 132 133 |
# File 'lib/websocket/handshake/server.rb', line 131 def host @host || @headers['host'].to_s.split(':')[0].to_s end |
#port ⇒ Integer
Port of server according to client header
137 138 139 |
# File 'lib/websocket/handshake/server.rb', line 137 def port (@port || @headers['host'].to_s.split(':')[1] || default_port).to_i end |
#should_respond? ⇒ Boolean
Should send content to client after finished parsing?
125 126 127 |
# File 'lib/websocket/handshake/server.rb', line 125 def should_respond? true end |