Skip to content

Commit

Permalink
Add completep in frame
Browse files Browse the repository at this point in the history
  • Loading branch information
ahyatt committed May 22, 2012
1 parent 3a7798f commit b0a2970
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
17 changes: 14 additions & 3 deletions websocket-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,26 @@

(ert-deftest websocket-read-frame ()
(should (equal (make-websocket-frame :opcode 'text :payload "Hello"
:length (length websocket-test-hello))
:length (length websocket-test-hello)
:completep t)
(websocket-read-frame websocket-test-hello)))
(should (equal (make-websocket-frame :opcode 'text :payload "Hello"
:length (length websocket-test-hello))
:length (length websocket-test-hello)
:completep t)
(websocket-read-frame (concat websocket-test-hello
"should-not-be-read"))))
(should (equal (make-websocket-frame :opcode 'text :payload "Hello"
:length (length websocket-test-masked-hello))
:length (length websocket-test-masked-hello)
:completep t)
(websocket-read-frame websocket-test-masked-hello)))
(should (equal (make-websocket-frame :opcode 'text :payload "Hello"
:length (length websocket-test-hello)
:completep nil)
(websocket-read-frame (concat (unibyte-string
(logand (string-to-char
(substring websocket-test-hello 0 1))
127))
(substring websocket-test-hello 1)))))
(dotimes (i (- (length websocket-test-hello) 1))
(should-not (websocket-read-frame
(substring websocket-test-hello 0
Expand Down
24 changes: 13 additions & 11 deletions websocket.el
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ many bytes were consumed from the string."
(cons (websocket-get-bytes (substring s 1) 2) 3))
(t (cons initial-val 1)))))

(defstruct websocket-frame opcode payload length)
(defstruct websocket-frame opcode payload length completep)

(defun websocket-mask (key data)
"Mask string DATA with string KEY according to the RFC.
Expand All @@ -149,21 +149,23 @@ the frame finishes. If the frame is not completed, return NIL."
(let* ((opcode (websocket-get-opcode s))
(payload-len (websocket-get-payload-len (substring s 1)))
(maskp (= 128 (logand 128 (websocket-get-bytes (substring s 1) 1))))
(fin (logand 128 (websocket-get-bytes s 1)))
(payload-start (+ (if maskp 5 1) (cdr payload-len)))
(payload-end (+ payload-start (car payload-len)))
(unmasked-payload (progn
(websocket-ensure-length s payload-end)
(substring s payload-start payload-end))))
(if maskp
(let ((masking-key (substring s (+ 1 (cdr payload-len))
(+ 5 (cdr payload-len)))))
(make-websocket-frame :opcode opcode
:payload
(websocket-mask masking-key unmasked-payload)
:length payload-end))
(make-websocket-frame :opcode opcode
:payload unmasked-payload
:length payload-end)))))
(make-websocket-frame
:opcode opcode
:payload
(if maskp
(let ((masking-key (substring s (+ 1 (cdr payload-len))
(+ 5 (cdr payload-len)))))
(websocket-mask masking-key unmasked-payload))
unmasked-payload
)
:length payload-end
:completep (> fin 0)))))

(defun websocket-open (url filter &optional close-callback)
"Open a websocket connection to URL.
Expand Down

0 comments on commit b0a2970

Please sign in to comment.