Class: Tep::WebSocket::Handshake

Inherits:
Object
  • Object
show all
Defined in:
lib/tep/websocket/handshake.rb

Defined Under Namespace

Classes: Result

Class Method Summary collapse

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).



71
72
73
74
75
76
77
78
79
80
# File 'lib/tep/websocket/handshake.rb', line 71

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
# 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).
  upgrade = req.headers["upgrade"]
  if Handshake.icontains(upgrade, "websocket") == false
    out.valid = false
    out.reason = "missing/invalid Upgrade"
    return out
  end
  conn = req.headers["connection"]
  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"]
  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.
  out.protocols = Handshake.split_csv(req.headers["sec-websocket-protocol"])
  out
end

.downcase(s) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/tep/websocket/handshake.rb', line 95

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.



86
87
88
89
90
91
92
93
# File 'lib/tep/websocket/handshake.rb', line 86

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<String>. Trims whitespace around each entry.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/tep/websocket/handshake.rb', line 112

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



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/tep/websocket/handshake.rb', line 131

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