Class: Tep::WebSocket::Handshake
- Inherits:
-
Object
- Object
- Tep::WebSocket::Handshake
- Defined in:
- lib/tep/websocket/handshake.rb
Defined Under Namespace
Classes: Result
Class Method Summary collapse
-
.build_response(accept_key, protocol) ⇒ Object
Build the 101 Switching Protocols response.
- .check(req) ⇒ Object
- .downcase(s) ⇒ Object
-
.icontains(hay, needle) ⇒ Object
Case-insensitive substring contains.
-
.split_csv(s) ⇒ Object
Parse comma-separated header value into an Array
. - .trim(s) ⇒ Object
Class Method Details
.build_response(accept_key, protocol) ⇒ Object
Build the 101 Switching Protocols response. protocol empty
omit Sec-WebSocket-Protocol entirely (spec-correct per
RFC 6455 ยง4.2.2; better than echoing a protocol the server doesn't actually implement).
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/tep/websocket/handshake.rb', line 77 def self.build_response(accept_key, protocol) out = "HTTP/1.1 101 Switching Protocols\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + "Sec-WebSocket-Accept: " + accept_key + "\r\n" if protocol.length > 0 out = out + "Sec-WebSocket-Protocol: " + protocol + "\r\n" end out + "\r\n" end |
.check(req) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/tep/websocket/handshake.rb', line 18 def self.check(req) out = Tep::WebSocket::Handshake::Result.new # Verb must be GET. if req.verb != "GET" out.valid = false out.reason = "bad verb" return out end # Upgrade + Connection headers (downcased per Tep::Request). # A missing header reads as nil; treat it as absent (""). upgrade = req.headers["upgrade"] upgrade = "" if upgrade.nil? if Handshake.icontains(upgrade, "websocket") == false out.valid = false out.reason = "missing/invalid Upgrade" return out end conn = req.headers["connection"] conn = "" if conn.nil? if Handshake.icontains(conn, "upgrade") == false out.valid = false out.reason = "missing/invalid Connection" return out end # Sec-WebSocket-Version must be 13. ver = req.headers["sec-websocket-version"] if ver != "13" out.valid = false out.reason = "bad/missing Sec-WebSocket-Version" return out end # Sec-WebSocket-Key: 24-char base64 (16-byte nonce). key = req.headers["sec-websocket-key"] key = "" if key.nil? if key.length == 0 out.valid = false out.reason = "missing Sec-WebSocket-Key" return out end out.valid = true out.accept_key = Crypto.sp_crypto_websocket_accept(key) # Parse Sec-WebSocket-Protocol (comma-separated). Handler # gets the offered list; can opt-in via Driver.accept_protocol. proto_blob = req.headers["sec-websocket-protocol"] proto_blob = "" if proto_blob.nil? out.protocols = Handshake.split_csv(proto_blob) out end |
.downcase(s) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/tep/websocket/handshake.rb', line 101 def self.downcase(s) out = "" i = 0 while i < s.length c = s[i] if c >= "A" && c <= "Z" out = out + (c.ord + 32).chr else out = out + c end i += 1 end out end |
.icontains(hay, needle) ⇒ Object
Case-insensitive substring contains. Hand-rolled because
Tep::Request normalises header names to lowercase but
leaves values as-is, and clients sometimes send
Connection: keep-alive, Upgrade capitalised.
92 93 94 95 96 97 98 99 |
# File 'lib/tep/websocket/handshake.rb', line 92 def self.icontains(hay, needle) if hay.length == 0 || needle.length == 0 return false end hl = Handshake.downcase(hay) nl = Handshake.downcase(needle) Tep.str_find(hl, nl, 0) >= 0 end |
.split_csv(s) ⇒ Object
Parse comma-separated header value into an Array
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/tep/websocket/handshake.rb', line 118 def self.split_csv(s) out = [""] out.delete_at(0) if s.length == 0 return out end pos = 0 while pos < s.length comma = Tep.str_find(s, ",", pos) if comma < 0 out.push(Handshake.trim(s[pos, s.length - pos])) return out end out.push(Handshake.trim(s[pos, comma - pos])) pos = comma + 1 end out end |
.trim(s) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/tep/websocket/handshake.rb', line 137 def self.trim(s) i = 0 while i < s.length && (s[i] == " " || s[i] == "\t") i += 1 end j = s.length - 1 while j >= i && (s[j] == " " || s[j] == "\t") j -= 1 end if j < i return "" end s[i, j - i + 1] end |