-
Notifications
You must be signed in to change notification settings - Fork 5
/
protocol-7.lisp
307 lines (283 loc) · 13 KB
/
protocol-7.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
72
73
74
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
(in-package #:ws)
;; draft 7/protocol 7 support, used by firefox 6
;; (also 8 (used by chrome 14/15, ff7) and 13, since they are pretty
;; much identical)
(defun make-challenge-o7 (k &aux (o7-guid "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"))
"Compute the WebSocket opening handshake challenge, according to:
http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-07#section-1.3
Test this with the example provided in the above document:
(string= (clws::make-challenge-o7 \"dGhlIHNhbXBsZSBub25jZQ==\")
\"s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\")
..which must return T."
(cl-base64:usb8-array-to-base64-string
(ironclad:digest-sequence
:sha1 (map '(vector (unsigned-byte 8)) #'char-code
(concatenate 'string k o7-guid)))))
(defun protocol-7+-handshake (client version-string origin-key)
;; required headers: Host, Sec-WebSocket-Key, Sec-WebSocket-Version
;; optional: Sec-Websocket-Origin, Sec-Websocket-Protocol, Sec-Websocket-Extensions
(flet ((error-exit (message)
(send-error-and-close client message)
(return-from protocol-7+-handshake nil)))
(let* ((headers (client-connection-headers client))
(host (gethash :host headers nil))
(key (gethash :sec-websocket-key headers nil))
(version (gethash :sec-websocket-version headers nil))
(origin (gethash origin-key headers nil))
#++(protocol (gethash :sec-websocket-protocol headers nil))
#++(extensions (gethash :sec-websocket-extensions headers nil))
(upgrade (gethash :upgrade headers ""))
(connection (mapcar (lambda (a) (string-trim " " a))
(split-sequence:split-sequence
#\, (gethash :connection headers ""))))
(resource-name (client-resource-name client)))
;; version 7 requires Host, Sec-Websocket-Key which base64 decodes
;; to 16 octets, and Sec-Websocket-Version = 7
;; also need Connection: Upgrade and Upgrade: WebSocket
;; (ff sends Connection: keep-alive, Upgrade, so split on , first)
(unless (and host key version (and (string= version version-string))
(string-equal upgrade "websocket")
(member "Upgrade" connection :test 'string-equal))
(error-exit *400-message*))
;; todo: validate Host: header
;; 404 if we don't recognize the requested resource
(destructuring-bind (resource check-origin)
(valid-resource-p (client-server client) resource-name)
(unless resource
(error-exit *404-message*))
(unless (funcall check-origin origin)
(error-exit *403-message*))
(multiple-value-bind (acceptp rqueue origin handshake-resource protocol)
(resource-accept-connection resource resource-name
headers
client)
(declare (ignorable origin handshake-resource protocol))
(when (not acceptp)
(error-exit *403-message*))
(setf (client-read-queue client) (or rqueue
(resource-read-queue resource)
(make-mailbox))
(client-resource client) resource)
(%write-to-client client
(string-to-shareable-octets
;; todo: Sec-WebSocket-Protocol, Sec-WebSocket-Extension
(format nil "HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: ~a
"
(make-challenge-o7 key))
:encoding :iso-8859-1))))
t)))
(defun dispatch-message (client)
(let ((opcode (message-opcode client))
(partial-message (partial-message client)))
(setf (partial-message client) nil)
(case opcode
(#x1 ;; text message
(let ((s (get-utf8-string-or-fail partial-message)))
(client-enqueue-read client (list client (list :text s)))))
(#x2 ;; binary message
(let ((*print-length* 32)
(v (get-octet-vector partial-message)))
(client-enqueue-read client (list client (list :binary v))))))
(when (> (mailbox-count (client-read-queue client))
*max-handler-read-backlog*)
;; if server isn't processing events fast enough, disable the
;; reader temporarily and tell the handler
(when (client-reader-active client)
(client-disable-handler client :read t)
(client-enqueue-read client (list client :flow-control))))))
(defun dispatch-control-message (client opcode)
(let ((len (frame-length client))
(chunks (chunks client)))
(case opcode
(#x8 ;; close
;; if close frame has a body, it should be big-endian 16bit code
(let* ((code (when (>= len 2)
(dpb (read-octet chunks) (byte 8 8)
(read-octet chunks))))
;; optionally followed by utf8 text
(message (when (> len 2)
(get-utf8-string-or-fail chunks))))
;; 1005 is status code to pass to applications when none was provided
;; by peer
(error 'close-from-peer :status-code (or code 1005)
:message message)))
(#x9 ;; ping
(let* ((v (get-octet-vector chunks))
(pong (pong-frame-for-protocol (client-websocket-version client)
v)))
(when pong
(%write-to-client client pong))))
(#xa ;; pong
(lg "got pong, body=~s~%" (get-octet-vector chunks)))
(t (error 'fail-the-websockets-connection
:status-code 1002
:message (format nil "unknown control frame #x~2,'0x" opcode))))))
(defun dispatch-frame (client length)
;; control frames (opcodes 8+) can't be fragmented, so FIN=T
;; if 0<opcode<8, partial message must be NIL, FIN can be T or NIL
;; if 0=opcode, partial message must be non-nil, fin can be T or NIL
(let ((opcode (frame-opcode client))
(fin (frame-fin client)))
(cond
((>= opcode 8)
(if (or (not fin) (> length 125))
(error 'fail-the-websockets-connection
:status-code 1002
:message (if fin "fragmented control frame"
"control frame too large"))
(dispatch-control-message client opcode)))
;; continuation frame, add to partial message
((zerop opcode)
(when (not (partial-message client))
;; no message in progress, fail connection
(error 'fail-the-websockets-connection
:status-code 1002
:message (format nil
"continuation frame without start frame")))
(when (and (not fin)
(> (+ length (buffer-size (partial-message client)))
*max-read-message-size*))
(setf (partial-message client) nil)
(error 'fail-the-websockets-connection
:status-code 1009
:message (format nil "message too large")))
(add-chunks (partial-message client) (chunks client))
(when fin
(dispatch-message client)))
;; text/binary message
((or (= opcode 1) (= opcode 2))
;; shouldn't have unfinished message
(when (partial-message client)
(error 'fail-the-websockets-connection
:status-code 1002
:message
(format nil "start frame without finishing previous message")))
;; check for too large partial message
(when (and (not fin)
(> length *max-read-message-size*))
(error 'fail-the-websockets-connection
:status-code 1009
:message (format nil "message too large")))
;; start new message
(setf (partial-message client) (make-instance 'chunk-buffer)
(message-opcode client) opcode)
(add-chunks (partial-message client) (chunks client))
(when fin
(dispatch-message client)))
(t
(error 'fail-the-websockets-connection
:status-code 1002
:message (format nil "unknown data frame #x~2,'0x" opcode))))))
(defun protocol-7+-read-frame (client length mask)
(cond
((and (>= (frame-opcode client) 8)
(or (not (frame-fin client))
(> length 125)))
(error 'fail-the-websockets-connection
:status-code 1002
:message (if (frame-fin client)
"fragmented control frame"
"control frame too large")))
((> length *max-read-frame-size*)
(error 'fail-the-websockets-connection
:status-code 1009
:message (format nil "frame too large"))))
(next-reader-state
client (octet-count-matcher length)
(lambda (client)
(when mask
(mask-octets (chunks client) mask))
(dispatch-frame client length)
(protocol-7+-start-frame client))))
(defun protocol-7+-read-mask (client length)
;; read 4 octet mask
(next-reader-state
client
(octet-count-matcher 4)
(lambda (client)
(with-buffer-as-stream (client s)
(let ((mask (make-array-ubyte8 4 :initial-element 0)))
(loop for i below 4
do (setf (aref mask i) (read-byte s)))
(protocol-7+-read-frame client length mask))))))
(defun protocol-7+-extended-length (client octets masked)
;; read 2/8 octets, extended length
(next-reader-state client
(octet-count-matcher octets)
(lambda (client)
(with-buffer-as-stream (client s)
(let ((length 0))
(loop for i below octets
do (setf length
(+ (* length 256) (read-byte s))))
(setf (frame-length client) length)
(if masked
(protocol-7+-read-mask client length)
(protocol-7+-read-frame client length nil)))))))
(defun protocol-7+-start-frame (client)
;; read 2 octets, opcode+flags and short length
(next-reader-state
client
(octet-count-matcher 2)
(lambda (client)
(with-buffer-as-stream (client s)
(let* ((opcode-octet (read-byte s))
(length-octet (read-byte s))
(fin (logbitp 7 opcode-octet))
(rsv (ldb (byte 3 4) opcode-octet))
(opcode (ldb (byte 4 0) opcode-octet))
(masked (logbitp 7 length-octet))
(length (ldb (byte 7 0) length-octet)))
;; TODO: move checks for continuation frames without start frame
;; here from dispatch-frame so we don't need to buffer data we
;; are just going to dump anyway
(unless masked
(error 'fail-the-websockets-connection
:status-code 1002
:message (format nil "client frames not masked")))
(unless (zerop rsv)
(error 'fail-the-websockets-connection
:status-code 1002
:message (format nil "reserved bits ~3,'0b expected 000" rsv)))
(setf (frame-opcode-octet client) opcode-octet
(frame-opcode client) opcode
(frame-length client) length
(frame-fin client) fin)
(cond
((> length 125)
(protocol-7+-extended-length client
(if (= length 126)
2
8)
masked))
(masked
(protocol-7+-read-mask client length))
(t
(protocol-7+-read-frame client length nil))))))))
(defun protocol-7-parse-headers (client)
(when (protocol-7+-handshake client "7" :sec-websocket-origin)
(setf (client-websocket-version client) 7)
(setf (client-connection-state client) :connected)
(client-enqueue-read client (list client :connect))
(protocol-7+-start-frame client)))
(defun protocol-8-parse-headers (client)
(when (protocol-7+-handshake client "8" :sec-websocket-origin)
(setf (client-websocket-version client) 8)
(setf (client-connection-state client) :connected)
(client-enqueue-read client (list client :connect))
(protocol-7+-start-frame client)))
(defun protocol-13-parse-headers (client)
(when (protocol-7+-handshake client "13" :origin)
(setf (client-websocket-version client) 8)
(setf (client-connection-state client) :connected)
(client-enqueue-read client (list client :connect))
(protocol-7+-start-frame client)))
(setf (gethash "7" *protocol-header-parsers*) 'protocol-7-parse-headers
(gethash "8" *protocol-header-parsers*) 'protocol-8-parse-headers
(gethash "13" *protocol-header-parsers*) 'protocol-13-parse-headers)
(push 7 *supported-protocol-versions*)
(push 8 *supported-protocol-versions*)
(push 13 *supported-protocol-versions*)