Permalink
Browse files

Return the length of the websocket frame from websocket-read-frame.

  • Loading branch information...
1 parent c619af7 commit 7900cdaed05644be1bac7a0cf01ed767eaf8ee3b @ahyatt committed May 20, 2012
Showing with 17 additions and 13 deletions.
  1. +8 −2 websocket-test.el
  2. +9 −11 websocket.el
View
@@ -84,9 +84,15 @@
(:val . [0 70000])))))))
(ert-deftest websocket-read-frame ()
- (should (equal (make-websocket-frame :opcode 'text :payload "Hello")
+ (should (equal (make-websocket-frame :opcode 'text :payload "Hello"
+ :length (length websocket-test-hello))
(websocket-read-frame websocket-test-hello)))
- (should (equal (make-websocket-frame :opcode 'text :payload "Hello")
+ (should (equal (make-websocket-frame :opcode 'text :payload "Hello"
+ :length (length websocket-test-hello))
+ (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))
(websocket-read-frame websocket-test-masked-hello)))
(dotimes (i (- (length websocket-test-hello) 1))
(should-not (websocket-read-frame
View
@@ -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)
+(defstruct websocket-frame opcode payload length)
(defun websocket-mask (key data)
"Mask string DATA with string KEY according to the RFC.
@@ -149,23 +149,21 @@ 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))))
+ (payload-start (+ (if maskp 5 1) (cdr payload-len)))
+ (payload-end (+ payload-start (car payload-len)))
(unmasked-payload (progn
- (websocket-ensure-length s (+ (if maskp 5 1)
- (car payload-len)
- (cdr payload-len)))
- (substring
- s
- (+ (if maskp 5 1) (cdr payload-len))
- (+ (if maskp 5 1) (car payload-len)
- (cdr payload-len))))))
+ (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)))
+ (websocket-mask masking-key unmasked-payload)
+ :length payload-end))
(make-websocket-frame :opcode opcode
- :payload unmasked-payload)))))
+ :payload unmasked-payload
+ :length payload-end)))))
(defun websocket-open (url filter &optional close-callback)
"Open a websocket connection to URL.

0 comments on commit 7900cda

Please sign in to comment.